aton.call
Description
Functions to handle bash calls and related operations on Linux systems.
Index
here() |
Runs the rest of the script inside a given folder |
bash() |
Run a bash shell commands |
git() |
Automatically update a Git repository |
1""" 2# Description 3 4Functions to handle bash calls and related operations on Linux systems. 5 6 7# Index 8 9| | | 10| --- | --- | 11| `here()` | Runs the rest of the script inside a given folder | 12| `bash()` | Run a bash shell commands | 13| `git()` | Automatically update a Git repository | 14 15--- 16""" 17 18 19import subprocess 20import datetime 21import sys 22import os 23from aton._version import __version__ 24 25 26def here(folder=None) -> str: 27 """Runs the rest of the script inside the specified `folder`. 28 29 If none is provided, it runs from the same directory where the current script lies. 30 This is really useful to run scripts from the VSCode terminal, etc. 31 Returns the path of the used `folder`, or the path of the script if the folder is not provided. 32 If it runs on the Python CLI and no folder is provided, it just returns the CWD.\n 33 Note that this changes not only the working directory of your script, 34 but also of other scripts that import and run your script. 35 """ 36 if folder: 37 caller = os.path.abspath(folder) 38 elif not sys.argv[0]: # This code is running in CLI 39 return os.getcwd() 40 else: # Return the parent folder of the current script 41 caller = os.path.dirname(os.path.abspath(os.path.realpath(sys.argv[0]))) 42 os.chdir(caller) 43 return caller 44 45 46def bash( 47 command:str, 48 cwd=None, 49 verbose:bool=True, 50 return_anyway:bool=False 51 ): 52 """Call bash shell commands. 53 54 A given `command` will be executed inside an optional `cwd` directory. 55 If empty, the current working directory will be used. 56 Prints the running command and outputs by default, override this with `verbose=False`. 57 Returns the result of the command used, except for when 58 errors are raised automatically; set `return_anyway=True` to override this. 59 """ 60 if verbose: 61 print(f'$ {command}') 62 result = subprocess.run(command, cwd=cwd, shell=True, text=True, capture_output=True) 63 if verbose and result.returncode == 0 and result.stdout: 64 print(result.stdout) 65 elif result.returncode != 0: 66 error_message = ( 67 f"aton.call.bash: Command failed with exit code {result.returncode}.\n" 68 f"{result.stderr.strip()}" 69 ) 70 if not return_anyway: 71 raise RuntimeError(error_message) 72 return result 73 74 75def git( 76 path=None, 77 verbose=True, 78 message=None, 79 tag=None 80 ) -> None: 81 """Update a Git repository with automatic bash calls.""" 82 if path: 83 os.chdir(path) 84 bash("git fetch", path, verbose) 85 bash("git add .", path, verbose) 86 if not message: 87 date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M") 88 message = f'Automatic push on {date} with ATON {__version__}' 89 bash(f'git commit -m "{message}"', path, verbose) 90 if tag: 91 bash(f'git tag -a {tag} HEAD -m {message}', path, verbose) 92 bash("git push origin main --tags", path, verbose) 93 else: 94 bash("git push", path, verbose) 95 print("Git updated!") 96 return None
27def here(folder=None) -> str: 28 """Runs the rest of the script inside the specified `folder`. 29 30 If none is provided, it runs from the same directory where the current script lies. 31 This is really useful to run scripts from the VSCode terminal, etc. 32 Returns the path of the used `folder`, or the path of the script if the folder is not provided. 33 If it runs on the Python CLI and no folder is provided, it just returns the CWD.\n 34 Note that this changes not only the working directory of your script, 35 but also of other scripts that import and run your script. 36 """ 37 if folder: 38 caller = os.path.abspath(folder) 39 elif not sys.argv[0]: # This code is running in CLI 40 return os.getcwd() 41 else: # Return the parent folder of the current script 42 caller = os.path.dirname(os.path.abspath(os.path.realpath(sys.argv[0]))) 43 os.chdir(caller) 44 return caller
Runs the rest of the script inside the specified folder.
If none is provided, it runs from the same directory where the current script lies.
This is really useful to run scripts from the VSCode terminal, etc.
Returns the path of the used folder, or the path of the script if the folder is not provided.
If it runs on the Python CLI and no folder is provided, it just returns the CWD.
Note that this changes not only the working directory of your script, but also of other scripts that import and run your script.
47def bash( 48 command:str, 49 cwd=None, 50 verbose:bool=True, 51 return_anyway:bool=False 52 ): 53 """Call bash shell commands. 54 55 A given `command` will be executed inside an optional `cwd` directory. 56 If empty, the current working directory will be used. 57 Prints the running command and outputs by default, override this with `verbose=False`. 58 Returns the result of the command used, except for when 59 errors are raised automatically; set `return_anyway=True` to override this. 60 """ 61 if verbose: 62 print(f'$ {command}') 63 result = subprocess.run(command, cwd=cwd, shell=True, text=True, capture_output=True) 64 if verbose and result.returncode == 0 and result.stdout: 65 print(result.stdout) 66 elif result.returncode != 0: 67 error_message = ( 68 f"aton.call.bash: Command failed with exit code {result.returncode}.\n" 69 f"{result.stderr.strip()}" 70 ) 71 if not return_anyway: 72 raise RuntimeError(error_message) 73 return result
Call bash shell commands.
A given command will be executed inside an optional cwd directory.
If empty, the current working directory will be used.
Prints the running command and outputs by default, override this with verbose=False.
Returns the result of the command used, except for when
errors are raised automatically; set return_anyway=True to override this.
76def git( 77 path=None, 78 verbose=True, 79 message=None, 80 tag=None 81 ) -> None: 82 """Update a Git repository with automatic bash calls.""" 83 if path: 84 os.chdir(path) 85 bash("git fetch", path, verbose) 86 bash("git add .", path, verbose) 87 if not message: 88 date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M") 89 message = f'Automatic push on {date} with ATON {__version__}' 90 bash(f'git commit -m "{message}"', path, verbose) 91 if tag: 92 bash(f'git tag -a {tag} HEAD -m {message}', path, verbose) 93 bash("git push origin main --tags", path, verbose) 94 else: 95 bash("git push", path, verbose) 96 print("Git updated!") 97 return None
Update a Git repository with automatic bash calls.