-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelegator.py
83 lines (67 loc) · 2.31 KB
/
delegator.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
76
77
78
79
80
81
82
83
import json
EXECUTABLE_BRANCHES = ['refs/heads/master']
class Delegator():
# Github payload
payload_repo = None
payload_branch = None
# Local Repo
local_branch = None
local_base_dir = None
local_json = None
# Actions
action = None
def __init__(self, payload):
payload_json = json.loads(payload)
self.payload_repo = payload_json['repository']['name']
if 'ref' in payload_json:
self.payload_branch = payload_json['ref']
self.local_json = self.parse_repo()
print self.local_json
print self.payload_repo
if self.local_json:
self.local_base_dir = self.local_json['base_dir']
self.local_branch = self.local_json['branch']
if self.payload_branch == self.local_branch or self.payload_branch in self.local_branch:
self.action = RepoActions(self.payload_repo, self.local_base_dir, self.payload_branch, self.local_json['cmds'], self.local_json['env'])
else:
print self.payload_branch
print self.local_branch
# Take no action
self.action = False
else:
print 'Delegator: NO MATCHING REPO'
# Take no action
self.action = False
@staticmethod
def get_repos():
with open('repos') as data:
json_data = json.load(data)
data.close()
return json_data
def parse_repo(self):
if self.payload_repo in Delegator.get_repos():
return Delegator.get_repos()[self.payload_repo]
return False
class RepoActions(object):
repo_name = None
base_dir = None
branch = None
cmds = []
env = None
def __init__(self, name, base, branch, cmds, env):
self.repo_name = name
self.base_dir = base
self.branch = branch
self.env = env
for cmd in cmds:
self.cmds.append(Commands(cmd['dir'], cmd['cmd']))
class Commands(object):
directory = None
cmd = None
def __init__(self, directory, cmd):
self.directory = directory
self.cmd = cmd
def cmd(self):
return u'cd %s && %s' % (self.directory, self.cmd)
def __str__(self):
return u'cd %s && %s' % (self.directory, self.cmd)