From 5f9f044196e28ff54887c2e2569d72218a873c5a Mon Sep 17 00:00:00 2001
From: Kevin Lyda <kevin@ie.suberic.net>
Date: Sun, 10 Jun 2012 13:43:20 +0100
Subject: [PATCH] Code clean up.

Add pylint config file.  Clean up some pylint errors.  Clean up setup.py
and make it work with tox/pip.
---
 .pylintrc           | 83 +++++++++++++++++++++++++++++++++++++++++++++
 chkcrontab          |  4 +--
 chkcrontab_lib.py   | 14 +-------
 setup.py            | 78 ++++++++++++++++++++++--------------------
 tests/test_check.py |  2 +-
 5 files changed, 128 insertions(+), 53 deletions(-)
 create mode 100644 .pylintrc
 mode change 100755 => 100644 chkcrontab_lib.py

diff --git a/.pylintrc b/.pylintrc
new file mode 100644
index 0000000..745d1ac
--- /dev/null
+++ b/.pylintrc
@@ -0,0 +1,83 @@
+[MASTER]
+profile=no
+ignore=CVS
+persistent=no
+load-plugins=
+
+[MESSAGES CONTROL]
+
+[REPORTS]
+output-format=colorized
+include-ids=no
+files-output=no
+reports=no
+evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10)
+comment=no
+
+[BASIC]
+required-attributes=
+bad-functions=map,filter,apply,input
+module-rgx=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$
+const-rgx=(([A-Z_][A-Z0-9_]*)|(__.*__))$
+class-rgx=[A-Z_][a-zA-Z0-9]+$
+function-rgx=[a-z_][a-z0-9_]{2,30}$
+method-rgx=[a-z_][a-z0-9_]{2,30}$
+attr-rgx=[a-z_][a-z0-9_]{2,30}$
+argument-rgx=[a-z_][a-z0-9_]{2,30}$
+# Regular expression which should only match correct variable names
+# Note: s/2/1/ to allow two char var names.
+variable-rgx=[a-z_][a-z0-9_]{1,30}$
+inlinevar-rgx=[A-Za-z_][A-Za-z0-9_]*$
+good-names=i,j,k,ex,f,fn,a,b,Run,_
+bad-names=foo,bar,baz,toto,tutu,tata
+no-docstring-rgx=__.*__
+
+[FORMAT]
+max-line-length=80
+max-module-lines=5000
+indent-string='  '
+
+[MISCELLANEOUS]
+notes=FIXME,XXX,TODO
+
+[SIMILARITIES]
+min-similarity-lines=4
+ignore-comments=yes
+ignore-docstrings=yes
+
+[TYPECHECK]
+ignore-mixin-members=yes
+ignored-classes=SQLObject
+zope=no
+generated-members=REQUEST,acl_users,aq_parent
+
+[VARIABLES]
+init-import=no
+dummy-variables-rgx=_|dummy
+additional-builtins=
+
+[CLASSES]
+ignore-iface-methods=isImplementedBy,deferred,extends,names,namesAndDescriptions,queryDescriptionFor,getBases,getDescriptionFor,getDoc,getName,getTaggedValue,getTaggedValueTags,isEqualOrExtendedBy,setTaggedValue,isImplementedByInstancesOf,adaptWith,is_implemented_by
+defining-attr-methods=__init__,__new__,setUp
+valid-classmethod-first-arg=cls
+
+[DESIGN]
+max-args=5
+ignored-argument-names=_.*
+max-locals=15
+max-returns=6
+max-branchs=12
+max-statements=50
+max-parents=7
+max-attributes=7
+min-public-methods=2
+max-public-methods=20
+
+[IMPORTS]
+deprecated-modules=regsub,string,TERMIOS,Bastion,rexec
+import-graph=
+ext-import-graph=
+int-import-graph=
+
+[EXCEPTIONS]
+overgeneral-exceptions=Exception
diff --git a/chkcrontab b/chkcrontab
index d39953d..9020dfb 100755
--- a/chkcrontab
+++ b/chkcrontab
@@ -24,18 +24,18 @@ Basic usage:
 
 __author__ = 'lyda@google.com (Kevin Lyda)'
 
-import os
 import sys
 import chkcrontab_lib as check
 
 
 def main(argv):
+  """main program."""
   if len(argv) != 2:
     print 'ERROR: No crontab file was specified.'
     sys.exit(1)
   log = check.LogCounter()
   print 'Checking correctness of %s' % argv[1]
-  return check.CheckCrontab(argv[1], log)
+  return check.check_crontab(argv[1], log)
 
 if __name__ == '__main__':
   main(sys.argv)
diff --git a/chkcrontab_lib.py b/chkcrontab_lib.py
old mode 100755
new mode 100644
index 88e6f6b..34b9eb0
--- a/chkcrontab_lib.py
+++ b/chkcrontab_lib.py
@@ -994,7 +994,7 @@ class LogCounter(object):
     return self._error_count
 
 
-def CheckCrontab(crontab_file, log):
+def check_crontab(crontab_file, log):
   """Check a crontab file.
 
   Checks crontab_file for a variety of errors or potential errors.  This only
@@ -1039,15 +1039,3 @@ def CheckCrontab(crontab_file, log):
 
   # Summarize the log messages if there were any.
   return log.Summary()
-
-
-def main():
-  log = LogCounter()
-  if len(sys.argv) != 2:
-    log.Error('Must provide a crontab file to check.')
-  print('Checking correctness of %s' % sys.argv[1])
-  return CheckCrontab(sys.argv[1], log)
-
-
-if __name__ == '__main__':
-  sys.exit(main())
diff --git a/setup.py b/setup.py
index 10d6439..6e5cacf 100755
--- a/setup.py
+++ b/setup.py
@@ -35,7 +35,7 @@ BASE_DIR = os.path.dirname(globals().get('__file__', os.getcwd()))
 
 class TestCmd(Command):
   description = 'Runs all available tests.'
-  user_options = [ ]
+  user_options = []
 
   def initialize_options(self):
     pass
@@ -54,7 +54,7 @@ class TestCmd(Command):
 
 class CleanCmd(Command):
   description = 'Remove all generated files.'
-  user_options = [ ]
+  user_options = []
 
   def initialize_options(self):
     pass
@@ -68,7 +68,7 @@ class CleanCmd(Command):
     dirs2del = [ './build', './dist' ]
     dirs2ign = [ './.git' ]
     # End config.
-    doomed = [ ]
+    doomed = set()
     # Change to base dir.
     os.chdir(BASE_DIR)
     for root, dirs, files in os.walk('.'):
@@ -76,18 +76,18 @@ class CleanCmd(Command):
       if root in dirs2ign:
         continue
       if root in dirs2del:
-        doomed.append(root)
+        doomed.add(root)
       # Handle files.
       for f in files:
         accused = os.path.join(root, f)
         for suffix in suffixes2del:
           if f.endswith(suffix):
-            doomed.append(accused)
+            doomed.add(accused)
             break
         if accused not in doomed:
           for d2del in dirs2del:
             if accused.startswith(d2del):
-              doomed.append(accused)
+              doomed.add(accused)
               break
       # Handle dirs.
       for d in dirs:
@@ -99,7 +99,7 @@ class CleanCmd(Command):
         if d in dirs:
           for d2del in dirs2del:
             if accused.startswith(d2del):
-              doomed.append(accused)
+              doomed.add(accused)
               break
     # Probably not required, but just to be safe.
     for accused in doomed:
@@ -107,8 +107,7 @@ class CleanCmd(Command):
         if accused.startswith(d2ign):
           doomed.remove(accused)
           break
-    doomed.sort(reverse=True)
-    for accused in doomed:
+    for accused in sorted(doomed, reverse=True):
       log.info('removing "%s"', os.path.normpath(accused))
       if not self.dry_run:
         try:
@@ -131,13 +130,13 @@ class InstallCmd(install):
 
   def finalize_options(self):
     install.finalize_options(self)
-    if self.manprefix is None :
+    if self.manprefix is None:
       self.manprefix = os.path.join(self.install_scripts,
                                     '..', 'share', 'man')
 
   def run(self):
     install.run(self)
-    manpages=['doc/chkcrontab.1']
+    manpages = ['doc/chkcrontab.1']
     if self.manprefix:
       for manpage in manpages:
         section = manpage.split('/')[-1].split('.')[-1]
@@ -153,31 +152,36 @@ class InstallCmd(install):
                             os.path.join(manpage_dir, manpage_file),
                             dry_run=self.dry_run)
 
+# Only override install if not being run by setuptools.
+cmdclass = {'test': TestCmd,
+            'dist_clean': CleanCmd,
+            }
+if 'setuptools' not in dir():
+  cmdclass['install'] = InstallCmd
+
 setup(
-    cmdclass={'test': TestCmd,
-              'dist_clean': CleanCmd,
-              'install': InstallCmd,
-             },
-    name='chkcrontab',
-    version='1.2',
-    url='http://code.google.com/p/chkcrontab',
-    author='Kevin Lyda',
-    author_email='lyda@google.com',
-    description='A tool to detect crontab errors',
-    long_description=open('README.rst').read(),
-    py_modules=['chkcrontab_lib'],
-    scripts=['chkcrontab'],
-    keywords='check lint crontab',
-    # See http://pypi.python.org/pypi?%3Aaction=list_classifiers
-    license = 'Apache Software License',
-    platforms = ['POSIX'],
-    classifiers=['Development Status :: 5 - Production/Stable',
-                 'Environment :: Console',
-                 'License :: OSI Approved :: Apache Software License',
-                 'Operating System :: POSIX',
-                 'Programming Language :: Python :: 2.5',
-                 'Programming Language :: Python :: 2.6',
-                 'Programming Language :: Python :: 2.7',
-                 'Topic :: Utilities',
-                ],
+  cmdclass=cmdclass,
+  name='chkcrontab',
+  version='1.2',
+  url='http://code.google.com/p/chkcrontab',
+  author='Kevin Lyda',
+  author_email='lyda@google.com',
+  description='A tool to detect crontab errors',
+  long_description=open('README.rst').read(),
+  py_modules=['chkcrontab_lib'],
+  scripts=['chkcrontab'],
+  keywords='check lint crontab',
+  # See http://pypi.python.org/pypi?%3Aaction=list_classifiers
+  license = 'Apache Software License',
+  platforms = ['POSIX'],
+  classifiers=['Development Status :: 5 - Production/Stable',
+               'Environment :: Console',
+               'License :: OSI Approved :: Apache Software License',
+               'Operating System :: POSIX',
+               'Programming Language :: Python :: 2.5',
+               'Programming Language :: Python :: 2.6',
+               'Programming Language :: Python :: 2.7',
+               'Programming Language :: Python :: 3',
+               'Topic :: Utilities',
+               ],
 )
diff --git a/tests/test_check.py b/tests/test_check.py
index c84a009..3bf95e5 100755
--- a/tests/test_check.py
+++ b/tests/test_check.py
@@ -311,7 +311,7 @@ class CheckCrontabUnitTest(unittest.TestCase):
     log = check.LogCounter()
     crontab_file = os.path.join(BASE_PATH, crontab)
     (exp_warn, exp_fail, exp_rc) = self.GetExpWFRs(crontab_file)
-    self.assertEquals(check.CheckCrontab(crontab_file, log), exp_rc,
+    self.assertEquals(check.check_crontab(crontab_file, log), exp_rc,
                       'Failed to return %d for crontab errors.' % exp_rc)
     self.assertEquals(log.warn_count, exp_warn,
                       'Found %d warns not %d.' % (log.warn_count, exp_warn))
-- 
GitLab