This repository has been archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
lolipop.py
223 lines (173 loc) · 6.3 KB
/
lolipop.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import sys
import os, os.path
import urllib.request
import json
import shutil
import platform
import hashlib
CONFIG = {
"install_path": "./pkg",
"catalog_url": "http://txlyre.ml/files/loli/pkg/catalog.json",
}
CONFIG["catalog_path"] = os.path.join(CONFIG["install_path"], "catalog_local.json")
if not os.path.isdir(CONFIG["install_path"]):
os.mkdir(CONFIG["install_path"])
def _error(msg):
print("[!] {}".format(msg))
sys.exit(1)
def _msg(msg):
print("[i] {}".format(msg))
def _format_file_size(num, suffix='B'):
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
if abs(num) < 1024.0:
return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)
def _get_hash(path):
if not os.path.isfile(path):
_error("{}: not a file".format(path))
sha1 = hashlib.sha1()
with open(path, 'rb') as f:
while True:
data = f.read(65536)
if not data:
break
sha1.update(data)
return sha1.hexdigest()
def _check_file(path, hash):
if not os.path.isfile(path):
_error("{}: not a file".format(path))
if _get_hash(path) != hash:
_error("{}: checking fail: cheksum does not match expected\n\tGot: {}\n\tExpected: {}".format(path,_get_hash(path),hash))
def _download_file(path, url):
file_name = path
u = urllib.request.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(u.getheader('Content-Length'))
_msg("Downloading {} to {} from {}".format(_format_file_size(file_size), file_name,url))
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = r"%10s [%3.2f%%]" % (_format_file_size(file_size_dl), file_size_dl * 100. / file_size)
status = status + chr(8)*(len(status)+1)
sys.stdout.write("\r"+status)
sys.stdout.flush()
f.close()
def _download_safe(local_name, url):
if os.path.isfile(local_name):
os.remove(local_name)
try:
_download_file(local_name, url)
except Exception as e:
_error("{}: download failed: {}".format(local_name,e))
if not os.path.isfile(local_name):
_error("{}: download failed".format(local_name))
def _check_catalog():
if not os.path.isfile(CONFIG["catalog_path"]):
return False
try:
json.load(open(CONFIG["catalog_path"], "r"))
except Exception as e:
return False
return True
def _is_package_installed(package_name):
package_name = package_name.strip().lower()
bin_suffix = ".so"
plat = platform.system().lower()
if plat == "windows":
bin_suffix = ".dll"
return os.path.isfile(os.path.join(CONFIG["install_path"], package_name, package_name+bin_suffix))
def action_update():
_download_safe(CONFIG["catalog_path"], CONFIG["catalog_url"])
def action_install(package_name):
package_name = package_name.strip().lower()
if _is_package_installed(package_name):
_error("package '{}' already installed".format(package_name))
if not _check_catalog():
action_update()
try:
catalog = json.load(open(CONFIG["catalog_path"], "r"))
except Exception as e:
_error("catalog parsing error: {}".format(e))
if type(catalog) != list:
_error("invalid catalog")
package_info = None
for package in catalog:
if type(package) != dict or "name" not in package:
_error("invalid catalog")
if package["name"] == package_name:
package_info = package
if package_info == None:
_error("could not find a package named '{}'".format(package_name))
bin_suffix = ".so"
plat = platform.system().lower()
if plat == "windows":
bin_suffix = ".dll"
if "binaries" not in package_info:
_error("invalid catalog")
if "requires" in package_info:
for require in package_info["requires"]:
if not _is_package_installed(require):
_msg("found required package '{}'".format(require))
action_install(require)
if plat not in package_info["binaries"]:
_error("there is no '{}' binary for your platform({})".format(package_name, plat))
if "checksum" not in package_info["binaries"][plat]:
_error("invalid catalog")
package_local_path = os.path.join(CONFIG["install_path"], package_name, package_name+bin_suffix)
if not os.path.isdir(os.path.join(CONFIG["install_path"], package_name)):
os.mkdir(os.path.join(CONFIG["install_path"], package_name))
hash = package_info["binaries"][plat]["checksum"]
_download_safe(package_local_path, package_info["binaries"][plat]["url"])
_check_file(package_local_path, hash)
if not _is_package_installed(package_name):
_error("installing failed due unknown error")
_msg("successfuly installed package '{}'".format(package_name))
def action_uninstall(package_name):
package_name = package_name.strip().lower()
if not _is_package_installed(package_name):
_error("cannot uninstall non-installed package '{}'".format(package_name))
shutil.rmtree(os.path.join(CONFIG["install_path"], package_name))
if _is_package_installed(package_name):
_error("uninstalling failed due unknown error")
_msg("successfuly uninstalled package '{}'".format(package_name))
def action_list():
bin_suffix = ".so"
plat = platform.system().lower()
if plat == "windows":
bin_suffix = ".dll"
packages = os.listdir(CONFIG["install_path"])
for name in packages:
if name != "catalog_local.json":
if os.path.isdir(os.path.join(CONFIG["install_path"], name)):
if os.path.isfile(os.path.join(CONFIG["install_path"], name, name+bin_suffix)):
print(name)
def print_usage(code):
print("""Usage: lolipop.py <install|uninstall|update|list> [package-name]""")
sys.exit(code)
args = sys.argv
argc = len(args)
if argc <= 1:
print_usage(1)
if argc >= 2:
action = args[1].lower().strip()
if action == "install":
if argc < 3:
print_usage(1)
action_install(args[2])
elif action == "uninstall":
if argc < 3:
print_usage(1)
action_uninstall(args[2])
elif action == "update":
action_update()
elif action == "list":
action_list()
else:
print_usage(1)