Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.0.4 #4

Open
wants to merge 35 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
33fb15f
instld mention
pomponchik Jul 23, 2024
2ecfdad
autosplit of commands by default
pomponchik Aug 13, 2024
3e0b119
a new version of cantok
pomponchik Aug 13, 2024
c796748
a new version of emptylog
pomponchik Aug 13, 2024
945155f
readme fixed
pomponchik Aug 13, 2024
8d00128
no extra f-strings
pomponchik Aug 13, 2024
11731ac
no extra imports in readme examples
pomponchik Aug 13, 2024
5a7831b
oslex instead of mslex
pomponchik Aug 13, 2024
4b2177f
mypy ignore comment
pomponchik Aug 13, 2024
061651c
new test
pomponchik Aug 13, 2024
d723f12
an old version of oslex
pomponchik Aug 13, 2024
1fb7e91
no cover comments
pomponchik Aug 13, 2024
e3bef6f
no version tag
pomponchik Aug 13, 2024
079d073
added a new type of exceptions - WrongCommandError
pomponchik Aug 13, 2024
3529a80
a new test
pomponchik Aug 13, 2024
b3f8a8e
skip the wrong command test for windows
pomponchik Aug 13, 2024
ca9936a
no cover comments for not-windows specific code
pomponchik Aug 13, 2024
90b2a1c
TODO comments about mslex
pomponchik Aug 13, 2024
c3c09ee
use shlex join for command instead of str join
pomponchik Aug 13, 2024
50b0583
no shlex join
pomponchik Aug 14, 2024
b197d50
no extra todos
pomponchik Aug 14, 2024
27ba315
new test
pomponchik Aug 15, 2024
5630928
python command instead of python3 in a test
pomponchik Aug 15, 2024
ea36e49
readme
pomponchik Aug 15, 2024
12487c6
hello world test
pomponchik Aug 15, 2024
0a5a6b7
readme
pomponchik Aug 15, 2024
17b9725
readme
pomponchik Aug 15, 2024
dda9ace
readme
pomponchik Aug 15, 2024
f7e3d5b
different tests for windows and not windows
pomponchik Aug 16, 2024
06dc62c
readme
pomponchik Aug 16, 2024
05f58b8
new version of the cantok
pomponchik Aug 16, 2024
615ebdb
fixed a windows test
pomponchik Aug 18, 2024
07b65de
fixed a windows test
pomponchik Aug 18, 2024
7b77c4c
print in a windows test
pomponchik Aug 18, 2024
09b859d
print in a windows test
pomponchik Aug 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 59 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Here is a small wrapper around the [subprocesses](https://docs.python.org/3/libr

- [**Quick start**](#quick-start)
- [**Run subprocess and look at the result**](#run-subprocess-and-look-at-the-result)
- [**Command parsing**](#command-parsing)
- [**Output**](#output)
- [**Logging**](#logging)
- [**Exceptions**](#exceptions)
Expand All @@ -45,10 +46,12 @@ And use:
```python
import suby

suby('python', '-c', 'print("hello, world!")')
suby('python -c "print(\'hello, world!\')"')
# > hello, world!
```

You can also quickly try out this and other packages without having to install using [instld](https://github.com/pomponchik/instld).


## Run subprocess and look at the result

Expand All @@ -64,10 +67,10 @@ If you use static type checking and get an error that it is impossible to call t
from suby import suby
```

Let's try to call `suby`. You can use strings or [`pathlib.Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path) objects as positional arguments, but now we call it with only simple strings:
Let's try to call `suby`:

```python
result = suby('python', '-c', 'print("hello, world!")')
result = suby('python -c "print(\'hello, world!\')"')
print(result)
# > SubprocessResult(id='e9f2d29acb4011ee8957320319d7541c', stdout='hello, world!\n', stderr='', returncode=0, killed_by_token=False)
```
Expand All @@ -81,28 +84,68 @@ We can see that it returns an object of the `SubprocessResult` class. It contain
- **killed_by_token** - a boolean flag indicating whether the subprocess was killed due to [token](https://cantok.readthedocs.io/en/latest/the_pattern/) cancellation.


## Command parsing

Each command you use to call `suby` is passed to a special [system call](https://en.wikipedia.org/wiki/System_call). Which one exactly depends on the operating system you are using. But regardless of the specific operating system, this system call usually accepts not one whole line of input, but a list of substrings. This means that somewhere under the hood, `suby` should cut the string you passed. The rules for this cutting are usually also different for different operating systems and depend on the specific shell you prefer. `suby` uses the [CMD](https://en.wikipedia.org/wiki/Cmd.exe) as a standard for [Windows](https://en.wikipedia.org/wiki/Microsoft_Windows) and [POSIX](https://en.wikipedia.org/wiki/POSIX) for POSIX-compatible systems.

In most cases, you will not notice any differences in the parsing rules. For example, the following line:

```bash
python -c "print('hello, world!')"
```

... on Windows should be escaped like here:

```python
suby('python -c "print^(\'hello,world^!\'^)"')
```

... and on other systems like here:

```python
suby('python -c "print(\'hello, world!\')"')
```

You can pass not only strings to suby, but also [`pathlib.Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path) objects:

```python
import sys
from pathlib import Path

suby(Path(sys.executable), '-c print(777)')
# This will work too.
```

If for some reason you want to disable the automatic splitting of strings into parts, pass `split=False`:

```python
suby('python', '-c', 'print(777)', split=False)
```

Of course, in this case, you will have to cut the command by yourself.


## Output

By default, the `stdout` and `stderr` of the subprocess are forwarded to the `stdout` and `stderr` of the current process. The reading from the subprocess is continuous, and the output is every time a full line is read. For continuous reading from `stderr`, a separate thread is created in the main process, so that `stdout` and `stderr` are read independently.

You can override the output functions for `stdout` and `stderr`. To do this, you need to pass as arguments `stdout_callback` and `stderr_callback`, respectively, some functions that accept a string as an argument. For example, you can color the output (the code example uses the [`termcolor`](https://github.com/termcolor/termcolor) library):

```python
import suby
from termcolor import colored

def my_new_stdout(string: str) -> None:
print(colored(string, 'red'), end='')

suby('python', '-c', 'print("hello, world!")', stdout_callback=my_new_stdout)
suby('python -c "print(\'hello, world!\')"', stdout_callback=my_new_stdout)
# > hello, world!
# You can't see it here, but believe me, if you repeat the code at home, the output in the console will be red!
```

You can also completely disable the output by passing `True` as the `catch_output` parameter:

```python
suby('python', '-c', 'print("hello, world!")', catch_output=True)
suby('python -c "print(\'hello, world!\')"', catch_output=True)
# There's nothing here.
```

Expand All @@ -115,7 +158,6 @@ By default, `suby` does not log command execution. However, you can pass a logge

```python
import logging
import suby

logging.basicConfig(
level=logging.INFO,
Expand All @@ -125,15 +167,15 @@ logging.basicConfig(
]
)

suby('python', '-c', 'pass', logger=logging.getLogger('logger_name'))
suby('python -c pass', logger=logging.getLogger('logger_name'))
# > 2024-02-22 02:15:08,155 [INFO] The beginning of the execution of the command "python -c pass".
# > 2024-02-22 02:15:08,190 [INFO] The command "python -c pass" has been successfully executed.
```

The message about the start of the command execution is always done with the `INFO` [level](https://docs.python.org/3.8/library/logging.html#logging-levels). If the command is completed successfully, the end message will also be with the `INFO` level. And if not - `ERROR`:

```python
suby('python', '-c', 'raise ValueError', logger=logging.getLogger('logger_name'), catch_exceptions=True, catch_output=True)
suby('python -c "raise ValueError"', logger=logging.getLogger('logger_name'), catch_exceptions=True, catch_output=True)
# > 2024-02-22 02:20:25,549 [INFO] The beginning of the execution of the command "python -c "raise ValueError"".
# > 2024-02-22 02:20:25,590 [ERROR] Error when executing the command "python -c "raise ValueError"".
```
Expand All @@ -150,10 +192,8 @@ By default, `suby` raises exceptions in three cases:
1. If the command you are calling ended with a return code not equal to `0`. In this case, you will see an exception `suby.RunningCommandError`:

```python
import suby

try:
suby('python', '-c', '1/0')
suby('python -c 1/0')
except suby.RunningCommandError as e:
print(e)
# > Error when executing the command "python -c 1/0".
Expand All @@ -166,7 +206,7 @@ except suby.RunningCommandError as e:
You can prevent `suby` from raising any exceptions. To do this, set the `catch_exceptions` parameter to `True`:

```python
result = suby('python', '-c', 'import time; time.sleep(10_000)', timeout=1, catch_exceptions=True)
result = suby('python -c "import time; time.sleep(10_000)"', timeout=1, catch_exceptions=True)
print(result)
# > SubprocessResult(id='c9125b90d03111ee9660320319d7541c', stdout='', stderr='', returncode=-9, killed_by_token=True)
```
Expand All @@ -175,7 +215,7 @@ Keep in mind that the full result of the subprocess call can also be found throu

```python
try:
suby('python', '-c', 'import time; time.sleep(10_000)', timeout=1)
suby('python -c "import time; time.sleep(10_000)"', timeout=1)
except suby.TimeoutCancellationError as e:
print(e.result)
# > SubprocessResult(id='a80dc26cd03211eea347320319d7541c', stdout='', stderr='', returncode=-9, killed_by_token=True)
Expand All @@ -192,19 +232,18 @@ So, you can pass your cancellation tokens to `suby`. By default, canceling a tok

```python
from random import randint
import suby
from cantok import ConditionToken

token = ConditionToken(lambda: randint(1, 1000) == 7) # This token will be cancelled when a random unlikely event occurs.
suby('python', '-c', 'import time; time.sleep(10_000)', token=token)
suby('python -c "import time; time.sleep(10_000)"', token=token)
# > cantok.errors.ConditionCancellationError: The cancellation condition was satisfied.
```

However, if you pass the `catch_exceptions=True` argument, the exception [will not be raised](#exceptions). Instead, you will get the [usual result](#run-subprocess-and-look-at-the-result) of calling `suby` with the `killed_by_token=True` flag:

```python
token = ConditionToken(lambda: randint(1, 1000) == 7)
print(suby('python', '-c', 'import time; time.sleep(10_000)', token=token, catch_exceptions=True))
print(suby('python -c "import time; time.sleep(10_000)"', token=token, catch_exceptions=True))
# > SubprocessResult(id='e92ccd54d24b11ee8376320319d7541c', stdout='', stderr='', returncode=-9, killed_by_token=True)
```

Expand All @@ -215,9 +254,7 @@ print(suby('python', '-c', 'import time; time.sleep(10_000)', token=token, catch
You can set a timeout for `suby`. It must be an integer greater than zero, which indicates the number of seconds that the subprocess can continue to run. If the timeout expires before the subprocess completes, an exception will be raised:

```python
import suby

suby('python', '-c', 'import time; time.sleep(10_000)', timeout=1)
suby('python -c "import time; time.sleep(10_000)"', timeout=1)
# > cantok.errors.TimeoutCancellationError: The timeout of 1 seconds has expired.
```

Expand All @@ -227,7 +264,7 @@ The exception corresponding to this token was be reimported to `suby`:

```python
try:
suby('python', '-c', 'import time; time.sleep(10_000)', timeout=1)
suby('python -c "import time; time.sleep(10_000)"', timeout=1)
except suby.TimeoutCancellationError as e: # As you can see, TimeoutCancellationError is available in the suby module.
print(e)
# > The timeout of 1 seconds has expired.
Expand All @@ -236,6 +273,6 @@ except suby.TimeoutCancellationError as e: # As you can see, TimeoutCancellatio
Just as with [regular cancellation tokens](#working-with-cancellation-tokens), you can prevent exceptions from being raised using the `catch_exceptions=True` argument:

```python
print(suby('python', '-c', 'import time; time.sleep(10_000)', timeout=1, catch_exceptions=True))
print(suby('python -c "import time; time.sleep(10_000)"', timeout=1, catch_exceptions=True))
# > SubprocessResult(id='ea88c518d25011eeb25e320319d7541c', stdout='', stderr='', returncode=-9, killed_by_token=True)
```
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ build-backend = "setuptools.build_meta"

[project]
name = "suby"
version = "0.0.3"
version = "0.0.4"
authors = [
{ name="Evgeniy Blinov", email="zheni-b@yandex.ru" },
]
description = 'Slightly simplified subprocesses'
readme = "README.md"
requires-python = ">=3.7"
dependencies = [
'emptylog>=0.0.7',
'cantok>=0.0.24',
'emptylog>=0.0.8',
'cantok>=0.0.30',
'oslex>=0.1.3 ; platform_system == "Windows"',
]
classifiers = [
"Operating System :: OS Independent",
Expand Down
2 changes: 1 addition & 1 deletion suby/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys

from suby.proxy_module import ProxyModule as ProxyModule
from suby.errors import RunningCommandError as RunningCommandError # noqa: F401
from suby.errors import RunningCommandError as RunningCommandError, WrongCommandError as WrongCommandError # noqa: F401

from cantok import TimeoutCancellationError as TimeoutCancellationError # noqa: F401

Expand Down
3 changes: 3 additions & 0 deletions suby/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ class RunningCommandError(Exception):
def __init__(self, message: str, subprocess_result: SubprocessResult) -> None:
self.result = subprocess_result
super().__init__(message)

class WrongCommandError(Exception):
pass
22 changes: 18 additions & 4 deletions suby/proxy_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
from emptylog import EmptyLogger, LoggerProtocol
from cantok import AbstractToken, TimeoutToken, DefaultToken, CancellationError

from suby.errors import RunningCommandError
try:
from oslex import split as shlex_split # type: ignore[import, unused-ignore]
except ImportError: # pragma: no cover
from shlex import split as shlex_split # pragma: no cover

from suby.errors import RunningCommandError, WrongCommandError
from suby.subprocess_result import SubprocessResult
from suby.callbacks import stdout_with_flush, stderr_with_flush

Expand All @@ -24,6 +29,7 @@ def __call__(
stdout_callback: Callable[[str], Any] = stdout_with_flush,
stderr_callback: Callable[[str], Any] = stderr_with_flush,
timeout: Optional[Union[int, float]] = None,
split: bool = True,
token: AbstractToken = DefaultToken(),
) -> SubprocessResult:
"""
Expand All @@ -34,7 +40,8 @@ def __call__(
elif timeout is not None:
token += TimeoutToken(timeout)

converted_arguments = self.convert_arguments(arguments)
converted_arguments = self.convert_arguments(arguments, split)

arguments_string_representation = ' '.join([argument if ' ' not in argument else f'"{argument}"' for argument in converted_arguments])

stdout_buffer: List[str] = []
Expand Down Expand Up @@ -84,14 +91,21 @@ def __call__(
return result

@staticmethod
def convert_arguments(arguments: Tuple[Union[str, Path], ...]) -> List[str]:
def convert_arguments(arguments: Tuple[Union[str, Path], ...], split: bool) -> List[str]:
converted_arguments = []

for argument in arguments:
if isinstance(argument, Path):
converted_arguments.append(str(argument))
elif isinstance(argument, str):
converted_arguments.append(argument)
if split:
try:
for sub_argument in shlex_split(argument):
converted_arguments.append(sub_argument)
except Exception as e: # pragma: no cover
raise WrongCommandError(f'The expression "{argument}" cannot be parsed.') from e # pragma: no cover
else:
converted_arguments.append(argument)
else:
raise TypeError(f'Only strings and pathlib.Path objects can be positional arguments when calling the suby function. You passed "{argument}" ({type(argument).__name__}).')

Expand Down
1 change: 1 addition & 0 deletions tests/documentation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

46 changes: 46 additions & 0 deletions tests/documentation/test_quick_start.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from io import StringIO
import platform
from contextlib import redirect_stdout, redirect_stderr

import suby
import pytest


@pytest.mark.skipif(platform.system() == 'Windows', reason='Windows and not windows have different rules of escaping characters.')
def test_run_hello_world_not_windows():
stderr_buffer = StringIO()
stdout_buffer = StringIO()

with redirect_stdout(stdout_buffer), redirect_stderr(stderr_buffer):
result = suby('python -c "print(\'hello, world!\')"')

assert stderr_buffer.getvalue() == ''
assert stdout_buffer.getvalue() == 'hello, world!\n'

assert result.stdout == 'hello, world!\n'
assert result.stderr == ''
assert result.returncode == 0
assert not result.killed_by_token


@pytest.mark.skipif(platform.system() != 'Windows', reason='Windows and not windows have different rules of escaping characters.')
def test_run_hello_world_windows():
stderr_buffer = StringIO()
stdout_buffer = StringIO()

with redirect_stdout(stdout_buffer), redirect_stderr(stderr_buffer):
#result = suby('python -c "print^(\'hello, world^!\'^)"', catch_exceptions=True)
result = suby('python -c "print(\'hello, world!\')"', catch_exceptions=True)
print(result)

print(result)

print(stderr_buffer.getvalue())

#assert stderr_buffer.getvalue() == ''
assert stdout_buffer.getvalue() == 'hello, world!\n'

assert result.stdout == 'hello, world!\n'
assert result.stderr == ''
assert result.returncode == 0
assert not result.killed_by_token
Loading
Loading