forked from tuna/tunasync-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackage.py
executable file
·117 lines (101 loc) · 4.75 KB
/
stackage.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
#!/usr/bin/env python3
# python deps: requests, pyyaml
# non-python deps: aria2, git
import os
import pathlib
import requests
import shutil
import subprocess
import yaml
MIRROR_BASE_URL = os.getenv("MIRROR_BASE_URL", 'https://mirrors.tuna.tsinghua.edu.cn/stackage')
MIRROR_GITHUB_RELEASE_URL = os.getenv("MIRROR_GITHUB_RELEASE_URL", 'https://mirrors.tuna.tsinghua.edu.cn/github-release')
class StackageSession(object):
def __init__(self):
self._base_path = pathlib.Path(os.environ['TUNASYNC_WORKING_DIR'])
def download(self, dir_, url, sha1=None, force=False):
dir_path = self._base_path / dir_
file_path = dir_path / url.split('/')[-1]
if force and file_path.is_file():
file_path.unlink()
if file_path.is_file():
print('{} exists, skipping'.format(file_path), flush=True)
else:
args = [
'aria2c', url, '--dir={}'.format(
dir_path), '--out={}.tmp'.format(url.split('/')[-1]),
'--file-allocation=none', '--quiet=true'
]
if sha1:
args.append('--checksum=sha-1={}'.format(sha1))
subprocess.run(args, check=True)
shutil.move('{}.tmp'.format(file_path), file_path)
print('Downloaded {} to {}'.format(url, file_path), flush=True)
def load_stack_setup(self):
d = yaml.load(
requests
.get('https://raw.githubusercontent.com/commercialhaskell/stackage-content/master/stack/stack-setup-2.yaml')
.content,
Loader=yaml.FullLoader
)
for platform in d['ghc']:
for ver in d['ghc'][platform]:
self.download(
'ghc',
d['ghc'][platform][ver]['url'],
d['ghc'][platform][ver]['sha1'],
)
d['ghc'][platform][ver]['url'] = (
'{}/ghc/{}'
.format(MIRROR_BASE_URL, d['ghc'][platform][ver]['url'].split('/')[-1])
)
if 'msys2' in d:
if 'windows32' in d['msys2']:
d['msys2']['windows32']['url'] = d['msys2']['windows32']['url'].replace(
'https://github.com/fpco/stackage-content/releases/download/',
f'{MIRROR_GITHUB_RELEASE_URL}/commercialhaskell/stackage-content/msys2-')
if 'windows64' in d['msys2']:
d['msys2']['windows64']['url'] = d['msys2']['windows64']['url'].replace(
'https://github.com/commercialhaskell/stackage-content/releases/download/',
f'{MIRROR_GITHUB_RELEASE_URL}/commercialhaskell/stackage-content/')
if 'sevenzexe-info' in d:
d['sevenzexe-info']['url'] = d['sevenzexe-info']['url'].replace(
'https://github.com/commercialhaskell/stackage-content/releases/download/',
f'{MIRROR_GITHUB_RELEASE_URL}/commercialhaskell/stackage-content/')
if 'sevenzdll-info' in d:
d['sevenzdll-info']['url'] = d['sevenzdll-info']['url'].replace(
'https://github.com/commercialhaskell/stackage-content/releases/download/',
f'{MIRROR_GITHUB_RELEASE_URL}/commercialhaskell/stackage-content/')
for i in ['portable-git', 'stack', 'ghcjs']:
del d[i]
with open(self._base_path / 'stack-setup.yaml', 'w') as f:
yaml.dump(d, f, default_flow_style=False)
print('Loaded stack-setup.yaml', flush=True)
def load_stackage_snapshots(self):
for channel in ['lts-haskell', 'stackage-nightly']:
if (self._base_path / channel).is_dir():
args = ['git', '-C', self._base_path / channel, 'pull']
else:
args = ['git', '-C', self._base_path, 'clone', '--depth', '1',
'https://github.com/commercialhaskell/{}.git'.format(channel)]
subprocess.run(args, check=True)
print('Loaded {}'.format(channel), flush=True)
self.download(
'',
'https://www.stackage.org/download/snapshots.json',
force=True,
)
print('Loaded snapshots.json', flush=True)
def stackage_snapshots_git_sync():
base_path = pathlib.Path(os.environ['TUNASYNC_WORKING_DIR'])
working_dir = base_path / "stackage-snapshots"
if working_dir.is_dir():
subprocess.run(
['git', '-C', working_dir.as_posix(), 'pull'], check=True)
else:
subprocess.run(['git', '-C', base_path.as_posix(), 'clone', '--depth', '1',
'https://github.com/commercialhaskell/stackage-snapshots.git'], check=True)
if __name__ == '__main__':
s = StackageSession()
stackage_snapshots_git_sync()
s.load_stackage_snapshots()
s.load_stack_setup()