Procházet zdrojové kódy

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

Ying Chen před 6 roky
rodič
revize
d50ee7b8e2

+ 12 - 9
tools/app_reg/app_reg.py

@@ -37,8 +37,10 @@ Usage:
 Optional flags:
     --debug             Turns on debugging output
 """
+from __future__ import print_function
 
 
+from builtins import str
 import getopt
 import logging
 import os
@@ -49,6 +51,7 @@ import build
 import common
 import pth
 import registry
+from functools import reduce
 
 PROG_NAME = sys.argv[0]
 
@@ -67,8 +70,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 +113,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 +171,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 +190,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 +201,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 +216,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 +243,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))
 

+ 1 - 0
tools/app_reg/common.py

@@ -15,6 +15,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from past.builtins import cmp
 import glob
 import logging
 import os

+ 1 - 0
tools/app_reg/pth.py

@@ -19,6 +19,7 @@
 Tools to manipulate the .pth file in the virtualenv.
 """
 
+from builtins import object
 import glob
 import logging
 import os

+ 8 - 6
tools/app_reg/registry.py

@@ -19,6 +19,8 @@
 Registry for the applications
 """
 
+from past.builtins import cmp
+from builtins import object
 import glob
 import logging
 import os
@@ -56,7 +58,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 +100,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 +191,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 +199,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 +227,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