Add job_local_cache_cleanups.py to make local cache cleanups
authorLingchao Xin <lingchaox.xin@intel.com>
Sat, 27 Apr 2013 07:41:46 +0000 (15:41 +0800)
committerLingchaox Xin <lingchaox.xin@intel.com>
Wed, 8 May 2013 02:17:25 +0000 (10:17 +0800)
Thanks for Ed's a lot of helpful advice, such as:
directory walking, env judgement and so on.

Change-Id: I39f69eb64ae774e4182e9779514eb4449dbee6b2

job_local_cache_cleanups.py [new file with mode: 0755]

diff --git a/job_local_cache_cleanups.py b/job_local_cache_cleanups.py
new file mode 100755 (executable)
index 0000000..0cf60b1
--- /dev/null
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+
+"""This script is used to delete local gerrit project when remote is deleted"""
+
+import os
+import shutil
+import sys
+
+from common.gerrit import Gerrit
+
+def find_local_projects(work_dir):
+    """Find gerrit projects in local git dirs"""
+
+    projects = []
+
+    for path in os.listdir(work_dir):
+        abs_path = os.path.abspath(os.path.join(work_dir, path))
+        if os.path.isdir(abs_path):
+            if os.path.exists(os.path.join(abs_path, '.git')):
+                projects.append(abs_path)
+            else:
+                projects.extend(find_local_projects(abs_path))
+
+    return projects
+
+def main():
+    """The main body"""
+
+    for env in ('GIT_CACHE_DIR', 'GERRIT_HOSTNAME', 'GERRIT_USERNAME'):
+        if env not in os.environ:
+            print >> sys.stderr, 'Environment variable %s is not defined' % env
+            return -1
+
+    work_dir = os.getenv('GIT_CACHE_DIR')
+
+    local_projects = set([project[len(work_dir)+1:] for \
+            project in find_local_projects(work_dir)])
+
+    remote_projects = set(Gerrit(os.getenv('GERRIT_HOSTNAME'), \
+            os.getenv('GERRIT_USERNAME')).ls_projects())
+
+    diff_projects = local_projects.difference(remote_projects)
+    # debug information
+    print 'Differences are: %s' % diff_projects
+
+    for project in diff_projects:
+        shutil.rmtree(os.path.join(work_dir, project))
+        print '%s is deleted ...' % project
+
+if __name__ == '__main__':
+    sys.exit(main())