#!/usr/bin/env python3 # This file is part of Cockpit. # # Copyright (C) 2017 Red Hat, Inc. # # Cockpit is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # # Cockpit is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with Cockpit; If not, see . import argparse import os import shutil import subprocess import sys from task import github sys.dont_write_bytecode = True # Check out the given ref and if necessary overlay the bots # directory on top of it as expected on non-master branches BOTS = os.path.dirname(__file__) BASE = os.path.normpath(os.path.join(BOTS, "..")) TARGET_DIR = os.path.join(BOTS, "make-checkout-workdir") def main(): parser = argparse.ArgumentParser(description="Fetch and checkout specific revision") parser.add_argument("-v", "--verbose", action="store_true", help="Verbose output") parser.add_argument("--base", nargs='?', help="Base branch that revision is for") parser.add_argument("--repo", nargs='?', help="Repository to check out") parser.add_argument("--bots-ref", help="bots/ ref to check out from cockpit, for non-default --repo") parser.add_argument("ref", help="The git ref to fetch") parser.add_argument("revision", nargs='?', help="Actual commit to check out, defaults to ref") opts = parser.parse_args() if not opts.revision: opts.revision = "FETCH_HEAD" api = github.GitHub(repo=opts.repo) def execute(*args, cwd=BASE, error_on_fail=True): output = None if opts.verbose: sys.stderr.write("+ " + " ".join(args) + "\n") try: output = subprocess.check_output(args, cwd=cwd, universal_newlines=True) except subprocess.CalledProcessError as ex: sys.exit(ex.returncode if error_on_fail else 0) finally: if opts.verbose and output: sys.stderr.write("> " + output + "\n") return output if os.path.exists(TARGET_DIR): shutil.rmtree(TARGET_DIR) cache = os.getenv('XDG_CACHE_HOME', ".") execute("git", "clone", "--reference-if-able", "{}/{}".format(cache, api.repo), "https://github.com/{}".format(api.repo), TARGET_DIR) execute("git", "fetch", "origin", opts.ref, cwd=TARGET_DIR, error_on_fail=False) execute("git", "checkout", "--detach", opts.revision, cwd=TARGET_DIR, error_on_fail=False) # get bots/ if api.repo != "cockpit-project/cockpit" or (opts.base and opts.base != "master"): execute("git", "remote", "add", "bots_origin", "https://github.com/cockpit-project/cockpit", cwd=TARGET_DIR) execute("git", "fetch", "--depth=1", "bots_origin", opts.bots_ref or "master", cwd=TARGET_DIR) bots_ref = execute("git", "rev-parse", "FETCH_HEAD", cwd=TARGET_DIR).strip() execute("git", "checkout", "--force", bots_ref or "bots_origin/master", "--", "bots/", cwd=TARGET_DIR, error_on_fail=False) if __name__ == '__main__': sys.exit(main())