-
Notifications
You must be signed in to change notification settings - Fork 27
/
upload-bintray-distribution
executable file
·125 lines (115 loc) · 4.15 KB
/
upload-bintray-distribution
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
#!/usr/bin/env python
from argparse import ArgumentParser
import yaml, urllib2
import requests
from os.path import expanduser, basename
import base64
import json
API="https://bintray.com/api/v1"
def format(s, **kwds):
return s % kwds
def http(method, url, data):
request = urllib2.Request(url)
request.get_method = lambda: method
request.add_header("Content-Type", "application/json")
request.add_header("Accept", "application/json")
print request
try:
return urllib2.urlopen(request, data).read()
except urllib2.HTTPError, e:
print e
print e.headers
print e.headers.has_key('WWW-Authenticate')
# Takes a YAML file as configuration with the following format:
#
# ```
# architecture: ...
# organization: ...
# packages:
# - name: ...
# version: ...
# file: ...
# level: ...
# ```
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("config")
parser.add_argument("--user", "-u", dest="user")
args = parser.parse_args()
if not args.user:
parser.error("Please specify a user.")
config = yaml.safe_load(file(args.config).read())
print config
secret = file(expanduser("~/.bintray-secret"), "r").read().strip()
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
# this creates a password manager
print args.user, secret
passman.add_password(None, API, args.user, secret)
# because we have put None at the start it will always
# use this username/password combination for urls
# for which `theurl` is a super-url
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
create_repo_url = format("%(api)s/repos/%(o)s/%(a)s-%(l)s",
api=API,
a=config["architecture"],
o=config["organization"],
l=config["level"])
create_repo_payload = json.dumps({
"type": config["format"],
"private": False,
"premium": False,
"desc": config["description"],
"labels": config["labels"]
})
print create_repo_url
print create_repo_payload
result = http("POST", create_repo_url, create_repo_payload)
print result
for package in config["packages"]:
print package
create_package_url = format("%(api)s/packages/%(o)s/%(a)s-%(l)s",
api=API,
a=config["architecture"],
o=config["organization"],
l=config["level"])
create_package_payload = json.dumps({
"name": package["name"],
"desc": package.get("description", ""),
"labels": package["labels"],
"licenses": package["licenses"],
"vcs_url": package["vcs_url"],
"website_url": package.get("website_url", ""),
"issue_tracker_url": package.get("issue_tracker_url", ""),
"github_repo": package.get("github_repo", ""),
"github_release_notes_file": package.get("github_release_notes_file", ""),
"public_download_numbers": True,
"public_stats": True
})
print create_package_url
print create_package_payload
http("POST", create_package_url, create_package_payload)
for package in config["packages"]:
upload_package_url = format("%(api)s/content/%(o)s/%(a)s-%(l)s/%(n)s/%(v)s/%(f)s?publish=1&override=1",
api=API,
a=config["architecture"],
o=config["organization"],
l=config["level"],
n=package["name"],
v=package["version"],
f=basename(package["file"]))
headers = {}
if config["format"] == "deb":
headers = {
"X-Bintray-Debian-Distribution": "trusty",
"X-Bintray-Debian-Component": "main",
"X-Bintray-Debian-Architecture": "amd64"
}
print upload_package_url
result = requests.put(upload_package_url,
data=open(package["file"], "rb"),
auth=(args.user, secret),
headers=headers)
print result.status_code
print result.content