forked from cockpit-project/bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-checkout
executable file
·84 lines (71 loc) · 3.54 KB
/
make-checkout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/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 <http://www.gnu.org/licenses/>.
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.realpath(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/
bots_clone_dir = os.path.join(TARGET_DIR, "bots")
if opts.bots_ref:
# TODO: remove this when cockpit-project/cockpit#12872 has landed
if os.path.exists(bots_clone_dir):
shutil.rmtree(bots_clone_dir)
execute("git", "clone", "--reference-if-able", "{}/{}".format(cache, "cockpit-project/bots"), "https://github.com/cockpit-project/bots", bots_clone_dir, cwd=TARGET_DIR)
execute("git", "fetch", "--depth=1", "origin", opts.bots_ref or "master", cwd=bots_clone_dir)
bots_ref = execute("git", "rev-parse", "FETCH_HEAD", cwd=bots_clone_dir).strip()
execute("git", "checkout", "--force", bots_ref or "bots_origin/master", cwd=bots_clone_dir, error_on_fail=False)
if __name__ == '__main__':
sys.exit(main())