- Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathget.py
executable file
·149 lines (132 loc) · 4.93 KB
/
get.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
#!/usr/bin/env python
"""Script to download and extract tools
This script will download and extract required tools into the current directory.
Tools list is obtained from package/package_esp8266com_index.template.json file.
"""
from __future__ importprint_function
__author__="Ivan Grokhotkov"
__version__="2015"
importos
importshutil
importerrno
importos.path
importhashlib
importjson
importplatform
importsys
importtarfile
importzipfile
importre
ifsys.version_info[0] ==3:
fromurllib.requestimporturlretrieve
unicode=lambdas: str(s)
else:
# Not Python 3 - today, it is most likely to be Python 2
fromurllibimporturlretrieve
if'Windows'inplatform.system():
importrequests
current_dir=os.path.dirname(os.path.realpath(unicode(__file__)))
dist_dir=current_dir+'/dist/'
defsha256sum(filename, blocksize=65536):
hash=hashlib.sha256()
withopen(filename, "rb") asf:
forblockiniter(lambda: f.read(blocksize), b""):
hash.update(block)
returnhash.hexdigest()
defmkdir_p(path):
try:
os.makedirs(path)
exceptOSErrorasexc:
ifexc.errno!=errno.EEXISTornotos.path.isdir(path):
raise
defreport_progress(count, blockSize, totalSize):
percent=int(count*blockSize*100/totalSize)
percent=min(100, percent)
sys.stdout.write("\r%d%%"%percent)
sys.stdout.flush()
defunpack(filename, destination):
dirname=''
print('Extracting {0}'.format(os.path.basename(filename)))
sys.stdout.flush()
iffilename.endswith('tar.gz'):
tfile=tarfile.open(filename, 'r:gz')
tfile.extractall(destination)
dirname=tfile.getnames()[0]
eliffilename.endswith('zip'):
zfile=zipfile.ZipFile(filename)
zfile.extractall(destination)
dirname=zfile.namelist()[0]
else:
raiseNotImplementedError('Unsupported archive type')
# a little trick to rename tool directories so they don't contain version number
rename_to=re.match(r'^([a-z][^\-]*\-*)+', dirname).group(0).strip('-')
ifrename_to!=dirname:
print('Renaming {0} to {1}'.format(dirname, rename_to))
ifos.path.isdir(rename_to):
shutil.rmtree(rename_to)
shutil.move(dirname, rename_to)
defget_tool(tool):
sys_name=platform.system()
archive_name=tool['archiveFileName']
local_path=dist_dir+archive_name
url=tool['url']
#real_hash = tool['checksum'].split(':')[1]
ifnotos.path.isfile(local_path):
print('Downloading '+archive_name)
sys.stdout.flush()
if'CYGWIN_NT'insys_name:
importssl
ctx=ssl.create_default_context()
ctx.check_hostname=False
ctx.verify_mode=ssl.CERT_NONE
urlretrieve(url, local_path, report_progress, context=ctx)
elif'Windows'insys_name:
r=requests.get(url)
f=open(local_path, 'wb')
f.write(r.content)
f.close()
else:
urlretrieve(url, local_path, report_progress)
sys.stdout.write("\rDone\n")
sys.stdout.flush()
else:
print('Tool {0} already downloaded'.format(archive_name))
sys.stdout.flush()
#local_hash = sha256sum(local_path)
#if local_hash != real_hash:
# print('Hash mismatch for {0}, delete the file and try again'.format(local_path))
# raise RuntimeError()
unpack(local_path, '.')
defload_tools_list(filename, platform):
tools_info=json.load(open(filename))['packages'][0]['tools']
tools_to_download= []
fortintools_info:
tool_platform= [pforpint['systems'] ifp['host'] ==platform]
iflen(tool_platform) ==0:
continue
tools_to_download.append(tool_platform[0])
returntools_to_download
defidentify_platform():
arduino_platform_names= {'Darwin' : {32 : 'i386-apple-darwin', 64 : 'x86_64-apple-darwin'},
'Linux' : {32 : 'i686-pc-linux-gnu', 64 : 'x86_64-pc-linux-gnu'},
'LinuxARM': {32 : 'arm-linux-gnueabihf', 64 : 'aarch64-linux-gnu'},
'Windows' : {32 : 'i686-mingw32', 64 : 'i686-mingw32'}}
bits=32
ifsys.maxsize>2**32:
bits=64
sys_name=platform.system()
sys_platform=platform.platform()
print('System: %s, Info: %s'% (sys_name, sys_platform))
if'Linux'insys_nameandsys_platform.find('arm') >0:
sys_name='LinuxARM'
if'CYGWIN_NT'insys_name:
sys_name='Windows'
returnarduino_platform_names[sys_name][bits]
if__name__=='__main__':
identified_platform=identify_platform()
print('Platform: {0}'.format(identified_platform))
tools_to_download=load_tools_list(current_dir+'/../package/package_esp32_index.template.json', identified_platform)
mkdir_p(dist_dir)
fortoolintools_to_download:
get_tool(tool)
print('Done')