-
Notifications
You must be signed in to change notification settings - Fork 1
/
amo.py
executable file
·133 lines (101 loc) · 3.45 KB
/
amo.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
#!/usr/bin/python
import argparse, certifi, httplib, json, jwt, os, random, subprocess, time, urllib3, xpifile
http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())
def _token():
with open(os.path.join(os.path.dirname(__file__), '.amorc'), 'r') as f:
j = json.load(f)
user = j['user']
secret = j['secret']
issuedAt = int(time.time())
payload = {
'iss': user,
'jti': random.random(),
'iat': issuedAt,
'exp': issuedAt + 60,
}
return jwt.encode(payload, secret, algorithm='HS256')
def upload(filepath):
if not filepath.endswith('.xpi'):
print 'Refusing to upload a non-xpi'
exit(1)
guid, version = xpifile.get_guid_and_version(filepath)
method = 'PUT'
path = 'https://addons.mozilla.org/api/v3/addons/%s/versions/%s/' % (guid, version)
headers = {
'Authorization': 'JWT %s' % _token()
}
fields = {
'upload': (os.path.basename(filepath), open(filepath, 'rb').read())
}
# print method
# print path
# print headers
# return
response = http.request_encode_body(method, path, headers=headers, fields=fields)
print response.status
# print response.getheaders()
print response.data
def check_status(filepath):
guid, version = xpifile.get_guid_and_version(filepath)
method = 'GET'
path = 'https://addons.mozilla.org/api/v3/addons/%s/versions/%s/' % (guid, version)
headers = {
'Authorization': 'JWT %s' % _token()
}
return http.request(method, path, headers=headers)
def download(filepath):
response = check_status(filepath)
if response.status != httplib.OK:
print response.status
print response.data
exit(1)
method = 'GET'
path = json.loads(response.data)['files'][0]['download_url']
headers = {
'Authorization': 'JWT %s' % _token()
}
response = http.request(method, path, headers=headers)
print response.status
print response.getheaders()
with open(os.path.basename(path).replace('?src=api', ''), 'wb') as f:
f.write(response.data)
def relnotes(filepath):
(guid, version,) = xpifile.get_guid_and_version(filepath)
stub = xpifile.get_amo_stub(guid)
# path = 'https://addons.mozilla.org/api/v3/addons/addon/%s/versions/' % stub
# response = http.request('GET', path)
# for result in json.loads(response.data)['results']:
# if result['version'] == version:
# print result['edit_url']
# return
print 'https://addons.mozilla.org/en-US/developers/addon/%s/versions' % stub
with open(os.path.join(os.path.dirname(__file__), '.amorc'), 'r') as f:
j = json.load(f)
repo = j['repos'].get(guid, None)
if repo is None:
return
new_tag = subprocess.check_output(['git', 'describe', '--tags']).splitlines()[0]
old_tag = subprocess.check_output(['git', 'describe', '--tags', '--always', 'HEAD^']).splitlines()[0]
old_tag = old_tag.split('-')[0]
print 'https://github.com/darktrojan/%s/compare/%s...%s' % (repo, old_tag, new_tag)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('action', choices=('upload', 'check', 'download', 'relnotes',))
parser.add_argument('path', nargs='?')
args = parser.parse_args()
path = os.getcwd()
if args.path is None:
if not os.path.exists('install.rdf') and os.path.exists('webextension'):
path = os.path.join(path, 'webextension')
else:
path = os.path.join(path, args.path)
if args.action == 'upload':
upload(path)
elif args.action == 'check':
response = check_status(path)
print response.status
print response.data
elif args.action == 'download':
download(path)
elif args.action == 'relnotes':
relnotes(path)