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()
26 "finalized-changelog",
31 # pylint: disable=E1120
32 InputStr = And(str, NotIn(["FIXME"]))
33 IsBuildManifest = And(IsFile(), Match(re.compile(r".*[.]build-manifest$")))
34 IsIsoFile = And(IsFile(), Match(re.compile(r".*[.]iso$")))
35 IsImgFile = And(IsFile(), Match(re.compile(r".*[.]img$")))
39 "tails_signature_key": InputStr,
42 "master_checkout": IsDir(),
43 "release_checkout": IsDir(),
45 "previous_version": InputStr,
46 "previous_stable_version": InputStr,
47 "next_planned_major_version": InputStr,
48 "second_next_planned_major_version": InputStr,
49 "next_planned_bugfix_version": InputStr,
50 "next_planned_version": InputStr,
51 "next_potential_emergency_version": InputStr,
52 "next_stable_changelog_version": InputStr,
53 "release_date": Date(),
54 "major_release": Any(0, 1),
55 "dist": Any("stable", "alpha"),
56 "release_branch": InputStr,
58 "previous_tag": InputStr,
59 "website_release_branch": InputStr,
61 "iuks_hashes": InputStr,
62 "milestone": InputStr,
63 "tails_signature_key_long_id": InputStr,
64 "iuk_source_versions": InputStr,
66 "built-almost-final": {
67 "almost_final_build_manifest": IsBuildManifest,
69 "finalized-changelog": {
70 "source_date_epoch": int,
72 "reproduced-images": {
73 "matching_jenkins_images_build_id": int,
76 "iso_path": IsIsoFile,
77 "img_path": IsImgFile,
80 "iso_size_in_bytes": int,
81 "img_size_in_bytes": int,
82 "candidate_jenkins_iuks_build_id": int,
83 "iuks_hashes": IsFile(),
86 # pylint: enable=E1120
90 """Returns the root of the current Git repository as a Path object"""
92 subprocess.check_output(["git", "rev-parse", "--show-toplevel"],
93 encoding="utf8").rstrip("\n"))
96 def sha256_file(filename):
97 """Returns the hex-encoded SHA256 hash of FILENAME"""
98 sha256 = hashlib.sha256()
99 with io.open(filename, mode="rb") as input_fd:
100 content = input_fd.read()
101 sha256.update(content)
102 return sha256.hexdigest()
106 """Load, validate, generate, and output Release Management configuration"""
107 def __init__(self, stage: str):
109 self.config_files = [
110 git_repo_root() / "config/release_management/defaults.yml"
112 (Path(xdg_config_home) / "tails/release_management").glob("*.yml"))
113 self.data = self.load_config_files()
114 self.data.update(self.generate_config())
115 log.debug("Configuration:\n%s", self.data)
118 def load_config_files(self):
120 Load all relevant configuration files and return the resulting
124 for config_file in self.config_files:
125 log.debug("Loading %s", config_file)
126 data.update(yaml.safe_load(open(config_file, 'r')))
129 def generate_config(self):
131 Returns a dict of supplemental, programmatically-generated,
134 version = self.data["version"]
135 tails_signature_key = self.data["tails_signature_key"]
136 tag = version.replace("~", "-")
137 release_branch = "testing" \
138 if self.data["major_release"] == 1 \
140 iuks_dir = Path(self.data["isos"]) / "iuks/v2"
141 iuk_hashes = Path(iuks_dir) / ("to_%s.sha256sum" % version)
142 iuk_source_versions = subprocess.check_output(
143 [git_repo_root() / "bin/iuk-source-versions", version],
144 encoding="utf8").rstrip("\n")
146 "release_branch": release_branch,
148 "previous_tag": self.data["previous_version"].replace("~", "-"),
149 "website_release_branch": "web/release-%s" % tag,
150 "iuk_source_versions": iuk_source_versions,
151 "iuks_dir": str(iuks_dir),
152 "iuks_hashes": str(iuk_hashes),
153 "milestone": re.sub('~.*', '', self.data["version"]),
154 "tails_signature_key_long_id": tails_signature_key[24:],
156 if self.stage == 'built-iuks':
157 iso_path = Path(self.data["isos"]) \
158 / ("tails-amd64-%s/tails-amd64-%s.iso" % (version, version))
159 img_path = Path(self.data["isos"]) \
160 / ("tails-amd64-%s/tails-amd64-%s.img" % (version, version))
161 generated_config.update({
162 "iso_path": str(iso_path),
163 "img_path": str(img_path),
164 "iso_sha256sum": sha256_file(iso_path),
165 "img_sha256sum": sha256_file(img_path),
166 "iso_size_in_bytes": iso_path.stat().st_size,
167 "img_size_in_bytes": img_path.stat().st_size,
169 return generated_config
173 Returns a configuration validation schema function for
178 schema.update(STAGE_SCHEMA[stage])
179 if stage == self.stage:
181 log.debug("Schema:\n%s", schema)
182 return Schema(schema, required=True)
185 """Checks that the configuration is valid, else raise exception"""
186 schema = self.schema()
191 Returns shell commands that, if executed, would export the
192 configuration into the environment.
195 "export %(key)s=%(val)s" % {
197 "val": shlex.quote(str(v))
198 } for (k, v) in self.data.items()
202 def generate_boilerplate(stage: str):
203 """Generate boilerplate for STAGE"""
204 log.debug("Generating boilerplate for stage '%s'", stage)
205 with open(git_repo_root() /
206 ("config/release_management/templates/%s.yml" % stage)) as src:
208 Path(xdg_config_home) / "tails/release_management/current.yml",
210 dst.write(src.read())
213 def generate_environment(stage: str):
215 Prints to stdout the path to a file that contains commands
216 that export the configuration for STAGE to the environment.
218 log.debug("Generating environment for stage '%s'", stage)
219 config = Config(stage=stage)
220 shell_snippet = tempfile.NamedTemporaryFile(delete=False)
221 with open(shell_snippet.name, 'w') as shell_snippet_fd:
222 shell_snippet_fd.write(config.to_shell())
223 print(shell_snippet.name)
226 def validate_configuration(stage: str):
227 """Validate configuration for STAGE, raise exception if invalid"""
228 log.debug("Validating configuration for stage '%s'", stage)
230 log.info("Configuration is valid")
234 """Command-line entry point"""
235 parser = argparse.ArgumentParser(
236 description="Query and manage Release Management configuration")
237 parser.add_argument("--debug", action="store_true", help="debug output")
238 subparsers = parser.add_subparsers(help="sub-command help", dest="command")
240 parser_generate_boilerplate = subparsers.add_parser(
241 "generate-boilerplate",
242 help="Creates a configuration file template that you will fill")
243 parser_generate_boilerplate.add_argument("--stage",
248 parser_generate_boilerplate.set_defaults(func=generate_boilerplate)
250 parser_validate_configuration = subparsers.add_parser(
251 "validate-configuration", help="Validate configuration files")
252 parser_validate_configuration.add_argument("--stage",
257 parser_validate_configuration.set_defaults(func=validate_configuration)
259 parser_generate_environment = subparsers.add_parser(
260 "generate-environment",
261 help="Creates a shell sourceable file with resulting environment")
262 parser_generate_environment.add_argument("--stage",
267 parser_generate_environment.set_defaults(func=generate_environment)
269 args = parser.parse_args()
272 logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
274 logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
276 if args.command is None:
279 args.func(stage=args.stage)
282 if __name__ == '__main__':