-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy.py
executable file
·244 lines (210 loc) · 8.14 KB
/
deploy.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Administrate LibrePCB Libraries
Usage:
deploy.py [--token=<token>] [--upgrade=<version>] [--apply] [<library>...]
deploy.py (-h | --help)
Options:
-h --help Show this screen.
--token=<token> GitHub API token.
--upgrade=<version> Upgrade the libraries to a new file format using the
given tag for the librepcb/librepcb-cli Docker image.
--apply Apply the required changes.
<library> Name of the library repositories to deploy (e.g.
"LibrePCB_Base.lplib"). If none is passed, all
libraries are deployed.
"""
import os
import json
from shutil import copytree
from subprocess import check_call, check_output
from docopt import docopt
from github import Github, Label
ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
FILES_DIR = os.path.join(ROOT_DIR, 'files')
CACHE_DIR = os.path.join(ROOT_DIR, 'cache')
LABELS = {
'addition': {
'description': 'New library element.',
'color': 'eae435',
},
'enhancement': {
'description': 'Improving an existing library element.',
'color': 'a2eeef',
},
'bug': {
'description': 'An existing library element contains issues.',
'color': 'd73a4a',
},
'fix': {
'description': 'Fix an error in an existing library element.',
'color': 'e99695',
},
'ready for review': {
'description': 'Waiting for review by maintainers.',
'color': '2164e0',
},
'needs corrections': {
'description': 'Pull request needs corrections before next review.',
'color': 'c9adff',
},
'blocked': {
'description': 'Blocked by other, dependent pull requests or issues.',
'color': '353535',
},
}
def deploy_labels(repo, apply):
existing_labels = []
for label in repo.get_labels():
existing_labels.append(label.name)
if label.name in LABELS:
update = False
# description
new_description = LABELS[label.name]['description']
if label.description != new_description:
print(' - CHANGE label description "{}": "{}" -> "{}"'.format(
label.name, label.description, new_description))
update = True
# color
new_color = LABELS[label.name]['color']
if label.color != new_color:
print(' - CHANGE label color "{}": "{}" -> "{}"'.format(
label.name, label.color, new_color))
update = True
if update and apply:
label.edit(name=label.name, color=new_color,
description=new_description)
else:
# remove
print(' - REMOVE label "{}"'.format(label.name))
if apply:
label.delete()
for name, properties in LABELS.items():
if name not in existing_labels:
print(' - ADD label "{}"'.format(name))
if apply:
repo.create_label(name=name, color=properties['color'],
description=properties['description'])
def deploy_settings(repo, apply):
update = False
if repo.has_issues != True:
print(' - CHANGE has_issues')
update = True
if repo.has_projects != False:
print(' - CHANGE has_projects')
update = True
if repo.has_wiki != False:
print(' - CHANGE has_wiki')
update = True
if repo.delete_branch_on_merge != True:
print(' - CHANGE delete_branch_on_merge')
update = True
if repo.default_branch != 'master':
print(' - CHANGE default_branch')
update = True
if update and apply:
repo.edit(
has_issues=True,
has_projects=False,
has_wiki=False,
delete_branch_on_merge=True,
default_branch='master',
)
def deploy_branch_protection(repo, apply):
update = False
branch = repo.get_branch("master")
if branch.protected != True:
print(' - CHANGE protected')
update = True
if branch.protected:
protection = branch.get_protection()
if protection.enforce_admins != True:
print(' - CHANGE enforce_admins')
update = True
if update and apply:
branch.edit_protection(
enforce_admins=True,
)
def checkout(repo, branch_name):
repo_dir = os.path.join(CACHE_DIR, repo.name)
if not os.path.isdir(repo_dir):
check_call(['git', 'clone', '-q', repo.ssh_url], cwd=CACHE_DIR)
else:
check_call(['git', 'reset', '-q', '--hard', 'master'], cwd=repo_dir)
check_call(['git', 'clean', '-q', '-f', '-d', '-x'], cwd=repo_dir)
check_call(['git', 'checkout', '-q', 'master'], cwd=repo_dir)
check_call(['git', 'pull', '-q'], cwd=repo_dir)
check_call(['git', 'checkout', '-q', '-B', branch_name], cwd=repo_dir)
return repo_dir
def commit_local_changes(repo_dir, message):
check_call(['git', 'add', '--all'], cwd=repo_dir)
changes = check_output(['git', 'status', '--porcelain'], cwd=repo_dir).\
decode('utf-8').strip()
if len(changes) > 0:
print(' ' + changes.replace('\n', '\n '))
check_call(['git', 'commit', '-q', '-m', message], cwd=repo_dir)
return len(changes)
def upgrade_file_format(repo_dir, image_tag, commit_msg):
lib_name = os.path.basename(repo_dir)
check_call([
'docker', 'run', '-i', '-t', '--rm',
'-u', str(os.getuid()) + ':' + str(os.getgid()),
'-v', str(repo_dir) + ':/work/' + lib_name,
'librepcb/librepcb-cli:' + image_tag,
'open-library', '--all', '--save', '/work/' + lib_name
])
return commit_local_changes(repo_dir, commit_msg)
def update_files(repo_dir):
copytree(FILES_DIR, repo_dir, dirs_exist_ok=True)
return commit_local_changes(repo_dir, 'Update from template')
def deploy_local_changes(repo, repo_dir, branch_name, pr_title, changes, apply):
pr_body = '*Automatically created pull request to update some files to ' \
'their latest version from [librepcb-libraries-admin]' \
'(https://github.com/LibrePCB/librepcb-libraries-admin).*'
if (changes > 0) and apply:
check_call(['git', 'push', '-q', '-f', 'origin', branch_name],
cwd=repo_dir)
pr = repo.get_pulls(state='open', head='LibrePCB-Libraries:' + branch_name,
base='master')
if (changes > 0) and (pr.totalCount == 0):
print(' OPEN pull request')
if apply:
pr = repo.create_pull(title=pr_title, body=pr_body,
head=branch_name, base='master')
pr.add_to_labels('ready for review')
def deploy_repo(repo, upgrade_version, branch_name, pr_title, apply):
print(repo.name + ':')
deploy_labels(repo, apply)
deploy_settings(repo, apply)
deploy_branch_protection(repo, apply)
changes = 0
repo_dir = checkout(repo, branch_name)
if upgrade_version:
changes += upgrade_file_format(repo_dir, upgrade_version, pr_title)
changes += update_files(repo_dir)
deploy_local_changes(repo, repo_dir, branch_name, pr_title, changes, apply)
def deploy(config):
gh = Github(config['--token'])
org = gh.get_organization('LibrePCB-Libraries')
upgrade_tag = config.get('--upgrade')
if upgrade_tag:
branch_name = 'upgrade-file-format'
pr_title = 'Upgrade to file format v{}'.format(
upgrade_tag.split('.')[0])
else:
branch_name = 'update-from-template'
pr_title = "Update from template"
libraries = config['<library>']
for repo in org.get_repos():
if (repo.name in libraries) or (not libraries):
deploy_repo(repo, upgrade_tag, branch_name, pr_title,
config['--apply'])
if __name__ == '__main__':
try:
options = json.load(open('options.json', mode='r'))
except:
options = {}
args = docopt(__doc__)
config = dict((str(key), options.get(key) or args.get(key))
for key in set(args) | set(options))
deploy(config)