cmd-queue 0.2.0__py3-none-any.whl → 0.2.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of cmd-queue might be problematic. Click here for more details.

cmd_queue/__init__.py CHANGED
@@ -306,7 +306,7 @@ Example:
306
306
  __mkinit__ = """
307
307
  mkinit -m cmd_queue
308
308
  """
309
- __version__ = '0.2.0'
309
+ __version__ = '0.2.2'
310
310
 
311
311
 
312
312
  __submodules__ = {
cmd_queue/serial_queue.py CHANGED
@@ -7,30 +7,7 @@ import ubelt as ub
7
7
  import uuid
8
8
  from cmd_queue import base_queue
9
9
  from cmd_queue.util import util_tags
10
-
11
-
12
- def indent(text, prefix=' '):
13
- r"""
14
- Indents a block of text
15
-
16
- Args:
17
- text (str): text to indent
18
- prefix (str, default = ' '): prefix to add to each line
19
-
20
- Returns:
21
- str: indented text
22
-
23
- >>> from cmd_queue.serial_queue import * # NOQA
24
- >>> text = ['aaaa', 'bb', 'cc\n dddd\n ef\n']
25
- >>> text = indent(text)
26
- >>> print(text)
27
- >>> text = indent(text)
28
- >>> print(text)
29
- """
30
- if isinstance(text, (list, tuple)):
31
- return indent('\n'.join(text), prefix)
32
- else:
33
- return prefix + text.replace('\n', '\n' + prefix)
10
+ from cmd_queue.util import util_bash
34
11
 
35
12
 
36
13
  class BashJob(base_queue.Job):
@@ -115,6 +92,21 @@ class BashJob(base_queue.Job):
115
92
  self.tags = util_tags.Tags.coerce(tags)
116
93
  self.allow_indent = allow_indent
117
94
 
95
+ def _test_bash_syntax_errors(self):
96
+ """
97
+ Check for bash syntax errors
98
+
99
+ Example:
100
+ >>> from cmd_queue.serial_queue import * # NOQA
101
+ >>> # Demo full boilerplate for a job with dependencies
102
+ >>> self = BashJob('basd syhi(', name='job1')
103
+ >>> import pytest
104
+ >>> with pytest.raises(SyntaxError):
105
+ >>> self._test_bash_syntax_errors()
106
+ """
107
+ bash_text = self.finalize_text()
108
+ _check_bash_text_for_syntax_errors(bash_text)
109
+
118
110
  def finalize_text(self, with_status=True, with_gaurds=True,
119
111
  conditionals=None, **kwargs):
120
112
  script = []
@@ -178,7 +170,8 @@ class BashJob(base_queue.Job):
178
170
  json_fmt_parts += [
179
171
  ('logs', '"%s"', self.log_fpath),
180
172
  ]
181
- dump_pre_status = _bash_json_dump(json_fmt_parts, self.stat_fpath)
173
+ dump_pre_status = util_bash.bash_json_dump(json_fmt_parts,
174
+ self.stat_fpath)
182
175
  script.append('# Mark job as running')
183
176
  script.append(dump_pre_status)
184
177
 
@@ -247,7 +240,8 @@ class BashJob(base_queue.Job):
247
240
  json_fmt_parts += [
248
241
  ('logs', '"%s"', self.log_fpath),
249
242
  ]
250
- dump_post_status = _bash_json_dump(json_fmt_parts, self.stat_fpath)
243
+ dump_post_status = util_bash.bash_json_dump(json_fmt_parts,
244
+ self.stat_fpath)
251
245
 
252
246
  on_pass_part = indent(_job_conditionals['on_pass'])
253
247
  on_fail_part = indent(_job_conditionals['on_fail'])
@@ -468,7 +462,8 @@ class SerialQueue(base_queue.Queue):
468
462
  ('name', '"%s"', self.name),
469
463
  ('rootid', '"%s"', self.rootid),
470
464
  ]
471
- dump_code = _bash_json_dump(json_fmt_parts, self.state_fpath)
465
+ dump_code = util_bash.bash_json_dump(json_fmt_parts,
466
+ self.state_fpath)
472
467
  script.append('# Update queue status')
473
468
  script.append(dump_code)
474
469
  # script.append('cat ' + str(self.state_fpath))
@@ -575,6 +570,10 @@ class SerialQueue(base_queue.Queue):
575
570
  r"""
576
571
  Print info about the commands, optionally with rich
577
572
 
573
+ Args:
574
+ *args: see :func:`cmd_queue.base_queue.Queue.print_commands`.
575
+ **kwargs: see :func:`cmd_queue.base_queue.Queue.print_commands`.
576
+
578
577
  CommandLine:
579
578
  xdoctest -m cmd_queue.serial_queue SerialQueue.print_commands
580
579
 
@@ -675,41 +674,38 @@ class SerialQueue(base_queue.Queue):
675
674
  return state
676
675
 
677
676
 
678
- def _bash_json_dump(json_fmt_parts, fpath):
679
- """
680
- Make a printf command that dumps a json file indicating some status in a
681
- bash environment.
677
+ def indent(text, prefix=' '):
678
+ r"""
679
+ Indents a block of text
682
680
 
683
681
  Args:
684
- List[Tuple[str, str, str]]: A list of 3-tupels indicating the name of
685
- the json key, the printf code, and the bash expression to fill the
686
- printf code.
687
-
688
- fpath (str): where bash should write the json file
682
+ text (str): text to indent
683
+ prefix (str, default = ' '): prefix to add to each line
689
684
 
690
685
  Returns:
691
- str : the bash that will perform the printf
686
+ str: indented text
692
687
 
693
- Example:
694
- >>> from cmd_queue.serial_queue import _bash_json_dump
695
- >>> json_fmt_parts = [
696
- >>> ('home', '%s', '$HOME'),
697
- >>> ('const', '%s', 'MY_CONSTANT'),
698
- >>> ('ps2', '"%s"', '$PS2'),
699
- >>> ]
700
- >>> fpath = 'out.json'
701
- >>> dump_code = _bash_json_dump(json_fmt_parts, fpath)
702
- >>> print(dump_code)
688
+ >>> from cmd_queue.serial_queue import * # NOQA
689
+ >>> text = ['aaaa', 'bb', 'cc\n dddd\n ef\n']
690
+ >>> text = indent(text)
691
+ >>> print(text)
692
+ >>> text = indent(text)
693
+ >>> print(text)
703
694
  """
704
- printf_body_parts = [
705
- '"{}": {}'.format(k, f) for k, f, v in json_fmt_parts
706
- ]
707
- printf_arg_parts = [
708
- '"{}"'.format(v) for k, f, v in json_fmt_parts
709
- ]
710
- printf_body = r"'{" + ", ".join(printf_body_parts) + r"}\n'"
711
- printf_args = ' '.join(printf_arg_parts)
712
- redirect_part = '> ' + str(fpath)
713
- printf_part = 'printf ' + printf_body + ' \\\n ' + printf_args
714
- dump_code = printf_part + ' \\\n ' + redirect_part
715
- return dump_code
695
+ if isinstance(text, (list, tuple)):
696
+ return indent('\n'.join(text), prefix)
697
+ else:
698
+ return prefix + text.replace('\n', '\n' + prefix)
699
+
700
+
701
+ def _check_bash_text_for_syntax_errors(bash_text):
702
+ import tempfile
703
+ tmpdir = tempfile.TemporaryDirectory()
704
+ with tmpdir:
705
+ dpath = ub.Path(tmpdir.name)
706
+ fpath = dpath / 'job_text.sh'
707
+ fpath.write_text(bash_text)
708
+ info = ub.cmd(['bash', '-nv', fpath])
709
+ if info.returncode != 0:
710
+ print(info.stderr)
711
+ raise SyntaxError('bash syntax error')