mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-10 04:43:33 +00:00
style: format python files with isort and double-quote-string-fixer
This commit is contained in:
@@ -16,20 +16,21 @@
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
from __future__ import print_function, division
|
||||
from __future__ import division, print_function
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
import tempfile
|
||||
import re
|
||||
import gen_esp32part as gen
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
import gen_esp32part as gen
|
||||
|
||||
__version__ = '2.0'
|
||||
|
||||
COMPONENTS_PATH = os.path.expandvars(os.path.join("$IDF_PATH", "components"))
|
||||
ESPTOOL_PY = os.path.join(COMPONENTS_PATH, "esptool_py", "esptool", "esptool.py")
|
||||
COMPONENTS_PATH = os.path.expandvars(os.path.join('$IDF_PATH', 'components'))
|
||||
ESPTOOL_PY = os.path.join(COMPONENTS_PATH, 'esptool_py', 'esptool', 'esptool.py')
|
||||
|
||||
PARTITION_TABLE_OFFSET = 0x8000
|
||||
|
||||
@@ -78,14 +79,14 @@ class ParttoolTarget():
|
||||
def parse_esptool_args(esptool_args):
|
||||
results = list()
|
||||
for arg in esptool_args:
|
||||
pattern = re.compile(r"(.+)=(.+)")
|
||||
pattern = re.compile(r'(.+)=(.+)')
|
||||
result = pattern.match(arg)
|
||||
try:
|
||||
key = result.group(1)
|
||||
value = result.group(2)
|
||||
results.extend(["--" + key, value])
|
||||
results.extend(['--' + key, value])
|
||||
except AttributeError:
|
||||
results.extend(["--" + arg])
|
||||
results.extend(['--' + arg])
|
||||
return results
|
||||
|
||||
self.esptool_args = parse_esptool_args(esptool_args)
|
||||
@@ -95,14 +96,14 @@ class ParttoolTarget():
|
||||
|
||||
if partition_table_file:
|
||||
partition_table = None
|
||||
with open(partition_table_file, "rb") as f:
|
||||
with open(partition_table_file, 'rb') as f:
|
||||
input_is_binary = (f.read(2) == gen.PartitionDefinition.MAGIC_BYTES)
|
||||
f.seek(0)
|
||||
if input_is_binary:
|
||||
partition_table = gen.PartitionTable.from_binary(f.read())
|
||||
|
||||
if partition_table is None:
|
||||
with open(partition_table_file, "r") as f:
|
||||
with open(partition_table_file, 'r') as f:
|
||||
f.seek(0)
|
||||
partition_table = gen.PartitionTable.from_csv(f.read())
|
||||
else:
|
||||
@@ -110,8 +111,8 @@ class ParttoolTarget():
|
||||
temp_file.close()
|
||||
|
||||
try:
|
||||
self._call_esptool(["read_flash", str(partition_table_offset), str(gen.MAX_PARTITION_LENGTH), temp_file.name])
|
||||
with open(temp_file.name, "rb") as f:
|
||||
self._call_esptool(['read_flash', str(partition_table_offset), str(gen.MAX_PARTITION_LENGTH), temp_file.name])
|
||||
with open(temp_file.name, 'rb') as f:
|
||||
partition_table = gen.PartitionTable.from_binary(f.read())
|
||||
finally:
|
||||
os.unlink(temp_file.name)
|
||||
@@ -125,18 +126,18 @@ class ParttoolTarget():
|
||||
esptool_args = [sys.executable, ESPTOOL_PY] + self.esptool_args
|
||||
|
||||
if self.port:
|
||||
esptool_args += ["--port", self.port]
|
||||
esptool_args += ['--port', self.port]
|
||||
|
||||
if self.baud:
|
||||
esptool_args += ["--baud", str(self.baud)]
|
||||
esptool_args += ['--baud', str(self.baud)]
|
||||
|
||||
esptool_args += args
|
||||
|
||||
print("Running %s..." % (" ".join(esptool_args)))
|
||||
print('Running %s...' % (' '.join(esptool_args)))
|
||||
try:
|
||||
subprocess.check_call(esptool_args, stdout=out, stderr=subprocess.STDOUT)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print("An exception: **", str(e), "** occurred in _call_esptool.", file=out)
|
||||
print('An exception: **', str(e), '** occurred in _call_esptool.', file=out)
|
||||
raise e
|
||||
|
||||
def get_partition_info(self, partition_id):
|
||||
@@ -149,37 +150,37 @@ class ParttoolTarget():
|
||||
if not partition_id.part_list:
|
||||
partition = partition[0]
|
||||
else: # default boot partition
|
||||
search = ["factory"] + ["ota_{}".format(d) for d in range(16)]
|
||||
search = ['factory'] + ['ota_{}'.format(d) for d in range(16)]
|
||||
for subtype in search:
|
||||
partition = next(self.partition_table.find_by_type("app", subtype), None)
|
||||
partition = next(self.partition_table.find_by_type('app', subtype), None)
|
||||
if partition:
|
||||
break
|
||||
|
||||
if not partition:
|
||||
raise Exception("Partition does not exist")
|
||||
raise Exception('Partition does not exist')
|
||||
|
||||
return partition
|
||||
|
||||
def erase_partition(self, partition_id):
|
||||
partition = self.get_partition_info(partition_id)
|
||||
self._call_esptool(["erase_region", str(partition.offset), str(partition.size)] + self.esptool_erase_args)
|
||||
self._call_esptool(['erase_region', str(partition.offset), str(partition.size)] + self.esptool_erase_args)
|
||||
|
||||
def read_partition(self, partition_id, output):
|
||||
partition = self.get_partition_info(partition_id)
|
||||
self._call_esptool(["read_flash", str(partition.offset), str(partition.size), output] + self.esptool_read_args)
|
||||
self._call_esptool(['read_flash', str(partition.offset), str(partition.size), output] + self.esptool_read_args)
|
||||
|
||||
def write_partition(self, partition_id, input):
|
||||
self.erase_partition(partition_id)
|
||||
|
||||
partition = self.get_partition_info(partition_id)
|
||||
|
||||
with open(input, "rb") as input_file:
|
||||
with open(input, 'rb') as input_file:
|
||||
content_len = len(input_file.read())
|
||||
|
||||
if content_len > partition.size:
|
||||
raise Exception("Input file size exceeds partition size")
|
||||
raise Exception('Input file size exceeds partition size')
|
||||
|
||||
self._call_esptool(["write_flash", str(partition.offset), input] + self.esptool_write_args)
|
||||
self._call_esptool(['write_flash', str(partition.offset), input] + self.esptool_write_args)
|
||||
|
||||
|
||||
def _write_partition(target, partition_id, input):
|
||||
@@ -214,41 +215,41 @@ def _get_partition_info(target, partition_id, info):
|
||||
try:
|
||||
for p in partitions:
|
||||
info_dict = {
|
||||
"name": '{}'.format(p.name),
|
||||
"type": '{}'.format(p.type),
|
||||
"subtype": '{}'.format(p.subtype),
|
||||
"offset": '0x{:x}'.format(p.offset),
|
||||
"size": '0x{:x}'.format(p.size),
|
||||
"encrypted": '{}'.format(p.encrypted)
|
||||
'name': '{}'.format(p.name),
|
||||
'type': '{}'.format(p.type),
|
||||
'subtype': '{}'.format(p.subtype),
|
||||
'offset': '0x{:x}'.format(p.offset),
|
||||
'size': '0x{:x}'.format(p.size),
|
||||
'encrypted': '{}'.format(p.encrypted)
|
||||
}
|
||||
for i in info:
|
||||
infos += [info_dict[i]]
|
||||
except KeyError:
|
||||
raise RuntimeError("Request for unknown partition info {}".format(i))
|
||||
raise RuntimeError('Request for unknown partition info {}'.format(i))
|
||||
|
||||
print(" ".join(infos))
|
||||
print(' '.join(infos))
|
||||
|
||||
|
||||
def main():
|
||||
global quiet
|
||||
|
||||
parser = argparse.ArgumentParser("ESP-IDF Partitions Tool")
|
||||
parser = argparse.ArgumentParser('ESP-IDF Partitions Tool')
|
||||
|
||||
parser.add_argument("--quiet", "-q", help="suppress stderr messages", action="store_true")
|
||||
parser.add_argument("--esptool-args", help="additional main arguments for esptool", nargs="+")
|
||||
parser.add_argument("--esptool-write-args", help="additional subcommand arguments when writing to flash", nargs="+")
|
||||
parser.add_argument("--esptool-read-args", help="additional subcommand arguments when reading flash", nargs="+")
|
||||
parser.add_argument("--esptool-erase-args", help="additional subcommand arguments when erasing regions of flash", nargs="+")
|
||||
parser.add_argument('--quiet', '-q', help='suppress stderr messages', action='store_true')
|
||||
parser.add_argument('--esptool-args', help='additional main arguments for esptool', nargs='+')
|
||||
parser.add_argument('--esptool-write-args', help='additional subcommand arguments when writing to flash', nargs='+')
|
||||
parser.add_argument('--esptool-read-args', help='additional subcommand arguments when reading flash', nargs='+')
|
||||
parser.add_argument('--esptool-erase-args', help='additional subcommand arguments when erasing regions of flash', nargs='+')
|
||||
|
||||
# By default the device attached to the specified port is queried for the partition table. If a partition table file
|
||||
# is specified, that is used instead.
|
||||
parser.add_argument("--port", "-p", help="port where the target device of the command is connected to; the partition table is sourced from this device \
|
||||
when the partition table file is not defined")
|
||||
parser.add_argument("--baud", "-b", help="baudrate to use", type=int)
|
||||
parser.add_argument('--port', '-p', help='port where the target device of the command is connected to; the partition table is sourced from this device \
|
||||
when the partition table file is not defined')
|
||||
parser.add_argument('--baud', '-b', help='baudrate to use', type=int)
|
||||
|
||||
parser.add_argument("--partition-table-offset", "-o", help="offset to read the partition table from", type=str)
|
||||
parser.add_argument("--partition-table-file", "-f", help="file (CSV/binary) to read the partition table from; \
|
||||
overrides device attached to specified port as the partition table source when defined")
|
||||
parser.add_argument('--partition-table-offset', '-o', help='offset to read the partition table from', type=str)
|
||||
parser.add_argument('--partition-table-file', '-f', help='file (CSV/binary) to read the partition table from; \
|
||||
overrides device attached to specified port as the partition table source when defined')
|
||||
|
||||
partition_selection_parser = argparse.ArgumentParser(add_help=False)
|
||||
|
||||
@@ -256,30 +257,30 @@ def main():
|
||||
# partition name or the first partition that matches the specified type/subtype
|
||||
partition_selection_args = partition_selection_parser.add_mutually_exclusive_group()
|
||||
|
||||
partition_selection_args.add_argument("--partition-name", "-n", help="name of the partition")
|
||||
partition_selection_args.add_argument("--partition-type", "-t", help="type of the partition")
|
||||
partition_selection_args.add_argument('--partition-boot-default', "-d", help='select the default boot partition \
|
||||
using the same fallback logic as the IDF bootloader', action="store_true")
|
||||
partition_selection_args.add_argument('--partition-name', '-n', help='name of the partition')
|
||||
partition_selection_args.add_argument('--partition-type', '-t', help='type of the partition')
|
||||
partition_selection_args.add_argument('--partition-boot-default', '-d', help='select the default boot partition \
|
||||
using the same fallback logic as the IDF bootloader', action='store_true')
|
||||
|
||||
partition_selection_parser.add_argument("--partition-subtype", "-s", help="subtype of the partition")
|
||||
partition_selection_parser.add_argument('--partition-subtype', '-s', help='subtype of the partition')
|
||||
|
||||
subparsers = parser.add_subparsers(dest="operation", help="run parttool -h for additional help")
|
||||
subparsers = parser.add_subparsers(dest='operation', help='run parttool -h for additional help')
|
||||
|
||||
# Specify the supported operations
|
||||
read_part_subparser = subparsers.add_parser("read_partition", help="read partition from device and dump contents into a file",
|
||||
read_part_subparser = subparsers.add_parser('read_partition', help='read partition from device and dump contents into a file',
|
||||
parents=[partition_selection_parser])
|
||||
read_part_subparser.add_argument("--output", help="file to dump the read partition contents to")
|
||||
read_part_subparser.add_argument('--output', help='file to dump the read partition contents to')
|
||||
|
||||
write_part_subparser = subparsers.add_parser("write_partition", help="write contents of a binary file to partition on device",
|
||||
write_part_subparser = subparsers.add_parser('write_partition', help='write contents of a binary file to partition on device',
|
||||
parents=[partition_selection_parser])
|
||||
write_part_subparser.add_argument("--input", help="file whose contents are to be written to the partition offset")
|
||||
write_part_subparser.add_argument('--input', help='file whose contents are to be written to the partition offset')
|
||||
|
||||
subparsers.add_parser("erase_partition", help="erase the contents of a partition on the device", parents=[partition_selection_parser])
|
||||
subparsers.add_parser('erase_partition', help='erase the contents of a partition on the device', parents=[partition_selection_parser])
|
||||
|
||||
print_partition_info_subparser = subparsers.add_parser("get_partition_info", help="get partition information", parents=[partition_selection_parser])
|
||||
print_partition_info_subparser.add_argument("--info", help="type of partition information to get",
|
||||
choices=["name", "type", "subtype", "offset", "size", "encrypted"], default=["offset", "size"], nargs="+")
|
||||
print_partition_info_subparser.add_argument('--part_list', help="Get a list of partitions suitable for a given type", action='store_true')
|
||||
print_partition_info_subparser = subparsers.add_parser('get_partition_info', help='get partition information', parents=[partition_selection_parser])
|
||||
print_partition_info_subparser.add_argument('--info', help='type of partition information to get',
|
||||
choices=['name', 'type', 'subtype', 'offset', 'size', 'encrypted'], default=['offset', 'size'], nargs='+')
|
||||
print_partition_info_subparser.add_argument('--part_list', help='Get a list of partitions suitable for a given type', action='store_true')
|
||||
|
||||
args = parser.parse_args()
|
||||
quiet = args.quiet
|
||||
@@ -295,40 +296,40 @@ def main():
|
||||
partition_id = PartitionName(args.partition_name)
|
||||
elif args.partition_type:
|
||||
if not args.partition_subtype:
|
||||
raise RuntimeError("--partition-subtype should be defined when --partition-type is defined")
|
||||
raise RuntimeError('--partition-subtype should be defined when --partition-type is defined')
|
||||
partition_id = PartitionType(args.partition_type, args.partition_subtype, getattr(args, 'part_list', None))
|
||||
elif args.partition_boot_default:
|
||||
partition_id = PARTITION_BOOT_DEFAULT
|
||||
else:
|
||||
raise RuntimeError("Partition to operate on should be defined using --partition-name OR \
|
||||
partition-type,--partition-subtype OR partition-boot-default")
|
||||
raise RuntimeError('Partition to operate on should be defined using --partition-name OR \
|
||||
partition-type,--partition-subtype OR partition-boot-default')
|
||||
|
||||
# Prepare the device to perform operation on
|
||||
target_args = {}
|
||||
|
||||
if args.port:
|
||||
target_args["port"] = args.port
|
||||
target_args['port'] = args.port
|
||||
|
||||
if args.baud:
|
||||
target_args["baud"] = args.baud
|
||||
target_args['baud'] = args.baud
|
||||
|
||||
if args.partition_table_file:
|
||||
target_args["partition_table_file"] = args.partition_table_file
|
||||
target_args['partition_table_file'] = args.partition_table_file
|
||||
|
||||
if args.partition_table_offset:
|
||||
target_args["partition_table_offset"] = int(args.partition_table_offset, 0)
|
||||
target_args['partition_table_offset'] = int(args.partition_table_offset, 0)
|
||||
|
||||
if args.esptool_args:
|
||||
target_args["esptool_args"] = args.esptool_args
|
||||
target_args['esptool_args'] = args.esptool_args
|
||||
|
||||
if args.esptool_write_args:
|
||||
target_args["esptool_write_args"] = args.esptool_write_args
|
||||
target_args['esptool_write_args'] = args.esptool_write_args
|
||||
|
||||
if args.esptool_read_args:
|
||||
target_args["esptool_read_args"] = args.esptool_read_args
|
||||
target_args['esptool_read_args'] = args.esptool_read_args
|
||||
|
||||
if args.esptool_erase_args:
|
||||
target_args["esptool_erase_args"] = args.esptool_erase_args
|
||||
target_args['esptool_erase_args'] = args.esptool_erase_args
|
||||
|
||||
target = ParttoolTarget(**target_args)
|
||||
|
||||
@@ -336,9 +337,9 @@ def main():
|
||||
common_args = {'target':target, 'partition_id':partition_id}
|
||||
parttool_ops = {
|
||||
'erase_partition':(_erase_partition, []),
|
||||
'read_partition':(_read_partition, ["output"]),
|
||||
'write_partition':(_write_partition, ["input"]),
|
||||
'get_partition_info':(_get_partition_info, ["info"])
|
||||
'read_partition':(_read_partition, ['output']),
|
||||
'write_partition':(_write_partition, ['input']),
|
||||
'get_partition_info':(_get_partition_info, ['info'])
|
||||
}
|
||||
|
||||
(op, op_args) = parttool_ops[args.operation]
|
||||
|
Reference in New Issue
Block a user