7 from pathlib import Path
13 from xdg.BaseDirectory import xdg_config_home # type: ignore
14 from voluptuous import Any, Schema # type: ignore
15 from voluptuous.validators import ( # type: ignore
16 And, Date, IsDir, IsFile, Match, NotIn
20 LOG_FORMAT = "%(levelname)s %(message)s"
21 log = logging.getLogger()
30 # pylint: disable=E1120
31 InputStr = And(str, NotIn(["FIXME"]))
32 IsBuildManifest = And(IsFile(), Match(re.compile(r".*[.]build-manifest$")))
33 IsIsoFile = And(IsFile(), Match(re.compile(r".*[.]iso$")))
34 IsImgFile = And(IsFile(), Match(re.compile(r".*[.]img$")))
38 "tails_signature_key": InputStr,
41 "master_checkout": IsDir(),
42 "release_checkout": IsDir(),
44 "previous_version": InputStr,
45 "previous_stable_version": InputStr,
46 "next_planned_major_version": InputStr,
47 "second_next_planned_major_version": InputStr,
48 "next_planned_bugfix_version": InputStr,
49 "next_planned_version": InputStr,
50 "next_potential_emergency_version": InputStr,
51 "next_stable_changelog_version": InputStr,
52 "release_date": Date(),
53 "major_release": Any(0, 1),
54 "dist": Any("stable", "alpha"),
55 "release_branch": InputStr,
57 "previous_tag": InputStr,
58 "website_release_branch": InputStr,
60 "iuks_hashes": InputStr,
61 "milestone": InputStr,
62 "tails_signature_key_long_id": InputStr,
63 "iuk_source_versions": InputStr,
65 "built-almost-final": {
66 "almost_final_build_manifest": IsBuildManifest,
68 "reproduced-images": {
69 "matching_jenkins_images_build_id": int,
72 "iso_path": IsIsoFile,
73 "img_path": IsImgFile,
76 "iso_size_in_bytes": int,
77 "img_size_in_bytes": int,
78 "candidate_jenkins_iuks_build_id": int,
79 "iuks_hashes": IsFile(),
82 # pylint: enable=E1120
86 """Returns the root of the current Git repository as a Path object"""
88 subprocess.check_output(["git", "rev-parse", "--show-toplevel"],
89 encoding="utf8").rstrip("\n"))
92 def sha256_file(filename):
93 """Returns the hex-encoded SHA256 hash of FILENAME"""
94 sha256 = hashlib.sha256()
95 with io.open(filename, mode="rb") as input_fd:
96 content = input_fd.read()
97 sha256.update(content)
98 return sha256.hexdigest()
102 """Load, validate, generate, and output Release Management configuration"""
103 def __init__(self, stage: str):
105 self.config_files = [
106 git_repo_root() / "config/release_management/defaults.yml"
108 (Path(xdg_config_home) / "tails/release_management").glob("*.yml"))
109 self.data = self.load_config_files()
110 self.data.update(self.generate_config())
111 log.debug("Configuration:\n%s", self.data)
114 def load_config_files(self):
116 Load all relevant configuration files and return the resulting
120 for config_file in self.config_files:
121 log.debug("Loading %s", config_file)
122 data.update(yaml.safe_load(open(config_file, 'r')))
125 def generate_config(self):
127 Returns a dict of supplemental, programmatically-generated,
130 version = self.data["version"]
131 tails_signature_key = self.data["tails_signature_key"]
132 tag = version.replace("~", "-")
133 release_branch = "testing" \
134 if self.data["major_release"] == 1 \
136 iuks_dir = Path(self.data["isos"]) / "iuks/v2"
137 iuk_hashes = Path(iuks_dir) / ("to_%s.sha256sum" % version)
138 iuk_source_versions = subprocess.check_output(
139 [git_repo_root() / "bin/iuk-source-versions", version],
140 encoding="utf8").rstrip("\n")
142 "release_branch": release_branch,
144 "previous_tag": self.data["previous_version"].replace("~", "-"),
145 "website_release_branch": "web/release-%s" % tag,
146 "iuk_source_versions": iuk_source_versions,
147 "iuks_dir": str(iuks_dir),
148 "iuks_hashes": str(iuk_hashes),
149 "milestone": re.sub('~.*', '', self.data["version"]),
150 "tails_signature_key_long_id": tails_signature_key[24:],
152 if self.stage == 'built-iuks':
153 iso_path = Path(self.data["isos"]) \
154 / ("tails-amd64-%s/tails-amd64-%s.iso" % (version, version))
155 img_path = Path(self.data["isos"]) \
156 / ("tails-amd64-%s/tails-amd64-%s.img" % (version, version))
157 generated_config.update({
158 "iso_path": str(iso_path),
159 "img_path": str(img_path),
160 "iso_sha256sum": sha256_file(iso_path),
161 "img_sha256sum": sha256_file(img_path),
162 "iso_size_in_bytes": iso_path.stat().st_size,
163 "img_size_in_bytes": img_path.stat().st_size,
165 return generated_config
169 Returns a configuration validation schema function for
174 schema.update(STAGE_SCHEMA[stage])
175 if stage == self.stage:
177 log.debug("Schema:\n%s", schema)
178 return Schema(schema, required=True)
181 """Checks that the configuration is valid, else raise exception"""
182 schema = self.schema()
187 Returns shell commands that, if executed, would export the
188 configuration into the environment.
191 "export %(key)s=%(val)s" % {
193 "val": shlex.quote(str(v))
194 } for (k, v) in self.data.items()
198 def generate_boilerplate(stage: str):
199 """Generate boilerplate for STAGE"""
200 log.debug("Generating boilerplate for stage '%s'", stage)
201 with open(git_repo_root() /
202 ("config/release_management/templates/%s.yml" % stage)) as src:
204 Path(xdg_config_home) / "tails/release_management/current.yml",
206 dst.write(src.read())
209 def generate_environment(stage: str):
211 Prints to stdout the path to a file that contains commands
212 that export the configuration for STAGE to the environment.
214 log.debug("Generating environment for stage '%s'", stage)
215 config = Config(stage=stage)
216 shell_snippet = tempfile.NamedTemporaryFile(delete=False)
217 with open(shell_snippet.name, 'w') as shell_snippet_fd:
218 shell_snippet_fd.write(config.to_shell())
219 print(shell_snippet.name)
222 def validate_configuration(stage: str):
223 """Validate configuration for STAGE, raise exception if invalid"""
224 log.debug("Validating configuration for stage '%s'", stage)
226 log.info("Configuration is valid")
230 """Command-line entry point"""
231 parser = argparse.ArgumentParser(
232 description="Query and manage Release Management configuration")
233 parser.add_argument("--debug", action="store_true", help="debug output")
234 subparsers = parser.add_subparsers(help="sub-command help", dest="command")
236 parser_generate_boilerplate = subparsers.add_parser(
237 "generate-boilerplate",
238 help="Creates a configuration file template that you will fill")
239 parser_generate_boilerplate.add_argument("--stage",
244 parser_generate_boilerplate.set_defaults(func=generate_boilerplate)
246 parser_validate_configuration = subparsers.add_parser(
247 "validate-configuration", help="Validate configuration files")
248 parser_validate_configuration.add_argument("--stage",
253 parser_validate_configuration.set_defaults(func=validate_configuration)
255 parser_generate_environment = subparsers.add_parser(
256 "generate-environment",
257 help="Creates a shell sourceable file with resulting environment")
258 parser_generate_environment.add_argument("--stage",
263 parser_generate_environment.set_defaults(func=generate_environment)
265 args = parser.parse_args()
268 logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
270 logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
272 if args.command is None:
275 args.func(stage=args.stage)
278 if __name__ == '__main__':