efuse: Fix python coding style

Set python's scripts attribute chmod
Add compatibility with Python3 for efuse_table_gen.py
This commit is contained in:
Konstantin Kondrashov 2018-12-07 20:32:58 +08:00 committed by bot
parent a5fa3b6965
commit cc094ba789
2 changed files with 91 additions and 112 deletions

130
components/efuse/efuse_table_gen.py Normal file → Executable file
View File

@ -21,11 +21,8 @@ from __future__ import print_function, division
import argparse
import os
import re
import struct
import sys
import hashlib
import binascii
import ntpath
__version__ = '1.0'
@ -33,9 +30,9 @@ quiet = False
coding_scheme = 0
CODE_SCHEME = {
"NONE" : 0,
"3/4" : 1,
"REPEAT" : 2,
"NONE": 0,
"3/4": 1,
"REPEAT": 2,
}
copyright = '''// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
@ -52,7 +49,8 @@ copyright = '''// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
// See the License for the specific language governing permissions and
// limitations under the License
'''
def status(msg):
""" Print status message to stderr """
if not quiet:
@ -74,6 +72,7 @@ class FuseTable(list):
def from_csv(cls, csv_contents):
res = FuseTable()
lines = csv_contents.splitlines()
def expand_vars(f):
f = os.path.expandvars(f)
m = re.match(r'(?<!\\)\$([A-Za-z_][A-Za-z0-9_]*)', f)
@ -94,18 +93,17 @@ class FuseTable(list):
raise
# fix up missing bit_start
last_enfl_error = 0
last_efuse_block = None
for e in res:
if last_efuse_block != e.efuse_block:
if last_efuse_block != e.efuse_block:
last_end = 0
if e.bit_start is None:
e.bit_start = last_end
last_end = e.bit_start + e.bit_count
last_efuse_block = e.efuse_block
res.verify_duplicate_name()
# fix up missing field_name
last_field = None
for e in res:
@ -114,7 +112,7 @@ class FuseTable(list):
elif e.field_name == "" and last_field is not None:
e.field_name = last_field.field_name
last_field = e
# fill group
names = [p.field_name for p in res]
duplicates = set(n for n in names if names.count(n) > 1)
@ -127,17 +125,17 @@ class FuseTable(list):
else:
i_count = 0
res.verify_duplicate_name()
# clac md5 for table
res.calc_md5()
return res
def verify_duplicate_name(self):
# check on duplicate name
names = [p.field_name for p in self]
duplicates = set(n for n in names if names.count(n) > 1)
# print sorted duplicate partitions by name
if len(duplicates) != 0:
fl_error = False
@ -145,17 +143,17 @@ class FuseTable(list):
field_name = p.field_name + p.group
if field_name != "" and len(duplicates.intersection([field_name])) != 0:
fl_error = True
print ("Field at %s, %s, %s, %s have dublicate field_name" %
(p.field_name, p.efuse_block, p.bit_start, p.bit_count))
if fl_error == True:
print("Field at %s, %s, %s, %s have dublicate field_name" %
(p.field_name, p.efuse_block, p.bit_start, p.bit_count))
if fl_error is True:
raise InputError("Field names must be unique")
def verify(self, type_table = None):
def verify(self, type_table=None):
for p in self:
p.verify(type_table)
self.verify_duplicate_name()
# check for overlaps
last = None
for p in sorted(self, key=lambda x:(x.efuse_block, x.bit_start)):
@ -164,28 +162,14 @@ class FuseTable(list):
(p.field_name, p.efuse_block, p.bit_start, p.bit_count,
last.field_name, last.efuse_block, last.bit_start, last.bit_count))
last = p
def fix_size_fields_from_blk1_blk2(self):
for p in self:
if (p.efuse_block == "EFUSE_BLK1" and custom_table_use_BLK1 == False) or (p.efuse_block == "EFUSE_BLK2" and custom_table_use_BLK2 == False):
max_bits = p.get_max_bits_of_block()
if p.bit_start == 0 and p.bit_count > max_bits:
print("Fix size `%s` field from %d to %d" % (p.field_name, p.bit_count, max_bits))
p.bit_count = max_bits
def keys_from_blk1_blk2_make_empty(self):
for p in self:
if (p.efuse_block == "EFUSE_BLK1" and custom_table_use_BLK1 == True) or (p.efuse_block == "EFUSE_BLK2" and custom_table_use_BLK2 == True):
p.bit_count = 0
print("efuse: `%s` field was changed from %d to 0" % (p.field_name, p.bit_count))
def calc_md5(self):
txt_table = ''
for p in self:
txt_table += "%s %s %d %d %s" % (p.field_name, p.efuse_block, p.bit_start, p.bit_count, p.comment) + "\n"
self.md5_digest_table = hashlib.md5(txt_table).hexdigest()
def show_range_used_bits(self):
self.md5_digest_table = hashlib.md5(txt_table.encode('utf-8')).hexdigest()
def show_range_used_bits(self):
# print used and free bits
rows = ''
rows += 'Sorted efuse table:\n'
@ -194,13 +178,13 @@ class FuseTable(list):
for p in sorted(self, key=lambda x:(x.efuse_block, x.bit_start)):
rows += "{0} \t{1:<30} \t{2} \t{3:^8} \t{4:^8}".format(num, p.field_name, p.efuse_block, p.bit_start, p.bit_count) + "\n"
num += 1
rows += '\nUsed bits in efuse table:\n'
last = None
for p in sorted(self, key=lambda x:(x.efuse_block, x.bit_start)):
if last is None:
rows += '%s \n[%d ' % (p.efuse_block, p.bit_start)
if last is not None:
if last is not None:
if last.efuse_block != p.efuse_block:
rows += '%d] \n\n%s \n[%d ' % (last.bit_start + last.bit_count - 1, p.efuse_block, p.bit_start)
elif last.bit_start + last.bit_count != p.bit_start:
@ -209,7 +193,7 @@ class FuseTable(list):
rows += '%d] \n' % (last.bit_start + last.bit_count - 1)
rows += '\nNote: Not printed ranges are free for using. (bits in EFUSE_BLK0 are reserved for Espressif)\n'
return rows
def to_header(self, file_name):
rows = [copyright]
rows += ["#ifdef __cplusplus",
@ -223,13 +207,13 @@ class FuseTable(list):
"// To show efuse_table run the command 'make show_efuse_table'.",
"",
""]
last_field_name = ''
for p in self:
if (p.field_name != last_field_name):
rows += ["extern const esp_efuse_desc_t* " + "ESP_EFUSE_" + p.field_name + "[];"]
last_field_name = p.field_name
rows += ["",
"#ifdef __cplusplus",
"}",
@ -248,7 +232,7 @@ class FuseTable(list):
"// To show efuse_table run the command 'make show_efuse_table'.",
"",
""]
last_name = ''
for p in self:
if (p.field_name != last_name):
@ -258,9 +242,8 @@ class FuseTable(list):
last_name = p.field_name
rows += [p.to_struct(debug) + ","]
rows += ["};\n"]
rows += ["\n\n\n"]
last_name = ''
for p in self:
if (p.field_name != last_name):
@ -272,8 +255,8 @@ class FuseTable(list):
index = str(0) if str(p.group) == "" else str(p.group)
rows += [" &" + p.field_name + "[" + index + "], \t\t// " + p.comment]
rows += [" NULL",
"};\n" ]
"};\n"]
return '\n'.join(rows) + "\n"
@ -306,13 +289,13 @@ class FuseDefinition(object):
if strval == "":
return None # Field will fill in default
return self.parse_int(strval)
def parse_int(self, v):
try:
return int(v, 0)
except ValueError:
raise InputError("Invalid field value %s" % v)
def parse_block(self, strval):
if strval == "":
raise InputError("Field 'efuse_block' can't be left empty.")
@ -334,35 +317,35 @@ class FuseDefinition(object):
else:
raise ValidationError(self, "Unknown coding scheme")
return max_bits
def verify(self, type_table):
if self.efuse_block is None:
raise ValidationError(self, "efuse_block field is not set")
if self.bit_count is None:
raise ValidationError(self, "bit_count field is not set")
if type_table is not None:
if type_table == "custom_table":
if self.efuse_block != "EFUSE_BLK3":
raise ValidationError(self, "custom_table should use only EFUSE_BLK3")
max_bits = self.get_max_bits_of_block()
if self.bit_start + self.bit_count > max_bits:
raise ValidationError(self, "The field is outside the boundaries(max_bits = %d) of the %s block" % (max_bits, self.efuse_block))
def get_full_name(self):
def get_postfix(group):
postfix = ""
if group != "":
postfix = "_PART_" + group
return postfix
return self.field_name + get_postfix(self.group)
def to_struct(self, debug):
start = " {"
if (debug == True):
if debug is True:
start = " {" + '"' + self.field_name + '" ,'
return ", ".join([start + self.efuse_block,
str(self.bit_start),
@ -390,24 +373,24 @@ def ckeck_md5_in_file(md5, filename):
def create_output_files(name, output_table, debug):
file_name = os.path.splitext(os.path.basename(name))[0]
gen_dir = os.path.dirname(name)
dir_for_file_h = gen_dir + "/include"
try:
os.stat(dir_for_file_h)
except:
except Exception:
os.mkdir(dir_for_file_h)
file_h_path = os.path.join(dir_for_file_h, file_name + ".h")
file_c_path = os.path.join(gen_dir, file_name + ".c")
# src files are the same
if ckeck_md5_in_file(output_table.md5_digest_table, file_c_path) == False:
if ckeck_md5_in_file(output_table.md5_digest_table, file_c_path) is False:
status("Creating efuse *.h file " + file_h_path + " ...")
output = output_table.to_header(file_name)
with open(file_h_path, 'w') as f:
f.write(output)
f.close()
status("Creating efuse *.c file " + file_c_path + " ...")
output = output_table.to_c_file(file_name, debug)
with open(file_c_path, 'w') as f:
@ -420,15 +403,14 @@ def create_output_files(name, output_table, debug):
def main():
global quiet
global coding_scheme
parser = argparse.ArgumentParser(description='ESP32 eFuse Manager')
parser.add_argument('--quiet', '-q', help="Don't print non-critical status messages to stderr", action='store_true')
parser.add_argument('--debug', help='Create header file with debug info', default=False, action="store_false")
parser.add_argument('--info', help='Print info about range of used bits', default=False, action="store_true")
parser.add_argument('--coding_scheme', help='Coding scheme', type=int, default=0)
parser.add_argument('common_input', help='Path to common CSV file to parse.', type=argparse.FileType('rb'))
parser.add_argument('custom_input', help='Path to custom CSV file to parse.', type=argparse.FileType('rb'), nargs='?', default=None)
parser.add_argument('common_input', help='Path to common CSV file to parse.', type=argparse.FileType('r'))
parser.add_argument('custom_input', help='Path to custom CSV file to parse.', type=argparse.FileType('r'), nargs='?', default=None)
args = parser.parse_args()
@ -441,37 +423,39 @@ def main():
print("eFuse coding scheme: REPEAT")
else:
raise InputError("unknown CODE_SCHEME = %s" % (coding_scheme))
quiet = args.quiet
debug = args.debug
info = args.info
common_table = process_input_file(args.common_input, "common_table")
two_table = common_table
if args.custom_input is not None:
custom_table = process_input_file(args.custom_input, "custom_table")
two_table += custom_table
two_table.verify()
# save files.
if info == False:
if info is False:
if args.custom_input is None:
create_output_files(args.common_input.name, common_table, debug)
else:
create_output_files(args.custom_input.name, custom_table, debug)
else:
print(two_table.show_range_used_bits())
return 0
class InputError(RuntimeError):
def __init__(self, e):
super(InputError, self).__init__(e)
class ValidationError(InputError):
def __init__(self, p, message):
super(ValidationError, self).__init__("Entry %s invalid: %s" % (p.field_name, message))
if __name__ == '__main__':
try:
main()

73
components/efuse/test_efuse_host/efuse_tests.py Normal file → Executable file
View File

@ -1,13 +1,7 @@
#!/usr/bin/env python
from __future__ import print_function, division
import unittest
import struct
import csv
import sys
import subprocess
import tempfile
import os
import StringIO
try:
import efuse_table_gen
@ -33,13 +27,13 @@ name2, EFUSE_BLK3, 5,
"""
t = efuse_table_gen.FuseTable.from_csv(csv)
t.verify()
self.assertEqual(t[0].field_name, 'name1')
self.assertEqual(t[0].efuse_block, 'EFUSE_BLK3')
self.assertEqual(t[0].bit_start, 0)
self.assertEqual(t[0].bit_count, 5)
self.assertEqual(t[0].comment, 'Use for test name 1')
self.assertEqual(t[1].field_name, 'name2')
self.assertEqual(t[1].efuse_block, 'EFUSE_BLK3')
self.assertEqual(t[1].bit_start, 5)
@ -54,11 +48,11 @@ name2, EFUSE_BLK3, ,
"""
t = efuse_table_gen.FuseTable.from_csv(csv)
t.verify()
self.assertEqual(t[0].field_name, 'name1')
self.assertEqual(t[0].bit_start, 0)
self.assertEqual(t[0].bit_count, 5)
self.assertEqual(t[1].field_name, 'name2')
self.assertEqual(t[1].bit_start, 5)
self.assertEqual(t[1].bit_count, 4)
@ -71,11 +65,11 @@ name2, EFUSE_BLK2, ,
"""
t = efuse_table_gen.FuseTable.from_csv(csv)
t.verify()
self.assertEqual(t[0].field_name, 'name1')
self.assertEqual(t[0].bit_start, 0)
self.assertEqual(t[0].bit_count, 5)
self.assertEqual(t[1].field_name, 'name2')
self.assertEqual(t[1].bit_start, 0)
self.assertEqual(t[1].bit_count, 4)
@ -90,15 +84,15 @@ name3, EFUSE_BLK2, 5,
"""
t = efuse_table_gen.FuseTable.from_csv(csv)
t.verify()
self.assertEqual(t[0].field_name, 'name1')
self.assertEqual(t[0].bit_start, 0)
self.assertEqual(t[0].bit_count, 5)
self.assertEqual(t[1].field_name, 'name2')
self.assertEqual(t[1].bit_start, 0)
self.assertEqual(t[1].bit_count, 4)
self.assertEqual(t[2].field_name, 'name3')
self.assertEqual(t[2].bit_start, 5)
self.assertEqual(t[2].bit_count, 4)
@ -112,7 +106,7 @@ name2, EFUSE_BLK2, ,
name1, EFUSE_BLK3, , 5,
"""
with self.assertRaisesRegexp(efuse_table_gen.InputError, "Field names must be unique"):
t = efuse_table_gen.FuseTable.from_csv(csv)
efuse_table_gen.FuseTable.from_csv(csv)
def test_seq_bit_start5_fill(self):
csv = """
@ -124,19 +118,19 @@ name3, EFUSE_BLK3, 5,
"""
t = efuse_table_gen.FuseTable.from_csv(csv)
t.verify()
self.assertEqual(t[0].field_name, 'name1')
self.assertEqual(t[0].bit_start, 0)
self.assertEqual(t[0].bit_count, 5)
self.assertEqual(t[1].field_name, 'name2')
self.assertEqual(t[1].bit_start, 0)
self.assertEqual(t[1].bit_count, 4)
self.assertEqual(t[2].field_name, 'name2')
self.assertEqual(t[2].bit_start, 4)
self.assertEqual(t[2].bit_count, 4)
self.assertEqual(t[3].field_name, 'name3')
self.assertEqual(t[3].bit_start, 5)
self.assertEqual(t[3].bit_count, 5)
@ -158,7 +152,7 @@ name2, EFUSE_BLK3, 5,
name2, EFUSE_BLK2, , 4,
"""
with self.assertRaisesRegexp(efuse_table_gen.InputError, "missing field name"):
t = efuse_table_gen.FuseTable.from_csv(csv)
efuse_table_gen.FuseTable.from_csv(csv)
def test_unique_field_name_fail(self):
csv = """
@ -167,7 +161,7 @@ name1, EFUSE_BLK3, 0,
name1, EFUSE_BLK3, 5, 4, Use for test name 2
"""
with self.assertRaisesRegexp(efuse_table_gen.InputError, "Field names must be unique"):
t = efuse_table_gen.FuseTable.from_csv(csv)
efuse_table_gen.FuseTable.from_csv(csv)
def test_bit_count_empty_fail(self):
csv = """
@ -176,7 +170,7 @@ name1, EFUSE_BLK3, 0,
name2, EFUSE_BLK3, 5, 4, Use for test name 2
"""
with self.assertRaisesRegexp(efuse_table_gen.InputError, "empty"):
t = efuse_table_gen.FuseTable.from_csv(csv)
efuse_table_gen.FuseTable.from_csv(csv)
def test_bit_start_num_fail(self):
csv = """
@ -185,7 +179,7 @@ name1, EFUSE_BLK3, k,
name2, EFUSE_BLK3, 5, 4, Use for test name 2
"""
with self.assertRaisesRegexp(efuse_table_gen.InputError, "Invalid field value"):
t = efuse_table_gen.FuseTable.from_csv(csv)
efuse_table_gen.FuseTable.from_csv(csv)
def test_join_entry(self):
csv = """
@ -198,27 +192,27 @@ name4, EFUSE_BLK2, 30,
"""
t = efuse_table_gen.FuseTable.from_csv(csv)
t.verify()
self.assertEqual(t[0].field_name, 'name1')
self.assertEqual(t[0].efuse_block, 'EFUSE_BLK2')
self.assertEqual(t[0].bit_start, 0)
self.assertEqual(t[0].bit_count, 6)
self.assertEqual(t[1].field_name, 'name2')
self.assertEqual(t[1].efuse_block, 'EFUSE_BLK2')
self.assertEqual(t[1].bit_start, 6)
self.assertEqual(t[1].bit_count, 5)
self.assertEqual(t[2].field_name, 'name3')
self.assertEqual(t[2].efuse_block, 'EFUSE_BLK3')
self.assertEqual(t[2].bit_start, 20)
self.assertEqual(t[2].bit_count, 5)
self.assertEqual(t[3].field_name, 'name3')
self.assertEqual(t[3].efuse_block, 'EFUSE_BLK3')
self.assertEqual(t[3].bit_start, 30)
self.assertEqual(t[3].bit_count, 5)
self.assertEqual(t[4].field_name, 'name4')
self.assertEqual(t[4].efuse_block, 'EFUSE_BLK2')
self.assertEqual(t[4].bit_start, 30)
@ -231,7 +225,7 @@ name1, EFUSE_BLK5, 0,
name2, EFUSE_BLK3, 5, 4, Use for test name 2
"""
with self.assertRaisesRegexp(efuse_table_gen.InputError, "'efuse_block' should consist from EFUSE_BLK0..EFUSE_BLK3"):
t = efuse_table_gen.FuseTable.from_csv(csv)
efuse_table_gen.FuseTable.from_csv(csv)
def test_field_size_is_ok(self):
csv = """
@ -239,7 +233,7 @@ name2, EFUSE_BLK3, 5,
name1, EFUSE_BLK0, 0, 224, Use for test name 1
name2, EFUSE_BLK1, 0, 256, Use for test name 2
"""
efuse_table_gen.coding_scheme = 0 # NONE
efuse_table_gen.coding_scheme = 0 # NONE
t = efuse_table_gen.FuseTable.from_csv(csv)
t.verify()
@ -249,7 +243,7 @@ name2, EFUSE_BLK1, 0,
name1, EFUSE_BLK3, 190, 1, Use for test name 1
name2, EFUSE_BLK3, 191, 5, Use for test name 2
"""
efuse_table_gen.coding_scheme = 1 #3/4 coding
efuse_table_gen.coding_scheme = 1 # 3/4 coding
t = efuse_table_gen.FuseTable.from_csv(csv)
with self.assertRaisesRegexp(efuse_table_gen.InputError, "The field is outside the boundaries"):
t.verify()
@ -266,7 +260,7 @@ name2, EFUSE_BLK1, 1,
class VerificationTests(unittest.TestCase):
def test_general(self):
csv = """
# field_name, efuse_block(EFUSE_BLK0..EFUSE_BLK3), bit_start(0..255), bit_count, comment
@ -277,22 +271,22 @@ name2_1, EFUSE_BLK2, 5,
"""
t = efuse_table_gen.FuseTable.from_csv(csv)
t.verify()
self.assertEqual(t[0].field_name, 'name1')
self.assertEqual(t[0].efuse_block, 'EFUSE_BLK3')
self.assertEqual(t[0].bit_start, 0)
self.assertEqual(t[0].bit_count, 5)
self.assertEqual(t[1].field_name, 'name2')
self.assertEqual(t[1].efuse_block, 'EFUSE_BLK3')
self.assertEqual(t[1].bit_start, 5)
self.assertEqual(t[1].bit_count, 4)
self.assertEqual(t[2].field_name, 'name1_1')
self.assertEqual(t[2].efuse_block, 'EFUSE_BLK2')
self.assertEqual(t[2].bit_start, 0)
self.assertEqual(t[2].bit_count, 5)
self.assertEqual(t[3].field_name, 'name2_1')
self.assertEqual(t[3].efuse_block, 'EFUSE_BLK2')
self.assertEqual(t[3].bit_start, 5)
@ -317,7 +311,7 @@ name2, EFUSE_BLK2, 5,
common_table = efuse_table_gen.FuseTable.from_csv(csv_common)
common_table.verify("common_table")
two_tables = common_table
csv_custom = """
# field_name, efuse_block(EFUSE_BLK0..EFUSE_BLK3), bit_start(0..255), bit_count, comment
name3, EFUSE_BLK3, 20, 5, Use for test name 1
@ -325,10 +319,11 @@ name4, EFUSE_BLK3, 4,
"""
custom_table = efuse_table_gen.FuseTable.from_csv(csv_custom)
custom_table.verify("custom_table")
two_tables += custom_table
with self.assertRaisesRegexp(efuse_table_gen.InputError, "overlaps"):
two_tables.verify()
if __name__ == "__main__":
unittest.main()