Patchwork [U-Boot,RFC,V2,6/9] tools: add reformat.py and (temporary merge.py)

login
register
mail settings
Submitter Albert ARIBAUD
Date July 26, 2013, 9:37 p.m.
Message ID <1374874635-12042-7-git-send-email-albert.u.boot@aribaud.net>
Download mbox | patch
Permalink /patch/262314/
State Superseded
Delegated to: Tom Rini
Headers show

Comments

Albert ARIBAUD - July 26, 2013, 9:37 p.m.
tools/merge.py is only in the RFC, and will actually
be removed once it has merged MAINTAINERS and boards.cfg

tools/reformat.py will be used to keep boards.cfg
properly sorted and formatted.

Signed-off-by: Albert ARIBAUD <albert.u.boot@aribaud.net>
---
 tools/merge.py    |  61 +++++++++++++++++++++++++
 tools/reformat.py | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 193 insertions(+)
 create mode 100755 tools/merge.py
 create mode 100755 tools/reformat.py

Patch

diff --git a/tools/merge.py b/tools/merge.py
new file mode 100755
index 0000000..c043ebb
--- /dev/null
+++ b/tools/merge.py
@@ -0,0 +1,61 @@ 
+#! /usr/bin/python
+
+import sys
+
+# read MAINTAINERS into an array of [ system, board, status, maintainers ]
+
+MArray = []
+MFile = open('MAINTAINERS', 'r')
+for mline in MFile:
+	mline = mline.strip('\n')
+	if (len(mline)>0) and (mline[0] != '#'):
+		mfields = mline.split(None,3)
+		while len(mfields)<4:
+			mfields.append('')
+		MArray.append(tuple(mfields))
+MFile.close
+
+# read boards.cfg into an array of [ Target, ARCH, CPU, Board name, Vendor,
+# SoC, Options ]
+
+BArray = []
+BFile = open('boards.cfg', 'r')
+for bline in BFile:
+	bline = bline.strip('\n')
+	if (len(bline)>0) and (bline[0] != '#'):
+		bfields = bline.split(None,6)
+		while len(bfields)<7:
+			bfields.append('')
+		BArray.append(tuple(bfields))
+BFile.close
+
+# Now, for each boards target: search for matching entries in MAINTAINERS
+# and collect their status and maintainer(s). Matching is done against
+# base target in opts if any, or target, or board.
+
+for tgt, arch, cpu, brd, vend, soc, opts in BArray:
+	if len(vend)==0:
+		vend = '-'
+	if len(soc)==0:
+		soc = '-'
+	if len(opts)==0:
+		opts = '-'
+	mtnrs = []
+	base = opts.split(':',1)
+	if len(base)>0:
+		base = base[0]
+	else:
+		base = ''
+	status = 'Active'
+	for sys, brd2, sts, mtnr in MArray:
+		if (brd2.lower() == base.lower()) \
+		or (brd2.lower() == tgt.lower()) \
+		or (brd2.lower() == brd.lower()):
+			mtnrs.append(mtnr)
+			status = sts
+	print status, arch, cpu, soc, vend, brd, tgt, opts,
+	if len(mtnrs) >0:
+		print ':'.join(set(mtnrs)),
+	else:
+		print '-',
+	print
diff --git a/tools/reformat.py b/tools/reformat.py
new file mode 100755
index 0000000..ff6633f
--- /dev/null
+++ b/tools/reformat.py
@@ -0,0 +1,132 @@ 
+#! /usr/bin/python
+########################################################################
+#
+# reorder and reformat a file in columns
+#
+# this utility takes lines from its standard input and reproduces them,
+# partially reordered and reformatted, on its standard output.
+#
+# It has the same effect as a 'sort | column -t', with the exception
+# that empty lines, as well as lines which start with a '#' sign, are
+# not affected, i.e. they keep their position and formatting, and act
+# as separators, i.e. the parts before and after them are each sorted
+# separately (but overall field widths are computed across the whole
+# input).
+#
+# Options:
+#   -i:
+#   --ignore-case:
+#	Do not consider case when sorting.
+#   -d:
+#   --default:
+#	What to chage empty fields to.
+#    -s <N>:
+#    --split=<N>:
+#       Treat only the first N whitespace sequences as separators.
+#       line content after the Nth separator will count as only one
+#       field even if it contains whitespace.
+#       Example : '-s 2' causes input 'a b c d e' to be split into
+#       three fields, 'a', 'b', and 'c d e'.
+#
+# boards.cfg requires -ids 6.
+#       	
+########################################################################
+
+import sys, getopt, locale
+
+# ensure we sort using the C locale.
+
+locale.setlocale(locale.LC_ALL, 'C')
+
+# check options
+
+maxsplit = 0
+ignore_case = 0
+default_field =''
+
+try:
+	opts, args = getopt.getopt(sys.argv[1:], "id:s:",
+		["ignore-case","default","split="])
+except getopt.GetoptError as err:
+	print str(err) # will print something like "option -a not recognized"
+        sys.exit(2)
+
+for o, a in opts:
+	if o in ("-s", "--split"):
+		maxsplit = eval(a)
+	elif o in ("-i", "--ignore-case"):
+		ignore_case = 1
+	elif o in ("-d", "--default"):
+		default_field = a
+	else:
+		assert False, "unhandled option"
+
+# collect all lines from standard input and, for the ones which must be
+# reformatted and sorted, count their fields and compute each field's
+# maximum size
+
+input_lines = []
+field_width = []
+
+for line in sys.stdin:
+	# remove final end of line
+	input_line = line.strip('\n')
+	if (len(input_line)>0) and (input_line[0] != '#'):
+		# sortable line: split into fields
+		fields = input_line.split(None,maxsplit)
+		# if there are new fields, top up field_widths
+		for f in range(len(field_width), len(fields)):
+			field_width.append(0)
+		# compute the maximum witdh of each field
+		for f in range(len(fields)):
+			field_width[f] = max(field_width[f],len(fields[f]))
+	# collect the line for next stage
+	input_lines.append(input_line)
+
+# run through collected input lines, collect the ones which must be
+# reformatted and sorted, and whenever a non-reformattable, non-sortable
+# line is met, sort the collected lines before it and append them to the
+# output lines, then add the non-sortable line too.
+
+output_lines = []
+sortable_lines = []
+for input_line in input_lines:
+	if (len(input_line)>0) and (input_line[0] != '#'):
+		# this line should be reformatted and sorted
+		input_fields = input_line.split(None,maxsplit)
+		output_fields = [];
+		# reformat each field to this field's column width
+		for f in range(len(input_fields)):
+			output_field = input_fields[f];
+			output_fields.append(output_field.ljust(field_width[f]))
+		# any missing field is set to default if it exists 
+		if default_field != '':
+			for f in range(len(input_fields),len(field_width)):
+				output_fields.append(default_field.ljust(field_width[f]))
+		# join fields using two spaces, like column -t would
+		output_line = '  '.join(output_fields);
+		# collect line for later
+		sortable_lines.append(output_line)
+	else:
+		# this line is non-sortable
+		# sort collected sortable lines
+		if ignore_case!=0:
+			sortable_lines.sort(key=lambda x: str.lower(locale.strxfrm(x)))
+		else:
+			sortable_lines.sort(key=lambda x: locale.strxfrm(x))
+		# append sortable lines to the final output
+		output_lines.extend(sortable_lines)
+		sortable_lines = []
+		# append non-sortable line to the final output
+		output_lines.append(input_line)
+# maybe we had sortable lines pending, so append them to the final output
+if ignore_case!=0:
+	sortable_lines.sort(key=lambda x: str.lower(locale.strxfrm(x)))
+else:
+	sortable_lines.sort(key=lambda x: locale.strxfrm(x))
+output_lines.extend(sortable_lines)
+
+# run through output lines and print them, except rightmost whitespace
+
+for output_line in output_lines:
+	print output_line.rstrip()