From 36580e5689b5f2db17eef33ff5845a71458019e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20H=C3=B8jgaard-Hansen?= Date: Thu, 17 Nov 2016 21:00:46 +0100 Subject: [PATCH] python3 preparation These changes have been made using the 2to3 tool, and each command have been briefly tested on the rpi demo manifest. Most are just plain print "statement" -> print() function anyway, so this should be safe enough. Fullblown Python3 support is not going to be easy, at least one big blocker is the Ply usage, but we can start by fixing up all the places where it is easy to make it compatible to both. --- oebakery/cmd/__init__.py | 4 +--- oebakery/cmd/clone.py | 4 ++-- oebakery/cmd/cmds.py | 14 +++++++------- oebakery/cmd/pull.py | 2 +- oebakery/cmd/update.py | 8 ++++---- oebakery/oe.py | 15 +++++++++------ oebakery/parse.py | 26 +++++++++++++------------- oebakery/path.py | 8 ++++---- oebakery/shell.py | 4 ++-- 9 files changed, 43 insertions(+), 42 deletions(-) diff --git a/oebakery/cmd/__init__.py b/oebakery/cmd/__init__.py index 542c85d..e5521a4 100644 --- a/oebakery/cmd/__init__.py +++ b/oebakery/cmd/__init__.py @@ -1,6 +1,4 @@ -from __future__ import with_statement # This isn't required in Python 2.6 - -from cmds import \ +from .cmds import \ clear, \ add_builtin_cmds, \ add_manifest_cmds, \ diff --git a/oebakery/cmd/clone.py b/oebakery/cmd/clone.py index ffb8386..8a217f8 100644 --- a/oebakery/cmd/clone.py +++ b/oebakery/cmd/clone.py @@ -71,7 +71,7 @@ def clone_checkout(options): oebakery.path.copy_local_conf_sample("conf") if not oebakery.call('git config push.default tracking'): - print 'Failed to set push.default = tracking' + print('Failed to set push.default = tracking') return ["update"] @@ -100,7 +100,7 @@ def clone_bare_recursive(repository, directory, branch=None): return submodules = oebakery.gitmodules.parse_dot_gitmodules(buffer=gitmodules) failed = [] - for submodule in submodules.values(): + for submodule in list(submodules.values()): path = submodule['path'] url = submodule['url'] if url != './' + path: # only clone relative submodules diff --git a/oebakery/cmd/cmds.py b/oebakery/cmd/cmds.py index f33bfe2..052363c 100644 --- a/oebakery/cmd/cmds.py +++ b/oebakery/cmd/cmds.py @@ -60,12 +60,12 @@ def add_manifest_cmds(): module = "oelite.cmd" cmds = oelite.cmd.manifest_cmds cmd_type = "manifest" - except ImportError, e: + except ImportError as e: logger.debug("import oelite.cmd failed", exc_info=True) try: import oelite.baker except e: - print e + print(e) return False module = "oebakery.cmd" cmds = ("bake", "show") @@ -81,7 +81,7 @@ def cmds_usage(): cmd = __cmds[cmd_name] usage += "\n %-21s "%(cmd_name) if "description" in dir(cmd) and cmd.description: - if isinstance(cmd.description, basestring): + if isinstance(cmd.description, str): usage += cmd.description else: usage += cmd.description[0] @@ -93,12 +93,12 @@ def cmds_usage(): def cmd_parser(cmd): try: description = cmd.description - if description and not isinstance(description, basestring): + if description and not isinstance(description, str): description = "\n".join(description) # FIXME: use something like format_textblock to format to console # width and handle the 2-space indentation here instead of # requiring command writers to bother about it. - except AttributeError, e: + except AttributeError as e: description = "" try: arguments = "" @@ -114,7 +114,7 @@ def cmd_parser(cmd): else: arguments += " ?%s?*"%(arg_name) description += "\n %-21s %s"%(arg_name, arg_descr) - except AttributeError, e: + except AttributeError as e: arguments = "" parser = optparse.OptionParser(""" %%prog %s [options]%s @@ -135,7 +135,7 @@ def call(cmd, function, *args): try: ret = function(*args) - except Exception, e: + except Exception as e: if hasattr(e, "print_details"): e.print_details() logger.exception("exception in %s.%s()", cmd.name, function.__name__) diff --git a/oebakery/cmd/pull.py b/oebakery/cmd/pull.py index a5d078e..bfd1b1d 100644 --- a/oebakery/cmd/pull.py +++ b/oebakery/cmd/pull.py @@ -2,7 +2,7 @@ import oebakery logger = oebakery.logger -import update +from . import update # oe pull diff --git a/oebakery/cmd/update.py b/oebakery/cmd/update.py index 3d7ddd6..f86011e 100644 --- a/oebakery/cmd/update.py +++ b/oebakery/cmd/update.py @@ -316,14 +316,14 @@ def git_update_remote(name, url, path=None): # get push urls remotes_push = {} - for iter_name in remotes_fetch.keys(): + for iter_name in list(remotes_fetch.keys()): iter_url = oebakery.call('git config --get remote.%s.pushurl'%iter_name, dir=path, quiet=True) if iter_url: remotes_push[iter_name] = iter_url.strip() # if remote is not found, add it and return - if not remotes_fetch.has_key(name): + if name not in remotes_fetch: if not oebakery.call('git remote add %s %s'%(name, fetch_url), dir=path): logger.error("Failed to add remote %s", name) @@ -347,7 +347,7 @@ def git_update_remote(name, url, path=None): # if push url is different url, change it if push_url: - if not remotes_push.has_key(name) or remotes_push[name] != push_url: + if name not in remotes_push or remotes_push[name] != push_url: if not oebakery.call( 'git config remote.%s.pushurl %s'%(name, push_url), dir=path): @@ -356,7 +356,7 @@ def git_update_remote(name, url, path=None): # if push url is currently set, but shouldn't be, remove it else: - if remotes_push.has_key(name) and remotes_push[name] != fetch_url: + if name in remotes_push and remotes_push[name] != fetch_url: if not oebakery.call('git config --unset remote.%s.pushurl'%(name), dir=path): logger.error("failed to unset pushurl for %s", name) diff --git a/oebakery/oe.py b/oebakery/oe.py index 3f0d347..bee1cca 100755 --- a/oebakery/oe.py +++ b/oebakery/oe.py @@ -1,5 +1,8 @@ #!/usr/bin/env python +#python2 compat +from __future__ import print_function + VERSION = "4.2.0" # New arguments strategy: oe [bakery options] [command options] @@ -45,18 +48,18 @@ def module_version(module): raise Exception() except: if bakery_options.debug: - print "DEBUG: bakery: importing oebakery module from source directory:", os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + print("DEBUG: bakery: importing oebakery module from source directory:", os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) sys.path.insert( 0, os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) try: import oebakery version = module_version(oebakery) if version != VERSION: - print >>sys.stderr, "CRITICAL: bad oebakery module version: %s (expected: %s)"%(version, VERSION) + print("CRITICAL: bad oebakery module version: %s (expected: %s)"%(version, VERSION), file=sys.stderr) sys.exit(1) - except ImportError, e: - print >>sys.stderr, "CRITICAL: cannot import oebakery module" - print e + except ImportError as e: + print("CRITICAL: cannot import oebakery module", file=sys.stderr) + print(e) sys.exit(1) # Initialize logging @@ -76,7 +79,7 @@ def module_version(module): oebakery.cmd.add_builtin_cmds() oebakery.cmd.add_manifest_cmds() usage = parser.get_usage() + "\n" + oebakery.cmd.cmds_usage() - print usage + print(usage) sys.exit(1) if cmd_argv[0] == "help": diff --git a/oebakery/parse.py b/oebakery/parse.py index afa2f07..2ab78db 100644 --- a/oebakery/parse.py +++ b/oebakery/parse.py @@ -52,7 +52,7 @@ def __str__(self): return self.msg def print_details(self): - print self.msg + print(self.msg) if not self.details: return "" lines = self.parser.text.splitlines() @@ -61,7 +61,7 @@ def print_details(self): firstline = max(self.errlineno - 5, 0) lastline = min(self.errlineno + 5, len(lines)) errlinepos = 0 - for lineno in xrange(self.errlineno): + for lineno in range(self.errlineno): errlinepos += len(lines[lineno]) + 1 #if isinstance(self.details, ply.lex.LexToken): # print "this is a LexToken" @@ -79,28 +79,28 @@ def print_details(self): lexpos = None linenofmtlen = len(str(lastline)) lineprintfmt = "%%s%%%dd %%s"%(linenofmtlen) - for lineno in xrange(firstline, lastline): + for lineno in range(firstline, lastline): if lineno == self.errlineno: if lineno: - print "" + print("") prefix = "-> " else: prefix = " " - print lineprintfmt%(prefix, lineno + 1, - lines[lineno].expandtabs()) + print(lineprintfmt%(prefix, lineno + 1, + lines[lineno].expandtabs())) if lineno == self.errlineno: if lexpos: prefixlen = len(prefix) + linenofmtlen + 1 - print "%s%s"%(" "*(prefixlen + errlinebefore), - "^"*len(self.symbol)) + print("%s%s"%(" "*(prefixlen + errlinebefore), + "^"*len(self.symbol))) else: - print "" + print("") if self.parser.parent: parent = self.parser.parent - print "Included from %s"%(parent.filename) + print("Included from %s"%(parent.filename)) parent = parent.parent while parent: - print " %s"%(parent.filename) + print(" %s"%(parent.filename)) return @@ -270,7 +270,7 @@ def p_simple_assignment(self, p): def p_exp_assignment(self, p): '''assignment : varflag EXPASSIGN STRING''' - print "EXPASSIGN %s"%(p[1][0]) + print("EXPASSIGN %s"%(p[1][0])) self.data.set_flag(p[1][0], p[1][1], self.data.expand(p[3], 1)) return @@ -320,7 +320,7 @@ def parse(self, filename, require=True): self.filename = os.path.realpath(filename) if not os.path.exists(self.filename): - print "No such file: %s"%(filename) + print("No such file: %s"%(filename)) return None mtime = os.path.getmtime(self.filename) diff --git a/oebakery/path.py b/oebakery/path.py index 758dbbf..59a5556 100644 --- a/oebakery/path.py +++ b/oebakery/path.py @@ -49,14 +49,14 @@ def chdir(dir, quiet=False): logger.debug("chdir: %s", dir) try: cwd = os.getcwd() - except OSError, e: + except OSError as e: cwd = None if (cwd and os.path.realpath(os.path.normpath(dir)) == os.path.normpath(cwd)): return if not quiet: - print '> cd', dir + print('> cd', dir) os.chdir(dir) @@ -67,8 +67,8 @@ def copy_local_conf_sample(confdir): config = os.path.join(confdir, 'local.conf') sample = os.path.join(confdir, 'local.conf.sample') if not os.path.exists(config) and os.path.exists(sample): - print '> cp %s %s'%(sample, config) + print('> cp %s %s'%(sample, config)) try: shutil.copyfile(sample, config) - except Exception, e: + except Exception as e: logger.warning("failed to write config file: %s: %s", config, e) diff --git a/oebakery/shell.py b/oebakery/shell.py index 0d78e36..070525e 100644 --- a/oebakery/shell.py +++ b/oebakery/shell.py @@ -31,9 +31,9 @@ def call(cmd, dir=None, quiet=False, success_returncode=0): if not quiet: if dir: - print '%s> %s'%(dir, cmd) + print('%s> %s'%(dir, cmd)) else: - print '> %s'%(cmd) + print('> %s'%(cmd)) retval = None if quiet: