瀏覽代碼

HUE-8737 [core] Futurize tools/app_reg for Python 3.5

Ying Chen 6 年之前
父節點
當前提交
7e1407adce
共有 4 個文件被更改,包括 29 次插入16 次删除
  1. 11 10
      tools/app_reg/app_reg.py
  2. 3 0
      tools/app_reg/common.py
  3. 4 0
      tools/app_reg/pth.py
  4. 11 6
      tools/app_reg/registry.py

+ 11 - 10
tools/app_reg/app_reg.py

@@ -37,7 +37,7 @@ Usage:
 Optional flags:
     --debug             Turns on debugging output
 """
-
+from __future__ import print_function
 
 import getopt
 import logging
@@ -49,6 +49,7 @@ import build
 import common
 import pth
 import registry
+from functools import reduce
 
 PROG_NAME = sys.argv[0]
 
@@ -67,8 +68,8 @@ def usage(msg=None):
   """Print the usage with an optional message. And exit."""
   global __doc__
   if msg is not None:
-    print >>sys.stderr, msg
-  print >>sys.stderr, __doc__ % dict(PROG_NAME=PROG_NAME)
+    print(msg, file=sys.stderr)
+  print(__doc__ % dict(PROG_NAME=PROG_NAME), file=sys.stderr)
   sys.exit(1)
 
 
@@ -110,7 +111,7 @@ def _do_install_one(reg, app_loc, relative_path):
     # Relative to cwd.
     app_loc = os.path.realpath(app_loc)
     app_name, version, desc, author = get_app_info(app_loc)
-  except (ValueError, OSError), ex:
+  except (ValueError, OSError) as ex:
     LOG.error(ex)
     return False
 
@@ -168,7 +169,7 @@ def do_remove(app_name):
     pthfile.remove(app)
     pthfile.save()
     return True
-  except (OSError, SystemError), ex:
+  except (OSError, SystemError) as ex:
     LOG.error("Failed to update the .pth file. Please fix any problem and run "
               "`%s --sync'\n%s" % (PROG_NAME, ex))
     return False
@@ -187,7 +188,7 @@ def do_sync(reg=None):
 
     build.make_syncdb()
     return True
-  except (OSError, SystemError), ex:
+  except (OSError, SystemError) as ex:
     LOG.error("Failed to update the .pth file. Please fix any problem and run "
               "`%s --sync'\n%s" % (PROG_NAME, ex))
     return False
@@ -198,7 +199,7 @@ def do_collectstatic():
   try:
     build.make_collectstatic()
     return True
-  except (OSError, SystemError), ex:
+  except (OSError, SystemError) as ex:
     LOG.error("Failed to collect the static files. Please fix any problem and run "
               "`%s --collectstatic'\n%s" % (PROG_NAME, ex))
     return False
@@ -213,7 +214,7 @@ def main():
     opts, tail = getopt.getopt(sys.argv[1:],
                                'ir:lds',
                                ('install', 'remove=', 'list', 'debug', 'sync'))
-  except getopt.GetoptError, ex:
+  except getopt.GetoptError as ex:
     usage(str(ex))
 
   def verify_action(current, new_val):
@@ -240,8 +241,8 @@ def main():
   if action == DO_INSTALL:
     # ['..', '--relative-paths', 'a', 'b'] => True
     # ['..', 'a', 'b'] -> False
-    relative_paths = reduce(lambda accum, x: accum or x, map(lambda x: x in ['--relative-paths'], tail))
-    app_loc_list = filter(lambda x: x not in ['--relative-paths'], tail)
+    relative_paths = reduce(lambda accum, x: accum or x, [x in ['--relative-paths'] for x in tail])
+    app_loc_list = [x for x in tail if x not in ['--relative-paths']]
   elif len(tail) != 0:
     usage("Unknown trailing arguments: %s" % ' '.join(tail))
 

+ 3 - 0
tools/app_reg/common.py

@@ -21,6 +21,9 @@ import os
 import sys
 from posixpath import curdir, sep, pardir, join
 
+if sys.version_info[0] > 2:
+  from past.builtins import cmp
+
 # The root of the Hue installation
 INSTALL_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..'))
 

+ 4 - 0
tools/app_reg/pth.py

@@ -22,9 +22,13 @@ Tools to manipulate the .pth file in the virtualenv.
 import glob
 import logging
 import os
+import sys
 
 import common
 
+if sys.version_info[0] > 2:
+  from builtins import object
+
 LOG = logging.getLogger(__name__)
 PTH_FILE = 'hue.pth'
 

+ 11 - 6
tools/app_reg/registry.py

@@ -22,10 +22,15 @@ Registry for the applications
 import glob
 import logging
 import os
+import sys
 import json
 
 import common
 
+if sys.version_info[0] > 2:
+  from builtins import object
+  from past.builtins import cmp
+
 LOG = logging.getLogger(__name__)
 
 class AppRegistry(object):
@@ -56,7 +61,7 @@ class AppRegistry(object):
   def _write(self, path):
     """Write out the registry to the given path"""
     outfile = file(path, 'w')
-    json.dump(self._apps.values(), outfile, cls=AppJsonEncoder, indent=2)
+    json.dump(list(self._apps.values()), outfile, cls=AppJsonEncoder, indent=2)
     outfile.close()
 
   def contains(self, app):
@@ -98,7 +103,7 @@ class AppRegistry(object):
 
   def get_all_apps(self):
     """get_all_apps() -> List of HueApp"""
-    return self._apps.values()
+    return list(self._apps.values())
 
   def save(self):
     """Save and write out the registry"""
@@ -189,7 +194,7 @@ class HueApp(object):
           if not os.path.exists(cur):
             os.unlink(link_name)
             LOG.warn("Removing broken link: %s" % (link_name,))
-        except OSError, ex:
+        except OSError as ex:
           LOG.warn("Error checking for existing link %s: %s" % (link_name, ex))
 
       # Actually install the link
@@ -197,12 +202,12 @@ class HueApp(object):
         os.symlink(target, link_name)
         LOG.info('Symlink config %s -> %s' % (link_name, target))
         installed.append(link_name)
-      except OSError, ex:
+      except OSError as ex:
         LOG.error("Failed to symlink %s to %s: %s" % (target, link_name, ex))
         for lnk in installed:
           try:
             os.unlink(lnk)
-          except OSError, ex2:
+          except OSError as ex2:
             LOG.error("Failed to cleanup link %s: %s" % (link_name, ex2))
         return False
     return True
@@ -225,7 +230,7 @@ class HueApp(object):
         try:
           os.unlink(path)
           LOG.info('Remove config symlink %s -> %s' % (path, target))
-        except OSError, ex:
+        except OSError as ex:
           LOG.error("Failed to remove configuration link %s: %s" % (path, ex))
           return False
     return True