commit:     e97d33dc96730d91c2070f2dc9aab8477dd0445a
Author:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Thu Feb 16 21:21:38 2017 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Thu Feb 16 21:21:38 2017 +0000
URL:        https://gitweb.gentoo.org/proj/pax-utils.git/commit/?id=e97d33dc

pylint: add a helper to keep python code clean

Fix up some lddtree logic at the same time so it's warning free.

 .pylintrc  | 34 ++++++++++++++++++++++++++++++++++
 lddtree.py | 16 +++++++++++-----
 pylint     | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+), 5 deletions(-)

diff --git a/.pylintrc b/.pylintrc
new file mode 100644
index 0000000..b58abfa
--- /dev/null
+++ b/.pylintrc
@@ -0,0 +1,34 @@
+[MESSAGES CONTROL]
+# Disable the message, report, category or checker with the given id(s). You
+# can either give multiple identifier separated by comma (,) or put this option
+# multiple times (only on the command line, not in the configuration file where
+# it should appear only once).
+disable=
+       too-many-lines,
+       too-many-branches,
+       too-many-statements,
+       too-few-public-methods,
+       too-many-instance-attributes,
+       too-many-public-methods,
+       too-many-locals,
+       too-many-arguments,
+       locally-enabled,
+       locally-disabled,
+       fixme,
+       invalid-name,
+
+[REPORTS]
+reports=no
+
+[FORMAT]
+max-line-length=80
+indent-string='  '
+
+[SIMILARITIES]
+min-similarity-lines=20
+
+[VARIABLES]
+dummy-variables-rgx=_
+
+[DESIGN]
+max-parents=10

diff --git a/lddtree.py b/lddtree.py
index 8480248..d1ec9e9 100755
--- a/lddtree.py
+++ b/lddtree.py
@@ -1,4 +1,5 @@
 #!/usr/bin/python
+# -*- coding: utf-8 -*-
 # Copyright 2012-2014 Gentoo Foundation
 # Copyright 2012-2014 Mike Frysinger <[email protected]>
 # Copyright 2012-2014 The Chromium OS Authors
@@ -73,7 +74,7 @@ def dbg(debug, *args, **kwargs):
 def bstr(buf):
   """Decode the byte string into a string"""
   if isinstance(buf, str):
-      return buf
+    return buf
   return buf.decode('utf-8')
 
 
@@ -351,6 +352,8 @@ def FindLib(elf, lib, ldpaths, root='/', debug=False):
   return (None, None)
 
 
+# We abuse the _all_libs state.  We probably shouldn't, but we do currently.
+# pylint: disable=dangerous-default-value
 def ParseELF(path, root='/', prefix='', ldpaths={'conf':[], 'env':[], 
'interp':[]},
              display=None, debug=False, _first=True, _all_libs={}):
   """Parse the ELF dependency tree of the specified file
@@ -491,6 +494,7 @@ def ParseELF(path, root='/', prefix='', ldpaths={'conf':[], 
'env':[], 'interp':[
     del elf
 
   return ret
+# pylint: enable=dangerous-default-value
 
 
 class _NormalizePathAction(argparse.Action):
@@ -608,7 +612,7 @@ def _ActionCopy(options, elf):
   # Similarly, we should provide an option for automatically copying over
   # the libnsl.so and libnss_*.so libraries, as well as an open ended list
   # for known libs that get loaded (e.g. curl will dlopen(libresolv)).
-  libpaths = set()
+  uniq_libpaths = set()
   for lib in elf['libs']:
     libdata = elf['libs'][lib]
     path = libdata['realpath']
@@ -616,17 +620,18 @@ def _ActionCopy(options, elf):
       warn('could not locate library: %s' % lib)
       continue
     if not options.libdir:
-      libpaths.add(_StripRoot(os.path.dirname(path)))
+      uniq_libpaths.add(_StripRoot(os.path.dirname(path)))
     _copy(path, libdata['path'], outdir=options.libdir)
 
   if not options.libdir:
-    libpaths = list(libpaths)
+    libpaths = list(uniq_libpaths)
     if elf['runpath']:
       libpaths = elf['runpath'] + libpaths
     else:
       libpaths = elf['rpath'] + libpaths
   else:
-    libpaths.add(options.libdir)
+    uniq_libpaths.add(options.libdir)
+    libpaths = list(uniq_libpaths)
 
   # We don't bother to copy this as ParseElf adds the interp to the 'libs',
   # so it was already copied in the libs loop above.
@@ -637,6 +642,7 @@ def _ActionCopy(options, elf):
 
 
 def main(argv):
+  """The main entry point!"""
   parser = argparse.ArgumentParser(
       description=__doc__,
       formatter_class=argparse.RawDescriptionHelpFormatter)

diff --git a/pylint b/pylint
new file mode 100755
index 0000000..0098e06
--- /dev/null
+++ b/pylint
@@ -0,0 +1,49 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+# Copyright 1999-2017 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+"""Run pylint with the right settings."""
+
+from __future__ import print_function
+
+import os
+import sys
+
+
+def find_all_modules(source_root):
+  """Locate all python modules in the tree for scanning"""
+  ret = []
+
+  for root, _dirs, files in os.walk(source_root, topdown=False):
+    # Add all of the .py modules in the tree.
+    ret += [os.path.join(root, x) for x in files if x.endswith('.py')]
+
+  # Add the main scripts that don't end in .py.
+  ret += [os.path.join(source_root, x) for x in ('pylint',)]
+
+  return ret
+
+
+def main(argv):
+  """The main entry point"""
+  source_root = os.path.dirname(os.path.realpath(__file__))
+
+  if not argv:
+    argv = find_all_modules(source_root)
+
+  pympath = source_root
+  pythonpath = os.environ.get('PYTHONPATH')
+  if pythonpath is None:
+    pythonpath = pympath
+  else:
+    pythonpath = pympath + ':' + pythonpath
+  os.environ['PYTHONPATH'] = pythonpath
+
+  pylintrc = os.path.join(source_root, '.pylintrc')
+  cmd = ['pylint', '--rcfile', pylintrc]
+  os.execvp(cmd[0], cmd + argv)
+
+
+if __name__ == '__main__':
+  sys.exit(main(sys.argv[1:]))

Reply via email to