forked from clintmod/macprefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.py
133 lines (107 loc) · 4.03 KB
/
publish.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
import sys
import glob
import os
import json
import urllib2
import urllib
from utils import execute_shell, is_none_or_empty_string
from version import __version__
def check_for_uncommitted_files():
print 'Checking for uncommitted files...'
result = execute_shell(['git', 'status'])
if not 'nothing to commit' in result:
raise ValueError(
'There are uncommitted files in the workspace. Commit or stash them before trying to publish.')
def create_version_tag_and_push(tag):
print 'Tagging git repository with version ' + tag
execute_shell(['git', 'tag', tag])
print 'Pushing the new tag to github...'
execute_shell(['git', 'push', 'origin', 'HEAD', '--tags'])
def download_tar(filename):
print 'Downloading the new version...'
urllib.urlretrieve(
'https://github.com/clintmod/macprefs/archive/' + filename, filename)
def calc_sha256(filename):
print 'Calculating the sha256 of the tarball...'
result = execute_shell(['shasum', '-a', '256', filename])
print result
sha256 = result.split(' ')[0]
print sha256
return sha256
def create_brew_formula_file_content(version, sha256):
print 'Generating base64 encoded brew formula...'
# Read in the file
with open('macprefs.template.rb', 'r') as f:
filedata = f.read()
# Replace
filedata = filedata.replace('###sha256###', sha256)
filedata = filedata.replace('###version###', version)
filedata = filedata.encode('base64').replace('\n', '')
return filedata
def get_sha_of_old_macprefs_formula():
print 'Getting sha of old macprefs formula from github...'
result = json.load(urllib2.urlopen(
'https://api.github.com/repos/clintmod/homebrew-formulas/contents/Formula/macprefs.rb'))
# print 'sha = ' + result['sha']
return result['sha']
def upload_new_brew_formula(content, version, sha):
print 'Uploading the new macprefs formula to https://github.com/clintmod/homebrew-formulas'
token = os.environ['MACPREFS_TOKEN']
auth_header = 'Authorization: token ' + token
json_header = 'Content-Type: application/json'
data = '{"path": "Formula/macprefs.rb", "message": "Updating to version ' + version + '", '
data += '"committer": {"name": "Clint M", "email": "[email protected]"}, '
data += '"content": "' + content + '", "branch": "master", "sha":"' + sha + '"}'
with open('github_request.json', 'w') as f:
f.write(data)
commands = [
'curl',
'-i',
'-X',
'PUT',
'-H',
auth_header,
'-H',
json_header,
'-d',
'@github_request.json',
'https://api.github.com/repos/clintmod/homebrew-formulas/contents/Formula/macprefs.rb'
]
result = execute_shell(commands)
if 'Status: 200 OK' not in result:
raise ValueError('Error uploading new brew formula to github - result\n', result)
return data
def cleanup():
print 'Cleaning up...'
for f in glob.glob('*.tar.gz'):
os.remove(f)
os.remove('github_request.json')
def download_macprefs():
print 'Running brew update macprefs to verify version...'
result = execute_shell(['brew', 'upgrade', 'macprefs'], False, '.', True)
if not is_none_or_empty_string(result):
print result
def verify_macprefs():
result = execute_shell(['macprefs', '--version'])
message = '\nworkspace:\t' + __version__ + '\ninstalled:\t' + result
assert __version__ in result, message
print 'version check verified' + message
def main():
if len(sys.argv) > 1 and sys.argv[1] == '-test':
return False
version = __version__
check_for_uncommitted_files()
create_version_tag_and_push(version)
filename = version + '.tar.gz'
download_tar(filename)
sha256 = calc_sha256(filename)
content = create_brew_formula_file_content(version, sha256)
sha = get_sha_of_old_macprefs_formula()
upload_new_brew_formula(content, version, sha)
cleanup()
download_macprefs()
verify_macprefs()
print 'Success'
return True
if __name__ == '__main__':
main()