Bug 1928997: Update tabs icon in Unified Search popup r=desktop-theme-reviewers,daleh...
[gecko.git] / security / manager / tools / mach_commands.py
blob283a6167a6bdf5df65649072df0976decf564ef5
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 import os
7 from mach.decorators import Command, CommandArgument
8 from mach.util import UserError
9 from mozpack.files import FileFinder
12 def run_module_main_on(module, input_filename, output_is_binary):
13 """Run the given module (pycert, pykey, etc.) on the given
14 file."""
15 # By convention, the specification files have names of the form
16 # "name.ext.*spec", where "ext" is some extension, and the "*" in
17 # "*spec" identifies what kind of specification it represents
18 # (certspec, keyspec, etc.). Taking off the ".*spec" part results in
19 # the desired filename for this file.
20 output_filename = os.path.splitext(input_filename)[0]
21 mode = "w"
22 encoding = "utf-8"
23 newline = "\n"
24 if output_is_binary:
25 mode = "wb"
26 encoding = None
27 newline = None
28 with open(output_filename, mode=mode, encoding=encoding, newline=newline) as output:
29 module.main(output, input_filename)
32 def is_certspec_file(filename):
33 """Returns True if the given filename is a certificate
34 specification file (.certspec) and False otherwise."""
35 return filename.endswith(".certspec")
38 def is_keyspec_file(filename):
39 """Returns True if the given filename is a key specification
40 file (.keyspec) and False otherwise."""
41 return filename.endswith(".keyspec")
44 def is_pkcs12spec_file(filename):
45 """Returns True if the given filename is a pkcs12
46 specification file (.pkcs12spec) and False otherwise."""
47 return filename.endswith(".pkcs12spec")
50 def is_sctspec_file(filename):
51 """Returns True if the given filename is an SCT
52 specification file (.sctspec) and False otherwise."""
53 return filename.endswith(".sctspec")
56 def is_specification_file(filename):
57 """Returns True if the given filename is a specification
58 file supported by this script, and False otherewise."""
59 return (
60 is_certspec_file(filename)
61 or is_keyspec_file(filename)
62 or is_pkcs12spec_file(filename)
63 or is_sctspec_file(filename)
67 @Command(
68 "generate-test-certs",
69 category="devenv",
70 description="Generate test certificates and keys from specifications.",
72 @CommandArgument(
73 "specifications",
74 nargs="*",
75 help="Specification files for test certs. If omitted, all certs are regenerated.",
77 def generate_test_certs(command_context, specifications):
78 """Generate test certificates and keys from specifications."""
79 import pycert
80 import pyct
81 import pykey
82 import pypkcs12
84 if not specifications:
85 specifications = find_all_specifications(command_context)
87 for specification in specifications:
88 output_is_binary = False
89 if is_certspec_file(specification):
90 module = pycert
91 elif is_keyspec_file(specification):
92 module = pykey
93 elif is_pkcs12spec_file(specification):
94 module = pypkcs12
95 output_is_binary = True
96 elif is_sctspec_file(specification):
97 module = pyct
98 output_is_binary = True
99 else:
100 raise UserError(
101 "'{}' is not a .certspec, .keyspec, or .pkcs12spec file".format(
102 specification
105 run_module_main_on(module, os.path.abspath(specification), output_is_binary)
106 return 0
109 def find_all_specifications(command_context):
110 """Searches the source tree for all specification files
111 and returns them as a list."""
112 specifications = []
113 inclusions = [
114 "netwerk/test/unit",
115 "security/manager/ssl/tests",
116 "services/settings/test/unit/test_remote_settings_signatures",
117 "testing/xpcshell/moz-http2",
118 "toolkit/mozapps/extensions/test/xpcshell/data/productaddons",
120 finder = FileFinder(command_context.topsrcdir)
121 for inclusion_path in inclusions:
122 for f, _ in finder.find(inclusion_path):
123 if is_specification_file(f):
124 specifications.append(os.path.join(command_context.topsrcdir, f))
125 return specifications