[U-Boot] [RFC PATCH 0/2] Make Python scripts compatible with older versions

It was reported by Miao Yan that the kconfig-related Python scripts are not working on Python 2.4.
Do we have any consensus in terms of Python version requirement?
This series avoids using "with ... as ..." and "except ... as ..." statements. But "with ... as ..." is used everywhere in buildman, which means buildman requires at least Python 2.6.
It is true we hope tools can work on broad range of versions, but it also means we have more restrictions when writing Python scripts.
I have no idea which version we should expect at least. Your comments are welcome.
Masahiro Yamada (2): kconfig: make multiconfig.py compatible with Python 2.4 tools: make genboardscfg.py compatible with Python2.5
scripts/multiconfig.py | 73 +++++++++++++++++++++++--------------------------- tools/genboardscfg.py | 4 +-- 2 files changed, 36 insertions(+), 41 deletions(-)

The statements "with ... as ..." and "exception ... as ..." are available in Python 2.6 or lator. Do not use them. Tested on Python 2.4.6.
Signed-off-by: Masahiro Yamada yamada.m@jp.panasonic.com ---
scripts/multiconfig.py | 73 +++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 39 deletions(-)
diff --git a/scripts/multiconfig.py b/scripts/multiconfig.py index 749abcb..de5ec51 100755 --- a/scripts/multiconfig.py +++ b/scripts/multiconfig.py @@ -165,7 +165,7 @@ def mkdirs(*dirs): for d in dirs: try: os.makedirs(d) - except OSError as exception: + except OSError, exception: # Ignore 'File exists' error if exception.errno != errno.EEXIST: raise @@ -175,7 +175,7 @@ def rmfiles(*files): for f in files: try: os.remove(f) - except OSError as exception: + except OSError, exception: # Ignore 'No such file or directory' error if exception.errno != errno.ENOENT: raise @@ -187,7 +187,7 @@ def rmdirs(*dirs): for d in dirs: try: os.rmdir(d) - except OSError as exception: + except OSError, exception: # Ignore 'No such file or directory' # and 'Directory not empty' error if exception.errno != errno.ENOENT and \ @@ -253,15 +253,14 @@ def get_enabled_subimages(ignore_error=False): for img in SUB_IMAGES ] try: f = open(KCONFIG_CONFIG) - except IOError as exception: + except IOError, exception: if not ignore_error or exception.errno != errno.ENOENT: raise return enabled - with f: - for line in f: - for img, pattern in match_patterns: - if line == pattern: - enabled += (img,) + for line in f: + for img, pattern in match_patterns: + if line == pattern: + enabled += (img,) return enabled
def do_silentoldconfig(cmd): @@ -307,8 +306,7 @@ def do_tmp_defconfig(output_lines, img): TMP_DIRS = ('arch', 'configs') defconfig_path = os.path.join('configs', TMP_DEFCONFIG) mkdirs(*TMP_DIRS) - with open(defconfig_path, 'w') as f: - f.write(''.join(output_lines[img])) + open(defconfig_path, 'w').write(''.join(output_lines[img])) cleanup = lambda: (rmfiles(defconfig_path), rmdirs(*TMP_DIRS)) run_make_config(TMP_DEFCONFIG, img, cleanup) cleanup() @@ -321,15 +319,14 @@ def do_board_defconfig(cmd): """ defconfig_path = os.path.join(srctree, 'configs', cmd) output_lines = dict([ (img, []) for img in IMAGES ]) - with open(defconfig_path) as f: - for line in f: - m = PATTERN_SYMBOL.match(line) - if m: - for idx, img in enumerate(IMAGES): - if m.group(idx + 1): - output_lines[img].append(m.group(4) + '\n') - continue - output_lines[''].append(line) + for line in open(defconfig_path): + m = PATTERN_SYMBOL.match(line) + if m: + for idx, img in enumerate(IMAGES): + if m.group(idx + 1): + output_lines[img].append(m.group(4) + '\n') + continue + output_lines[''].append(line) do_tmp_defconfig(output_lines, '') for img in get_enabled_subimages(): do_tmp_defconfig(output_lines, img) @@ -356,29 +353,27 @@ def do_savedefconfig(cmd): run_make_config(cmd, '') output_lines = [] prefix = {} - with open(DEFCONFIG) as f: - for line in f: - output_lines.append(line) - prefix[line] = '+' + for line in open(DEFCONFIG): + output_lines.append(line) + prefix[line] = '+' for img in subimages: run_make_config(cmd, img) unmatched_lines = [] - with open(DEFCONFIG) as f: - for line in f: - if line in output_lines: - index = output_lines.index(line) - output_lines[index:index] = unmatched_lines - unmatched_lines = [] - prefix[line] += SYMBOL_MAP[img] - else: - ummatched_lines.append(line) - prefix[line] = SYMBOL_MAP[img] - with open(DEFCONFIG, 'w') as f: - for line in output_lines: - if prefix[line] == '+': - f.write(line) + for line in open(DEFCONFIG): + if line in output_lines: + index = output_lines.index(line) + output_lines[index:index] = unmatched_lines + unmatched_lines = [] + prefix[line] += SYMBOL_MAP[img] else: - f.write(prefix[line] + ':' + line) + ummatched_lines.append(line) + prefix[line] = SYMBOL_MAP[img] + f = open(DEFCONFIG, 'w') + for line in output_lines: + if prefix[line] == '+': + f.write(line) + else: + f.write(prefix[line] + ':' + line)
def do_others(cmd): """Run the make command other than 'silentoldconfig', 'defconfig',

The statement "exception ... as ..." can be used in Python 2.6 or lator. Avoid using it. Tested on Python 2.5.6.
Signed-off-by: Masahiro Yamada yamada.m@jp.panasonic.com ---
tools/genboardscfg.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tools/genboardscfg.py b/tools/genboardscfg.py index 734d90b..9e4892f 100755 --- a/tools/genboardscfg.py +++ b/tools/genboardscfg.py @@ -57,7 +57,7 @@ def get_terminal_columns(): arg = struct.pack('hhhh', 0, 0, 0, 0) try: ret = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, arg) - except IOError as exception: + except IOError, exception: if exception.errno != errno.ENOTTY: raise # If 'Inappropriate ioctl for device' error occurs, @@ -473,7 +473,7 @@ def gen_boards_cfg(jobs): # We should remove incomplete boards.cfg try: os.remove(BOARD_FILE) - except OSError as exception: + except OSError, exception: # Ignore 'No such file or directory' error if exception.errno != errno.ENOENT: raise
participants (1)
-
Masahiro Yamada