--- /dev/null
+#!/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())