-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeman.py
executable file
·75 lines (65 loc) · 2.79 KB
/
codeman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
import os
import requests
import yaml
import signal
from requests.auth import HTTPBasicAuth
class Config:
def __init__(self, config_file):
self.organisation = None
self.repos_per_page = None
self.basedir = None
self.username = None
self.password = None
with open(config_file, "r") as stream:
self.raw_config = yaml.safe_load(stream)
self.organisation = self.raw_config["organisation"]
self.username = self.raw_config["username"]
self.password = self.raw_config["password"]
self.basedir = self.raw_config["baseDir"]
self.repos_per_page = self.raw_config["reposPerCall"]
config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "conf")
config_file = os.path.join(config_dir, "config.yaml")
config = Config(config_file)
proceed = True
page = 1
def signal_handler(sig, frame):
global proceed
print('Aborting...')
proceed = False
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
while proceed:
response = requests.get(
"https://github.com/gitapi/orgs/{organisation}/repos?per_page={per_page}&page={page}"
.format(organisation=config.organisation,
per_page=config.repos_per_page,
page=page),
auth=HTTPBasicAuth(config.username, config.password))
repositories = response.json()
if response.status_code != 200 or isinstance(response.json(), str):
print("api call failed: \n{response}".format(response=response.text))
exit(1)
else:
page += 1
if len(repositories) == 0:
proceed = False
else:
for repo in repositories:
if not proceed:
exit(1)
ssh_url = repo.get("ssh_url")
project = ssh_url.split("/")[-1].split(".")[0]
fullpath = "{basedir}/{project}".format(basedir=config.basedir, project=project)
if not os.path.exists(fullpath):
print("cloning {project}".format(project=project))
git_command = "git clone {url} {fullpath}".format(url=ssh_url, fullpath=fullpath)
os.system("echo {git_command}".format(git_command=git_command))
os.system(git_command)
else:
print("updating {project}".format(project=project))
for command in ["fetch --all", "checkout master", "pull"]:
git_command = "git -C {fullpath} {command}".format(fullpath=fullpath, command=command)
os.system("echo {git_command}".format(git_command=git_command))
if 0 != os.system(git_command):
break