-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.py
78 lines (64 loc) · 2.75 KB
/
bootstrap.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
import subprocess
import os
import sys
from pathlib import Path
import urllib.request
import zipfile
import io
import shutil
import shlex
import re
from typing import Optional, Union, Collection, Set
OptionalPath = Union[None, Path, str]
def download_tshock(url=r"https://github.com/Pryaxis/TShock/releases/download/v4.4.0-pre12/TShock4.4.0_Pre12_Terraria1.4.0.5.zip", dest: OptionalPath=None):
if dest is None:
dest = "./TShock"
dest: Path = Path(dest)
with urllib.request.urlopen(url) as response:
buff = io.BytesIO()
shutil.copyfileobj(response, buff)
buff.seek(0)
with zipfile.ZipFile(buff) as zf:
dest.mkdir(exist_ok=True)
zf.extractall(dest)
def submodule_init(name: str, remote_url:str, cwd: OptionalPath='./', version='latest'):
if cwd is None:
cwd = './'
cwd: Path = Path(cwd)
cmd = f'''git submodule add --force -- "{remote_url}" "{name}"'''
subprocess.check_call(shlex.split(cmd), cwd=str(cwd))
if version != 'latest':
cmd = f"git checkout -f {version} --"
else:
cmd = f"git reset --hard origin/master --"
subprocess.check_call(shlex.split(cmd), cwd=str(cwd / name))
def submodule_rename_namespace(name: str, namespaces: Collection[str], cwd: OptionalPath='./', suffix: str='tie'):
if cwd is None:
cwd = './'
cwd: Path = Path(cwd)
submodule: Path = Path(cwd, name)
modified: Set[Path] = set()
for path in submodule.glob("**/*.cs"):
source: str = path.read_text()
original = source
for namespace in namespaces:
for ns_prefix in ('namespace', 'using'):
source = re.sub(rf'({re.escape(ns_prefix)}\s+{re.escape(namespace)})', rf'\1_{suffix}', source)
if original != source:
path.write_text(source)
modified.add(path.relative_to(submodule))
if modified:
cmd = 'git update-index --assume-unchanged -- '
cmd += " ".join(map(lambda path: f'"{path}"', modified))
subprocess.check_call(shlex.split(cmd), cwd=str(submodule))
return modified
def deltaq_init(name: str='deltaq', remote_url="https://github.com/jzebedee/deltaq.git", version='v1.1', namespaces=('deltaq', 'bz2core')):
submodule_init(name=name, remote_url=remote_url, version=version)
submodule_rename_namespace(name=name, namespaces=namespaces)
def human_date_parser_init(name: str='HumanDateParser', remote_url="https://github.com/ti-ka/human-date-parser.git", version='latest', namespaces=('HumanDateParser',)):
submodule_init(name=name, remote_url=remote_url, version=version)
submodule_rename_namespace(name=name, namespaces=namespaces)
if __name__ == "__main__":
download_tshock()
deltaq_init()
human_date_parser_init()