Kirill Simonov avatar Kirill Simonov committed 35288c3

Added a distutils command.

Comments (0)

Files changed (4)

 PBBT can also read configuration from the following files:
 
 ``setup.cfg``
-    This file is in INI format with  PBBT settings defined in section
+    This file is in INI format with PBBT settings defined in section
     ``[pbbt]``.  The following parameters are recognized: ``extend``,
     ``input``, ``output``, ``define``, ``suite``, ``train``, ``purge``,
     ``max_errors``, ``quiet``.
     ``input``, ``output``, ``define``, ``suite``, ``train``, ``purge``,
     ``max-errors``, ``quiet``.
 
+PBBT can also be executed as a Distutils command::
+
+    python setup.py pbbt
+
+In this case, PBBT configuration could be specified in ``setup.cfg``
+or via command-line parameters.
+
 
 API Reference
 =============
     'console_scripts': [
         'pbbt = pbbt:main',
     ],
+    'distutils.commands': [
+        'pbbt = pbbt.setup:pbbt',
+    ],
 }
 USE_2TO3 = True
 
     if not re.match(r'^[A-Za-z_][0-9A-Za-z_]*'
                     r'(?:\.[A-Za-z_][0-9A-Za-z_]*)*', text):
         raise ValueError("invalid module or file name: %r" % text)
+    return text
 
 
 DESCRIPTION = """\
         setup_cfg.read('setup.cfg')
         if setup_cfg.has_option('pbbt', 'extend'):
             lines = setup_cfg.get('pbbt', 'extend')
-            extend.extend(lines.split())
+            extend.extend(module(line) for line in lines.split())
         if setup_cfg.has_option('pbbt', 'input'):
             input = setup_cfg.get('pbbt', 'input')
         if setup_cfg.has_option('pbbt', 'output'):

src/pbbt/setup.py

+#
+# Copyright (c) 2013, Prometheus Research, LLC
+# Released under MIT license, see `LICENSE` for details.
+#
+
+
+from distutils.cmd import Command
+from distutils.errors import DistutilsError, DistutilsOptionError
+from .run import variable, module, run
+
+
+class pbbt(Command):
+    # Distutils command: `python setup.py pbbt`.
+
+    description = 'run PBBT tests'
+    user_options = [
+            ('input', 'i', "file with input data"),
+            ('output', 'o', "file with output data"),
+            ('quiet', 'q', "display warnings and errors only"),
+            ('train', 'T', "run tests in training mode"),
+            ('purge', 'P', "purge stale output data"),
+            ('max-errors', 'M', "halt after N errors"),
+            ('define', 'D', "set a conditional variable"),
+            ('extend', 'E', "load an extension"),
+            ('suite', 'S', "run a specific test suite"),
+    ]
+    boolean_options = ['quiet', 'train', 'purge']
+
+    def initialize_options(self):
+        self.input = None
+        self.output = None
+        self.quiet = False
+        self.train = False
+        self.purge = False
+        self.max_errors = None
+        self.define = None
+        self.extend = None
+        self.suite = None
+
+    def finalize_options(self):
+        if self.input is None:
+            raise DistutilsOptionError("missing input file")
+        if self.max_errors is not None:
+            try:
+                self.max_errors = int(self.max_errors)
+            except ValueError as exc:
+                raise DistutilsOptionError("invalid max-errors: %s" % exc)
+        if self.define is not None:
+            try:
+                self.define = dict(variable(line)
+                                   for line in self.define.split())
+            except ValueError as exc:
+                raise DistutilsOptionError("invalid define: %s" % exc)
+        else:
+            self.define = {}
+        if self.extend is not None:
+            try:
+                self.extend = [module(line) for line in self.extend.split()]
+            except ValueError as exc:
+                raise DistutilsOptionError("invalid extend: %s" % exc)
+        else:
+            self.extend = []
+        if self.suite is not None:
+            self.suite = self.suite.split()
+
+    def run(self):
+        # Load extensions.
+        for path in self.extend:
+            if os.path.isfile(path):
+                exec open(path) in {}
+            else:
+                __import__(path)
+
+        # Execute the tests.
+        exit = run(self.input, self.output,
+                   variables=self.define,
+                   targets=self.suite,
+                   training=self.train,
+                   purging=self.purge,
+                   max_errors=self.max_errors,
+                   quiet=self.quiet)
+        if exit != 0:
+            raise DistutilsError()
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.