Repository navigation
Expand file tree
/
Copy pathprogrammer.py
More file actions
120 lines (105 loc) · 5.06 KB
/
Copy pathprogrammer.py
File metadata and controls
120 lines (105 loc) · 5.06 KB
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
import esptool
import sys
import os
import argparse
import json
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
def main():
# add optional --port argument using argparse
parser = argparse.ArgumentParser()
parser.add_argument('--port', help='Serial port', default=None, type=str)
# add optional --no-fs flag using argparse which will disable the additional filesystem flashing using -flash_args
parser.add_argument('--no-fs', help='No filesystems, do not flash filesystem image(s)',
action='store_true', default=False)
# parse the arguments
args = parser.parse_args()
# load flasher_args.json. flasher_args.json is a json file that contains the
# arguments to be passed to esptool this file is generated by the build
# script
flasher_args_file = open(resource_path(os.path.join('build', 'flasher_args.json')), 'r')
flasher_args = json.loads(flasher_args_file.read())
print(f'Loaded flasher args: {flasher_args}')
# build the command arguments from flasher_args
# get the extra_esptool_args
esptool_args = flasher_args['extra_esptool_args']
command = ['--baud', '460800',
'--before', esptool_args['before'],
'--after', esptool_args['after'],
'--chip', esptool_args['chip']]
stub = esptool_args['stub']
print(f'Use flasher stub? {stub}')
if not stub:
command.append('--no-stub')
# esptool 5 renamed its commands with dashes (write-flash) and the ESP-IDF
# builds that use it generate dash-spelled options into flasher_args.json;
# esptool 4 only knows the underscore form. The packaged esptool is the
# version the action was given, so pick the spelling it understands.
esptool_version = getattr(esptool, '__version__', '0')
print(f'Packaged esptool version: {esptool_version}')
esptool_major = int(esptool_version.split('.')[0]) if esptool_version[:1].isdigit() else 0
command.append('write-flash' if esptool_major >= 5 else 'write_flash')
write_flash_args = flasher_args['write_flash_args']
# oddly, write_flash_args is a list (whereas everything else is a dict), so
# we'll just append the items
for arg in write_flash_args:
command.append(arg)
for offset, filepath in flasher_args['flash_files'].items():
command.append(offset)
# prepend the filepath with build/ and convert it using resource_path
actual_path = resource_path(os.path.join('build', filepath))
command.append(actual_path)
# if the --no-fs flag is set, then we should NOT look for -flash_args files
if args.no_fs:
print('Skipping filesystem flashing (--no-fs flag set) - no -flash_args files will be used')
else:
print('Looking for -flash_args files in build directory... ')
# for each of the files in resource_path('build'), if they have
# '-flash_args' in the name, then we should:
#
# 1. open that file
# 2. split the file by newline
# 3. for each line, if it contains '0x', then
# 1. split the line by space into <offset> and <binary>
# 2. convert <binary> using resource_path(<binary>)
# 3. append the offset and binary to the command.
for filename in os.listdir(resource_path('build')):
print(f'Checking {filename}...')
# if the filename contains '-flash_args', then we should process it
if '-flash_args' in filename:
print(f'Found {filename}...')
flash_args_file = open(resource_path(os.path.join('build', filename)), 'r').read()
# split the file by newline
for line in flash_args_file.splitlines():
print(f'Processing line: {line}')
# if the line contains '0x', then split the line by space into
if '0x' in line:
offset, binary = line.split(' ')
print(f'Found offset: {offset} and binary: {binary}')
command.append(offset)
command.append(resource_path(os.path.join('build', binary)))
print(f'Added {offset} and {resource_path(binary)} to command')
# if the port argument is provided, add it to the command
if args.port:
port = args.port
print(f'Using port: {port}')
command.insert(0, '--port')
command.insert(1, port)
else:
print(f'Automatically detecting {esptool_args["chip"]} on serial port')
print('Using command arguments: %s' % ' '.join(command))
try:
esptool.main(command)
except SystemExit:
raise
except Exception as e: # esptool 5 raises on failure (esptool 4 exits)
print(f'Flashing failed: {e}')
sys.exit(2)
if __name__ == '__main__':
main()