From f968fb28730cb083230313bec04259aa8430f2bf Mon Sep 17 00:00:00 2001 From: jaykv Date: Sun, 21 May 2023 00:35:13 -0400 Subject: [PATCH] Implemented script runner --- Makefile | 5 +---- README.md | 22 +++++++++++++++++----- demo.py => examples/demo.py | 1 + examples/hello.py | 3 +++ pybash/__main__.py | 26 ++++++++++++++++++++++++++ pybash/hook.py | 2 +- pyproject.toml | 4 ++-- test.txt | 2 -- 8 files changed, 51 insertions(+), 14 deletions(-) rename demo.py => examples/demo.py (97%) create mode 100644 examples/hello.py create mode 100644 pybash/__main__.py delete mode 100644 test.txt diff --git a/Makefile b/Makefile index 23b02d5..d7806a8 100644 --- a/Makefile +++ b/Makefile @@ -11,9 +11,6 @@ test: dev: python run.py -debug: - python -m ideas demo -a pybash -s - clean: rm -rf build/ dist/ *.egg-info .pytest_cache find . -name '*.pyc' -type f -exec rm -rf {} + @@ -40,6 +37,6 @@ shell: source $(poetry env info --path)/bin/activate debug: - python -m ideas demo -a pybash.hook -s + python -m ideas examples.demo -a pybash.hook -s .PHONY: test clean \ No newline at end of file diff --git a/README.md b/README.md index 877c992..5f8519c 100644 --- a/README.md +++ b/README.md @@ -23,12 +23,23 @@ from pybash.transformer import transform transform(">echo hello world") # returns the python code for the bash command as string ``` -## As ideas hook -`pip install "pybash[ideas]"` +## As script runner +`pip install "pybash[script]"` -See [run.py](run.py) for an example. -# Usage +### Example +```py +# +text = "HELLO WORLD" +>echo f{text} +``` + +### Run script: +```bash +$ python -m pybash hello.py +``` + +# Supported transforms ### 1. Simple execution with output ```python @@ -155,7 +166,8 @@ cp_test() # Dev #### Demo -`python run.py` +`python -m pybash examples/hello.py` +`python -m pybash demo` #### Debug `make debug` to view the transformed source code diff --git a/demo.py b/examples/demo.py similarity index 97% rename from demo.py rename to examples/demo.py index d53dbd2..8806203 100644 --- a/demo.py +++ b/examples/demo.py @@ -19,6 +19,7 @@ #print(f"test: {a}") # 1. use inside methods +>echo "SORT TEST\nHELLO WORLD" > test.txt def cp_test(): print("1. >cp test.txt test_copy.txt") >cp test.txt test_copy.txt diff --git a/examples/hello.py b/examples/hello.py new file mode 100644 index 0000000..7e0731c --- /dev/null +++ b/examples/hello.py @@ -0,0 +1,3 @@ + +text = "HELLO WORLD" +>echo f{text} \ No newline at end of file diff --git a/pybash/__main__.py b/pybash/__main__.py new file mode 100644 index 0000000..c8edba6 --- /dev/null +++ b/pybash/__main__.py @@ -0,0 +1,26 @@ +## +# Setup hook +import importlib.util +import sys +from pathlib import Path + +from pybash.hook import add_hook + +hook = add_hook() + +# Run script dynamically +if __name__ == "__main__": + spec = None + + if path_or_module := sys.argv[1]: + file_path = Path(path_or_module) + if file_path.exists() and file_path.is_file(): + spec = hook.find_spec(path_or_module.split('.py')[0], None) + else: + spec = importlib.util.find_spec(path_or_module) + + if spec and spec.loader: + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + else: + print(f"Could not import {path_or_module}") diff --git a/pybash/hook.py b/pybash/hook.py index 75fd81f..ef54072 100644 --- a/pybash/hook.py +++ b/pybash/hook.py @@ -8,7 +8,7 @@ def source_init(): return "import subprocess" -def add_hook(**_kwargs): +def add_hook(): """Creates and automatically adds the import hook in sys.meta_path""" return import_hook.create_hook( hook_name=__name__, diff --git a/pyproject.toml b/pyproject.toml index 99a7d2b..404e88b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "PyBash" -version = "0.3.0" +version = "0.3.1" description = ">execute bash commands from python easily" authors = ["Jay "] readme = "README.md" @@ -12,7 +12,7 @@ ideas = { version = "^0.1.5", optional = true } token-utils = "^0.1.8" [tool.poetry.extras] -ideas = ["ideas"] +script = ["ideas"] [tool.poetry.group.dev.dependencies] black = "^22.12.0" diff --git a/test.txt b/test.txt deleted file mode 100644 index b59565d..0000000 --- a/test.txt +++ /dev/null @@ -1,2 +0,0 @@ -SORT TEST -HELLO WORLD