is_buildconfig_yml_up_to_date.py (2300B)
1 #!/usr/bin/env python3 2 3 # This Source Code Form is subject to the terms of the Mozilla Public 4 # License, v. 2.0. If a copy of the MPL was not distributed with this 5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 7 8 import logging 9 import os 10 import subprocess 11 import sys 12 13 from update_buildconfig_from_gradle import main as update_build_config 14 15 OUTPUT_DIR = os.environ.get("ARTIFACTS_DIR", "/builds/worker/artifacts") 16 BUILDCONFIG_DIFF_FILE_NAME = "buildconfig.diff" 17 BUILDCONFIG_DIFF_FILE = os.path.join(OUTPUT_DIR, BUILDCONFIG_DIFF_FILE_NAME) 18 BUILDCONFIG_FILE_NAME = ".buildconfig.yml" 19 20 logger = logging.getLogger(__name__) 21 22 23 def _buildconfig_files_changed(): 24 cmd = ["hg", "status", "-I", "**/.buildconfig.yml", "-m"] 25 p = subprocess.run(cmd, capture_output=True, text=True, check=True) 26 for line in p.stdout.splitlines(): 27 _, path = line.strip().split(None, 1) 28 yield path 29 30 31 def _buildconfig_files_diff(): 32 cmd = [ 33 "hg", 34 "diff", 35 "-I", 36 "**/.buildconfig.yml", 37 ] 38 p = subprocess.run(cmd, capture_output=True, text=True, check=True) 39 return p.stdout 40 41 42 def _execute_taskcluster_steps(diff, task_id): 43 os.makedirs(OUTPUT_DIR, exist_ok=True) 44 with open(BUILDCONFIG_DIFF_FILE, mode="w") as f: 45 f.write(diff) 46 tc_root_url = os.environ["TASKCLUSTER_ROOT_URL"] 47 artifact_url = f"{tc_root_url}/api/queue/v1/task/{task_id}/artifacts/public%2F{BUILDCONFIG_DIFF_FILE_NAME}" # noqa E501 48 message = f"""{BUILDCONFIG_FILE_NAME} file changed! Please update it by running: 49 50 curl --location --compressed {artifact_url} | git apply 51 52 Then commit and push! 53 """ 54 logger.error(message) 55 for file in _buildconfig_files_changed(): 56 logger.error(f"TEST-UNEXPECTED-FAIL | {file} | build config changed") 57 58 59 def _execute_local_steps(): 60 logger.error(f"{BUILDCONFIG_FILE_NAME} file updated! Please commit these changes.") 61 62 63 def main(): 64 update_build_config() 65 diff = _buildconfig_files_diff() 66 if diff: 67 task_id = os.environ.get("TASK_ID") 68 if task_id: 69 _execute_taskcluster_steps(diff, task_id) 70 else: 71 _execute_local_steps() 72 sys.exit(1) 73 74 logger.info(f"All good! {BUILDCONFIG_FILE_NAME} is up-to-date with gradle.") 75 76 77 __name__ == "__main__" and main()