aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilo Casagrande <milo@ubuntu.com>2013-01-30 13:48:55 +0100
committerMilo Casagrande <milo@ubuntu.com>2013-01-30 13:48:55 +0100
commit32c1fbcb5b5e5eb8d2eafd1f1294bb5a4a70c178 (patch)
tree65e3ac5463022106fe4ffc1aa44c4fd4c06181eb
parentb73d49839dca960cfe2715163b8553e9d9b2f388 (diff)
downloadrhodecode-config-32c1fbcb5b5e5eb8d2eafd1f1294bb5a4a70c178.tar.gz
Added simple mechanism for locking process.
-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)