Compare commits

..

5 commits

4 changed files with 109 additions and 71 deletions

View file

@ -13,6 +13,22 @@
See [usage](https://github.com/daylinmorgan/viv/blob/main/docs/usage.md) for more demo gifs.
---
Python is a great choice to quickly prototype or accomplish small tasks in scripts.
However, leveraging it's vast ecosystem can be tedious for one-off or rarely used scripts.
This is were `viv` comes in handy.
`Viv` is a standalone dependency-free `venv` creator.
It is meant to be invoked in any script that has third-party dependencies,
prior to loading of any of the external modules.
These `venvs` can be identified by name or by their specification.
In any case they will be re-used across scripts (and generated on-demand, if needed).
**Importantly**, `viv` will remove your user site directory (`python -m 'import site;print(site.USER_SITE)'`),
to ensure the script isn't using anything outside the standard library and the `viv`-managed `venv`.
## Setup
### Manual (Recommended)
@ -33,35 +49,49 @@ Place this directory on the python path in your rc file.
export PYTHONPATH="$PYTHONPATH:$HOME/.viv/src"
```
Advanced users may recognize that principally,
the module just needs to be recognized at run time
and the single script at `./src/viv/viv.py` can be invoked directly for the CLI.
How you accomplish these options is ultimately up to you but the above instructions can get you started.
### Pypi (Not Recommended)
```sh
pip install viv
```
Why is this *not recommended*? Mainly, because `viv` is all about hacking your `sys.path`.
Placing it in it's own virtual environment or installing in a user site directory may complicate this endeavor.
## Usage
Then in any python script with external dependencies you can add this line,
to automate env creation and installation of dependencies.
In any python script with external dependencies you can add this line,
to automate `vivenv` creation and installation of dependencies.
```python
__import__("viv").activate("click")
```
To remove all `vivenvs`:
```sh
viv remove $(viv list -q)
```
## Standalone Function
# Standalone Viv
*Requires* `python>=3.8`
Supposing you want to increase the portability of your script while still employing `viv`.
The below function can be freely pasted at the top of your scripts and requires
no modification of your PYTHONPATH or import of additional modules.
no modification of your PYTHONPATH or import of additional modules (including downloading/installing `viv`).
It can be auto-generated with for example: `viv freeze rich --standalone`.
It can be auto-generated with for example: `viv freeze <spec> --standalone`.
The only part necessary to modify if copied verbatim from here is the call to `_viv_activate`.
The only part necessary to modify if copied verbatim from below is the call to `_viv_activate`.
output of `viv freeze rich --standalone`:
```python
# <<<<< auto-generated by daylinmorgan/viv (v.22.12a3)
# fmt: off
@ -70,9 +100,9 @@ def _viv_activate(*pkgs: str, track_exe: bool = False, name: str = "") -> None:
if not {*m(type,pkgs)}=={s}: raise ValueError(f"spec: {pkgs} is invalid") # noqa
ge,sys,P,ew=i("os").getenv,i("sys"),i("pathlib").Path,i("sys").stderr.write # noqa
(cache:=(P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs")).mkdir(parents=True,exist_ok=True) # noqa
((hash:=i("hashlib").sha256()).update((s(spec)+ # noqa
(((exe:=s(P(i("sys").executable).resolve()) if track_exe else "N/A")))).encode())) # noqa
if (env:=cache/(name if name else (_id:=hash.hexdigest()))) not in cache.glob("*/") or ge("VIV_FORCE"): # noqa
((sha256:=i("hashlib").sha256()).update((s(spec)+ # noqa
(((exe:=("N/A",s(P(i("sys").executable).resolve()))[e(track_exe)])))).encode())) # noqa
if (env:=cache/(name if name else (_id:=sha256.hexdigest()))) not in cache.glob("*/") or ge("VIV_FORCE"): # noqa
v=e(ge("VIV_VERBOSE"));ew(f"generating new vivenv -> {env.name}\n") # noqa
i("venv").EnvBuilder(with_pip=True,clear=True).create(env) # noqa
with (env/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check=true") # noqa

View file

@ -1,4 +1,9 @@
#!/usr/bin/env python3
"""
Example using the output of `viv freeze pyfiglet --standalone`.
With this function it's not necessary for
`viv` to exist anywhere on the system.
"""
# <<<<< auto-generated by daylinmorgan/viv (v.22.12a3)
# fmt: off
@ -7,9 +12,9 @@ def _viv_activate(*pkgs: str, track_exe: bool = False, name: str = "") -> None:
if not {*m(type,pkgs)}=={s}: raise ValueError(f"spec: {pkgs} is invalid") # noqa
ge,sys,P,ew=i("os").getenv,i("sys"),i("pathlib").Path,i("sys").stderr.write # noqa
(cache:=(P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs")).mkdir(parents=True,exist_ok=True) # noqa
((hash:=i("hashlib").sha256()).update((s(spec)+ # noqa
(((exe:=s(P(i("sys").executable).resolve()) if track_exe else "N/A")))).encode())) # noqa
if (env:=cache/(name if name else (_id:=hash.hexdigest()))) not in cache.glob("*/") or ge("VIV_FORCE"): # noqa
((sha256:=i("hashlib").sha256()).update((s(spec)+ # noqa
(((exe:=("N/A",s(P(i("sys").executable).resolve()))[e(track_exe)])))).encode())) # noqa
if (env:=cache/(name if name else (_id:=sha256.hexdigest()))) not in cache.glob("*/") or ge("VIV_FORCE"): # noqa
v=e(ge("VIV_VERBOSE"));ew(f"generating new vivenv -> {env.name}\n") # noqa
i("venv").EnvBuilder(with_pip=True,clear=True).create(env) # noqa
with (env/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check=true") # noqa

View file

@ -67,22 +67,22 @@ class Spinner:
def write_next(self):
with self._screen_lock:
if not self.spinner_visible:
sys.stdout.write(next(self.spinner))
sys.stderr.write(next(self.spinner))
self.spinner_visible = True
sys.stdout.flush()
sys.stderr.flush()
def remove_spinner(self, cleanup=False):
with self._screen_lock:
if self.spinner_visible:
sys.stdout.write("\b\b\b")
sys.stderr.write("\b\b\b")
# sys.stdout.write("\b")
self.spinner_visible = False
if cleanup:
sys.stdout.write(" ") # overwrite spinner with blank
sys.stderr.write(" ") # overwrite spinner with blank
# sys.stdout.write("\r") # move to next line
# move back then delete the line
sys.stdout.write("\r\033[K")
sys.stdout.flush()
sys.stderr.write("\r\033[K")
sys.stderr.flush()
def spinner_task(self):
while self.busy:
@ -91,18 +91,18 @@ class Spinner:
self.remove_spinner()
def __enter__(self):
if sys.stdout.isatty():
if sys.stderr.isatty():
self._screen_lock = threading.Lock()
self.busy = True
self.thread = threading.Thread(target=self.spinner_task)
self.thread.start()
def __exit__(self, exc_type, exc_val, exc_traceback): # noqa
if sys.stdout.isatty():
if sys.stderr.isatty():
self.busy = False
self.remove_spinner(cleanup=True)
else:
sys.stdout.write("\r")
sys.stderr.write("\r")
BOX: Dict[str, str] = {
@ -136,7 +136,7 @@ class Ansi:
metavar: str = "\033[33m" # normal yellow
def __post_init__(self):
if os.getenv("NO_COLOR") or not sys.stdout.isatty():
if os.getenv("NO_COLOR") or not sys.stderr.isatty():
for attr in self.__dict__:
setattr(self, attr, "")
@ -248,9 +248,9 @@ class Ansi:
)
)
sys.stdout.write(f" {BOX['tl']}{BOX['h']*(sum(sizes)+5)}{BOX['tr']}\n")
sys.stdout.write("\n".join(table_rows) + "\n")
sys.stdout.write(f" {BOX['bl']}{BOX['h']*(sum(sizes)+5)}{BOX['br']}\n")
sys.stderr.write(f" {BOX['tl']}{BOX['h']*(sum(sizes)+5)}{BOX['tr']}\n")
sys.stderr.write("\n".join(table_rows) + "\n")
sys.stderr.write(f" {BOX['bl']}{BOX['h']*(sum(sizes)+5)}{BOX['br']}\n")
a = Ansi()
@ -268,12 +268,12 @@ def warn(msg):
echo(f"{a.yellow}warn:{a.end} {msg}", style="yellow")
def echo(msg: str, style="magenta", newline=True) -> None:
def echo(msg: str, style="magenta", newline=True, fd=sys.stderr) -> None:
"""output general message to stdout"""
output = f"{a.cyan}Viv{a.end}{a.__dict__[style]}::{a.end} {msg}"
if newline:
output += "\n"
sys.stdout.write(output)
fd.write(output)
def run(
@ -333,14 +333,15 @@ def get_hash(spec: Tuple[str, ...] | List[str], track_exe: bool = False) -> str:
Returns:
sha256 representation of dependencies for vivenv
"""
pkg_hash = hashlib.sha256()
pkg_hash.update(str(spec).encode())
# generate unique venvs for unique python exe's
if track_exe:
pkg_hash.update(str(Path(sys.executable).resolve()).encode())
sha256 = hashlib.sha256()
sha256.update(
(
str(spec) + (str(Path(sys.executable).resolve()) if track_exe else "N/A")
).encode()
)
return pkg_hash.hexdigest()
return sha256.hexdigest()
class ViVenv:
@ -491,9 +492,9 @@ def _viv_activate(*pkgs: str, track_exe: bool = False, name: str = "") -> None:
if not {{*m(type,pkgs)}}=={{s}}: raise ValueError(f"spec: {{pkgs}} is invalid")
ge,sys,P,ew=i("os").getenv,i("sys"),i("pathlib").Path,i("sys").stderr.write
(cache:=(P(ge("XDG_CACHE_HOME",P.home()/".cache"))/"viv"/"venvs")).mkdir(parents=True,exist_ok=True)
((hash:=i("hashlib").sha256()).update((s(spec)+
(((exe:=s(P(i("sys").executable).resolve()) if track_exe else "N/A")))).encode()))
if (env:=cache/(name if name else (_id:=hash.hexdigest()))) not in cache.glob("*/") or ge("VIV_FORCE"):
((sha256:=i("hashlib").sha256()).update((s(spec)+
(((exe:=("N/A",s(P(i("sys").executable).resolve()))[e(track_exe)])))).encode()))
if (env:=cache/(name if name else (_id:=sha256.hexdigest()))) not in cache.glob("*/") or ge("VIV_FORCE"):
v=e(ge("VIV_VERBOSE"));ew(f"generating new vivenv -> {{env.name}}\n")
i("venv").EnvBuilder(with_pip=True,clear=True).create(env)
with (env/"pip.conf").open("w") as f:f.write("[global]\ndisable-pip-version-check=true")

View file

@ -1,7 +1,9 @@
# VIV Todo's
- [x] swap flake8 for ruff
- [ ] use stdout and stderr more effectively (or switch to logging?)
- [x] use stdout and stderr more effectively (or switch to logging?)
- [ ] use config file (probably ini or toml for python>=3.11)
- [ ] enable a garbage collection based on time or file existence (configurable)
- [ ] unit tests (v important)
- [ ] add more options to filter `viv list`