diff options
-rw-r--r-- | scripts/mirror-repos | 25 | ||||
-rw-r--r-- | scripts/update-repos | 25 |
2 files changed, 43 insertions, 7 deletions
diff --git a/scripts/mirror-repos b/scripts/mirror-repos index b75555f..d013b14 100644 --- a/scripts/mirror-repos +++ b/scripts/mirror-repos @@ -8,12 +8,18 @@ import sys import urlparse import pwd +from tempfile import gettempdir + + # Default read-only git URL. BASE_PATH = "http://git.linaro.org/git-ro/" # Path to local bin directory, %s is the user name. LOCAL_BIN_DIR = "/home/%s/.local/bin" # Default API host for RhodeCode. DEFAULT_API_HOST = "http://0.0.0.0:5000" +# Name for a lock file. +LOCK_FILE_NAME = "mirror-repos.lock" +LOCK_FILE = os.path.join(gettempdir(), LOCK_FILE_NAME) def args_parser(): @@ -159,7 +165,20 @@ if __name__ == '__main__': parser = args_parser() args = parser.parse_args() check_args(args, parser) - mirror_repos(args.repos_list, args.checkout_dir, user=args.user) - if args.rescan_repos: - rescan_git_directory(args.api_key, args.api_host, user=args.user) + if os.path.exists(LOCK_FILE): + print "Another process is still running: cannot acquire lock." + else: + try: + with open(LOCK_FILE, 'w'): + mirror_repos(args.repos_list, + args.checkout_dir, + user=args.user) + + if args.rescan_repos: + print "Re-scanning git repositories directory..." + rescan_git_directory(args.api_key, + args.api_host, + user=args.user) + finally: + os.unlink(LOCK_FILE) diff --git a/scripts/update-repos b/scripts/update-repos index 73a6539..154e0fa 100644 --- a/scripts/update-repos +++ b/scripts/update-repos @@ -6,9 +6,14 @@ 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-repos.lock" +LOCK_FILE = os.path.join(gettempdir(), LOCK_FILE_NAME) def args_parser(): @@ -22,10 +27,8 @@ def args_parser(): return parser -if __name__ == '__main__': - parser = args_parser() - args = parser.parse_args() - +def fetch_updates(): + """Traverse the file system and fetc updates.""" for root, dirs, files in os.walk(os.path.abspath(args.repos_dir)): if GIT_DIRECTORY_ENDS.match(root): if files: @@ -49,3 +52,17 @@ if __name__ == '__main__': # git repositories always have a HEAD file, or it means they # are empty. continue + + +if __name__ == '__main__': + parser = args_parser() + args = parser.parse_args() + + if os.path.exists(LOCK_FILE): + print "Another process is still running: cannot acquire lock." + else: + try: + with open(LOCK_FILE, 'w'): + fetch_updates() + finally: + os.unlink(LOCK_FILE) |