-
Notifications
You must be signed in to change notification settings - Fork 2
/
get-reposense.py
executable file
·87 lines (63 loc) · 2.52 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
#!/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')
parser.add_argument('-o', '--overwrite', action='store_true', help='Overwrite RepoSense.jar file, if exists. Default: false')
return parser.parse_args()
def handle_specific_release(tag):
get_reposense_jar('https://api.github.com/repos/reposense/RepoSense/releases/tags/' + 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):
response = requests.get(url)
if tag and response.status_code == 404:
print('Error, tag does not exists!')
exit(1)
if response.status_code == 403:
print('GitHub API has exceed the rate limit. Falling back to alternative method...')
clone_and_make_reposense(tag)
return
url = response.json()['assets'][0]['browser_download_url']
download_file(url)
def clone_and_make_reposense(tag=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)
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.master:
clone_and_make_reposense()
exit()
# If no arguments are provided or --release
handle_latest_release()