From 32c1fbcb5b5e5eb8d2eafd1f1294bb5a4a70c178 Mon Sep 17 00:00:00 2001 From: Milo Casagrande Date: Wed, 30 Jan 2013 13:48:55 +0100 Subject: Added simple mechanism for locking process. --- scripts/update-repos | 57 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 23 deletions(-) (limited to 'scripts') 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) -- cgit v1.2.3