Skip to content

Commit

Permalink
Allow setting custom state directory for Hapless (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwant authored Dec 10, 2024
1 parent 6ea0533 commit 5998f81
Show file tree
Hide file tree
Showing 15 changed files with 245 additions and 284 deletions.
180 changes: 0 additions & 180 deletions CHANGELOG.md

This file was deleted.

32 changes: 16 additions & 16 deletions DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@
Install [Poetry](https://python-poetry.org/) and project's dependencies

```bash
$ poetry install
poetry install
```

Add new feature and launch tests

```bash
$ poetry run pytest -sv tests
poetry run pytest -sv tests
# or
$ make tests
make tests
```

Install git hooks for the automatic linting and code formatting with [pre-commit](https://pre-commit.com/)

```bash
$ pre-commit install
$ pre-commit run --all-files # to initialize for the first time
$ pre-commit run flake8 # run a hook individually
pre-commit install
pre-commit run --all-files # to initialize for the first time
pre-commit run flake8 # run a hook individually
```

Enable extra logging

```bash
$ export HAPLESS_DEBUG=1
export HAPLESS_DEBUG=1
```

Check coverage

```bash
$ poetry run pytest --cov=hapless --cov-report=html tests/
poetry run pytest --cov=hapless --cov-report=html tests/
# or
$ make coverage-report
make coverage-report
```

Run against multiple Python versions with [nox](https://nox.thea.codes/en/stable/index.html)
Expand All @@ -52,16 +52,16 @@ nox -s test
Bump a version with features you want to include and build a package

```bash
$ poetry version patch # patch version update
$ poetry version minor
$ poetry version major # choose one based on semver rules
$ poetry build
poetry version patch # patch version update
poetry version minor
poetry version major # choose one based on semver rules
poetry build
```

Upload package to GitHub and PyPI

```bash
$ git tag -a v0.1.2 -m "Version 0.1.2"
$ git push --tags
$ poetry publish
git tag -a v0.1.2 -m "Version 0.1.2"
git push --tags
poetry publish
```
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ See [USAGE.md](https://github.com/bmwant/hapless/blob/main/USAGE.md) for the com

See [DEVELOP.md](https://github.com/bmwant/hapless/blob/main/DEVELOP.md) to setup your local development environment and feel free to create a pull request with a new feature.

### Releases
### Releases changelog

See [CHANGELOG.md](https://github.com/bmwant/hapless/blob/main/CHANGELOG.md) for the new features included within each release.
Changelog file was removed; you can now view the changes made in each release on the [Releases](https://github.com/bmwant/hapless/releases) page.

### See also

Expand Down
29 changes: 28 additions & 1 deletion USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ hap restart [hap-alias]

➡️ Rename existing hap.

- When restart command is called, hap will stop the process and start it again. The command is rerun from the current working directory.
- Change name of the existing hap to the new one. Does not allow to have duplicate names. Hap id (integer identificator) stays the same after renaming.

```bash
hap rename [hap-alias] [new-hap-name]
Expand All @@ -137,3 +137,30 @@ hap rename 4 hap-new-name
# or by hap current name like
hap rename hap-name hap-new-name
```

### ✏️ State directory

- By default state directory is picked automatically within system's temporary directory. In case you want to store state somewhere else, you can override it by setting dedicated environment variable

```bash
export HAPLESS_DIR="/home/myuser/mystate"

hap run echo hello
```

- To check that correct directory is used you can open detailed view for the hap and check that both `stdout`/`stderr` files are stored under the desired location.

```bash
hap show -v [hap-alias]
```

Alternatively, you can set debug flag and state folder will be printed on each invocation

```bash
export HAPLESS_DEBUG=1
hap
# DEBUG:hapless:Initialized within /home/myuser/mystate dir
unset HAPLESS_DEBUG
```

> NOTE: make sure to update your shell initialization file `.profile`/`.bashrc`/`.zshrc`/etc for the change to persist between different terminal sessions. Otherwise, state will be saved in custom directory only within current shell.
30 changes: 10 additions & 20 deletions hapless/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Optional

import click
from rich.console import Console

from hapless import config
from hapless.main import Hapless
Expand All @@ -14,17 +13,17 @@
# Fallback for Python 3.7
from hapless.utils import shlex_join_backport as shlex_join

console = Console(highlight=False)
hapless = Hapless()
hapless = Hapless(hapless_dir=config.HAPLESS_DIR)
console = hapless.ui


def get_or_exit(hap_alias: str):
hap = hapless.get_hap(hap_alias)
if hap is None:
console.print(
f"{config.ICON_INFO} No such hap: {hap_alias}",
style=f"{config.COLOR_ERROR} bold",
)
console.error(f"No such hap: {hap_alias}")
sys.exit(1)
if not hap.accessible:
console.error(f"Cannot manage hap launched by another user. Owner: {hap.owner}")
sys.exit(1)
return hap

Expand Down Expand Up @@ -57,7 +56,7 @@ def _status(hap_alias: Optional[str] = None, verbose: bool = False):
hap = get_or_exit(hap_alias)
hapless.show(hap, verbose=verbose)
else:
haps = hapless.get_haps()
haps = hapless.get_haps(accessible_only=False)
hapless.stats(haps, verbose=verbose)


Expand Down Expand Up @@ -107,20 +106,14 @@ def cleanall():
def run(cmd, name, check):
hap = hapless.get_hap(name)
if hap is not None:
console.print(
f"{config.ICON_INFO} Hap with such name already exists: {hap}",
style=f"{config.COLOR_ERROR} bold",
)
console.error(f"Hap with such name already exists: {hap}")
sys.exit(1)

# NOTE: click doesn't like `required` property for `cmd` argument
# https://click.palletsprojects.com/en/latest/arguments/#variadic-arguments
cmd_escaped = shlex_join(cmd).strip()
if not cmd_escaped:
console.print(
f"{config.ICON_INFO} You have to provide a command to run",
style=f"{config.COLOR_ERROR} bold",
)
console.error("You have to provide a command to run")
sys.exit(1)
hapless.run(cmd_escaped, name=name, check=check)

Expand Down Expand Up @@ -181,10 +174,7 @@ def rename(hap_alias: str, new_name: str):
hap = get_or_exit(hap_alias)
same_name_hap = hapless.get_hap(new_name)
if same_name_hap is not None:
console.print(
f"{config.ICON_INFO} Hap with such name already exists: {same_name_hap}",
style=f"{config.COLOR_ERROR} bold",
)
console.print(f"Hap with such name already exists: {same_name_hap}")
sys.exit(1)
hapless.rename_hap(hap, new_name)

Expand Down
2 changes: 2 additions & 0 deletions hapless/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
DEBUG = bool(os.getenv("HAPLESS_DEBUG", default=""))
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))

HAPLESS_DIR = os.getenv("HAPLESS_DIR")

COLOR_MAIN = "#fdca40"
COLOR_ACCENT = "#3aaed8"
COLOR_ERROR = "#f64740"
Expand Down
Loading

0 comments on commit 5998f81

Please sign in to comment.