patman: Update to support Python 3
cover patman: Update to support Python 3
This series updates patman to support Python 3:
- Avoid using 'unicode' type directly
- Use items() instead of iteritems()
- Make sure file I/O uses binary mode where necessary
- Change print statements to functions
- Use the built-in set() class
- Fix up generation of repeated bytes
A few patches for binman are included, but this still requires Python 2.
Simon Glass (21):
patman: Update cros_subprocess to use bytearray
patman: Convert print statements to Python 3
binman: Convert print statements to Python 3
binman: Don't show errors for failed tests
binman: Remove use of Set()
patman: Use items() instead of iteritems()
binman: Use items() instead of iteritems()
tools: binman: Open all binary files in binary mode
tools: dtoc: Open all binary files in binary mode
patman: Provide a way to get program output in binary mode
binman: Use binary mode when compressing data
binman: Drop an unused input file
binman: Handle repeated bytes for Python 3
Add a simple script to remove boards
patman: Support use of stringIO in Python 3
patman: Move unicode helpers to tools
patman: Sort series output for repeatabily
patman: Avoid unicode type in settings unit tests
patman: Adjust functional tests for Python 3
patman: Tidy up a few more unicode conversions
patman: Don't require Python 2
tools/binman/binman.py | 26 +++- tools/binman/bsection.py | 7 +- tools/binman/control.py | 8 +- tools/binman/elf.py | 4 +- tools/binman/elf_test.py | 2 +- tools/binman/entry.py | 5 +- tools/binman/etype/blob.py | 2 +- tools/binman/etype/fill.py | 4 +- tools/binman/etype/gbb.py | 2 +- tools/binman/etype/u_boot_spl_bss_pad.py | 2 +- tools/binman/ftest.py | 81 ++++++------ tools/binman/state.py | 7 +- tools/dtoc/fdt.py | 2 +- tools/patman/cros_subprocess.py | 53 +++++--- tools/patman/func_test.py | 41 ++++--- tools/patman/gitutil.py | 16 +-- tools/patman/patman.py | 2 +- tools/patman/series.py | 20 +-- tools/patman/settings.py | 34 ++--- tools/patman/test_util.py | 16 +-- tools/patman/tools.py | 55 ++++++++- tools/rmboard.py | 150 +++++++++++++++++++++++ 22 files changed, 385 insertions(+), 154 deletions(-) create mode 100755 tools/rmboard.py
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 1/21 patman: Update cros_subprocess to use bytearray
At present this function uses lists and strings. This does not work so well with Python 3, and testing against '' does not work for a bytearray. Update the code to fix these issues.
---
tools/patman/cros_subprocess.py | 48 ++++++++++++++++++++------------- tools/patman/gitutil.py | 2 ++ 2 files changed, 32 insertions(+), 18 deletions(-)
tools/patman/cros_subprocess.py +30 -18
@@ -100,6 +100,24 @@ class Popen(subprocess.Popen): if kwargs: raise ValueError("Unit tests do not test extra args - please add tests") + def ConvertData(self, data): + """Convert stdout/stderr data to the correct format for output + + Args: + data: Data to convert, or None for '' + + Returns: + Converted data to a unicode string + """ + if data is None: + return '' + else: + try: + data = data.decode('utf-8') + except UnicodeDecodeError: + data = data.decode('utf-8', 'ignore') + return data + def CommunicateFilter(self, output): """Interact with process: Read data from stdout and stderr.
@@ -156,11 +174,11 @@ class Popen(subprocess.Popen): self.stdin.close() if self.stdout: read_set.append(self.stdout) - stdout = [] + stdout = bytearray() if self.stderr and self.stderr != self.stdout: read_set.append(self.stderr) - stderr = [] - combined = [] + stderr = bytearray() + combined = bytearray() input_offset = 0 while read_set or write_set:
@@ -192,12 +210,12 @@ class Popen(subprocess.Popen): data = os.read(self.stdout.fileno(), 1024) except OSError: pass - if data == "": + if not len(data): self.stdout.close() read_set.remove(self.stdout) else: - stdout.append(data) - combined.append(data) + stdout += data + combined += data if output: output(sys.stdout, data) if self.stderr in rlist:
@@ -207,25 +225,19 @@ class Popen(subprocess.Popen): data = os.read(self.stderr.fileno(), 1024) except OSError: pass - if data == "": + if not len(data): self.stderr.close() read_set.remove(self.stderr) else: - stderr.append(data) - combined.append(data) + stderr += data + combined += data if output: output(sys.stderr, data) # All data exchanged. Translate lists into strings. - if stdout is not None: - stdout = ''.join(stdout) - else: - stdout = '' - if stderr is not None: - stderr = ''.join(stderr) - else: - stderr = '' - combined = ''.join(combined) + stdout = self.ConvertData(stdout) + stderr = self.ConvertData(stderr) + combined = self.ConvertData(combined) # Translate newlines, if requested. We cannot let the file # object do the translation: It is based on stdio, which is
tools/patman/gitutil.py +2 -0
@@ -326,6 +326,8 @@ def BuildEmailList(in_list, tag=None, alias=None, raise_on_error=True): result = [] for item in raw: if not item in result: + if type(item) == unicode: + item = item.encode('utf-8') result.append(item) if tag: return ['%s %s%s%s' % (tag, quote, email, quote) for email in result]
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 2/21 patman: Convert print statements to Python 3
In reply to Simon GlassUpdate all print statements to be functions, as required by Python 3.
---
tools/patman/func_test.py | 3 --- tools/patman/gitutil.py | 10 +++++----- tools/patman/test_util.py | 16 +++++++++------- 3 files changed, 14 insertions(+), 15 deletions(-)
tools/patman/func_test.py +0 -3
@@ -159,7 +159,6 @@ class TestFunctional(unittest.TestCase): os.remove(cc_file) lines = out[0].splitlines() - #print '\n'.join(lines) self.assertEqual('Cleaned %s patches' % len(series.commits), lines[0]) self.assertEqual('Change log missing for v2', lines[1]) self.assertEqual('Change log missing for v3', lines[2])
@@ -223,7 +222,6 @@ Simon Glass (2): ''' lines = open(cover_fname).read().splitlines() - #print '\n'.join(lines) self.assertEqual( 'Subject: [RFC PATCH v3 0/2] test: A test patch series', lines[3])
@@ -231,7 +229,6 @@ Simon Glass (2): for i, fname in enumerate(args): lines = open(fname).read().splitlines() - #print '\n'.join(lines) subject = [line for line in lines if line.startswith('Subject')] self.assertEqual('Subject: [RFC %d/%d]' % (i + 1, count), subject[0][:18])
tools/patman/gitutil.py +5 -5
@@ -397,11 +397,11 @@ def EmailPatches(series, cover_fname, args, dry_run, raise_on_error, cc_fname, git_config_to = command.Output('git', 'config', 'sendemail.to', raise_on_error=False) if not git_config_to: - print ("No recipient.\n" - "Please add something like this to a commit\n" - "Series-to: Fred Bloggs <[email protected]>\n" - "Or do something like this\n" - "git config sendemail.to u-boot at lists.denx.de") + print("No recipient.\n" + "Please add something like this to a commit\n" + "Series-to: Fred Bloggs <[email protected]>\n" + "Or do something like this\n" + "git config sendemail.to u-boot at lists.denx.de") return cc = BuildEmailList(list(set(series.get('cc')) - set(series.get('to'))), '--cc', alias, raise_on_error)
tools/patman/test_util.py +9 -7
@@ -3,6 +3,8 @@ # Copyright (c) 2016 Google, Inc # +from __future__ import print_function + from contextlib import contextmanager import glob import os
@@ -54,18 +56,18 @@ def RunTestCoverage(prog, filter_fname, exclude_list, build_dir, required=None): missing_list = required missing_list.difference_update(test_set) if missing_list: - print 'Missing tests for %s' % (', '.join(missing_list)) - print stdout + print('Missing tests for %s' % (', '.join(missing_list))) + print(stdout) ok = False coverage = lines[-1].split(' ')[-1] ok = True - print coverage + print(coverage) if coverage != '100%': - print stdout - print ("Type 'python-coverage html' to get a report in " - 'htmlcov/index.html') - print 'Coverage error: %s, but should be 100%%' % coverage + print(stdout) + print("Type 'python-coverage html' to get a report in " + 'htmlcov/index.html') + print('Coverage error: %s, but should be 100%%' % coverage) ok = False if not ok: raise ValueError('Test coverage failure')
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 3/21 binman: Convert print statements to Python 3
In reply to Simon GlassUpdate all print statements to be functions, as required by Python 3.
---
tools/binman/binman.py | 14 ++++++++------ tools/binman/control.py | 6 ++++-- 2 files changed, 12 insertions(+), 8 deletions(-)
tools/binman/binman.py +8 -6
@@ -9,6 +9,8 @@ """See README for more information""" +from __future__ import print_function + import glob import multiprocessing import os
@@ -85,13 +87,13 @@ def RunTests(debug, processes, args): else: suite.run(result) - print result + print(result) for test, err in result.errors: - print test.id(), err + print(test.id(), err) for test, err in result.failures: - print err, result.failures + print(err, result.failures) if result.errors or result.failures: - print 'binman tests FAILED' + print('binman tests FAILED') return 1 return 0
@@ -143,9 +145,9 @@ def RunBinman(options, args): try: ret_code = control.Binman(options, args) except Exception as e: - print 'binman: %s' % e + print('binman: %s' % e) if options.debug: - print + print() traceback.print_exc() ret_code = 1 return ret_code
tools/binman/control.py +4 -2
@@ -5,6 +5,8 @@ # Creates binary images from input files controlled by a description # +from __future__ import print_function + from collections import OrderedDict import os import sys
@@ -134,7 +136,7 @@ def Binman(options, args): del images[name] skip.append(name) if skip and options.verbosity >= 2: - print 'Skipping images: %s' % ', '.join(skip) + print('Skipping images: %s' % ', '.join(skip)) state.Prepare(images, dtb)
@@ -170,7 +172,7 @@ def Binman(options, args): except Exception as e: if options.map: fname = image.WriteMap() - print "Wrote map file '%s' to show errors" % fname + print("Wrote map file '%s' to show errors" % fname) raise image.SetImagePos() if options.update_fdt:
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 4/21 binman: Don't show errors for failed tests
In reply to Simon GlassAn unfortunate new feature in Python 3.5 causes binman to print errors for non-existent tests, when the test filter is used. Work around this by detecting the unwanted tests and removing them from the result.
---
tools/binman/binman.py | 12 ++++++++++++ 1 file changed, 12 insertions(+)
tools/binman/binman.py +12 -0
@@ -87,6 +87,18 @@ def RunTests(debug, processes, args): else: suite.run(result) + # Remove errors which just indicate a missing test. Since Python v3.5 If an + # ImportError or AttributeError occurs while traversing name then a + # synthetic test that raises that error when run will be returned. These + # errors are included in the errors accumulated by result.errors. + if test_name: + errors = [] + for test, err in result.errors: + if ("has no attribute '%s'" % test_name) not in err: + errors.append((test, err)) + result.testsRun -= 1 + result.errors = errors + print(result) for test, err in result.errors: print(test.id(), err)
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 5/21 binman: Remove use of Set()
In reply to Simon GlassA new built-in set() is used in both Python 2 and 3 now. Move it to use that instead of Set().
---
tools/binman/bsection.py | 3 +-- tools/binman/entry.py | 5 ++--- tools/binman/state.py | 7 +++---- 3 files changed, 6 insertions(+), 9 deletions(-)
tools/binman/bsection.py +1 -2
@@ -8,7 +8,6 @@ from __future__ import print_function from collections import OrderedDict -from sets import Set import sys import fdt_util
@@ -109,7 +108,7 @@ class Section(object): def GetFdtSet(self): """Get the set of device tree files used by this image""" - fdt_set = Set() + fdt_set = set() for entry in self._entries.values(): fdt_set.update(entry.GetFdtSet()) return fdt_set
tools/binman/entry.py +2 -3
@@ -18,7 +18,6 @@ except: have_importlib = False import os -from sets import Set import sys import fdt_util
@@ -178,8 +177,8 @@ class Entry(object): # It would be better to use isinstance(self, Entry_blob_dtb) here but # we cannot access Entry_blob_dtb if fname and fname.endswith('.dtb'): - return Set([fname]) - return Set() + return set([fname]) + return set() def ExpandEntries(self): pass
tools/binman/state.py +3 -4
@@ -7,7 +7,6 @@ import hashlib import re -from sets import Set import os import tools
@@ -24,10 +23,10 @@ entry_args = {} use_fake_dtb = False # Set of all device tree files references by images -fdt_set = Set() +fdt_set = set() # Same as above, but excluding the main one -fdt_subset = Set() +fdt_subset = set() # The DTB which contains the full image information main_dtb = None
@@ -136,7 +135,7 @@ def Prepare(images, dtb): main_dtb = dtb fdt_files.clear() fdt_files['u-boot.dtb'] = dtb - fdt_subset = Set() + fdt_subset = set() if not use_fake_dtb: for image in images.values(): fdt_subset.update(image.GetFdtSet())
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 6/21 patman: Use items() instead of iteritems()
In reply to Simon GlassPython 3 requires this, and Python 2 allows it. Convert the code over to ensure compatibility with Python 3.
---
tools/patman/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/patman/settings.py +1 -1
@@ -163,7 +163,7 @@ class _ProjectConfigParser(ConfigParser.SafeConfigParser): item_dict = dict(top_items) item_dict.update(project_items) return {(self._to_unicode(item), self._to_unicode(val)) - for item, val in item_dict.iteritems()} + for item, val in item_dict.items()} def ReadGitAliases(fname): """Read a git alias file. This is in the form used by git:
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 7/21 binman: Use items() instead of iteritems()
In reply to Simon GlassPython 3 requires this, and Python 2 allows it. Convert the code over to ensure compatibility with Python 3.
---
tools/binman/bsection.py | 2 +- tools/binman/control.py | 2 +- tools/binman/elf.py | 4 ++-- tools/binman/etype/gbb.py | 2 +- tools/binman/ftest.py | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-)
tools/binman/bsection.py +1 -1
@@ -253,7 +253,7 @@ class Section(object): """ for entry in self._entries.values(): offset_dict = entry.GetOffsets() - for name, info in offset_dict.iteritems(): + for name, info in offset_dict.items(): self._SetEntryOffsetSize(name, *info) def PackEntries(self):
tools/binman/control.py +1 -1
@@ -131,7 +131,7 @@ def Binman(options, args): if options.image: skip = [] - for name, image in images.iteritems(): + for name, image in images.items(): if name not in options.image: del images[name] skip.append(name)
tools/binman/elf.py +2 -2
@@ -59,7 +59,7 @@ def GetSymbols(fname, patterns): flags[1] == 'w') # Sort dict by address - return OrderedDict(sorted(syms.iteritems(), key=lambda x: x[1].address)) + return OrderedDict(sorted(syms.items(), key=lambda x: x[1].address)) def GetSymbolAddress(fname, sym_name): """Get a value of a symbol from an ELF file
@@ -98,7 +98,7 @@ def LookupAndWriteSymbols(elf_fname, entry, section): base = syms.get('__image_copy_start') if not base: return - for name, sym in syms.iteritems(): + for name, sym in syms.items(): if name.startswith('_binman'): msg = ("Section '%s': Symbol '%s'\n in entry '%s'" % (section.GetPath(), name, entry.GetPath()))
tools/binman/etype/gbb.py +1 -1
@@ -64,7 +64,7 @@ class Entry_gbb(Entry): self.gbb_flags = 0 flags_node = node.FindNode('flags') if flags_node: - for flag, value in gbb_flag_properties.iteritems(): + for flag, value in gbb_flag_properties.items(): if fdt_util.GetBool(flags_node, flag): self.gbb_flags |= value
tools/binman/ftest.py +1 -1
@@ -214,7 +214,7 @@ class TestFunctional(unittest.TestCase): if verbosity is not None: args.append('-v%d' % verbosity) if entry_args: - for arg, value in entry_args.iteritems(): + for arg, value in entry_args.items(): args.append('-a%s=%s' % (arg, value)) if images: for image in images:
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 8/21 tools: binman: Open all binary files in binary mode
In reply to Simon GlassAt present some files are opened in text mode despite containing binary data. This works on Python 2 but not always on Python 3, due to unicode problems. BC&D are not my favourite people. Adjust the affected open() statements to use binary mode.
---
tools/binman/ftest.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)
tools/binman/ftest.py +15 -15
@@ -119,11 +119,11 @@ class TestFunctional(unittest.TestCase): TestFunctional._MakeInputFile('refcode.bin', REFCODE_DATA) # ELF file with a '_dt_ucode_base_size' symbol - with open(self.TestFile('u_boot_ucode_ptr')) as fd: + with open(self.TestFile('u_boot_ucode_ptr'), 'rb') as fd: TestFunctional._MakeInputFile('u-boot', fd.read()) # Intel flash descriptor file - with open(self.TestFile('descriptor.bin')) as fd: + with open(self.TestFile('descriptor.bin'), 'rb') as fd: TestFunctional._MakeInputFile('descriptor.bin', fd.read()) shutil.copytree(self.TestFile('files'),
@@ -236,7 +236,7 @@ class TestFunctional(unittest.TestCase): """ tools.PrepareOutputDir(None) dtb = fdt_util.EnsureCompiled(self.TestFile(fname)) - with open(dtb) as fd: + with open(dtb, 'rb') as fd: data = fd.read() TestFunctional._MakeInputFile(outfile, data) tools.FinaliseOutputDir()
@@ -317,7 +317,7 @@ class TestFunctional(unittest.TestCase): map_data = fd.read() else: map_data = None - with open(image_fname) as fd: + with open(image_fname, 'rb') as fd: return fd.read(), dtb_data, map_data, out_dtb_fname finally: # Put the test file back
@@ -379,7 +379,7 @@ class TestFunctional(unittest.TestCase): Args: Filename of ELF file to use as SPL """ - with open(self.TestFile(src_fname)) as fd: + with open(self.TestFile(src_fname), 'rb') as fd: TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read()) @classmethod
@@ -541,7 +541,7 @@ class TestFunctional(unittest.TestCase): self.assertEqual(len(U_BOOT_DATA), image._size) fname = tools.GetOutputFilename('image1.bin') self.assertTrue(os.path.exists(fname)) - with open(fname) as fd: + with open(fname, 'rb') as fd: data = fd.read() self.assertEqual(U_BOOT_DATA, data)
@@ -549,7 +549,7 @@ class TestFunctional(unittest.TestCase): self.assertEqual(3 + len(U_BOOT_DATA) + 5, image._size) fname = tools.GetOutputFilename('image2.bin') self.assertTrue(os.path.exists(fname)) - with open(fname) as fd: + with open(fname, 'rb') as fd: data = fd.read() self.assertEqual(U_BOOT_DATA, data[3:7]) self.assertEqual(chr(0) * 3, data[:3])
@@ -970,7 +970,7 @@ class TestFunctional(unittest.TestCase): """Test that a U-Boot binary without the microcode symbol is detected""" # ELF file without a '_dt_ucode_base_size' symbol try: - with open(self.TestFile('u_boot_no_ucode_ptr')) as fd: + with open(self.TestFile('u_boot_no_ucode_ptr'), 'rb') as fd: TestFunctional._MakeInputFile('u-boot', fd.read()) with self.assertRaises(ValueError) as e:
@@ -980,7 +980,7 @@ class TestFunctional(unittest.TestCase): finally: # Put the original file back - with open(self.TestFile('u_boot_ucode_ptr')) as fd: + with open(self.TestFile('u_boot_ucode_ptr'), 'rb') as fd: TestFunctional._MakeInputFile('u-boot', fd.read()) def testMicrocodeNotInImage(self):
@@ -993,7 +993,7 @@ class TestFunctional(unittest.TestCase): def testWithoutMicrocode(self): """Test that we can cope with an image without microcode (e.g. qemu)""" - with open(self.TestFile('u_boot_no_ucode_ptr')) as fd: + with open(self.TestFile('u_boot_no_ucode_ptr'), 'rb') as fd: TestFunctional._MakeInputFile('u-boot', fd.read()) data, dtb, _, _ = self._DoReadFileDtb('044_x86_optional_ucode.dts', True)
@@ -1357,7 +1357,7 @@ class TestFunctional(unittest.TestCase): fname = pipe_list[0][-1] # Append our GBB data to the file, which will happen every time the # futility command is called. - with open(fname, 'a') as fd: + with open(fname, 'ab') as fd: fd.write(GBB_DATA) return command.CommandResult()
@@ -1431,7 +1431,7 @@ class TestFunctional(unittest.TestCase): def testTpl(self): """Test that an image with TPL and ots device tree can be created""" # ELF file with a '__bss_size' symbol - with open(self.TestFile('bss_data')) as fd: + with open(self.TestFile('bss_data'), 'rb') as fd: TestFunctional._MakeInputFile('tpl/u-boot-tpl', fd.read()) data = self._DoReadFile('078_u_boot_tpl.dts') self.assertEqual(U_BOOT_TPL_DATA + U_BOOT_TPL_DTB_DATA, data)
@@ -1693,7 +1693,7 @@ class TestFunctional(unittest.TestCase): u-boot-tpl.dtb with the microcode removed the microcode """ - with open(self.TestFile('u_boot_ucode_ptr')) as fd: + with open(self.TestFile('u_boot_ucode_ptr'), 'rb') as fd: TestFunctional._MakeInputFile('tpl/u-boot-tpl', fd.read()) first, pos_and_size = self._RunMicrocodeTest('093_x86_tpl_ucode.dts', U_BOOT_TPL_NODTB_DATA)
@@ -1748,14 +1748,14 @@ class TestFunctional(unittest.TestCase): def testElf(self): """Basic test of ELF entries""" self._SetupSplElf() - with open(self.TestFile('bss_data')) as fd: + with open(self.TestFile('bss_data'), 'rb') as fd: TestFunctional._MakeInputFile('-boot', fd.read()) data = self._DoReadFile('096_elf.dts') def testElfStripg(self): """Basic test of ELF entries""" self._SetupSplElf() - with open(self.TestFile('bss_data')) as fd: + with open(self.TestFile('bss_data'), 'rb') as fd: TestFunctional._MakeInputFile('-boot', fd.read()) data = self._DoReadFile('097_elf_strip.dts')
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 9/21 tools: dtoc: Open all binary files in binary mode
In reply to Simon GlassFix an open() statement to use binary mode, so that it works as expected with Python 3.
---
tools/dtoc/fdt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/dtoc/fdt.py +1 -1
@@ -469,7 +469,7 @@ class Fdt: if self._fname: self._fname = fdt_util.EnsureCompiled(self._fname) - with open(self._fname) as fd: + with open(self._fname, 'rb') as fd: self._fdt_obj = libfdt.Fdt(fd.read()) @staticmethod
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 10/21 patman: Provide a way to get program output in binary mode
In reply to Simon GlassAt present cros_subprocess and the tools library use a string to obtain stdout from a program. This works fine on Python 2. With Python 3 we end up with unicode errors in some cases. Fix this by providing a binary mode, which returns the data as bytes() instead of a string.
---
tools/patman/cros_subprocess.py | 15 +++++++++------ tools/patman/tools.py | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-)
tools/patman/cros_subprocess.py +9 -6
@@ -54,7 +54,7 @@ class Popen(subprocess.Popen): """ def __init__(self, args, stdin=None, stdout=PIPE_PTY, stderr=PIPE_PTY, - shell=False, cwd=None, env=None, **kwargs): + shell=False, cwd=None, env=None, binary=False, **kwargs): """Cut-down constructor Args:
@@ -72,6 +72,7 @@ class Popen(subprocess.Popen): """ stdout_pty = None stderr_pty = None + self.binary = binary if stdout == PIPE_PTY: stdout_pty = pty.openpty()
@@ -107,15 +108,17 @@ class Popen(subprocess.Popen): data: Data to convert, or None for '' Returns: - Converted data to a unicode string + Converted data, normally as a unicode string, but this uses bytes + in binary mode """ if data is None: return '' else: - try: - data = data.decode('utf-8') - except UnicodeDecodeError: - data = data.decode('utf-8', 'ignore') + if not self.binary: + try: + data = data.decode('utf-8') + except UnicodeDecodeError: + data = data.decode('utf-8', 'ignore') return data def CommunicateFilter(self, output):
tools/patman/tools.py +2 -2
@@ -167,9 +167,9 @@ def PathHasFile(fname): return True return False -def Run(name, *args): +def Run(name, *args, **kwargs): try: - return command.Run(name, *args, cwd=outdir, capture=True) + return command.Run(name, *args, cwd=outdir, capture=True, **kwargs) except: if not PathHasFile(name): msg = "Plesae install tool '%s'" % name
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 11/21 binman: Use binary mode when compressing data
In reply to Simon GlassThe lz4 utility inserts binary data in its output which cannot always be converted to unicode (nor should it be). Fix this by using the new binary mode for program output.
---
tools/binman/etype/blob.py | 2 +- tools/binman/ftest.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
tools/binman/etype/blob.py +1 -1
@@ -60,7 +60,7 @@ class Entry_blob(Entry): except AttributeError: data = lz4.compress(data) ''' - data = tools.Run('lz4', '-c', self._pathname) + data = tools.Run('lz4', '-c', self._pathname, binary=True) self.SetContents(data) return True
tools/binman/ftest.py +1 -1
@@ -1557,7 +1557,7 @@ class TestFunctional(unittest.TestCase): out = os.path.join(self._indir, 'lz4.tmp') with open(out, 'wb') as fd: fd.write(data) - return tools.Run('lz4', '-dc', out) + return tools.Run('lz4', '-dc', out, binary=True) ''' try: orig = lz4.frame.decompress(data)
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 12/21 binman: Drop an unused input file
In reply to Simon GlassDrop this line which is not used or needed.
---
tools/binman/ftest.py | 1 - 1 file changed, 1 deletion(-)
tools/binman/ftest.py +0 -1
@@ -291,7 +291,6 @@ class TestFunctional(unittest.TestCase): # Use the compiled test file as the u-boot-dtb input if use_real_dtb: dtb_data = self._SetupDtb(fname) - infile = os.path.join(self._indir, 'u-boot.dtb') # For testing purposes, make a copy of the DT for SPL and TPL. Add # a node indicating which it is, so aid verification.
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 13/21 binman: Handle repeated bytes for Python 3
In reply to Simon GlassThe method of multiplying a character by a number works well for creating a repeated string in Python 2. But in Python 3 we need to use bytes() instead, to avoid unicode problems, since 'bytes' is no-longer just an alias of 'str'.
Create a function to handle this detail and call it from the relevant places in binman.
---
tools/binman/bsection.py | 2 +- tools/binman/elf_test.py | 2 +- tools/binman/etype/fill.py | 4 +-- tools/binman/etype/u_boot_spl_bss_pad.py | 2 +- tools/binman/ftest.py | 46 +++++++++++++----------- tools/patman/tools.py | 19 ++++++++++ 6 files changed, 50 insertions(+), 25 deletions(-)
tools/binman/bsection.py +1 -1
@@ -332,7 +332,7 @@ class Section(object): def GetData(self): """Get the contents of the section""" - section_data = chr(self._pad_byte) * self._size + section_data = tools.GetBytes(self._pad_byte, self._size) for entry in self._entries.values(): data = entry.GetData()
tools/binman/elf_test.py +1 -1
@@ -122,7 +122,7 @@ class TestElf(unittest.TestCase): section = FakeSection(sym_value=None) elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms') syms = elf.LookupAndWriteSymbols(elf_fname, entry, section) - self.assertEqual(chr(255) * 16 + 'a' * 4, entry.data) + self.assertEqual(tools.GetBytes(255, 16) + 'a' * 4, entry.data) def testDebug(self): """Check that enabling debug in the elf module produced debug output"""
tools/binman/etype/fill.py +2 -2
@@ -5,7 +5,7 @@ from entry import Entry import fdt_util - +import tools class Entry_fill(Entry): """An entry which is filled to a particular byte value
@@ -28,5 +28,5 @@ class Entry_fill(Entry): self.fill_value = fdt_util.GetByte(self._node, 'fill-byte', 0) def ObtainContents(self): - self.SetContents(chr(self.fill_value) * self.size) + self.SetContents(tools.GetBytes(self.fill_value, self.size)) return True
tools/binman/etype/u_boot_spl_bss_pad.py +1 -1
@@ -38,5 +38,5 @@ class Entry_u_boot_spl_bss_pad(Entry_blob): bss_size = elf.GetSymbolAddress(fname, '__bss_size') if not bss_size: self.Raise('Expected __bss_size symbol in spl/u-boot-spl') - self.SetContents(chr(0) * bss_size) + self.SetContents(tools.GetBytes(0, bss_size)) return True
tools/binman/ftest.py +26 -20
118 lines not on this page · raw message
tools/patman/tools.py +19 -0
@@ -7,6 +7,7 @@ import command import glob import os import shutil +import sys import tempfile import tout
@@ -239,3 +240,21 @@ def WriteFile(fname, data): #(fname, len(data), len(data))) with open(Filename(fname), 'wb') as fd: fd.write(data) + +def GetBytes(byte, size): + """Get a string of bytes of a given size + + This handles the unfortunate different between Python 2 and Python 2. + + Args: + byte: Numeric byte value to use + size: Size of bytes/string to return + + Returns: + A bytes type with 'byte' repeated 'size' times + """ + if sys.version_info[0] >= 3: + data = bytes([byte]) * size + else: + data = chr(byte) * size + return data
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 14/21 Add a simple script to remove boards
In reply to Simon GlassThis script attempts to create a git commit which removes a single board. It is quite fallible and everything it does needs checking. But it can help speed up the process.
---
tools/rmboard.py | 150 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100755 tools/rmboard.py
tools/rmboard.py new file +150 -0
@@ -0,0 +1,150 @@ +#! /usr/bin/python +# SPDX-License-Identifier: GPL-2.0+ +# Copyright 2019 Google LLC +# + +""" +Script to remove boards + +Usage: + rmboard.py <board_name>... + +A single commit is created for each board removed. + +Some boards may depend on files provided by another and this will cause +problems, generally the removal of files which should not be removed. + +This script works by: + - Looking through the MAINTAINERS files which mention a board to find out + what files the board uses + - Looking through the Kconfig files which mention a board to find one that + needs to have material removed + +Search for ## to update the commit message manually. +""" + +from __future__ import print_function + +import glob +import os +import re +import sys + +# Bring in the patman libraries +our_path = os.path.dirname(os.path.realpath(__file__)) +sys.path.append(os.path.join(our_path, '../tools/patman')) + +import command + +def rm_kconfig_include(path): + """Remove a path from Kconfig files + + This function finds the given path in a 'source' statement in a Kconfig + file and removes that line from the file. This is needed because the path + is going to be removed, so any reference to it will cause a problem with + Kconfig parsing. + + The changes are made locally and then added to the git staging area. + + Args: + path: Path to search for and remove + """ + cmd = ['git', 'grep', path] + stdout = command.RunPipe([cmd], capture=True, raise_on_error=False).stdout + if not stdout: + return + fname = stdout.split(':')[0] + + print("Fixing up '%s' to remove reference to '%s'" % (fname, path)) + cmd = ['sed', '-i', '\|%s|d' % path, fname] + stdout = command.RunPipe([cmd], capture=True).stdout + + cmd = ['git', 'add', fname] + stdout = command.RunPipe([cmd], capture=True).stdout + +def rm_board(board): + """Create a commit which removes a single board + + This looks up the MAINTAINERS file to file files that need to be removed, + then removes pieces from the Kconfig files that mention the board. + + + Args: + board: Board name to remove + """ + + # Find all MAINTAINERS and Kconfig files which mention the board + cmd = ['git', 'grep', '-l', board] + stdout = command.RunPipe([cmd], capture=True).stdout + maintain = [] + kconfig = [] + for line in stdout.splitlines(): + line = line.strip() + if 'MAINTAINERS' in line: + if line not in maintain: + maintain.append(line) + elif 'Kconfig' in line: + kconfig.append(line) + paths = [] + cc = [] + + # Look through the MAINTAINERS file to find things to remove + for fname in maintain: + with open(fname) as fd: + for line in fd: + line = line.strip() + fields = re.split('[ \t]', line, 1) + if len(fields) == 2: + if fields[0] == 'M:': + cc.append(fields[1]) + elif fields[0] == 'F:': + paths.append(fields[1].strip()) + + # Expannd any wildcards in the MAINTAINRERS file + real = [] + for path in paths: + if path[-1] == '/': + path = path[:-1] + if '*' in path: + globbed = glob.glob(path) + print("Expanded '%s' to '%s'" % (path, globbed)) + real += globbed + else: + real.append(path) + + # Search for Kconfig files in the resulting list. Remove any 'source' lines + # which reference Kconfig files we want to remove + for path in real: + cmd = ['find', path] + stdout = (command.RunPipe([cmd], capture=True, raise_on_error=False). + stdout) + for fname in stdout.splitlines(): + if fname.endswith('Kconfig'): + rm_kconfig_include(fname) + + # Remove unwanted files + cmd = ['git', 'rm', '-r'] + real + stdout = command.RunPipe([cmd], capture=True).stdout + + ## Change the messages as needed + msg = '''arm: Remove %s board + +This board has not been converted to CONFIG_DM_MMC by the deadline. +Remove it. + +''' % board + for name in cc: + msg += 'Patch-cc: %s\n' % name + + # Create the commit + cmd = ['git', 'commit', '-s', '-m', msg] + stdout = command.RunPipe([cmd], capture=True).stdout + + # Check if the board is mentioned anywhere else. The user will need to deal + # with this + cmd = ['git', 'grep', '-il', board] + print(command.RunPipe([cmd], capture=True, raise_on_error=False).stdout) + print(' '.join(cmd)) + +for board in sys.argv[1:]: + rm_board(board)
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 14/21 Add a simple script to remove boards
Quoted 7 lines · Simon Glass
This script attempts to create a git commit which removes a single board. It is quite fallible and everything it does needs checking. But it can help speed up the process.
Signed-off-by Simon Glass <[email protected]>---
Did you mean to include this file in the series. It seems unrelated to the other patman/binman changes.
Quoted 54 lines
tools/rmboard.py | 150 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100755 tools/rmboard.pytools/rmboard.py new file +43
-0@@ -0,0 +1,150 @@ +#! /usr/bin/python +# SPDX-License-Identifier: GPL-2.0+ +# Copyright 2019 Google LLC +# + +""" +Script to remove boards + +Usage: + rmboard.py <board_name>... + +A single commit is created for each board removed. + +Some boards may depend on files provided by another and this will cause +problems, generally the removal of files which should not be removed. + +This script works by: + - Looking through the MAINTAINERS files which mention a board to find out + what files the board uses + - Looking through the Kconfig files which mention a board to find one that + needs to have material removed + +Search for ## to update the commit message manually. +""" + +from __future__ import print_function + +import glob +import os +import re +import sys + +# Bring in the patman libraries +our_path = os.path.dirname(os.path.realpath(__file__)) +sys.path.append(os.path.join(our_path, '../tools/patman')) + +import command + +def rm_kconfig_include(path): + """Remove a path from Kconfig files + + This function finds the given path in a 'source' statement in a Kconfig + file and removes that line from the file. This is needed because the path
tools/rmboard.py line 53
Quoted 10 lines
tools/rmboard.py new file +10
-0+ is going to be removed, so any reference to it will cause a problem with + Kconfig parsing. + + The changes are made locally and then added to the git staging area. + + Args: + path: Path to search for and remove + """ + cmd = ['git', 'grep', path] + stdout = command.RunPipe([cmd], capture=True, raise_on_error=False).stdout
Is there any specific advantage of using patman/command.py instead of python's subprocess module directly?
Quoted 40 lines
tools/rmboard.py new file +40
-0+ if not stdout: + return + fname = stdout.split(':')[0] + + print("Fixing up '%s' to remove reference to '%s'" % (fname, path)) + cmd = ['sed', '-i', '\|%s|d' % path, fname] + stdout = command.RunPipe([cmd], capture=True).stdout + + cmd = ['git', 'add', fname] + stdout = command.RunPipe([cmd], capture=True).stdout + +def rm_board(board): + """Create a commit which removes a single board + + This looks up the MAINTAINERS file to file files that need to be removed, + then removes pieces from the Kconfig files that mention the board. + + + Args: + board: Board name to remove + """ + + # Find all MAINTAINERS and Kconfig files which mention the board + cmd = ['git', 'grep', '-l', board] + stdout = command.RunPipe([cmd], capture=True).stdout + maintain = [] + kconfig = [] + for line in stdout.splitlines(): + line = line.strip() + if 'MAINTAINERS' in line: + if line not in maintain: + maintain.append(line) + elif 'Kconfig' in line: + kconfig.append(line) + paths = [] + cc = [] + + # Look through the MAINTAINERS file to find things to remove + for fname in maintain: + with open(fname) as fd:
tools/rmboard.py line 103
Quoted 10 lines
tools/rmboard.py new file +10
-0+ for line in fd: + line = line.strip() + fields = re.split('[ \t]', line, 1) + if len(fields) == 2: + if fields[0] == 'M:': + cc.append(fields[1]) + elif fields[0] == 'F:': + paths.append(fields[1].strip()) + + # Expannd any wildcards in the MAINTAINRERS file
Couple of typos s/Expannd/Expand/ and s/MAINTAINRERS/MAINTAINERS/
Quoted 50 lines
tools/rmboard.py new file +47
-1+ real = [] + for path in paths: + if path[-1] == '/': + path = path[:-1] + if '*' in path: + globbed = glob.glob(path) + print("Expanded '%s' to '%s'" % (path, globbed)) + real += globbed + else: + real.append(path) + + # Search for Kconfig files in the resulting list. Remove any 'source' lines + # which reference Kconfig files we want to remove + for path in real: + cmd = ['find', path] + stdout = (command.RunPipe([cmd], capture=True, raise_on_error=False). + stdout) + for fname in stdout.splitlines(): + if fname.endswith('Kconfig'): + rm_kconfig_include(fname) + + # Remove unwanted files + cmd = ['git', 'rm', '-r'] + real + stdout = command.RunPipe([cmd], capture=True).stdout + + ## Change the messages as needed + msg = '''arm: Remove %s board + +This board has not been converted to CONFIG_DM_MMC by the deadline. +Remove it. + +''' % board + for name in cc: + msg += 'Patch-cc: %s\n' % name + + # Create the commit + cmd = ['git', 'commit', '-s', '-m', msg] + stdout = command.RunPipe([cmd], capture=True).stdout + + # Check if the board is mentioned anywhere else. The user will need to deal + # with this + cmd = ['git', 'grep', '-il', board] + print(command.RunPipe([cmd], capture=True, raise_on_error=False).stdout) + print(' '.join(cmd)) + +for board in sys.argv[1:]: + rm_board(board) --2.21.0.1020.gf2820cf01a-goog
PATCH 14/21 Add a simple script to remove boards
Hi Chris,
Quoted 7 lines · Chris Packham
Quoted 5 lines · Simon Glass
This script attempts to create a git commit which removes a single board. It is quite fallible and everything it does needs checking. But it can help speed up the process.
Quoted 5 lines
Quoted 2 lines
Signed-off-by Simon Glass <[email protected]>---
Did you mean to include this file in the series. It seems unrelated to the other patman/binman changes.
Well I originally had it as part of the series to drop/mangle boards that have not been migrated. But I decided it might fit better here since it was Python.
I'll send it again as a separate tool.
Quoted 60 lines
Quoted 60 lines
tools/rmboard.py | 150 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100755 tools/rmboard.pytools/rmboard.py new file +48
-0@@ -0,0 +1,150 @@ +#! /usr/bin/python +# SPDX-License-Identifier: GPL-2.0+ +# Copyright 2019 Google LLC +# + +""" +Script to remove boards + +Usage: + rmboard.py <board_name>... + +A single commit is created for each board removed. + +Some boards may depend on files provided by another and this will cause +problems, generally the removal of files which should not be removed. + +This script works by: + - Looking through the MAINTAINERS files which mention a board to find out + what files the board uses + - Looking through the Kconfig files which mention a board to find one that + needs to have material removed + +Search for ## to update the commit message manually. +""" + +from __future__ import print_function + +import glob +import os +import re +import sys + +# Bring in the patman libraries +our_path = os.path.dirname(os.path.realpath(__file__)) +sys.path.append(os.path.join(our_path, '../tools/patman')) + +import command + +def rm_kconfig_include(path): + """Remove a path from Kconfig files + + This function finds the given path in a 'source' statement in a Kconfig + file and removes that line from the file. This is needed because the path + is going to be removed, so any reference to it will cause a problem with + Kconfig parsing. + + The changes are made locally and then added to the git staging area. +
Quoted 8 lines
tools/rmboard.py line 53
Quoted 5 lines
tools/rmboard.py new file +5
-0+ Args: + path: Path to search for and remove + """ + cmd = ['git', 'grep', path] + stdout = command.RunPipe([cmd], capture=True, raise_on_error=False).stdoutIs there any specific advantage of using
patman/command.pyinstead of python's subprocess module directly?
Just for consistency with the rest of the code.
The only difference really is that 'command' allows output to be collected while the command is running rather than hanging the process until the command exits. In this case the difference doesn't matter though. since the command is pretty quick and we just wait for all the output to come.
So, mostly just for consistency.
Quoted 45 lines
Quoted 45 lines
tools/rmboard.py new file +44
-0+ if not stdout: + return + fname = stdout.split(':')[0] + + print("Fixing up '%s' to remove reference to '%s'" % (fname, path)) + cmd = ['sed', '-i', '\|%s|d' % path, fname] + stdout = command.RunPipe([cmd], capture=True).stdout + + cmd = ['git', 'add', fname] + stdout = command.RunPipe([cmd], capture=True).stdout + +def rm_board(board): + """Create a commit which removes a single board + + This looks up the MAINTAINERS file to file files that need to be removed, + then removes pieces from the Kconfig files that mention the board. + + + Args: + board: Board name to remove + """ + + # Find all MAINTAINERS and Kconfig files which mention the board + cmd = ['git', 'grep', '-l', board] + stdout = command.RunPipe([cmd], capture=True).stdout + maintain = [] + kconfig = [] + for line in stdout.splitlines(): + line = line.strip() + if 'MAINTAINERS' in line: + if line not in maintain: + maintain.append(line) + elif 'Kconfig' in line: + kconfig.append(line) + paths = [] + cc = [] + + # Look through the MAINTAINERS file to find things to remove + for fname in maintain: + with open(fname) as fd: + for line in fd: + line = line.strip() + fields = re.split('[ \t]', line, 1) + if len(fields) == 2:
Quoted 9 lines
tools/rmboard.py line 103
Quoted 6 lines
tools/rmboard.py new file +6
-0+ if fields[0] == 'M:': + cc.append(fields[1]) + elif fields[0] == 'F:': + paths.append(fields[1].strip()) + + # Expannd any wildcards in the MAINTAINRERS fileCouple of typos s/Expannd/Expand/ and s/MAINTAINRERS/MAINTAINERS/
OK will fix, thanks.
Regards,
Simon
PATCH 15/21 patman: Support use of stringIO in Python 3
In reply to Simon GlassWith Python 3 this class has moved. Update the code to handle both cases.
---
tools/patman/func_test.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
tools/patman/func_test.py +6 -1
@@ -12,15 +12,20 @@ import sys import tempfile import unittest +try: + from StringIO import StringIO +except ImportError: + from io import StringIO + import gitutil import patchstream import settings +import tools @contextlib.contextmanager def capture(): import sys - from cStringIO import StringIO oldout,olderr = sys.stdout, sys.stderr try: out=[StringIO(), StringIO()]
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 16/21 patman: Move unicode helpers to tools
In reply to Simon GlassCreate helper functions in the tools module to deal with the differences between unicode in Python 2 (where we use the 'unicode' type) and Python 3 (where we use the 'str' type).
---
tools/patman/gitutil.py | 4 ++-- tools/patman/series.py | 3 ++- tools/patman/settings.py | 16 +++------------- tools/patman/tools.py | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 16 deletions(-)
tools/patman/gitutil.py +2 -2
@@ -12,6 +12,7 @@ import terminal import checkpatch import settings +import tools # True to use --no-decorate - we check this in Setup() use_no_decorate = True
@@ -325,9 +326,8 @@ def BuildEmailList(in_list, tag=None, alias=None, raise_on_error=True): raw += LookupEmail(item, alias, raise_on_error=raise_on_error) result = [] for item in raw: + item = tools.FromUnicode(item) if not item in result: - if type(item) == unicode: - item = item.encode('utf-8') result.append(item) if tag: return ['%s %s%s%s' % (tag, quote, email, quote) for email in result]
tools/patman/series.py +2 -1
@@ -11,6 +11,7 @@ import get_maintainer import gitutil import settings import terminal +import tools # Series-xxx tags that we understand valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name',
@@ -249,7 +250,7 @@ class Series(dict): cover_cc = gitutil.BuildEmailList(self.get('cover_cc', '')) cover_cc = [m.encode('utf-8') if type(m) != str else m for m in cover_cc] - cc_list = ', '.join([x.decode('utf-8') + cc_list = ', '.join([tools.ToUnicode(x) for x in set(cover_cc + all_ccs)]) print(cover_fname, cc_list.encode('utf-8'), file=fd)
tools/patman/settings.py +3 -13
@@ -14,6 +14,7 @@ import re import command import gitutil +import tools """Default settings per-project.
@@ -99,17 +100,6 @@ class _ProjectConfigParser(ConfigParser.SafeConfigParser): for setting_name, setting_value in project_defaults.items(): self.set(project_settings, setting_name, setting_value) - def _to_unicode(self, val): - """Make sure a value is of type 'unicode' - - Args: - val: string or unicode object - - Returns: - unicode version of val - """ - return val if isinstance(val, unicode) else val.decode('utf-8') - def get(self, section, option, *args, **kwargs): """Extend SafeConfigParser to try project_section before section.
@@ -127,7 +117,7 @@ class _ProjectConfigParser(ConfigParser.SafeConfigParser): val = ConfigParser.SafeConfigParser.get( self, section, option, *args, **kwargs ) - return self._to_unicode(val) + return tools.ToUnicode(val) def items(self, section, *args, **kwargs): """Extend SafeConfigParser to add project_section to section.
@@ -162,7 +152,7 @@ class _ProjectConfigParser(ConfigParser.SafeConfigParser): item_dict = dict(top_items) item_dict.update(project_items) - return {(self._to_unicode(item), self._to_unicode(val)) + return {(tools.ToUnicode(item), tools.ToUnicode(val)) for item, val in item_dict.items()} def ReadGitAliases(fname):
tools/patman/tools.py +32 -0
@@ -258,3 +258,35 @@ def GetBytes(byte, size): else: data = chr(byte) * size return data + +def ToUnicode(val): + """Make sure a value is a unicode string + + This allows some amount of compatibility between Python 2 and Python3. For + the former, it returns a unicode object. + + Args: + val: string or unicode object + + Returns: + unicode version of val + """ + if sys.version_info[0] >= 3: + return val + return val if isinstance(val, unicode) else val.decode('utf-8') + +def FromUnicode(val): + """Make sure a value is a non-unicode string + + This allows some amount of compatibility between Python 2 and Python3. For + the former, it converts a unicode object to a string. + + Args: + val: string or unicode object + + Returns: + non-unicode version of val + """ + if sys.version_info[0] >= 3: + return val + return val if isinstance(val, str) else val.encode('utf-8')
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 17/21 patman: Sort series output for repeatabily
In reply to Simon GlassWe use sets to produce the list of To and Cc lines for a series. This does not result in stable ordering of the recipients. Sort each list to ensure that the output is repeatable. This is necessary for tests.
---
tools/patman/func_test.py | 12 ++++++------ tools/patman/series.py | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-)
tools/patman/func_test.py +6 -6
@@ -183,10 +183,10 @@ class TestFunctional(unittest.TestCase): self.assertEqual('Prefix:\t RFC', lines[line + 3]) self.assertEqual('Cover: 4 lines', lines[line + 4]) line += 5 - self.assertEqual(' Cc: %s' % mel.encode('utf-8'), lines[line + 0]) - self.assertEqual(' Cc: %s' % rick, lines[line + 1]) - self.assertEqual(' Cc: %s' % fred, lines[line + 2]) - self.assertEqual(' Cc: %s' % ed.encode('utf-8'), lines[line + 3]) + self.assertEqual(' Cc: %s' % fred, lines[line + 0]) + self.assertEqual(' Cc: %s' % ed.encode('utf-8'), lines[line + 1]) + self.assertEqual(' Cc: %s' % mel.encode('utf-8'), lines[line + 2]) + self.assertEqual(' Cc: %s' % rick, lines[line + 3]) expected = ('Git command: git send-email --annotate ' '--in-reply-to="%s" --to "u-boot at lists.denx.de" ' '--cc "%s" --cc-cmd "%s --cc-cmd %s" %s %s'
@@ -197,8 +197,8 @@ class TestFunctional(unittest.TestCase): self.assertEqual(('%s %s, %s' % (args[0], rick, stefan)) .encode('utf-8'), cc_lines[0]) - self.assertEqual(('%s %s, %s, %s, %s' % (args[1], fred, rick, stefan, - ed)).encode('utf-8'), cc_lines[1]) + self.assertEqual(('%s %s, %s, %s, %s' % (args[1], fred, ed, rick, + stefan)).encode('utf-8'), cc_lines[1]) expected = ''' This is a test of how the cover
tools/patman/series.py +6 -6
@@ -115,16 +115,16 @@ class Series(dict): commit = self.commits[upto] print(col.Color(col.GREEN, ' %s' % args[upto])) cc_list = list(self._generated_cc[commit.patch]) - for email in set(cc_list) - to_set - cc_set: + for email in sorted(set(cc_list) - to_set - cc_set): if email == None: email = col.Color(col.YELLOW, "<alias '%s' not found>" % tag) if email: print(' Cc: ', email) print - for item in to_set: + for item in sorted(to_set): print('To:\t ', item) - for item in cc_set - to_set: + for item in sorted(cc_set - to_set): print('Cc:\t ', item) print('Version: ', self.get('version')) print('Prefix:\t ', self.get('prefix'))
@@ -132,7 +132,7 @@ class Series(dict): print('Cover: %d lines' % len(self.cover)) cover_cc = gitutil.BuildEmailList(self.get('cover_cc', '')) all_ccs = itertools.chain(cover_cc, *self._generated_cc.values()) - for email in set(all_ccs) - to_set - cc_set: + for email in sorted(set(all_ccs) - to_set - cc_set): print(' Cc: ', email) if cmd: print('Git command: %s' % cmd)
@@ -243,7 +243,7 @@ class Series(dict): if limit is not None: cc = cc[:limit] all_ccs += cc - print(commit.patch, ', '.join(set(cc)), file=fd) + print(commit.patch, ', '.join(sorted(set(cc))), file=fd) self._generated_cc[commit.patch] = cc if cover_fname:
@@ -251,7 +251,7 @@ class Series(dict): cover_cc = [m.encode('utf-8') if type(m) != str else m for m in cover_cc] cc_list = ', '.join([tools.ToUnicode(x) - for x in set(cover_cc + all_ccs)]) + for x in sorted(set(cover_cc + all_ccs))]) print(cover_fname, cc_list.encode('utf-8'), file=fd) fd.close()
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 18/21 patman: Avoid unicode type in settings unit tests
In reply to Simon GlassThe unicode type does not exist in Python 3 and when displaying strings they do not have the 'u' prefix. Adjusts the settings unit tests to deal with this difference, by converting the comparison value to a string, thus dropping the 'u'.
---
tools/patman/settings.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
tools/patman/settings.py +8 -8
@@ -58,26 +58,26 @@ class _ProjectConfigParser(ConfigParser.SafeConfigParser): # Check to make sure that bogus project gets general alias. >>> config = _ProjectConfigParser("zzz") >>> config.readfp(StringIO(sample_config)) - >>> config.get("alias", "enemies") - u'Evil <[email protected]>' + >>> str(config.get("alias", "enemies")) + 'Evil <[email protected]>' # Check to make sure that alias gets overridden by project. >>> config = _ProjectConfigParser("sm") >>> config.readfp(StringIO(sample_config)) - >>> config.get("alias", "enemies") - u'Green G. <[email protected]>' + >>> str(config.get("alias", "enemies")) + 'Green G. <[email protected]>' # Check to make sure that settings get merged with project. >>> config = _ProjectConfigParser("linux") >>> config.readfp(StringIO(sample_config)) - >>> sorted(config.items("settings")) - [(u'am_hero', u'True'), (u'process_tags', u'False')] + >>> sorted((str(a), str(b)) for (a, b) in config.items("settings")) + [('am_hero', 'True'), ('process_tags', 'False')] # Check to make sure that settings works with unknown project. >>> config = _ProjectConfigParser("unknown") >>> config.readfp(StringIO(sample_config)) - >>> sorted(config.items("settings")) - [(u'am_hero', u'True')] + >>> sorted((str(a), str(b)) for (a, b) in config.items("settings")) + [('am_hero', 'True')] """ def __init__(self, project_name): """Construct _ProjectConfigParser.
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 19/21 patman: Adjust functional tests for Python 3
In reply to Simon GlassChange the code so that it works on both Python 2 and Python 3. This works by using unicode instead of latin1 for the test input, and ensuring that the output is converted to a string rather than a unicode object on Python 2.
---
tools/patman/func_test.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-)
tools/patman/func_test.py +14 -11
@@ -129,10 +129,10 @@ class TestFunctional(unittest.TestCase): """ process_tags = True ignore_bad_tags = True - stefan = u'Stefan Brüns <[email protected]>' + stefan = b'Stefan Br\xc3\xbcns <[email protected]>'.decode('utf-8') rick = 'Richard III <[email protected]>' - mel = u'Lord Mëlchett <[email protected]>' - ed = u'Lond Edmund Blackaddër <weasel at blackadder.org' + mel = b'Lord M\xc3\xablchett <[email protected]>'.decode('utf-8') + ed = b'Lond Edmund Blackadd\xc3\xabr <weasel at blackadder.org'.decode('utf-8') fred = 'Fred Bloggs <[email protected]>' add_maintainers = [stefan, rick] dry_run = True
@@ -178,27 +178,30 @@ class TestFunctional(unittest.TestCase): while 'Cc:' in lines[line]: line += 1 self.assertEqual('To: u-boot at lists.denx.de', lines[line]) - self.assertEqual('Cc: %s' % stefan.encode('utf-8'), lines[line + 1]) + self.assertEqual('Cc: %s' % tools.FromUnicode(stefan), + lines[line + 1]) self.assertEqual('Version: 3', lines[line + 2]) self.assertEqual('Prefix:\t RFC', lines[line + 3]) self.assertEqual('Cover: 4 lines', lines[line + 4]) line += 5 self.assertEqual(' Cc: %s' % fred, lines[line + 0]) - self.assertEqual(' Cc: %s' % ed.encode('utf-8'), lines[line + 1]) - self.assertEqual(' Cc: %s' % mel.encode('utf-8'), lines[line + 2]) + self.assertEqual(' Cc: %s' % tools.FromUnicode(ed), + lines[line + 1]) + self.assertEqual(' Cc: %s' % tools.FromUnicode(mel), + lines[line + 2]) self.assertEqual(' Cc: %s' % rick, lines[line + 3]) expected = ('Git command: git send-email --annotate ' '--in-reply-to="%s" --to "u-boot at lists.denx.de" ' '--cc "%s" --cc-cmd "%s --cc-cmd %s" %s %s' % (in_reply_to, stefan, sys.argv[0], cc_file, cover_fname, - ' '.join(args))).encode('utf-8') + ' '.join(args))) line += 4 - self.assertEqual(expected, lines[line]) + self.assertEqual(expected, tools.ToUnicode(lines[line])) - self.assertEqual(('%s %s, %s' % (args[0], rick, stefan)) - .encode('utf-8'), cc_lines[0]) + self.assertEqual(('%s %s, %s' % (args[0], rick, stefan)), + tools.ToUnicode(cc_lines[0])) self.assertEqual(('%s %s, %s, %s, %s' % (args[1], fred, ed, rick, - stefan)).encode('utf-8'), cc_lines[1]) + stefan)), tools.ToUnicode(cc_lines[1])) expected = ''' This is a test of how the cover
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 20/21 patman: Tidy up a few more unicode conversions
In reply to Simon GlassUse the new functions in the tools module to handle conversion.
---
tools/patman/gitutil.py | 4 +--- tools/patman/series.py | 5 ++--- 2 files changed, 3 insertions(+), 6 deletions(-)
tools/patman/gitutil.py +1 -3
@@ -412,9 +412,7 @@ def EmailPatches(series, cover_fname, args, dry_run, raise_on_error, cc_fname, if smtp_server: cmd.append('--smtp-server=%s' % smtp_server) if in_reply_to: - if type(in_reply_to) != str: - in_reply_to = in_reply_to.encode('utf-8') - cmd.append('--in-reply-to="%s"' % in_reply_to) + cmd.append('--in-reply-to="%s"' % tools.FromUnicode(in_reply_to)) if thread: cmd.append('--thread')
tools/patman/series.py +2 -3
@@ -239,7 +239,7 @@ class Series(dict): for x in set(cc) & set(settings.bounces): print(col.Color(col.YELLOW, 'Skipping "%s"' % x)) cc = set(cc) - set(settings.bounces) - cc = [m.encode('utf-8') if type(m) != str else m for m in cc] + cc = [tools.FromUnicode(m) for m in cc] if limit is not None: cc = cc[:limit] all_ccs += cc
@@ -248,8 +248,7 @@ class Series(dict): if cover_fname: cover_cc = gitutil.BuildEmailList(self.get('cover_cc', '')) - cover_cc = [m.encode('utf-8') if type(m) != str else m - for m in cover_cc] + cover_cc = [tools.FromUnicode(m) for m in cover_cc] cc_list = ', '.join([tools.ToUnicode(x) for x in sorted(set(cover_cc + all_ccs))]) print(cover_fname, cc_list.encode('utf-8'), file=fd)
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
PATCH 21/21 patman: Don't require Python 2
In reply to Simon GlassUpdate the shebang to allow either Python 2 or Python 3.
---
tools/patman/patman.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/patman/patman.py +1 -1
@@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python # SPDX-License-Identifier: GPL-2.0+ # # Copyright (c) 2011 The Chromium OS Authors.
Signature · 1 line
2.21.0.1020.gf2820cf01a-goog
cover patman: Update to support Python 3
In reply to Simon GlassQuoted 12 lines · Simon Glass
This series updates patman to support Python 3:
- Avoid using 'unicode' type directly
- Useitems()instead ofiteritems()
- Make sure file I/O uses binary mode where necessary
- Change print statements to functions
- Use the built-inset()class
- Fix up generation of repeated bytesA few patches for binman are included, but this still requires Python 2.
Couple of comments on 14/21 but the rest of the series
Quoted 52 lines
Simon Glass (21):
patman: Updatecros_subprocessto use bytearray
patman: Convert print statements to Python 3
binman: Convert print statements to Python 3
binman: Don't show errors for failed tests
binman: Remove use ofSet()
patman: Useitems()instead ofiteritems()
binman: Useitems()instead ofiteritems()
tools: binman: Open all binary files in binary mode
tools: dtoc: Open all binary files in binary mode
patman: Provide a way to get program output in binary mode
binman: Use binary mode when compressing data
binman: Drop an unused input file
binman: Handle repeated bytes for Python 3
Add a simple script to remove boards
patman: Support use of stringIO in Python 3
patman: Move unicode helpers to tools
patman: Sort series output for repeatabily
patman: Avoid unicode type in settings unit tests
patman: Adjust functional tests for Python 3
patman: Tidy up a few more unicode conversions
patman: Don't require Python 2tools/binman/binman.py | 26 +++- tools/binman/bsection.py | 7 +- tools/binman/control.py | 8 +- tools/binman/elf.py | 4 +- tools/binman/elf_test.py | 2 +- tools/binman/entry.py | 5 +- tools/binman/etype/blob.py | 2 +- tools/binman/etype/fill.py | 4 +- tools/binman/etype/gbb.py | 2 +- tools/binman/etype/u_boot_spl_bss_pad.py | 2 +- tools/binman/ftest.py | 81 ++++++------ tools/binman/state.py | 7 +- tools/dtoc/fdt.py | 2 +- tools/patman/cros_subprocess.py | 53 +++++--- tools/patman/func_test.py | 41 ++++--- tools/patman/gitutil.py | 16 +-- tools/patman/patman.py | 2 +- tools/patman/series.py | 20 +-- tools/patman/settings.py | 34 ++--- tools/patman/test_util.py | 16 +-- tools/patman/tools.py | 55 ++++++++- tools/rmboard.py | 150 +++++++++++++++++++++++ 22 files changed, 385 insertions(+), 154 deletions(-) create mode 100755 tools/rmboard.py--
2.21.0.1020.gf2820cf01a-goog
cover patman: Update to support Python 3
Quoted 12 lines · Chris Packham
Quoted 10 lines · Simon Glass
This series updates patman to support Python 3:
- Avoid using 'unicode' type directly
- Useitems()instead ofiteritems()
- Make sure file I/O uses binary mode where necessary
- Change print statements to functions
- Use the built-inset()class
- Fix up generation of repeated bytes
Quoted 59 lines
Quoted 2 lines
A few patches for binman are included, but this still requires Python 2.
Couple of comments on 14/21 but the rest of the series
Reviewed-by Chris Packham <[email protected]>Quoted 52 lines
Simon Glass (21):
patman: Updatecros_subprocessto use bytearray
patman: Convert print statements to Python 3
binman: Convert print statements to Python 3
binman: Don't show errors for failed tests
binman: Remove use ofSet()
patman: Useitems()instead ofiteritems()
binman: Useitems()instead ofiteritems()
tools: binman: Open all binary files in binary mode
tools: dtoc: Open all binary files in binary mode
patman: Provide a way to get program output in binary mode
binman: Use binary mode when compressing data
binman: Drop an unused input file
binman: Handle repeated bytes for Python 3
Add a simple script to remove boards
patman: Support use of stringIO in Python 3
patman: Move unicode helpers to tools
patman: Sort series output for repeatabily
patman: Avoid unicode type in settings unit tests
patman: Adjust functional tests for Python 3
patman: Tidy up a few more unicode conversions
patman: Don't require Python 2tools/binman/binman.py | 26 +++- tools/binman/bsection.py | 7 +- tools/binman/control.py | 8 +- tools/binman/elf.py | 4 +- tools/binman/elf_test.py | 2 +- tools/binman/entry.py | 5 +- tools/binman/etype/blob.py | 2 +- tools/binman/etype/fill.py | 4 +- tools/binman/etype/gbb.py | 2 +- tools/binman/etype/u_boot_spl_bss_pad.py | 2 +- tools/binman/ftest.py | 81 ++++++------ tools/binman/state.py | 7 +- tools/dtoc/fdt.py | 2 +- tools/patman/cros_subprocess.py | 53 +++++--- tools/patman/func_test.py | 41 ++++--- tools/patman/gitutil.py | 16 +-- tools/patman/patman.py | 2 +- tools/patman/series.py | 20 +-- tools/patman/settings.py | 34 ++--- tools/patman/test_util.py | 16 +-- tools/patman/tools.py | 55 ++++++++- tools/rmboard.py | 150 +++++++++++++++++++++++ 22 files changed, 385 insertions(+), 154 deletions(-) create mode 100755 tools/rmboard.py--
2.21.0.1020.gf2820cf01a-goog
Applied series to u-boot-dm/next
Due to a patchwork bug, some patches show up with missing spaces in the UI, e.g. here http://patchwork.ozlabs.org/bundle/sjg/dm/
So my script which emails on each patch doesn't work, sorry.
- Simon