-
Notifications
You must be signed in to change notification settings - Fork 112
/
get-reposense.py
executable file
·129 lines (99 loc) · 4.05 KB
/
get-reposense.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
#!/usr/bin/env python
import argparse
import sys
import os
import shutil
import requests
import subprocess
JAR_FILENAME = 'RepoSense.jar'
def parse_args():
parser = argparse.ArgumentParser(description='Downloads a specific version of RepoSense.jar from our repository.')
group = parser.add_mutually_exclusive_group()
group.add_argument('-r', '--release', action='store_true', help='Get RepoSense.jar from the latest release (Stable)')
group.add_argument('-m', '--master', action='store_true', help='Get RepoSense.jar from the latest master (Beta)')
group.add_argument('-t', '--tag', help='Get RepoSense.jar of a specific release version tag')
group.add_argument('-l', '--latest', help='Get RepoSense.jar of the latest release of a specific version tag')
group.add_argument('-c', '--commit', help='Get RepoSense.jar of a specific commit')
parser.add_argument('-o', '--overwrite', action='store_true', help='Overwrite RepoSense.jar file, if exists. Default: false')
return parser.parse_args()
def handle_latest_tag(tag):
page = 1
major = tag.strip('. ').split('.')
while True:
response = requests.get(f'https://api.github.com/repos/reposense/RepoSense/releases?per_page=100&page={page}')
if response.status_code in [403, 500]:
print('GitHub API has exceed the rate limit.')
exit(1)
releases = response.json()
if not releases:
print('Error, the provided tag does not exist!')
exit(1)
for i in releases:
release_tag = i['tag_name']
if release_tag.strip('. ').split('.')[:len(major)] == major:
handle_specific_release(release_tag)
exit()
page += 1
def handle_specific_commit(commit):
get_reposense_jar('https://api.github.com/repos/reposense/RepoSense/commits/' + commit, commit=commit)
def handle_specific_release(tag):
get_reposense_jar('https://api.github.com/repos/reposense/RepoSense/releases/tags/' + tag, tag=tag)
def handle_latest_release():
get_reposense_jar('https://api.github.com/repos/reposense/RepoSense/releases/latest')
def get_reposense_jar(url, tag=None, commit=None):
response = requests.get(url)
if tag and response.status_code == 404:
print('Error, the provided tag does not exist!')
exit(1)
if commit and response.status_code in [404, 422]:
print('Error, the provided commit does not exist!')
exit(1)
if response.status_code in [403, 500]:
print('GitHub API has exceed the rate limit. Falling back to alternative method...')
clone_and_make_reposense(tag=tag, commit=commit)
return
if commit:
clone_and_make_reposense(commit=commit)
return
url = response.json()['assets'][0]['browser_download_url']
download_file(url)
def clone_and_make_reposense(tag=None, commit=None):
# Cleanup cached RepoSense folder
shutil.rmtree('RepoSense', ignore_errors=True)
command = \
'''
git clone 'https://github.com/reposense/RepoSense.git' &&
cd RepoSense &&
'''
if tag:
command += 'git checkout tags/{} -b deploy &&'.format(tag)
elif commit:
command += 'git checkout {} -b deploy &&'.format(commit)
command += \
'''
./gradlew zipreport shadowjar &&
mv build/jar/RepoSense.jar ../
'''
subprocess.check_call(command, shell=True)
def download_file(url):
response = requests.get(url, allow_redirects=True)
open(JAR_FILENAME, 'wb').write(response.content)
if __name__ == "__main__":
args = parse_args()
if os.path.exists(JAR_FILENAME) and args.overwrite is False:
print(JAR_FILENAME + ' already exists. Quitting.')
exit()
if args.tag:
handle_specific_release(args.tag)
exit()
if args.latest:
handle_latest_tag(args.latest)
exit()
if args.commit:
handle_specific_commit(args.commit)
exit()
if args.master:
clone_and_make_reposense()
exit()
# If no arguments are provided or --release
handle_latest_release()