This repository has been archived by the owner on Dec 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
installer.py
88 lines (76 loc) · 1.92 KB
/
installer.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
# -*- coding:utf-8 -*-
"""
@author: SiriYang
@file: installer.py
@createTime: 2020-04-05 20:03:25
@updateTime: 2021-02-09 17:27:01
@codeLines: 61
"""
import requests
import sys
import os
import zipfile
import time
APPNAME='Transista'
VERSION="v-1-0-2"
FILE=APPNAME+'_'+VERSION+'.zip'
URL_BASE='http://img.siriyang.cn/source/Transista/archive/'
BASE_DIR=os.path.expanduser('~')
DEFAULT_INSTALL_DIR=os.path.join(BASE_DIR,'Documents/')
def download(url,path):
print("下载中...: {} ".format(url))
r=requests.get(url,stream=True)
file_size=r.headers.get('Content-Length')
if file_size is not None:
file_size=int(file_size)
with open(path,'wb') as outs:
block_sz = 8192
for chunk in r.iter_content(block_sz):
outs.write(chunk)
def unzip_into(path, outpath, verbose=False):
"""
Unzip zipfile at path into outpath.
:param path: path to zipfile
:type path: str
:param outpath: path to extract to
:type outpath: str
"""
if not os.path.exists(outpath):
os.makedirs(outpath)
if verbose:
print('Unzipping into %s ...' % outpath)
with zipfile.ZipFile(path) as zipfp:
toplevel_directory = None
namelist = zipfp.namelist()
for name in namelist:
data = zipfp.read(name)
fname = os.path.join(outpath, name)
if fname.endswith('/'): # A directory
if not os.path.exists(fname):
os.makedirs(fname)
else:
fp = open(fname, 'wb')
try:
fp.write(data)
finally:
fp.close()
def main():
starTime=time.time()
file=FILE
url=URL_BASE+file
zip_path=DEFAULT_INSTALL_DIR+file
try:
download(url,zip_path)
print('下载完成!')
except:
print('下载失败!')
return
print('正在安装 ...')
install_path=DEFAULT_INSTALL_DIR+APPNAME+'/'
zip_file=zipfile.ZipFile(zip_path)
unzip_into(zip_path,install_path)
os.remove(zip_path)
print('安装成功!应用已安装到:'+install_path)
print('耗时: {:.2f}s'.format(time.time()-starTime))
if __name__ == '__main__':
main()