Skip to main content

Magic Run Command

This provide a clean way to express scripts inside your python code. The run engine will be shell by default. This will allow you to run shell scripts from your python code.

from magic_cmd.script import Script

cmd = Script('''
        ls
        touch test.py
        ls
        rm test.py
        ''')
restults:list[str] = cmd()

Installation:

pip

You can install it via pip:

pip install magic-run-cmd

poetry

To install this code, you will need to first install Poetry (https://python-poetry.org/docs/#installation). Poetry is a dependency manager for Python that will allow you to easily install this code and its dependencies.

Once you have Poetry installed, you can install this code by running the following command from the root directory of this code:

poetry add magic-run-cmd

This will install this code and all of its dependencies.

Usage:

To run a simple shell command you can use run_cmd. This is what the shell engine uses under the hood:

from magic_cmd.run_cmd import run_cmd

print(run_cmd('ls'))

output:
LICENSE  README.md  log/  poetry.lock  pyproject.toml  run_cmd/

Then, you can call the run_cmd function with a shell command as a string:

If an error thrown it caught, and logged before, erroring out.

You can also specify whether you want the output to be returned as a list or a string:

run_cmd('ls', split=True)

This will output the result as a list, with each element being one line of output.

Script

Scripts allow you express you script inside your python code inside triple quote. The extra white spaces are removed so format as needed. Scripts can be append using append(str). Or you can add str or other Scripts. Since the scripts are callable, to run a script just call its name followed by ().

        script = Scripts('''
        echo "This is a line"
        echo "The white space are ignored"
        ''')
        script()
        script += 'echo "you can add string"'
        script += Script('echo "or another Script"')
        script.append("And you can append too")
        script()
        #[
        # This is a line,
        #The white space are ignored,
        #you can add string,
        #or another Script
        #And you can append too,
        #]
        #Write to a file and return the path
        shell_script:Path = script.writefile(name="shell.sh")

Metadata

Release files for magic-run-cmd 2.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for magic-run-cmd 2.3.2
File Size Uploaded
magic_run_cmd-2.3.2.tar.gz 7.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for magic-run-cmd 2.3.2
File Interpreter ABI Platform
magic_run_cmd-2.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 16.9 kB

Release files / magic_run_cmd-2.3.2.tar.gz

Download URL magic_run_cmd-2.3.2.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ba07401934b1d7d6da0bb4f57e72c25c3cdbb3645d63f0561ec324113e1444cc
BLAKE2b-256 checksum
How to use checksums
cada0a4db52f8f0312f51f2cbc96337cf2381acdf6d688e1b65910a43fd23e07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.0 CPython/3.10.9 Linux/5.15.133.1-microsoft-standard-WSL2

Release files / magic_run_cmd-2.3.2-py3-none-any.whl

Download URL magic_run_cmd-2.3.2-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c1df75cd818b5670a292e896d1dda43019bf79032384058090909b7eb5509a31
BLAKE2b-256 checksum
How to use checksums
a451fd4ffbd0e16fabfdc34c82db6c34f55c4fffd0166c2477f89c909e248a40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.0 CPython/3.10.9 Linux/5.15.133.1-microsoft-standard-WSL2

Release history Release notifications | RSS feed

This release

2.3.2 This release

2 release files

2.3.1

2 release files

2.3.0

2 release files

2.2.5

2 release files

2.2.4

2 release files

2.2.3

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.1.1

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page