Commits

Lennart Regebro committed 63b284b

Using optparse. (because I support Python 2.6)

Comments (0)

Files changed (2)

 
 - Dropping support for Python 3.1. It still works, but it is unsupported.
 
+- Added support for command line options, implementing #14 and #15.
+
 
 1.4 (2013-05-29)
 ----------------

pyroma/__init__.py

+import os
 import sys
-import os
+from optparse import OptionParser
 from pyroma import projectdata, distributiondata, pypidata, ratings
 
 import logging
                 sys.exit(1)
 
 
-def run(argument):
+def main():
+    usage = "usage: %prog [-a|-d|-f|-p] <project directory|distribution file|pypi package name>"
+    parser = OptionParser(usage)
+    parser.add_option("-a", "--auto", dest="auto", default=False,
+                      action='store_true', help="Select mode automatically (default)",)
+    parser.add_option("-d", "--directory", dest="directory",
+                      action='store_true', default=False, 
+                      help="Run pyroma on a module in a project directory",)
+    parser.add_option("-f", "--file", dest="file", action='store_true',
+                      default=False, help="Run pyroma on a distribution file",)
+    parser.add_option("-p", "--pypi", dest="pypi", action='store_true',
+                      default=False, help="Run pyroma on a package on PyPI",)
+    
+    (options, args) = parser.parse_args()    
+
+    if len(args) < 1:
+        parser.print_help()
+        sys.exit(1)
+
+    modes = (options.auto, options.directory, options.file, options.pypi)
+    if sum([1 if x else 0 for x in modes]) > 1:
+        print("You can only select one of the options -a, -d, -f and -p")
+        sys.exit(1)
+
+    argument = args[0]        
+    
+    if not any(modes) or options.auto:
+        if os.path.isdir(argument):
+            mode = 'directory'
+        elif os.path.isfile(argument):
+            mode = 'file'
+    elif options.directory:
+        mode = 'directory'
+    elif options.file:
+        mode = 'file'
+    else:
+        mode = 'pypi'
+        
     logging.info('-'*30)
     logging.info('Checking ' + argument)
-    
-    if os.path.isdir(argument):
+            
+    if mode == 'directory':   
         data = projectdata.get_data(os.path.abspath(argument))
         logging.info('Found ' + data.get('name', 'nothing'))
-    
-    elif os.path.isfile(argument):
+    elif mode == 'file':
         data = distributiondata.get_data(os.path.abspath(argument))
         logging.info('Found ' + data.get('name', 'nothing'))
-        
     else:
         # It's probably a package name
         data = pypidata.get_data(argument)
         logging.info('Found ' + data.get('name', 'nothing'))
+        
     rating = ratings.rate(data)
         
     logging.info('-'*30)
     logging.info(ratings.LEVELS[rating[0]])
     logging.info('-'*30)
     return rating[0]
-
-
-def main():
-    if len(sys.argv) < 2:
-        logging.info("Usage: pyroma <project directory|file|project name>")
-        sys.exit(1)
-
-    run(sys.argv[1])