Skip to content

Commit f636b25

Browse files
authored
Fetch configlet python (#2180)
* convert fetch-configlet to Python * allow retry on failure * use more reliable methods for os/arch detection
1 parent 904dd41 commit f636b25

File tree

2 files changed

+55
-33
lines changed

2 files changed

+55
-33
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ matrix:
1616
- env: JOB=HOUSEKEEPING
1717
python: 3.7
1818
install:
19+
- pip install requests
1920
- ./bin/fetch-configlet
2021
- git clone https://github.com/exercism/problem-specifications spec
2122
- pip install -r requirements-generator.txt

bin/fetch-configlet

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,54 @@
1-
#!/bin/bash
2-
3-
LATEST=https://github.com/exercism/configlet/releases/latest
4-
5-
OS=$(
6-
case $(uname) in
7-
(Darwin*)
8-
echo "mac";;
9-
(Linux*)
10-
echo "linux";;
11-
(Windows*)
12-
echo "windows";;
13-
(*)
14-
echo "linux";;
15-
esac)
16-
17-
ARCH=$(
18-
case $(uname -m) in
19-
(*64*)
20-
echo 64bit;;
21-
(*686*)
22-
echo 32bit;;
23-
(*386*)
24-
echo 32bit;;
25-
(*)
26-
echo 64bit;;
27-
esac)
28-
29-
VERSION="$(curl --head --silent $LATEST | awk -v FS=/ '/Location:/{print $NF}' | tr -d '\r')"
30-
URL=https://github.com/exercism/configlet/releases/download/$VERSION/configlet-$OS-${ARCH}.tgz
31-
32-
echo "Downloading configlet $VERSION for $OS-$ARCH..."
33-
curl -s --location $URL | tar xz -C bin/
1+
#!/usr/bin/env python3.7
2+
import io
3+
import tarfile
4+
import platform
5+
import requests
6+
import sys
7+
8+
9+
def download_and_extract(url):
10+
resp = requests.get(url)
11+
print('extracting...')
12+
file = tarfile.open(fileobj=io.BytesIO(resp.content), mode='r:gz')
13+
file.extractall(path='bin/')
14+
15+
16+
def get_os():
17+
os_ = platform.platform()
18+
if os_ == "Darwin":
19+
return "mac"
20+
elif os_ == "Windows":
21+
return "windows"
22+
return "linux"
23+
24+
25+
def get_arch():
26+
return '64bit' if sys.maxsize > 2**32 else '32bit'
27+
28+
29+
def fetch_configlet():
30+
latest = "https://api.github.com/repos/exercism/configlet/releases/latest"
31+
resp = requests.get(latest)
32+
data = resp.json()
33+
version = data["tag_name"]
34+
machine_info = f"{get_os()}-{get_arch()}"
35+
name = f"configlet-{machine_info}.tgz"
36+
for asset in data["assets"]:
37+
if asset["name"] == name:
38+
print(f"Downloading configlet {version} for {machine_info}")
39+
download_and_extract(asset["browser_download_url"])
40+
return 0
41+
return 1
42+
43+
44+
def main():
45+
max_retries = 5
46+
for i in range(max_retries):
47+
ret = fetch_configlet()
48+
if ret == 0:
49+
break
50+
return ret
51+
52+
53+
if __name__ == "__main__":
54+
sys.exit(main())

0 commit comments

Comments
 (0)