aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--scripts/update-repos57
1 files changed, 34 insertions, 23 deletions
diff --git a/scripts/update-repos b/scripts/update-repos
index 73a6539..b31810b 100644
--- a/scripts/update-repos
+++ b/scripts/update-repos
@@ -5,10 +5,14 @@ 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():
@@ -26,26 +30,33 @@ if __name__ == '__main__':
parser = args_parser()
args = parser.parse_args()
- 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
+ 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)