Skip to content

Commit

Permalink
refactor and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhl8 committed Dec 12, 2023
1 parent 3d1218a commit 511856d
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 200 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
## Install

```
pip install -U python-shellrunner
pip install -U shellrunner
```

## Usage
Expand Down Expand Up @@ -83,7 +83,7 @@ Because writing anything remotely complicated in bash kinda sucks :)

One of the primary advantages of ShellRunner's approach is that you can seamlessly swap between the shell and Python. Some things are just easier to do in a shell (e.g. pipelines) and a lot of things are easier/better in Python (control flow, error handling, etc).

Also, users of [fish](https://github.com/fish-shell/fish-shell) might know that it [does not offer a way to easily exit a script if a command fails](https://github.com/fish-shell/fish-shell/issues/510). ShellRunner adds `set -e` and `pipefail` like functionality to any shell. Leverage the improved syntax of your preferred shell and the (optional) saftey of bash.
Also, users of [fish](https://github.com/fish-shell/fish-shell) might know that it [does not offer a way to easily exit a script if a command fails](https://github.com/fish-shell/fish-shell/issues/510). ShellRunner adds `set -e` and `pipefail` like functionality to any shell. Leverage the improved syntax of your preferred shell and the (optional) safety of bash.

### Similar Projects

Expand All @@ -95,7 +95,7 @@ ShellRunner is very similar to zxpy and shellpy but aims to be more simple in it

## Advanced Usage

A note on compatability: ShellRunner should work with on any POSIX-compliant system (and shell). No Windows support at this time.
A note on compatibility: ShellRunner should work with on any POSIX-compliant system (and shell). No Windows support at this time.

Confirmed compatible with `sh` (dash), `bash`, `zsh`, and `fish`.

Expand All @@ -104,7 +104,7 @@ Commands are automatically run with the shell that invoked your python script (t
```python
# my_script.py
X("echo hello | string match hello")
# Works if my_script.py is executed under fish. Will obviously fail if using bash.
# Works if my_script.py is executed under fish (string match). Will obviously fail if using bash.
```

### Shell Command Result
Expand Down Expand Up @@ -186,7 +186,7 @@ X([
There are a few keyword arguments you can provide to adjust the behavior of `X`:

```python
X("command", shell="bash", check=True, show_output=True, show_commands=True)
X("command", shell="bash", check=True, show_output=True, show_command=True)
```

`shell: str` (Default: the invoking shell) - Shell that will be used to execute the commands. Can be a path or simply the name (e.g. "/bin/bash", "bash").
Expand All @@ -195,7 +195,7 @@ X("command", shell="bash", check=True, show_output=True, show_commands=True)

`show_output: bool` (Default: True) - If True, command output will be printed.

`show_commands: bool` (Default: True) - If True, the current command will be printed before execution.
`show_command: bool` (Default: True) - If True, the current command will be printed before execution.

### Output

Expand All @@ -212,7 +212,7 @@ shellrunner: echo hello world
hello world
```

To hide the `shellrunner:` lines, set `show_commands=False`.
To hide the `shellrunner:` lines, set `show_command=False`.

To hide actual command output, set `show_output=False`.

Expand All @@ -226,7 +226,7 @@ Each option also has a corresponding environment variable to allow you to set th

`show_output` = `SHELLRUNNER_SHOW_OUTPUT`

`show_commands` = `SHELLRUNNER_SHOW_COMMANDS`
`show_command` = `SHELLRUNNER_SHOW_COMMAND`

Environment variables are evaluated on each call of `X`, so you could also do something like this:

Expand All @@ -253,5 +253,5 @@ packages = packages.splitlines()

for package in packages:
print(f"=== {package} ===")
X(f"pip show {package} | grep -E 'Requires|Required-by'", show_commands=False)
X(f"pip show {package} | grep -E 'Requires|Required-by'", show_command=False)
```
45 changes: 24 additions & 21 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[project]
name = "python-shellrunner"
name = "shellrunner"
version = "0.3.5"
description = "Write safe shell scripts in Python."
authors = [
{name = "adamhl8", email = "adamhl@pm.me"},
]
dependencies = [
"psutil>=5.9.5",
"psutil>=5.9.6",
]
requires-python = ">=3.10"
readme = "README.md"
Expand All @@ -24,43 +24,46 @@ classifiers = [
keywords = ["shell", "scripting", "bash", "zsh", "fish"]

[project.urls]
"Homepage" = "https://github.com/adamhl8/python-shellrunner"
"Source" = "https://github.com/adamhl8/python-shellrunner"
"Bug Tracker" = "https://github.com/adamhl8/python-shellrunner/issues"
"Homepage" = "https://github.com/adamhl8/shellrunner"
"Source" = "https://github.com/adamhl8/shellrunner"
"Bug Tracker" = "https://github.com/adamhl8/shellrunner/issues"

[tool.rye]
managed = true
dev-dependencies = [
"black>=23.9.1",
"ruff>=0.0.292",
"pytest>=7.4.2",
"ruff>=0.1.7",
"pytest>=7.4.3",
"pyroma>=4.2",
"types-psutil>=5.9.5.16",
"types-psutil>=5.9.5.17",
]

[tool.rye.scripts]
test = "pytest shellrunner"
"lint:pyright" = "pyright ."
"lint:ruff" = "ruff check ."
"lint:black" = "black --check ."
"lint:ruff:format" = "ruff format --check ."
"lint:pyroma" = "pyroma -n 10 ."
lint = { chain = ["lint:pyright", "lint:ruff", "lint:black", "lint:pyroma"] }
format = "black ."
lint = { chain = ["lint:pyright", "lint:ruff", "lint:ruff:format", "lint:pyroma"] }
format = "ruff format ."

[tool.pyright]
typeCheckingMode = "strict"

[tool.black]
line-length = 120
target-version = ["py312"]

[tool.ruff]
line-length = 120
target-version = "py312"
select = ["E", "F", "W", "I", "N", "UP", "BLE", "FBT", "B", "A", "COM", "C4", "T10", "EM", "EXE", "ISC", "ICN", "G",
"INP", "PIE", "PYI", "PT", "Q", "RSE", "RET", "SLF", "SIM", "TID", "TCH", "ARG", "PTH", "ERA", "PL", "PLC", "PLE",
"PLR", "PLW", "TRY", "RUF"]
ignore = ["E501", "PLR0915", "PLR0912", "PLR0913", "PLC1901"]
output-format = "grouped"

[tool.ruff.lint]
select = ["ALL"]
ignore = [
"ANN101",
# ruff conflicts
"D203",
"D213",
# ruff format conflicts
"COM812",
"ISC001",
]

[build-system]
requires = ["hatchling"]
Expand Down
27 changes: 11 additions & 16 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,23 @@
# all-features: false

-e file:.
black==23.9.1
build==1.0.3
certifi==2023.7.22
charset-normalizer==3.3.0
click==8.1.7
certifi==2023.11.17
charset-normalizer==3.3.2
docutils==0.20.1
idna==3.4
idna==3.6
iniconfig==2.0.0
mypy-extensions==1.0.0
packaging==23.2
pathspec==0.11.2
platformdirs==3.11.0
pluggy==1.3.0
psutil==5.9.5
pygments==2.16.1
psutil==5.9.6
pygments==2.17.2
pyproject-hooks==1.0.0
pyroma==4.2
pytest==7.4.2
pytest==7.4.3
requests==2.31.0
ruff==0.0.292
trove-classifiers==2023.9.19
types-psutil==5.9.5.16
urllib3==2.0.6
ruff==0.1.7
trove-classifiers==2023.11.29
types-psutil==5.9.5.17
urllib3==2.1.0
# The following packages are considered to be unsafe in a requirements file:
setuptools==68.2.2
setuptools==69.0.2
2 changes: 1 addition & 1 deletion requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
# all-features: false

-e file:.
psutil==5.9.5
psutil==5.9.6
6 changes: 4 additions & 2 deletions shellrunner/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""shellrunner package."""

from ._exceptions import ShellCommandError, ShellCommandResult, ShellResolutionError, ShellRunnerError
from ._shellrunner import run as X # noqa: N812

__all__ = [
"ShellCommandError",
"X",
"ShellCommandResult",
"ShellCommandError",
"ShellResolutionError",
"ShellRunnerError",
"X",
]
2 changes: 1 addition & 1 deletion shellrunner/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ShellRunnerError(RuntimeError):


class ShellCommandError(ShellRunnerError):
def __init__(self, message: str, result: ShellCommandResult):
def __init__(self, message: str, result: ShellCommandResult) -> None:
super().__init__(message)
self.out = result.out
self.status = result.status
Expand Down
Loading

0 comments on commit 511856d

Please sign in to comment.