Commits

Felix Krull committed 0828837

cmdline: Add a jobs option.

Comments (0)

Files changed (2)

provence/core/cmdline.py

 
 
 # Option parsing and main function.
-def _output_level_callback(option, opt_str, value, parser):
-    if value < 0 or value > OUTPUT_MOST_VERBOSE:
+def _jobs_callback(option, opt_str, value, parser):
+    if value < 1:
         raise optparse.OptionValueError(
-            "option %s: output level needs to in [0..%s]" %
-            (opt_str, OUTPUT_MOST_VERBOSE))
-    parser.values.output_level = value
+            "option %s: needs to be >= 1" % (opt_str))
+    parser.values.jobs = value
 
 
 def _build_option_parser():
         "--check", action="store_true", dest="check")
     parser.add_option(
         "--log-file", action="store", dest="log_file")
+    parser.add_option(
+        "-j", "--jobs", action="callback", type="int", callback=_jobs_callback)
     parser.set_defaults(
         commands=[], output_level=OUTPUT_ERROR_LOGS, check=False,
         log_file=os.path.join(os.path.expanduser("~"), ".provence.log"))
         _configure_logging(opts)
         cmd = ProvenceCommandLine(
             stdout_verbosity=opts.output_level,
-            check_only=opts.check)
+            check_only=opts.check,
+            num_jobs=opts.jobs)
         sys.exit(cmd.run(args, opts.commands))
     except KeyboardInterrupt:
         print("Interrupted")

provence/tests/test_cmdline.py

 
     @mock.patch("provence.core.cmdline.GraphRunner",
                 mock.Mock(return_value=mock.Mock(
+                    provision_graph=mock.Mock(return_value=[]))))
+    @mock.patch("provence.core.cmdline.ThreadPoolWorker")
+    def test_num_jobs_arg(self, tpworker):
+        self.main.num_jobs = mock.Mock()
+        self.main.do_provision(mock.Mock())
+        tpworker.assert_called_once_with(self.main.num_jobs)
+
+    @mock.patch("provence.core.cmdline.GraphRunner",
+                mock.Mock(return_value=mock.Mock(
                     provision_graph=mock.Mock(side_effect=KeyboardInterrupt))))
     @mock.patch("provence.core.cmdline.ThreadPoolWorker",
                 mock.MagicMock())
         self.assertEqual(
             opts.log_file,
             os.path.join(os.path.expanduser("~"), ".provence.log"))
+        self.assertEqual(opts.jobs, None)
 
     @mock.patch("sys.argv", ["provence", "script_1.py", "script_2.py",
                              "script_3.py"])
         with self.assertRaises(SystemExit):
             self.parser.parse_args()
 
+    @mock.patch("sys.argv", ["provence", "-j", "6"])
+    def test_param_jobs_short(self):
+        opts, args = self.parser.parse_args()
+        self.assertEqual(opts.jobs, 6)
+
+    @mock.patch("sys.argv", ["provence", "--jobs", "6"])
+    def test_param_jobs_long(self):
+        opts, args = self.parser.parse_args()
+        self.assertEqual(opts.jobs, 6)
+
+    @mock.patch("sys.argv", ["provence", "--jobs"])
+    def test_bare_jobs_param(self):
+        with self.assertRaises(SystemExit):
+            self.parser.parse_args()
+
+    @mock.patch("sys.argv", ["provence", "--jobs", "some_string"])
+    def test_param_jobs_not_an_int(self):
+        with self.assertRaises(SystemExit):
+            self.parser.parse_args()
+
+    @mock.patch("sys.argv", ["provence", "--jobs", "0"])
+    def test_param_jobs_zero(self):
+        with self.assertRaises(SystemExit):
+            self.parser.parse_args()
+
+    @mock.patch("sys.argv", ["provence", "--jobs", "-10"])
+    def test_param_jobs_negative_int(self):
+        with self.assertRaises(SystemExit):
+            self.parser.parse_args()
+
 
 class TestConfigureLogging(TestCase):
     @mock.patch("atexit.register")
     @mock.patch("provence.core.cmdline.ProvenceCommandLine")
     def test_main_args(self, main_mock):
         args = [
-            (["provence"], {"stdout_verbosity": 3, "check_only": False}),
+            (["provence"],
+             {"stdout_verbosity": 3, "check_only": False, "num_jobs": None}),
             (["provence", "--quiet"],
-             {"stdout_verbosity": 0, "check_only": False}),
+             {"stdout_verbosity": 0, "check_only": False, "num_jobs": None}),
             (["provence", "--check"],
-             {"stdout_verbosity": 3, "check_only": True}),
+             {"stdout_verbosity": 3, "check_only": True, "num_jobs": None}),
             (["provence", "--check", "-q"],
-             {"stdout_verbosity": 0, "check_only": True}),
+             {"stdout_verbosity": 0, "check_only": True, "num_jobs": None}),
+            (["provence", "-j", "10"],
+             {"stdout_verbosity": 3, "check_only": False, "num_jobs": 10}),
         ]
         for argv, main_args in args:
             with mock.patch("sys.argv", argv):