flake8
-
rbtools/utils/tests/test_process.py (Diff revision 1) Show all issues
Review Request #12564 — Created Aug. 23, 2022 and submitted
Information | |
---|---|
chipx86 | |
RBTools | |
release-4.x | |
Reviewers | |
rbtools | |
Our
execute()
function, used for calling out into other processes and
capturing results, has grown out of control over the years, providing 4
sets of inputs that produce different outputs in different combinations.
An attempt to add type annotations for these combinations quickly grew
out of control, and along with causing numerous type-related regressions
in the codebase over the years, it's finally become time to put
execute()
to rest.This change introduces the successor,
run_process()
. The interface is
like a slimmed-down version ofexecute()
, allowing basic options like
the working directory and environment to be set, stderr to be redirected
to stdout, expected output encoding, and control over error handling and
logging.There is no control over typing. Instead, this always returns a
RunProcessResult
object, which stores a string version of the command
that was run, the exit code, and byte streams for the standard output
and error output.From there, a caller can access the raw bytes (via
stdout_bytes.read()
andstderr_bytes.read()
), Unicode-decoded content (viastdout.read()
andstderr.read()
), or line-based versions (via.readlines()
on
any of the streams).Results are always byte strings. Decoding to Unicode strings happens
on-the-fly viaio.TextIOWrapper
(which is what Python uses for
sys.stdout
andsys.stderr
). These wrappers are created only on
access.While these are all stream objects, they don't stream output from the
program. They always contain the full results from the program. If we
were to add streaming someday for some SCM, we could do this with a new
argument torun_process()
and new flags onRunProcessResult
without
a redesign.Like with
execute()
, all errors (non-0 exit codes) or selective errors
can be ignored. Unlikeexecute()
, the caller can still get results
when an error is raised. The exception,RunProcessError
, contains the
results as an attribute (result
), containing all the same information
that would normally be returned. Given that, the support for ignoring
errors is really just a convenience around catching an exception.This new object has full type safety, so type checkers can make sure
that consumers are processing results correctly.
run_process()
usesrun_process_exec()
to perform the actual execution
of a command. This is a wrapper aroundsubprocess.run()
that takes just
a few flags and returns a tuple of(exit_code, stdout, stderr)
. Spies
should connect to this instead ofrun_process()
, as it's as low-level
as a test would need to go, and the results from this are checked against
the parameters passed torun_process()
.Logging has been improved to provide better output, distinguishing non-0
exit codes representing failure from those with ignored errors (to help
alleviate concerns when people look at debug logs).Old Python 2 compatibility code around
subprocess
have been removed,
and new defaults (likeclose_fds
) utilized.
execute()
has been updated to wraprun_process()
. This function is
now deprecated, though still widely used in the codebase. Upcoming work
will transition over torun_process()
instead.
All unit tests pass on Python 3.7-3.11.
Summary | |
---|---|
Description | From | Last Updated |
---|---|---|
'rbtools.deprecation.RemovedInRBTools50Warning' imported but unused Column: 1 Error code: F401 |
![]() |
|
local variable 'e' is assigned to but never used Column: 5 Error code: F841 |
![]() |
|
This should be Raises: and should list the TypeError for the command arg. Should we also list Exception in here … |
![]() |
|
Wrap run_process with :py:func:. |
![]() |
|
Shouldn't these 2 come before RunProcessError? |
![]() |
|
unicode -> string |
|
|
This seems a little mangled. |
|
Updated
ProcessRunResult
's constructor to supply defaults, to help with unit testing.
Commits: |
|
|||||||||
---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 2 (+1848 -226) |
Added documentation, tests, and logging for
PermissionError
,FileNotFoundError
, and general exceptions when running a command.
Commits: |
|
|||||||||
---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 3 (+1950 -226) |
rbtools/utils/process.py (Diff revision 3) |
---|
local variable 'e' is assigned to but never used Column: 5 Error code: F841
stdout
or stderr
is None
.run_process_exec()
, which spies should connect to in order to return results.Description: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Commits: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Diff: |
Revision 4 (+2108 -226) |
rbtools/utils/process.py (Diff revision 4) |
---|
This should be
Raises:
and should list theTypeError
for thecommand
arg. Should we also listException
in here for the unexpected errors that may happen when running a command?Also need to add a
Returns:
section.
rbtools/utils/tests/test_process.py (Diff revision 4) |
---|
Shouldn't these 2 come before
RunProcessError
?
Returns
section to the run_process()
docs.Returns
to Raises
and added missing exceptions.Commits: |
|
|||||||||
---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 5 (+2132 -226) |
rbtools/utils/process.py (Diff revision 5) |
---|
It would be pretty cool if we had a decorator that allowed us to mark something as not-to-be-spied-upon.
str
instead of unicode
.Commits: |
|
|||||||||
---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 6 (+2138 -234) |