Fix merge conflicts
Markus Gerstel
3 years ago
1 | 1 | History |
2 | 2 | ======= |
3 | 3 | |
4 | 2.2.0 (????-??-??) | |
4 | 2.3.0 (????-??-??) | |
5 | 5 | ------------------ |
6 | * Python 3.6+ only, support for Python 3.5 has been dropped | |
6 | 7 | |
7 | * Python 3.6+ only, support for Python 3.5 has been dropped | |
8 | 2.2.0 (2020-09-07) | |
9 | ------------------ | |
10 | * Calling the run() function with unnamed arguments (other than the command | |
11 | list as the first argument) is now deprecated. As a number of arguments | |
12 | will be removed in a future version the use of unnamed arguments will | |
13 | cause future confusion. `Use explicit keyword arguments instead (#62). <https://github.com/DiamondLightSource/python-procrunner/pull/62>`_ | |
14 | * `The run() function debug argument has been deprecated (#63). <https://github.com/DiamondLightSource/python-procrunner/pull/63>`_ | |
15 | This is only used to debug the NonBlockingStream* classes. Those are due | |
16 | to be replaced in a future release, so the argument will no longer serve | |
17 | a purpose. Debugging information remains available via standard logging | |
18 | mechanisms. | |
19 | * Final version supporting Python 3.5 | |
8 | 20 | |
9 | 21 | 2.1.0 (2020-09-05) |
10 | 22 | ------------------ |
11 | ||
12 | 23 | * `Deprecated array access on the return object (#60). <https://github.com/DiamondLightSource/python-procrunner/pull/60>`_ |
13 | 24 | The return object will become a subprocess.CompletedProcess in a future |
14 | 25 | release, which no longer allows array-based access. For a translation table |
15 | 26 | of array elements to attributes please see the pull request linked above. |
16 | * Add a `new parameter 'raise_timeout_exceptions' (#61). <https://github.com/DiamondLightSource/python-procrunner/pull/61>`_ | |
27 | * Add a `new parameter 'raise_timeout_exception' (#61). <https://github.com/DiamondLightSource/python-procrunner/pull/61>`_ | |
17 | 28 | When set to 'True' a subprocess.TimeoutExpired exception is raised when the |
18 | 29 | process runtime exceeds the timeout threshold. This defaults to 'False' and |
19 | 30 | will be set to 'True' in a future release. |
20 | * Final version supporting Python 3.5 | |
21 | 31 | |
22 | 32 | 2.0.0 (2020-06-24) |
23 | 33 | ------------------ |
24 | ||
25 | 34 | * Python 3.5+ only, support for Python 2.7 has been dropped |
26 | 35 | * Deprecated function alias run_process() has been removed |
27 | 36 | * Fixed a stability issue on Windows |
28 | 37 | |
29 | 38 | 1.1.0 (2019-11-04) |
30 | 39 | ------------------ |
31 | ||
32 | 40 | * Add Python 3.8 support, drop Python 3.4 support |
33 | 41 | |
34 | 42 | 1.0.2 (2019-05-20) |
35 | 43 | ------------------ |
36 | ||
37 | 44 | * Stop environment override variables leaking into the process environment |
38 | 45 | |
39 | 46 | 1.0.1 (2019-04-16) |
40 | 47 | ------------------ |
41 | ||
42 | 48 | * Minor fixes on the return object (implement equality, |
43 | 49 | mark as unhashable) |
44 | 50 | |
45 | 51 | 1.0.0 (2019-03-25) |
46 | 52 | ------------------ |
47 | ||
48 | 53 | * Support file system path objects (PEP-519) in arguments |
49 | 54 | * Change the return object to make it similar to |
50 | 55 | subprocess.CompletedProcess, introduced with Python 3.5+ |
51 | 56 | |
52 | 57 | 0.9.1 (2019-02-22) |
53 | 58 | ------------------ |
54 | ||
55 | 59 | * Have deprecation warnings point to correct code locations |
56 | 60 | |
57 | 61 | 0.9.0 (2018-12-07) |
58 | 62 | ------------------ |
59 | ||
60 | 63 | * Trap UnicodeEncodeError when printing output. Offending characters |
61 | 64 | are replaced and a warning is logged once. Hints at incorrectly set |
62 | 65 | PYTHONIOENCODING. |
63 | 66 | |
64 | 67 | 0.8.1 (2018-12-04) |
65 | 68 | ------------------ |
66 | ||
67 | 69 | * Fix a few deprecation warnings |
68 | 70 | |
69 | 71 | 0.8.0 (2018-10-09) |
70 | 72 | ------------------ |
71 | ||
72 | 73 | * Add parameter working_directory to set the working directory |
73 | 74 | of the subprocess |
74 | 75 | |
75 | 76 | 0.7.2 (2018-10-05) |
76 | 77 | ------------------ |
77 | ||
78 | 78 | * Officially support Python 3.7 |
79 | 79 | |
80 | 80 | 0.7.1 (2018-09-03) |
81 | 81 | ------------------ |
82 | ||
83 | 82 | * Accept environment variable overriding with numeric values. |
84 | 83 | |
85 | 84 | 0.7.0 (2018-05-13) |
86 | 85 | ------------------ |
87 | ||
88 | 86 | * Unicode fixes. Fix crash on invalid UTF-8 input. |
89 | 87 | * Clarify that stdout/stderr values are returned as bytestrings. |
90 | 88 | * Callbacks receive the data decoded as UTF-8 unicode strings |
94 | 92 | |
95 | 93 | 0.6.1 (2018-05-02) |
96 | 94 | ------------------ |
97 | ||
98 | 95 | * Maintenance release to add some tests for executable resolution. |
99 | 96 | |
100 | 97 | 0.6.0 (2018-05-02) |
101 | 98 | ------------------ |
102 | ||
103 | 99 | * Fix Win32 API executable resolution for commands containing a dot ('.') in |
104 | 100 | addition to a file extension (say '.bat'). |
105 | 101 | |
106 | 102 | 0.5.1 (2018-04-27) |
107 | 103 | ------------------ |
108 | ||
109 | 104 | * Fix Win32API dependency installation on Windows. |
110 | 105 | |
111 | 106 | 0.5.0 (2018-04-26) |
112 | 107 | ------------------ |
113 | ||
114 | 108 | * New keyword 'win32resolve' which only takes effect on Windows and is enabled |
115 | 109 | by default. This causes procrunner to call the Win32 API FindExecutable() |
116 | 110 | function to try and lookup non-.exe files with the corresponding name. This |
119 | 113 | |
120 | 114 | 0.4.0 (2018-04-23) |
121 | 115 | ------------------ |
122 | ||
123 | 116 | * Python 2.7 support on Windows. Python3 not yet supported on Windows. |
124 | 117 | |
125 | 118 | 0.3.0 (2018-04-17) |
126 | 119 | ------------------ |
127 | ||
128 | 120 | * run_process() renamed to run() |
129 | 121 | * Python3 compatibility fixes |
130 | 122 | |
131 | 123 | 0.2.0 (2018-03-12) |
132 | 124 | ------------------ |
133 | ||
134 | 125 | * Procrunner is now Python3 3.3-3.6 compatible. |
135 | 126 | |
136 | 127 | 0.1.0 (2018-03-12) |
137 | 128 | ------------------ |
138 | ||
139 | 129 | * First release on PyPI. |
47 | 47 | |
48 | 48 | # General information about the project. |
49 | 49 | project = "ProcRunner" |
50 | copyright = "2020, Markus Gerstel" | |
51 | author = "Markus Gerstel" | |
50 | copyright = "2020, Diamond Light Source" | |
51 | author = "Diamond Light Source - Scientific Software" | |
52 | 52 | |
53 | 53 | # The version info for the project you're documenting, acts as replacement |
54 | 54 | # for |version| and |release|, also used in various other places throughout |
127 | 127 | ( |
128 | 128 | master_doc, |
129 | 129 | "procrunner.tex", |
130 | "ProcRunner Documentation", | |
131 | "Markus Gerstel", | |
130 | "procrunner Documentation", | |
131 | "Diamond Light Source - Scientific Software", | |
132 | 132 | "manual", |
133 | 133 | ) |
134 | 134 | ] |
138 | 138 | |
139 | 139 | # One entry per manual page. List of tuples |
140 | 140 | # (source start file, name, description, authors, manual section). |
141 | man_pages = [(master_doc, "procrunner", "ProcRunner Documentation", [author], 1)] | |
141 | man_pages = [(master_doc, "procrunner", "procrunner Documentation", [author], 1)] | |
142 | 142 | |
143 | 143 | |
144 | 144 | # -- Options for Texinfo output ---------------------------------------- |
150 | 150 | ( |
151 | 151 | master_doc, |
152 | 152 | "procrunner", |
153 | "ProcRunner Documentation", | |
153 | "procrunner Documentation", | |
154 | 154 | author, |
155 | 155 | "procrunner", |
156 | "One line description of project.", | |
156 | "Versatile utility function to run external processes", | |
157 | 157 | "Miscellaneous", |
158 | 158 | ) |
159 | 159 | ] |
0 | 0 | import codecs |
1 | import functools | |
1 | 2 | import io |
2 | 3 | import logging |
3 | 4 | import os |
51 | 52 | |
52 | 53 | __author__ = """Markus Gerstel""" |
53 | 54 | __email__ = "scientificsoftware@diamond.ac.uk" |
54 | __version__ = "2.1.0" | |
55 | __version__ = "2.2.0" | |
55 | 56 | |
56 | 57 | logger = logging.getLogger("procrunner") |
57 | 58 | logger.addHandler(logging.NullHandler()) |
208 | 209 | self._buffer = data |
209 | 210 | self._buffer_len = len(data) |
210 | 211 | self._buffer_pos = 0 |
211 | self._debug = debug | |
212 | 212 | self._max_block_len = 4096 |
213 | 213 | self._stream = stream |
214 | 214 | self._terminated = False |
412 | 412 | self._extras.update(dictionary) |
413 | 413 | |
414 | 414 | |
415 | def _deprecate_argument_calling(f): | |
416 | @functools.wraps(f) | |
417 | def wrapper(*args, **kwargs): | |
418 | if len(args) > 1: | |
419 | warnings.warn( | |
420 | "Calling procrunner.run() with unnamed arguments (apart from " | |
421 | "the command) is deprecated. Use keyword arguments instead.", | |
422 | DeprecationWarning, | |
423 | stacklevel=2, | |
424 | ) | |
425 | return f(*args, **kwargs) | |
426 | ||
427 | return wrapper | |
428 | ||
429 | ||
430 | @_deprecate_argument_calling | |
415 | 431 | def run( |
416 | 432 | command, |
417 | 433 | timeout=None, |
418 | debug=False, | |
434 | debug=None, | |
419 | 435 | stdin=None, |
420 | 436 | print_stdout=True, |
421 | 437 | print_stderr=True, |
435 | 451 | |
436 | 452 | :param array command: Command line to be run, specified as array. |
437 | 453 | :param timeout: Terminate program execution after this many seconds. |
438 | :param boolean debug: Enable further debug messages. | |
454 | :param boolean debug: Enable further debug messages. (deprecated) | |
439 | 455 | :param stdin: Optional bytestring that is passed to command stdin. |
440 | 456 | :param boolean print_stdout: Pass stdout through to sys.stdout. |
441 | 457 | :param boolean print_stderr: Pass stderr through to sys.stderr. |
469 | 485 | else: |
470 | 486 | assert sys.platform != "win32", "stdin argument not supported on Windows" |
471 | 487 | stdin_pipe = subprocess.PIPE |
488 | if debug is not None: | |
489 | warnings.warn( | |
490 | "Use of the debug parameter is deprecated", DeprecationWarning, stacklevel=3 | |
491 | ) | |
472 | 492 | |
473 | 493 | start_time = timeit.default_timer() |
474 | 494 | if timeout is not None: |
477 | 497 | warnings.warn( |
478 | 498 | "Using procrunner with timeout and without raise_timeout_exception set is deprecated", |
479 | 499 | DeprecationWarning, |
480 | stacklevel=2, | |
500 | stacklevel=3, | |
481 | 501 | ) |
482 | 502 | |
483 | 503 | if environment is not None: |
0 | 0 | bump2version==1.0.0 |
1 | coverage==5.2.1 | |
1 | coverage==5.3 | |
2 | 2 | flake8==3.8.3 |
3 | 3 | mock==3.0.5 |
4 | pip==20.2.2 | |
5 | pytest==6.0.1 | |
4 | pip==20.2.3 | |
5 | pytest==6.1.0 | |
6 | 6 | Sphinx==3.2.1 |
7 | tox==3.19.0 | |
7 | tox==3.20.0 | |
8 | 8 | twine==1.15.0 |
9 | 9 | wheel==0.35.1 |
0 | 0 | [bumpversion] |
1 | current_version = 2.1.0 | |
1 | current_version = 2.2.0 | |
2 | 2 | commit = True |
3 | 3 | tag = True |
4 | 4 |
43 | 43 | test_suite="tests", |
44 | 44 | tests_require=test_requirements, |
45 | 45 | url="https://github.com/DiamondLightSource/python-procrunner", |
46 | version="2.1.0", | |
46 | version="2.2.0", | |
47 | 47 | zip_safe=False, |
48 | 48 | ) |
21 | 21 | |
22 | 22 | with pytest.raises(RuntimeError): |
23 | 23 | with pytest.warns(DeprecationWarning, match="timeout"): |
24 | procrunner.run(task, -1, False) | |
24 | procrunner.run(task, timeout=-1, debug=False) | |
25 | 25 | |
26 | 26 | assert mock_subprocess.Popen.called |
27 | 27 | assert mock_process.terminate.called |
42 | 42 | task = ["___"] |
43 | 43 | |
44 | 44 | with pytest.raises(RuntimeError): |
45 | procrunner.run(task, -1, False, raise_timeout_exception=True) | |
45 | procrunner.run(task, timeout=-1, raise_timeout_exception=True) | |
46 | 46 | |
47 | 47 | assert mock_subprocess.Popen.called |
48 | 48 | assert mock_process.terminate.called |
83 | 83 | |
84 | 84 | actual = procrunner.run( |
85 | 85 | command, |
86 | 0.5, | |
87 | False, | |
86 | timeout=0.5, | |
88 | 87 | callback_stdout=mock.sentinel.callback_stdout, |
89 | 88 | callback_stderr=mock.sentinel.callback_stderr, |
90 | 89 | working_directory=pathlib.Path("somecwd"), |
102 | 101 | mock.call( |
103 | 102 | stream_stdout, |
104 | 103 | output=mock.ANY, |
105 | debug=mock.ANY, | |
104 | debug=None, | |
106 | 105 | notify=mock.ANY, |
107 | 106 | callback=mock.sentinel.callback_stdout, |
108 | 107 | ), |
109 | 108 | mock.call( |
110 | 109 | stream_stderr, |
111 | 110 | output=mock.ANY, |
112 | debug=mock.ANY, | |
111 | debug=None, | |
113 | 112 | notify=mock.ANY, |
114 | 113 | callback=mock.sentinel.callback_stderr, |
115 | 114 | ), |
131 | 130 | def test_default_process_environment_is_parent_environment(mock_subprocess): |
132 | 131 | mock_subprocess.Popen.side_effect = NotImplementedError() # cut calls short |
133 | 132 | with pytest.raises(NotImplementedError): |
134 | procrunner.run([mock.Mock()], -1, False, raise_timeout_exception=True) | |
133 | procrunner.run([mock.Mock()], timeout=-1, raise_timeout_exception=True) | |
135 | 134 | assert mock_subprocess.Popen.call_args[1]["env"] == os.environ |
135 | ||
136 | ||
137 | @mock.patch("procrunner.subprocess") | |
138 | def test_using_debug_parameter_raises_warning(mock_subprocess): | |
139 | mock_subprocess.Popen.side_effect = NotImplementedError() # cut calls short | |
140 | with pytest.warns(DeprecationWarning, match="debug"): | |
141 | with pytest.raises(NotImplementedError): | |
142 | procrunner.run([mock.Mock()], debug=True) | |
143 | with pytest.warns(DeprecationWarning, match="debug"): | |
144 | with pytest.raises(NotImplementedError): | |
145 | procrunner.run([mock.Mock()], debug=False) | |
136 | 146 | |
137 | 147 | |
138 | 148 | @mock.patch("procrunner.subprocess") |
143 | 153 | with pytest.raises(NotImplementedError): |
144 | 154 | procrunner.run( |
145 | 155 | [mock.Mock()], |
146 | -1, | |
147 | False, | |
156 | timeout=-1, | |
148 | 157 | environment=copy.copy(mock_env), |
149 | 158 | raise_timeout_exception=True, |
150 | 159 | ) |
160 | 169 | with pytest.raises(NotImplementedError): |
161 | 170 | procrunner.run( |
162 | 171 | [mock.Mock()], |
163 | -1, | |
164 | False, | |
172 | timeout=-1, | |
165 | 173 | environment=copy.copy(mock_env1), |
166 | 174 | environment_override=copy.copy(mock_env2), |
167 | 175 | raise_timeout_exception=True, |
178 | 186 | with pytest.raises(NotImplementedError): |
179 | 187 | procrunner.run( |
180 | 188 | [mock.Mock()], |
181 | -1, | |
182 | False, | |
189 | timeout=-1, | |
183 | 190 | environment_override=copy.copy(mock_env2), |
184 | 191 | raise_timeout_exception=True, |
185 | 192 | ) |
207 | 214 | with pytest.raises(NotImplementedError): |
208 | 215 | procrunner.run( |
209 | 216 | [mock.Mock()], |
210 | -1, | |
211 | False, | |
217 | timeout=-1, | |
212 | 218 | environment_override={ |
213 | 219 | random_environment_variable: "X" + random_environment_value |
214 | 220 | }, |
116 | 116 | assert te.value.stderr == b"" |
117 | 117 | assert te.value.timeout == 0.1 |
118 | 118 | assert te.value.cmd == command |
119 | ||
120 | ||
121 | def test_argument_deprecation(tmp_path): | |
122 | with pytest.warns(DeprecationWarning, match="keyword arguments"): | |
123 | result = procrunner.run( | |
124 | [sys.executable, "-V"], | |
125 | None, | |
126 | working_directory=tmp_path, | |
127 | ) | |
128 | assert not result.returncode | |
129 | assert result.stderr or result.stdout |