Skip to content

Commit 90fc4f9

Browse files
committed
Add tool to download other tools
1 parent 342c4ae commit 90fc4f9

File tree

3 files changed

+226
-6
lines changed

3 files changed

+226
-6
lines changed

tools/espota.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
#!/usr/bin/env python
22
#
3-
# Original espoty.py comes from ...?
3+
# Original espota.py by Ivan Grokhotkov:
4+
# https://gist.github.com/igrr/d35ab8446922179dc58c
45
#
56
# Modified since 2015-09-18 from Pascal Gollor (https://github.com/pgollor)
67
#
78
# This script will push an OTA update to the ESP
89
# use it like: python espota.py -i <ESP_IP_address> -p <ESP_port> -f <sketch.bin>
10+
# Or to upload SPIFFS image:
11+
# python espota.py -i <ESP_IP_address> -p <ESP_port> -s <spiffs.bin>
912
#
1013
# Changes
1114
# 2015-09-18:
1215
# - Add option parser.
1316
# - Add logging.
1417
# - Send command to controller to differ between flashing and transmitting SPIFFS image.
1518
#
16-
19+
1720
from __future__ import print_function
1821
import socket
1922
import sys
@@ -25,7 +28,7 @@
2528
FLASH = 0
2629
SPIFFS = 100
2730

28-
31+
2932
def serve(remoteAddr, remotePort, filename, command = FLASH):
3033
# Create a TCP/IP socket
3134
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -38,18 +41,18 @@ def serve(remoteAddr, remotePort, filename, command = FLASH):
3841
except:
3942
logging.error("Listen Failed")
4043
return 1
41-
44+
4245
content_size = os.path.getsize(filename)
4346
logging.info('Upload size: %d', content_size)
4447
message = '%d %d %d\n' % (command, serverPort, content_size)
45-
48+
4649
# Wait for a connection
4750
logging.info('Sending invitation to: %s', remoteAddr)
4851
sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
4952
remote_address = (remoteAddr, int(remotePort))
5053
sent = sock2.sendto(message, remote_address)
5154
sock2.close()
52-
55+
5356
logging.info('Waiting for device...\n')
5457
try:
5558
sock.settimeout(10)

tools/get.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python
2+
# This script will download and extract required tools into the current directory
3+
# Tools list is obtained from tools.json file
4+
# Written by Ivan Grokhotkov, 2015
5+
#
6+
from __future__ import print_function
7+
import urllib
8+
import os
9+
import os.path
10+
import hashlib
11+
import json
12+
import platform
13+
import sys
14+
import tarfile
15+
import zipfile
16+
import re
17+
18+
dist_dir = 'dist/'
19+
20+
def sha256sum(filename, blocksize=65536):
21+
hash = hashlib.sha256()
22+
with open(filename, "rb") as f:
23+
for block in iter(lambda: f.read(blocksize), b""):
24+
hash.update(block)
25+
return hash.hexdigest()
26+
27+
def report_progress(count, blockSize, totalSize):
28+
percent = int(count*blockSize*100/totalSize)
29+
percent = min(100, percent)
30+
sys.stdout.write("\r%d%%" % percent)
31+
sys.stdout.flush()
32+
33+
def unpack(filename, destination):
34+
dirname = ''
35+
print('Extracting {0}'.format(filename))
36+
if filename.endswith('tar.gz'):
37+
tfile = tarfile.open(filename, 'r:gz')
38+
tfile.extractall(destination)
39+
dirname= tfile.getnames()[0]
40+
elif filename.endswith('zip'):
41+
zfile = zipfile.ZipFile(filename)
42+
zfile.extractall(destination)
43+
dirname = zfile.namelist()[0]
44+
else:
45+
raise NotImplementedError('Unsupported archive type')
46+
47+
# a little trick to rename tool directories so they don't contain version number
48+
rename_to = re.match(r'^([a-z][^\-]*\-*)+', dirname).group(0).encode('ascii').strip('-')
49+
if rename_to != dirname:
50+
print('Renaming {0} to {1}'.format(dirname, rename_to))
51+
os.rename(dirname, rename_to)
52+
53+
def get_tool(tool):
54+
archive_name = tool['archiveFileName']
55+
local_path = dist_dir + archive_name
56+
url = tool['url']
57+
real_hash = tool['checksum'].split(':')[1]
58+
if not os.path.isfile(local_path):
59+
print('Downloading ' + archive_name);
60+
urllib.urlretrieve(url, local_path, report_progress)
61+
sys.stdout.write("\rDone\n")
62+
sys.stdout.flush()
63+
else:
64+
print('Tool {0} already downloaded'.format(archive_name))
65+
local_hash = sha256sum(local_path)
66+
if local_hash != real_hash:
67+
print('Hash mismatch for {0}, delete the file and try again'.format(local_path))
68+
raise RuntimeError()
69+
unpack(local_path, '.')
70+
71+
def load_tools_list(filename, platform):
72+
tools_info = json.load(open(filename))
73+
tools_to_download = []
74+
for t in tools_info:
75+
tool_platform = [p for p in t['systems'] if p['host'] == platform]
76+
if len(tool_platform) == 0:
77+
continue
78+
tools_to_download.append(tool_platform[0])
79+
return tools_to_download
80+
81+
def identify_platform():
82+
arduino_platform_names = {'Darwin' : {32 : 'i386-apple-darwin', 64 : 'x86_64-apple-darwin'},
83+
'Linux' : {32 : 'i686-pc-linux-gnu', 64 : 'x86_64-pc-linux-gnu'},
84+
'Windows' : {32 : 'i686-mingw32', 64 : 'i686-mingw32'}}
85+
bits = 32
86+
if sys.maxsize > 2**32:
87+
bits = 64
88+
return arduino_platform_names[platform.system()][bits]
89+
90+
if __name__ == '__main__':
91+
print('Platform: {0}'.format(identify_platform()))
92+
tools_to_download = load_tools_list('tools.json', identify_platform())
93+
for tool in tools_to_download:
94+
get_tool(tool)

tools/tools.json

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
[ {
2+
"name":"esptool",
3+
"version":"0.4.6",
4+
"systems": [
5+
{
6+
"host":"i686-mingw32",
7+
"url":"https://github.com/igrr/esptool-ck/releases/download/0.4.6/esptool-0.4.6-win32.zip",
8+
"archiveFileName":"esptool-0.4.6-win32.zip",
9+
"checksum":"SHA-256:0248bf78514a3195f583e29218ca7828a66e13c6e5545a078f1c1257033e4927",
10+
"size":"17481"
11+
},
12+
{
13+
"host":"x86_64-apple-darwin",
14+
"url":"https://github.com/igrr/esptool-ck/releases/download/0.4.6/esptool-0.4.6-osx.tar.gz",
15+
"archiveFileName":"esptool-0.4.6-osx.tar.gz",
16+
"checksum":"SHA-256:0fe87ba7e29ee90a9fc72492aada8c0796f9e8f8a1c594b6b26cee2610d09bb3",
17+
"size":"20926"
18+
},
19+
{
20+
"host":"i386-apple-darwin",
21+
"url":"https://github.com/igrr/esptool-ck/releases/download/0.4.6/esptool-0.4.6-osx.tar.gz",
22+
"archiveFileName":"esptool-0.4.6-osx.tar.gz",
23+
"checksum":"SHA-256:0fe87ba7e29ee90a9fc72492aada8c0796f9e8f8a1c594b6b26cee2610d09bb3",
24+
"size":"20926"
25+
},
26+
{
27+
"host":"x86_64-pc-linux-gnu",
28+
"url":"https://github.com/igrr/esptool-ck/releases/download/0.4.6/esptool-0.4.6-linux64.tar.gz",
29+
"archiveFileName":"esptool-0.4.6-linux64.tar.gz",
30+
"checksum":"SHA-256:f9f456e9a42bb2597126c513cb8865f923fb978865d4838b9623d322216b74d0",
31+
"size":"12885"
32+
},
33+
{
34+
"host":"i686-pc-linux-gnu",
35+
"url":"https://github.com/igrr/esptool-ck/releases/download/0.4.6/esptool-0.4.6-linux32.tar.gz",
36+
"archiveFileName":"esptool-0.4.6-linux32.tar.gz",
37+
"checksum":"SHA-256:85275ca03a82bfc456f5a84e86962ca1e470ea2e168829c38ca29ee668831d93",
38+
"size":"13417"
39+
}
40+
]
41+
},
42+
{
43+
"name":"xtensa-lx106-elf-gcc",
44+
"version":"1.20.0-26-gb404fb9-2",
45+
"systems": [
46+
{
47+
"host":"i686-mingw32",
48+
"url":"http://arduino.esp8266.com/win32-xtensa-lx106-elf-gb404fb9-2.tar.gz",
49+
"archiveFileName":"win32-xtensa-lx106-elf-gb404fb9-2.tar.gz",
50+
"checksum":"SHA-256:10476b9c11a7a90f40883413ddfb409f505b20692e316c4e597c4c175b4be09c",
51+
"size":"153527527"
52+
},
53+
{
54+
"host":"x86_64-apple-darwin",
55+
"url":"http://arduino.esp8266.com/osx-xtensa-lx106-elf-gb404fb9-2.tar.gz",
56+
"archiveFileName":"osx-xtensa-lx106-elf-gb404fb9-2.tar.gz",
57+
"checksum":"SHA-256:0cf150193997bd1355e0f49d3d49711730035257bc1aee1eaaad619e56b9e4e6",
58+
"size":"35385382"
59+
},
60+
{
61+
"host":"i386-apple-darwin",
62+
"url":"http://arduino.esp8266.com/osx-xtensa-lx106-elf-gb404fb9-2.tar.gz",
63+
"archiveFileName":"osx-xtensa-lx106-elf-gb404fb9-2.tar.gz",
64+
"checksum":"SHA-256:0cf150193997bd1355e0f49d3d49711730035257bc1aee1eaaad619e56b9e4e6",
65+
"size":"35385382"
66+
},
67+
{
68+
"host":"x86_64-pc-linux-gnu",
69+
"url":"http://arduino.esp8266.com/linux64-xtensa-lx106-elf-gb404fb9.tar.gz",
70+
"archiveFileName":"linux64-xtensa-lx106-elf-gb404fb9.tar.gz",
71+
"checksum":"SHA-256:46f057fbd8b320889a26167daf325038912096d09940b2a95489db92431473b7",
72+
"size":"30262903"
73+
},
74+
{
75+
"host":"i686-pc-linux-gnu",
76+
"url":"http://arduino.esp8266.com/linux32-xtensa-lx106-elf.tar.gz",
77+
"archiveFileName":"linux32-xtensa-lx106-elf.tar.gz",
78+
"checksum":"SHA-256:b24817819f0078fb05895a640e806e0aca9aa96b47b80d2390ac8e2d9ddc955a",
79+
"size":"32734156"
80+
}
81+
]
82+
},
83+
{
84+
"name":"mkspiffs",
85+
"version":"0.1.2",
86+
"systems": [
87+
{
88+
"host":"i686-mingw32",
89+
"url":"https://github.com/igrr/mkspiffs/releases/download/0.1.2/mkspiffs-0.1.2-windows.zip",
90+
"archiveFileName":"mkspiffs-0.1.2-windows.zip",
91+
"checksum":"SHA-256:0a29119b8458b61a877408f7995e4944623a712e0d313a2c2f76af9ab55cc9f2",
92+
"size":"230802"
93+
},
94+
{
95+
"host":"x86_64-apple-darwin",
96+
"url":"https://github.com/igrr/mkspiffs/releases/download/0.1.2/mkspiffs-0.1.2-osx.tar.gz",
97+
"archiveFileName":"mkspiffs-0.1.2-osx.tar.gz",
98+
"checksum":"SHA-256:df656fae21a41c1269ea50cb53752dcaf6a4e1437255f3a9fb50b4025549b58e",
99+
"size":"115091"
100+
},
101+
{
102+
"host":"i386-apple-darwin",
103+
"url":"https://github.com/igrr/mkspiffs/releases/download/0.1.2/mkspiffs-0.1.2-osx.tar.gz",
104+
"archiveFileName":"mkspiffs-0.1.2-osx.tar.gz",
105+
"checksum":"SHA-256:df656fae21a41c1269ea50cb53752dcaf6a4e1437255f3a9fb50b4025549b58e",
106+
"size":"115091"
107+
},
108+
{
109+
"host":"x86_64-pc-linux-gnu",
110+
"url":"https://github.com/igrr/mkspiffs/releases/download/0.1.2/mkspiffs-0.1.2-linux64.tar.gz",
111+
"archiveFileName":"mkspiffs-0.1.2-linux64.tar.gz",
112+
"checksum":"SHA-256:1a1dd81b51daf74c382db71b42251757ca4136e8762107e69feaa8617bac315f",
113+
"size":"46281"
114+
},
115+
{
116+
"host":"i686-pc-linux-gnu",
117+
"url":"https://github.com/igrr/mkspiffs/releases/download/0.1.2/mkspiffs-0.1.2-linux32.tar.gz",
118+
"archiveFileName":"mkspiffs-0.1.2-linux32.tar.gz",
119+
"checksum":"SHA-256:e990d545dfcae308aabaac5fa9e1db734cc2b08167969e7eedac88bd0839667c",
120+
"size":"45272"
121+
}
122+
]
123+
} ]

0 commit comments

Comments
 (0)