Move BOM replacements to a separate csv file https://github.com/rusefi/hellen121vag/issues/7

This commit is contained in:
Andrei 2021-03-23 08:58:14 +02:00
parent b4e5c36d2e
commit 045b380244
2 changed files with 40 additions and 29 deletions

View File

@ -8,21 +8,21 @@ from collections import OrderedDict
import csv, sys, re import csv, sys, re
if len(sys.argv) < 2: if len(sys.argv) < 2:
print "Error! Please specify a BOM file name." print ("Error! Please specify a BOM file name.")
sys.exit(1) sys.exit(1)
fileName = sys.argv[1] fileName = sys.argv[1]
if len(sys.argv) > 2: if len(sys.argv) > 2:
repl = sys.argv[2] repl_csv = sys.argv[2]
print "Opening BOM file " + fileName + "..." print ("Opening BOM file " + fileName + "...")
rows = OrderedDict() rows = OrderedDict()
emptyId = 1 emptyId = 1
with open(fileName, 'rb') as f: with open(fileName, 'rb') as f:
reader = csv.reader(f, delimiter=',') reader = csv.reader(f, delimiter=',')
print "Searching for duplicates..." print ("Searching for duplicates...")
for row in reader: for row in reader:
rowName = row[3] rowName = row[3]
# all empty names should be saved separately # all empty names should be saved separately
@ -34,45 +34,53 @@ with open(fileName, 'rb') as f:
if rowName in rows: if rowName in rows:
oldRow = rows[rowName] oldRow = rows[rowName]
if oldRow[0] != row[0]: if oldRow[0] != row[0]:
print "* Error! Comment mismatch for the part #" + rowName + ": " + oldRow[0] + " != " + row[0] print ("* Error! Comment mismatch for the part #" + rowName + ": " + oldRow[0] + " != " + row[0])
sys.exit(2) sys.exit(2)
if oldRow[2] != row[2]: if oldRow[2] != row[2]:
print "* Warning! Footprint mismatch for the part #" + rowName + ": " + oldRow[2] + " != " + row[2] print ("* Warning! Footprint mismatch for the part #" + rowName + ": " + oldRow[2] + " != " + row[2])
#sys.exit(3) #sys.exit(3)
print "* Duplicates found for " + rowName + " (" + row[0] + ")! Merging..." print ("* Duplicates found for " + rowName + " (" + row[0] + ")! Merging...")
row[1] = oldRow[1] + row[1] row[1] = oldRow[1] + row[1]
rows[rowName] = row rows[rowName] = row
#for idx,item in enumerate(row): #for idx,item in enumerate(row):
# print idx , ": ", item # print idx , ": ", item
print "Processing the board replacements..." replList = list()
replList = repl.split(";") print ("Reading replacement list from the CSV file " + repl_csv + "...")
for r in replList: with open(repl_csv, 'rb') as f:
rel = re.search("(\w+)=([\w\-]+)?,?([\w\-]+)?,?(\w+)?", r) reader = csv.reader(f, delimiter=',')
if not rel: for row in reader:
# skip empty lines and comments (this is not strictly CSV-compliant, but useful for our purposes)
if (len(row) < 1 or row[0].startswith("#")):
continue continue
reDesignator = rel.group(1) replList.append(row)
reComment = rel.group(2)
reFootprint = rel.group(3) print ("Processing the board replacements...")
rePartNumber = rel.group(4) for r in replList:
reDesignator = r[0]
for rowName in rows: for rowName in rows:
row = rows[rowName] row = rows[rowName]
if reDesignator in row[1]: if reDesignator in row[1]:
print "* Removing " + reDesignator + " from the old row..." print ("* Removing " + reDesignator + " from the old row...")
row[1].remove(reDesignator) row[1].remove(reDesignator)
if not row[1]: if not row[1]:
print "* Deleting an empty row..." print ("* Deleting an empty row...")
del rows[rowName] del rows[rowName]
if len(r) < 4:
continue
reComment = r[1]
reFootprint = r[2]
rePartNumber = r[3]
# find the matching row by partnumber (if set) # find the matching row by partnumber (if set)
if rePartNumber: if rePartNumber:
if rePartNumber in rows: if rePartNumber in rows:
print "* Adding " + reDesignator + " to another existing row..." print ("* Adding " + reDesignator + " to another existing row...")
rows[rePartNumber][1] += [reDesignator] rows[rePartNumber][1] += [reDesignator]
else: else:
print "* Appending a new row for " + reDesignator + "..." print ("* Appending a new row for " + reDesignator + "...")
rows[rePartNumber] = [reComment, [reDesignator], reFootprint, rePartNumber] rows[rePartNumber] = [reComment, [reDesignator], reFootprint, rePartNumber]
print "Saving..." print ("Saving...")
with open (fileName, 'wb') as new_f: with open (fileName, 'wb') as new_f:
rowIdx = 0 rowIdx = 0
for rowName in rows: for rowName in rows:
@ -90,4 +98,4 @@ with open (fileName, 'wb') as new_f:
row[1] = ", ".join(row[1]) row[1] = ", ".join(row[1])
writer.writerow(row) writer.writerow(row)
rowIdx += 1 rowIdx += 1
print "Done!" print ("Done!")

View File

@ -11,16 +11,17 @@ import csv, re
import subprocess import subprocess
if len(sys.argv) < 4: if len(sys.argv) < 4:
print ("Error! Please specify the project base name, frame name, revision and optional BOM replacement list.") print ("Error! Please specify the project base name, frame name, revision and optional BOM replacement file.")
sys.exit(1) sys.exit(1)
project_base = sys.argv[1] project_base_path = sys.argv[1]
frame_name = sys.argv[2] frame_name = sys.argv[2]
frame_rev = sys.argv[3] frame_rev = sys.argv[3]
if len(sys.argv) > 3: if len(sys.argv) > 3:
bom_replace = sys.argv[4] bom_replace_csv = sys.argv[4]
else: else:
bom_replace = "" bom_replace_csv = ""
board_prefix = "hellen" board_prefix = "hellen"
@ -29,7 +30,8 @@ imageDpi = "600"
# these should match the definitions in the parent shell script (create_board.sh)! # these should match the definitions in the parent shell script (create_board.sh)!
project_name = board_prefix + frame_name project_name = board_prefix + frame_name
board_name = project_name + "-" + frame_rev board_name = project_name + "-" + frame_rev
project_path = project_base + "/" + project_name + "/boards/" + board_name project_path = project_base_path + "/boards/" + board_name
bom_replace_csv_path = project_base_path + "/" + bom_replace_csv
frame_path = project_path + "/frame" frame_path = project_path + "/frame"
board_path = project_path + "/board" board_path = project_path + "/board"
board_path_name = board_path + "/" + board_name board_path_name = board_path + "/" + board_name
@ -297,9 +299,10 @@ p.communicate(input='y\n')[0]
print ("Post-processing BOM...") print ("Post-processing BOM...")
try: try:
subprocess.check_output([sys.executable, "bin/process_BOM.py", out = subprocess.check_output([sys.executable, "bin/process_BOM.py",
board_bom, board_bom,
bom_replace]) bom_replace_csv_path], stderr=subprocess.STDOUT)
print (out)
except subprocess.CalledProcessError, e: except subprocess.CalledProcessError, e:
print ("BOM processing error:\n" + e.output) print ("BOM processing error:\n" + e.output)
sys.exit(2) sys.exit(2)