import json import os class repo(dict): KEY_URL = "URL" KEY_TARGET = "TARGET" INIT_SCRIPTNAME = "reposinit" def __init__(self, data, base_url): self.__base_url__ = base_url dict.__init__(self, data) def print_gn(self, txt): print("\033[1;32m" + txt + "\033[00m") def print_ye(self, txt): print("\033[1;33m" + txt + "\033[00m") def init_repo(self): if os.path.exists(self.target_path()): self.print_ye("skip: Cloning repository %s (already exists)..." % self.repo_url()) else: self.print_gn("clone: Cloning repository %s..." % self.repo_url()) os.system("git clone %s %s" % (self.url(), self.target_path())) if os.path.exists(os.path.join(self.target_path(), self.INIT_SCRIPTNAME)): self.print_gn("exec: Executing init script for repository %s..." % self.repo_url()) os.system("cd %s; ./%s" % (self.target_path(), self.INIT_SCRIPTNAME)) print() def submodules(self): return self[self.KEY_SUBMODULES] def url(self): return self.__base_url__ + '/' + self.repo_url() def repo_url(self): return self[self.KEY_URL] def target_path(self): return os.path.join(self[self.KEY_TARGET], os.path.basename(self.url()) if not os.path.basename(self.url()).lower().endswith('.git') else os.path.basename(self.url())[:-4]) class repositories(dict): KEY_BASEURL = "BASE_URL" KEY_REPOS = "REPO_LIST" def __init__(self, filename): with open(filename, 'r') as fh: data = json.loads(fh.read()) for i in range(0, len(data[self.KEY_REPOS])): data[self.KEY_REPOS][i] = repo(data[self.KEY_REPOS][i], data[self.KEY_BASEURL]) dict.__init__(self, data) def repolist(self): return self[self.KEY_REPOS] if __name__ == "__main__": rl = repositories("repos.json") for r in rl.repolist(): r.init_repo()