Structure to init and manage my repositories
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

init.py 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import json
  2. import os
  3. class repo(dict):
  4. KEY_URL = "URL"
  5. KEY_TARGET = "TARGET"
  6. INIT_SCRIPTNAME = "reposinit"
  7. def __init__(self, data, base_url):
  8. self.__base_url__ = base_url
  9. dict.__init__(self, data)
  10. def print_gn(self, txt):
  11. print("\033[1;32m" + txt + "\033[00m")
  12. def print_ye(self, txt):
  13. print("\033[1;33m" + txt + "\033[00m")
  14. def init_repo(self):
  15. if os.path.exists(self.target_path()):
  16. self.print_ye("skip: Cloning repository %s (already exists)..." % self.repo_url())
  17. else:
  18. self.print_gn("clone: Cloning repository %s..." % self.repo_url())
  19. os.system("git clone %s %s" % (self.url(), self.target_path()))
  20. if os.path.exists(os.path.join(self.target_path(), self.INIT_SCRIPTNAME)):
  21. self.print_gn("exec: Executing init script for repository %s..." % self.repo_url())
  22. os.system("cd %s; ./%s" % (self.target_path(), self.INIT_SCRIPTNAME))
  23. print()
  24. def submodules(self):
  25. return self[self.KEY_SUBMODULES]
  26. def url(self):
  27. return self.__base_url__ + '/' + self.repo_url()
  28. def repo_url(self):
  29. return self[self.KEY_URL]
  30. def target_path(self):
  31. 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])
  32. class repositories(dict):
  33. KEY_BASEURL = "BASE_URL"
  34. KEY_REPOS = "REPO_LIST"
  35. def __init__(self, filename):
  36. with open(filename, 'r') as fh:
  37. data = json.loads(fh.read())
  38. for i in range(0, len(data[self.KEY_REPOS])):
  39. data[self.KEY_REPOS][i] = repo(data[self.KEY_REPOS][i], data[self.KEY_BASEURL])
  40. dict.__init__(self, data)
  41. def repolist(self):
  42. return self[self.KEY_REPOS]
  43. if __name__ == "__main__":
  44. rl = repositories("repos.json")
  45. for r in rl.repolist():
  46. r.init_repo()