Просмотр исходного кода

More coding style cleanup

Fixing more issues found with pylint.  Some that were supposed to
have been fixed in the previous sweep (Ie0db839e) but were missed:

C0321: More than one statement on a single line
W0622: Redefining built-in 'name'

And some more:

W0631: Using possibly undefined loop variable 'name'
W0223: Method 'name' is abstract in class 'name' but is not overridden
W0231: __init__ method from base class 'name' is not called

Change-Id: Ie119183708609d6279e973057a385fde864230c3
David Pursehouse 13 лет назад
Родитель
Сommit
5c6eeac8f0
10 измененных файлов с 54 добавлено и 28 удалено
  1. 7 0
      command.py
  2. 7 1
      error.py
  3. 1 1
      main.py
  4. 2 1
      manifest_xml.py
  5. 16 8
      project.py
  6. 4 4
      subcmds/__init__.py
  7. 3 3
      subcmds/abandon.py
  8. 6 6
      subcmds/branches.py
  9. 3 3
      subcmds/checkout.py
  10. 5 1
      subcmds/forall.py

+ 7 - 0
command.py

@@ -123,6 +123,11 @@ class Command(object):
     result.sort(key=_getpath)
     result.sort(key=_getpath)
     return result
     return result
 
 
+# pylint: disable-msg=W0223
+# Pylint warns that the `InteractiveCommand` and `PagedCommand` classes do not
+# override method `Execute` which is abstract in `Command`.  Since that method
+# is always implemented in classes derived from `InteractiveCommand` and
+# `PagedCommand`, this warning can be suppressed.
 class InteractiveCommand(Command):
 class InteractiveCommand(Command):
   """Command which requires user interaction on the tty and
   """Command which requires user interaction on the tty and
      must not run within a pager, even if the user asks to.
      must not run within a pager, even if the user asks to.
@@ -137,6 +142,8 @@ class PagedCommand(Command):
   def WantPager(self, opt):
   def WantPager(self, opt):
     return True
     return True
 
 
+# pylint: enable-msg=W0223
+
 class MirrorSafeCommand(object):
 class MirrorSafeCommand(object):
   """Command permits itself to run within a mirror,
   """Command permits itself to run within a mirror,
      and does not require a working directory.
      and does not require a working directory.

+ 7 - 1
error.py

@@ -25,6 +25,7 @@ class EditorError(Exception):
   """Unspecified error from the user's text editor.
   """Unspecified error from the user's text editor.
   """
   """
   def __init__(self, reason):
   def __init__(self, reason):
+    super(EditorError, self).__init__()
     self.reason = reason
     self.reason = reason
 
 
   def __str__(self):
   def __str__(self):
@@ -34,6 +35,7 @@ class GitError(Exception):
   """Unspecified internal error from git.
   """Unspecified internal error from git.
   """
   """
   def __init__(self, command):
   def __init__(self, command):
+    super(GitError, self).__init__()
     self.command = command
     self.command = command
 
 
   def __str__(self):
   def __str__(self):
@@ -43,6 +45,7 @@ class UploadError(Exception):
   """A bundle upload to Gerrit did not succeed.
   """A bundle upload to Gerrit did not succeed.
   """
   """
   def __init__(self, reason):
   def __init__(self, reason):
+    super(UploadError, self).__init__()
     self.reason = reason
     self.reason = reason
 
 
   def __str__(self):
   def __str__(self):
@@ -52,6 +55,7 @@ class DownloadError(Exception):
   """Cannot download a repository.
   """Cannot download a repository.
   """
   """
   def __init__(self, reason):
   def __init__(self, reason):
+    super(DownloadError, self).__init__()
     self.reason = reason
     self.reason = reason
 
 
   def __str__(self):
   def __str__(self):
@@ -61,6 +65,7 @@ class NoSuchProjectError(Exception):
   """A specified project does not exist in the work tree.
   """A specified project does not exist in the work tree.
   """
   """
   def __init__(self, name=None):
   def __init__(self, name=None):
+    super(NoSuchProjectError, self).__init__()
     self.name = name
     self.name = name
 
 
   def __str__(self):
   def __str__(self):
@@ -73,6 +78,7 @@ class InvalidProjectGroupsError(Exception):
   """A specified project is not suitable for the specified groups
   """A specified project is not suitable for the specified groups
   """
   """
   def __init__(self, name=None):
   def __init__(self, name=None):
+    super(InvalidProjectGroupsError, self).__init__()
     self.name = name
     self.name = name
 
 
   def __str__(self):
   def __str__(self):
@@ -86,6 +92,7 @@ class RepoChangedException(Exception):
      use exec to re-execute repo with the new code and manifest.
      use exec to re-execute repo with the new code and manifest.
   """
   """
   def __init__(self, extra_args=None):
   def __init__(self, extra_args=None):
+    super(RepoChangedException, self).__init__()
     self.extra_args = extra_args or []
     self.extra_args = extra_args or []
 
 
 class HookError(Exception):
 class HookError(Exception):
@@ -93,4 +100,3 @@ class HookError(Exception):
 
 
   The common case is that the file wasn't present when we tried to run it.
   The common case is that the file wasn't present when we tried to run it.
   """
   """
-  pass

+ 1 - 1
main.py

@@ -45,7 +45,7 @@ from error import RepoChangedException
 from manifest_xml import XmlManifest
 from manifest_xml import XmlManifest
 from pager import RunPager
 from pager import RunPager
 
 
-from subcmds import all as all_commands
+from subcmds import all_commands
 
 
 global_options = optparse.OptionParser(
 global_options = optparse.OptionParser(
                  usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
                  usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"

+ 2 - 1
manifest_xml.py

@@ -321,7 +321,8 @@ class XmlManifest(object):
       raise ManifestParseError("no <manifest> in %s" % (path,))
       raise ManifestParseError("no <manifest> in %s" % (path,))
 
 
     nodes = []
     nodes = []
-    for node in manifest.childNodes:
+    for node in manifest.childNodes:  # pylint:disable-msg=W0631
+                                      # We only get here if manifest is initialised
         if node.nodeName == 'include':
         if node.nodeName == 'include':
             name = self._reqatt(node, 'name')
             name = self._reqatt(node, 'name')
             fp = os.path.join(include_root, name)
             fp = os.path.join(include_root, name)

+ 16 - 8
project.py

@@ -724,17 +724,25 @@ class Project(object):
     paths.sort()
     paths.sort()
 
 
     for p in paths:
     for p in paths:
-      try: i = di[p]
-      except KeyError: i = None
+      try:
+        i = di[p]
+      except KeyError:
+        i = None
 
 
-      try: f = df[p]
-      except KeyError: f = None
+      try:
+        f = df[p]
+      except KeyError:
+        f = None
 
 
-      if i: i_status = i.status.upper()
-      else: i_status = '-'
+      if i:
+        i_status = i.status.upper()
+      else:
+        i_status = '-'
 
 
-      if f: f_status = f.status.lower()
-      else: f_status = '-'
+      if f:
+        f_status = f.status.lower()
+      else:
+        f_status = '-'
 
 
       if i and i.src_path:
       if i and i.src_path:
         line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status,
         line = ' %s%s\t%s => %s (%s%%)' % (i_status, f_status,

+ 4 - 4
subcmds/__init__.py

@@ -15,7 +15,7 @@
 
 
 import os
 import os
 
 
-all = {}
+all_commands = {}
 
 
 my_dir = os.path.dirname(__file__)
 my_dir = os.path.dirname(__file__)
 for py in os.listdir(my_dir):
 for py in os.listdir(my_dir):
@@ -43,7 +43,7 @@ for py in os.listdir(my_dir):
 
 
     name = name.replace('_', '-')
     name = name.replace('_', '-')
     cmd.NAME = name
     cmd.NAME = name
-    all[name] = cmd
+    all_commands[name] = cmd
 
 
-if 'help' in all:
-  all['help'].commands = all
+if 'help' in all_commands:
+  all_commands['help'].commands = all_commands

+ 3 - 3
subcmds/abandon.py

@@ -42,10 +42,10 @@ It is equivalent to "git branch -D <branchname>".
     nb = args[0]
     nb = args[0]
     err = []
     err = []
     success = []
     success = []
-    all = self.GetProjects(args[1:])
+    all_projects = self.GetProjects(args[1:])
 
 
-    pm = Progress('Abandon %s' % nb, len(all))
-    for project in all:
+    pm = Progress('Abandon %s' % nb, len(all_projects))
+    for project in all_projects:
       pm.update()
       pm.update()
 
 
       status = project.AbandonBranch(nb)
       status = project.AbandonBranch(nb)

+ 6 - 6
subcmds/branches.py

@@ -93,17 +93,17 @@ is shown, then the branch appears in all projects.
   def Execute(self, opt, args):
   def Execute(self, opt, args):
     projects = self.GetProjects(args)
     projects = self.GetProjects(args)
     out = BranchColoring(self.manifest.manifestProject.config)
     out = BranchColoring(self.manifest.manifestProject.config)
-    all = {}
+    all_branches = {}
     project_cnt = len(projects)
     project_cnt = len(projects)
 
 
     for project in projects:
     for project in projects:
       for name, b in project.GetBranches().iteritems():
       for name, b in project.GetBranches().iteritems():
         b.project = project
         b.project = project
-        if name not in all:
-          all[name] = BranchInfo(name)
-        all[name].add(b)
+        if name not in all_branches:
+          all_branches[name] = BranchInfo(name)
+        all_branches[name].add(b)
 
 
-    names = all.keys()
+    names = all_branches.keys()
     names.sort()
     names.sort()
 
 
     if not names:
     if not names:
@@ -116,7 +116,7 @@ is shown, then the branch appears in all projects.
         width = len(name)
         width = len(name)
 
 
     for name in names:
     for name in names:
-      i = all[name]
+      i = all_branches[name]
       in_cnt = len(i.projects)
       in_cnt = len(i.projects)
 
 
       if i.IsCurrent:
       if i.IsCurrent:

+ 3 - 3
subcmds/checkout.py

@@ -39,10 +39,10 @@ The command is equivalent to:
     nb = args[0]
     nb = args[0]
     err = []
     err = []
     success = []
     success = []
-    all = self.GetProjects(args[1:])
+    all_projects = self.GetProjects(args[1:])
 
 
-    pm = Progress('Checkout %s' % nb, len(all))
-    for project in all:
+    pm = Progress('Checkout %s' % nb, len(all_projects))
+    for project in all_projects:
       pm.update()
       pm.update()
 
 
       status = project.CheckoutBranch(nb)
       status = project.CheckoutBranch(nb)

+ 5 - 1
subcmds/forall.py

@@ -141,12 +141,16 @@ terminal and are not redirected.
       for cn in cmd[1:]:
       for cn in cmd[1:]:
         if not cn.startswith('-'):
         if not cn.startswith('-'):
           break
           break
-      if cn in _CAN_COLOR:
+      else:
+        cn = None
+      # pylint: disable-msg=W0631
+      if cn and cn in _CAN_COLOR:
         class ColorCmd(Coloring):
         class ColorCmd(Coloring):
           def __init__(self, config, cmd):
           def __init__(self, config, cmd):
             Coloring.__init__(self, config, cmd)
             Coloring.__init__(self, config, cmd)
         if ColorCmd(self.manifest.manifestProject.config, cn).is_on:
         if ColorCmd(self.manifest.manifestProject.config, cn).is_on:
           cmd.insert(cmd.index(cn) + 1, '--color')
           cmd.insert(cmd.index(cn) + 1, '--color')
+      # pylint: enable-msg=W0631
 
 
     mirror = self.manifest.IsMirror
     mirror = self.manifest.IsMirror
     out = ForallColoring(self.manifest.manifestProject.config)
     out = ForallColoring(self.manifest.manifestProject.config)