Skip to content

Commit

Permalink
Added exit_code method & suppress_std_err option (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored Jul 20, 2020
1 parent e9b8519 commit 1805234
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v0.2.2

- `exit_code` is now a property, `status_code` is still supported but will be deprecated
- `suppress_std_err` keyworded argument is added to prevent from always printing stderr

## v0.2.1

- Custom enviroment variables can be passed to commands
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
upload:
twine check dist/*
twine upload dist/*
rm -rf dist/*
7 changes: 5 additions & 2 deletions docs/api_documentation.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
API Documentation
=================

.. function:: soldier.run(command, background=False, std_id='', sudo=None, timeout=0, kill_on_timeout=False, shell=False)
.. function:: soldier.run(command, background=False, std_id='', sudo=None, timeout=0, kill_on_timeout=False, stream=False, suppress_std_err=False, shell=False)

The main run command which executes the system process

Expand All @@ -17,6 +17,8 @@ API Documentation
:type env: dict(str, str)
:param stream: When set to true, the output of your command will be streamed. (It does not work with piped commands)
:type stream: bool
:param suppress_std_err: When set to true, the output from stderr would not be printed.
:type suppress_std_err: bool
:param timeout: The timeout for the process in seconds
:type timeout: int
:param kill_on_timeout: If set to true, your process will killed when the time is up, and if it is False, it will throw a ``soldier.ProcessTimeoutError``
Expand Down Expand Up @@ -55,7 +57,8 @@ API Documentation
**Properties**

- pid - Returns the pid of the process
- status_code - Returns the status code of the process
- exit_code - Returns the exit code of the process
- status_code - Returns the exit code of the process (To be deprecated, prefer exit_code)
- output - Returns the stdout (standard output) of the process
- error - Returns the stderr (standard error) of the process
- start_ts - Returns the start time (:class:`datetime.datetime` object) of the process
Expand Down
2 changes: 1 addition & 1 deletion docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ After successfully installing soldier, you will now be able to communicate with
>> print(current_path.output)
/home/pythonista
# Status Code
>> print(current_path.status_code)
>> print(current_path.exit_code)
0
**Run a process in background, and later terminate it**
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='soldier',
version='0.2.1',
version='0.2.2',
author='Yash Mehrotra',
author_email='[email protected]',
packages=['soldier'],
Expand Down
15 changes: 10 additions & 5 deletions soldier/soldier.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self, command, **kwargs):
"""

self._command = command
self._status_code = None
self._exit_code = None
self._background = kwargs.get('background', False)
self._process = None
self._pid = None
Expand All @@ -60,6 +60,7 @@ def __init__(self, command, **kwargs):
self._err = None
self._timeout = kwargs.get('timeout', 0)
self._kill_on_timeout = kwargs.get('kill_on_timeout', False)
self._suppress_std_err = kwargs.get('suppress_std_err', False)

self._parse()
self._validate()
Expand Down Expand Up @@ -164,10 +165,10 @@ def _set_communication_params(self, wait=False):
self._output, self._err = self._process.communicate(self._output)

# This even comes as an output for stderr
if self._err:
warnings.warn(self._err, RuntimeWarning)
if self._err and not self._suppress_std_err:
print(self._err)

self._status_code = self._process.returncode
self._exit_code = self._process.returncode

def _finish(self):
"""
Expand Down Expand Up @@ -208,7 +209,11 @@ def pid(self):

@property
def status_code(self):
return self._status_code
return self._exit_code

@property
def exit_code(self):
return self._exit_code

@property
def output(self):
Expand Down

0 comments on commit 1805234

Please sign in to comment.