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
|
#!/usr/bin/env python
# Copyright (C) 2013 Linaro Ltd.
import argparse
import os
import re
import subprocess
from tempfile import gettempdir
# Match a directory that ends with .git, but not only .git
GIT_DIRECTORY_ENDS = re.compile(".+\.git$")
# Name for a lock file.
LOCK_FILE_NAME = "update-repost.lock"
LOCK_FILE = os.path.join(gettempdir(), LOCK_FILE_NAME)
def args_parser():
"""Sets up the argument parser."""
parser = argparse.ArgumentParser()
parser.add_argument("--repos-dir",
required=True,
help="The directory where repositories are stored.")
parser.add_argument("--user",
help="User to run the commands as.")
return parser
if __name__ == '__main__':
parser = args_parser()
args = parser.parse_args()
try:
if os.path.exists(LOCK_FILE):
print "Another process is still running: cannot acquire lock."
else:
with open(LOCK_FILE, 'w'):
for root, dirs, files in os.walk(os.path.abspath(args.repos_dir)):
if GIT_DIRECTORY_ENDS.match(root):
if files:
# We really are in a git repository.
cmd_args = []
if args.user:
cmd_args = ["sudo", "-u", args.user, "-H"]
cmd_args += ["git", "fetch", "--all", "-q"]
repo_name = os.path.basename(root)
process = subprocess.Popen(cmd_args,
cwd=root,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
print "Updating '%s' repository..." % repo_name
p_out, p_err = process.communicate()
if process.returncode != 0:
print ("Error fetching updates for repository '%s'" % \
repo_name)
else:
# git repositories always have a HEAD file, or it means they
# are empty.
continue
finally:
os.unlink(LOCK_FILE)
|