{"id":13453584,"url":"https://github.com/lamerman/shellpy","last_synced_at":"2025-04-12T16:38:26.193Z","repository":{"id":44869277,"uuid":"49777913","full_name":"lamerman/shellpy","owner":"lamerman","description":"A tool for convenient shell scripting in python","archived":false,"fork":false,"pushed_at":"2022-02-13T19:18:00.000Z","size":86,"stargazers_count":640,"open_issues_count":18,"forks_count":62,"subscribers_count":43,"default_branch":"master","last_synced_at":"2025-04-03T16:14:39.683Z","etag":null,"topics":["python","shell","shell-script"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lamerman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-16T15:04:41.000Z","updated_at":"2025-03-26T10:39:07.000Z","dependencies_parsed_at":"2022-08-12T11:40:16.691Z","dependency_job_id":null,"html_url":"https://github.com/lamerman/shellpy","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamerman%2Fshellpy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamerman%2Fshellpy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamerman%2Fshellpy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamerman%2Fshellpy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lamerman","download_url":"https://codeload.github.com/lamerman/shellpy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248597767,"owners_count":21130943,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python","shell","shell-script"],"created_at":"2024-07-31T08:00:43.771Z","updated_at":"2025-04-12T16:38:26.171Z","avatar_url":"https://github.com/lamerman.png","language":"Python","readme":"# shellpy\nA tool for convenient shell scripting in Python. It allows you to write all your shell scripts in Python in a convenient way and in many cases replace Bash/Sh. \n\n## Preface - Why do we need shell python?\n\nFor many people bash/sh seem to be pretty complicated. An example would be regular expressions, working with json/yaml/xml, named arguments parsing and so on. There are many things that are much easier in python to understand and work with.\n\n## Introduction\n\nShell python has no differences from python except for one. Grave accent symbol (`) does not mean eval, it means execution of shell commands. So\n\n    `ls -l`\n\nwill execute `ls -l` in shell. You can also skip one ` in the end of line\n\n    `ls -l\n\nand it will also be a correct syntax. It is also possible to write multiline expressions\n\n    `\n    echo test \u003e test.txt\n    cat test.txt\n    `\n\nand long lines\n\n    `echo This is \\\n      a very long \\\n      line\n\nEvery shellpy expression returns a Result\n\n    result = `ls -l\n    \nor normally raises an error in case of non zero output of a command\n\n    try:\n      result = `ls -l non_existent_file\n    except NonZeroReturnCodeError as e:\n      result = e.result\n\nThe result can be either [Result](https://github.com/lamerman/shellpy/wiki/Simple-mode#result) or [InteractiveResult](https://github.com/lamerman/shellpy/wiki/Interactive-mode#interactive-result). Let's start with a simple Result. You can check returncode of a command\n\n    result = `ls -l\n    print result.returncode\n\nYou can also get text from stdout or stderr\n\n    result = `ls -l\n    result_text = result.stdout\n    result_error = result.stderr\n\nYou can iterate over lines of result stdout\n\n    result = `ls -l\n    for line in result:\n        print line.upper()\n\nand so on. \n\n## Integration with python and code reuse\n\nAs it was said before shellpython does not differ a lot from ordinary python. You can import python modules and use them as usual\n\n    import os.path\n    \n    `mkdir /tmp/mydir\n    os.path.exists('/tmp/mydir') # True\n\nAnd you can do the same with shellpython modules. Suppose you have shellpy module `common` as in examples directory. So this is how it looks\n\n    ls common/\n    common.spy  __init__.spy\n\nSo you have directory `common` and two files inside: `__init__.spy` and `common.spy`. Looks like a python module right? Exactly. The only difference is file extension. For `__init__.spy` and other files it must be `.spy`. Let's look inside `common.spy`\n\n    def common_func():\n        return `echo 5\n\nA simple function that returns [Result](https://github.com/lamerman/shellpy/wiki/Simple-mode#result) of `echo 5` execution. How is it used how in code? As same as in python\n\n    from common.common import common_func\n    \n    print('Result of imported function is ' + str(common_func()))\n\nNote that the `common` directory must be in pythonpath to be imported.\n\n### How does import work?\n\nIt uses import hooks described in [PEP 0302 -- New Import Hooks](https://www.python.org/dev/peps/pep-0302/). So, whenever importer finds a shellpy module or a file with .spy extension and with the name that you import, it will try to first preprocess it from shellpy to python and then import it using standard python import. Once preprocessed, the file is cached in your system temp directory and the second time it will be just imported directly.\n\n### Important note about import\n\nImport of shellpython modules requires import hook to be installed. There are two way how to do it:\n - run shellpython scripts with the `shellpy` tool as described below in the section [Running](https://github.com/lamerman/shellpy#running)\n - run your python scripts as usual with `python` but initialize shellpython before importing any module with `shellpython.init()` as in the [Example](https://github.com/lamerman/shellpy/blob/master/example/import_from_python/import.py)\n\n### Example\n\nThis script clones shellpython to temporary directory and finds the commit hash where README was created\n\n```python\n\nimport tempfile\nimport os.path\nfrom shellpython.helpers import Dir\n\n# We will make everything in temp directory. Dir helper allows you to change current directory\n# withing 'with' block\nwith Dir(tempfile.gettempdir()):\n    if not os.path.exists('shellpy'):\n        # just executes shell command\n        `git clone https://github.com/lamerman/shellpy.git\n\n    # switch to newly created tempdirectory/shellpy\n    with Dir('shellpy'):\n        # here we capture result of shell execution. log here is an instance of Result class\n        log = `git log --pretty=oneline --grep='Create'\n\n        # shellpy allows you to iterate over lines in stdout with this syntactic sugar\n        for line in log:\n            if line.find('README.md'):\n                hashcode = log.stdout.split(' ')[0]\n                print hashcode\n                exit(0)\n\n        print 'The commit where the readme was created was not found'\n\nexit(1)\n```\n\nTwo lines here are executed in shell ```git clone https://github.com/lamerman/shellpy.git``` and ```git log --pretty=oneline --grep='Create'```. The result of the second line is assigned to variable ```log``` and then we iterate over the result line by line in the for cycle\n\n### Installation\n\nYou can install it either with ```pip install shellpy``` or by cloning this repository and execution of ```setup.py install```. After that you will have ```shellpy``` command installed.\n\n### Running\n\nYou can try shellpython by running examples after installation. Download this repository and run the following command in the root folder of the cloned repository:\n\n```shellpy example/curl.spy```\n\n```shellpy example/git.spy```\n\nThere is also so called allinone example which you can have a look at and execute like this:\n\n```shellpy example/allinone/test.spy```\n\nIt is called all in one because it demonstrates all features available in shellpy. If you have python3 run instead:\n\n```shellpy example/allinone/test3.spy```\n\n### Documentation\n\n[Wiki](https://github.com/lamerman/shellpy/wiki)\n\n### Compatibility\n\nIt works on Linux and Mac for both Python 2.x and 3.x. It should also work on Windows.\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamerman%2Fshellpy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flamerman%2Fshellpy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamerman%2Fshellpy/lists"}