-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
91 lines (70 loc) · 2.64 KB
/
install.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
import json
import logging
import os
import subprocess
from os import path
from urllib import request, parse
IMAGE_URL = "https://raw.githubusercontent.com/hayashikun/fugaku36/master/resouces/images/{}"
JSON_URL = "https://raw.githubusercontent.com/hayashikun/fugaku36/master/resouces/resource.json"
PY_SCRIPT_URL = "https://raw.githubusercontent.com/hayashikun/fugaku36/master/fugaku36.py"
PLIST_URL = "https://raw.githubusercontent.com/hayashikun/fugaku36/master/fugaku36.plist"
HOME_PATH = path.expanduser("~")
INSTALL_PATH = path.join(HOME_PATH, ".cache", "fugaku36")
IMAGE_PATH = path.join(INSTALL_PATH, "images")
JSON_PATH = path.join(INSTALL_PATH, "resource.json")
PY_SCRIPT_PATH = path.join(INSTALL_PATH, "fugaku36.py")
SH_SCRIPT_PATH = path.join(INSTALL_PATH, "fugaku36")
PLIST_PATH = path.join(INSTALL_PATH, "fugaku36.plist")
def install():
logger = logging.getLogger("fugaku36_install")
sh = logging.StreamHandler()
logger.addHandler(sh)
logger.setLevel(logging.INFO)
if not path.exists(INSTALL_PATH):
os.makedirs(INSTALL_PATH)
if not path.exists(IMAGE_PATH):
os.makedirs(IMAGE_PATH)
update_resource = False
if path.exists(JSON_PATH):
with open(JSON_PATH) as f:
j = json.load(f)
old_version = j["version"]
else:
old_version = 0
request.urlretrieve(JSON_URL, JSON_PATH)
logger.info("Image JSON file has been downloaded.")
with open(JSON_PATH) as f:
j = json.load(f)
if old_version < j["version"]:
update_resource = True
logger.info("Resources will be updated.")
image_names = j["images"]
for name in image_names:
if not update_resource and path.exists(path.join(IMAGE_PATH, name)):
continue
request.urlretrieve(IMAGE_URL.format(parse.quote(name)), path.join(IMAGE_PATH, name))
logger.info("Image %s has been downloaded.", name)
request.urlretrieve(PY_SCRIPT_URL, PY_SCRIPT_PATH)
logger.info("Python scripts have been downloaded.")
with open(PY_SCRIPT_PATH) as f:
py_script = f.read()
py_script = "#!/usr/bin/python3\n" + py_script
with open(SH_SCRIPT_PATH, "w") as f:
f.write(py_script)
cmd = [
"chmod", "a+x", SH_SCRIPT_PATH
]
subprocess.run(cmd)
cmd = [
"ln",
"-s",
SH_SCRIPT_PATH,
"/usr/local/bin/fugaku36"
]
subprocess.run(cmd)
logger.info("Symbolic link is created.")
request.urlretrieve(PLIST_URL, PLIST_PATH)
logger.info("fugaku36.plist have been downloaded.")
logger.info("fugaku36 is successfully installed!")
if __name__ == "__main__":
install()