-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload-converted-svn.py
executable file
·106 lines (84 loc) · 3.72 KB
/
upload-converted-svn.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
#!/usr/bin/python
import sys, os.path, subprocess, urllib2, json
sys.path.append("/home/ai/src/wesnoth-git/data/tools/")
import wesnoth.libgithub as libgithub
upload_dirs = []
def prepare_repo(repo):
name, version = os.path.basename(repo).split("-")
versiondir = os.path.join("/tmp", "wescamp-upload", version)
if versiondir not in upload_dirs:
upload_dirs.append(versiondir)
if not os.path.exists(versiondir):
os.makedirs(versiondir)
execute(["git", "clone", repo, os.path.join(versiondir, name)], versiondir)
def upload_versiondir(versiondir):
github = libgithub.GitHub(versiondir, os.path.basename(versiondir))
for addon in os.listdir(versiondir):
addon = github.addon(addon)
reponame = "{0}-{1}".format(addon.name, addon.github.version)
print "Upload add-on {0} version {1} from dir {2}".format(addon.name, addon.github.version, addon._get_dir())
execute(["git", "remote", "set-url", "origin", "[email protected]:wescamp/{0}".format(reponame)], addon._get_dir())
create_remote(reponame)
addon.commit("Initial upload")
_GITHUB_API_BASE = "https://api.github.com/"
_GITHUB_API_REPOS = "orgs/wescamp/repos"
_GITHUB_API_TEAMS = "orgs/wescamp/teams"
# PUT /teams/:id/repos/:org/:repo
_GITHUB_API_TEAM_REPO = "teams/{0}/repos/wescamp/{1}"
def create_remote(reponame):
url = _GITHUB_API_BASE + _GITHUB_API_REPOS
request = urllib2.Request(url)
requestdata = { "name" : reponame }
repodata = github_api_request(request, requestdata, authenticate=True)
url = _GITHUB_API_BASE + _GITHUB_API_TEAMS
request = urllib2.Request(url)
teams = github_api_request(request, authenticate=True)
# This can probably be cleaner
team_number = [team["id"] for team in teams if team["name"] == "Developers"][0]
# PUT /teams/:id/repos/:org/:repo
url = _GITHUB_API_BASE + _GITHUB_API_TEAM_REPO
request = urllib2.Request(url.format(team_number, reponame))
request.get_method = lambda: "PUT"
# Github requires data for every modifying request, even if there is none
github_api_request(request, data="", authenticate=True)
def github_api_request(request, data=None, authenticate=False):
if data == "":
# Workaround for PUTs requiring data, even if you have nothing to pass
request.add_data(data)
elif data:
request.add_data(json.dumps(data))
# Manually adding authentication data
# Basic works in curl, but urllib2 doesn't
# probably because github's API doesn't send a www-authenticate header
if authenticate:
from base64 import encodestring
base64string = encodestring(github_userpass()).replace('\n','')
request.add_header("Authorization", "Basic {0}".format(base64string))
try:
response = urllib2.urlopen(request)
except IOError as e:
raise libgithub.Error("GitHub API failure: " + str(e))
if response.code == 204:
# 204 = No content
return None
else:
return json.load(response)
def github_userpass():
raise libgithub.Error("Replace this line with something returning a user:pass string")
def execute(cmd, cwd=None):
p = subprocess.Popen(cmd, close_fds=True, cwd=cwd)
while(p.poll() == None):
pass
if __name__ == "__main__":
if len(sys.argv) < 2:
print "Usage: {0} <things to upload> ...".format(sys.argv[0])
sys.exit(1)
for repo in sys.argv[1:]:
if not os.path.isabs(repo):
print "Rejecting {0}: not an absolute path".format(repo)
sys.exit(1)
for repo in sys.argv[1:]:
prepare_repo(os.path.realpath(repo))
for versiondir in upload_dirs:
print "Uploading from dir {0}".format(versiondir)
upload_versiondir(versiondir)