# Executes Cygwin bash scripts/commands in Python, captures and prints the output
## pip install cygsubprocess
### Tested against Windows 10 / Python 3.10 / Anaconda / cygcheck (cygwin) 3.4.6
```python
from cygsubprocess import Bashsubprocess
ba = Bashsubprocess(
cygfolder=r'C:\cygwin',mirrorsite=None,addtopath=True
) # If cygwin is not installed, it will be installed and added to the PATH
ba.convert_path_cyg2win("/cygdrive/c/Users/hansc/Downloads/ClipAngel 2.09")
# r'C:\Users\hansc\Downloads\ClipAngel 2.09'
ba.convert_path_win2cyg(path=r"C:\Users\hansc\Downloads\ClipAngel 2.09")
# '/cygdrive/c/Users/hansc/Downloads/ClipAngel 2.09'
cmd1 = ba.execute_capture("ls -la")
print(cmd1.stdout_lines[:5])
cmd2 = ba.execute_capture(["ls -la | grep 'py'"])
print(cmd2.stdout_lines[:5])
cmd1 = ba.execute_print_capture("ls -la")
print(cmd1.stdout_lines[:5])
cmd2 = ba.execute_print_capture(["ls -la | grep 'py'"])
print(cmd2.stdout_lines[:5])
# gist download
scriptexec = r"""
slugify(){ echo "$1" | iconv -t ascii//TRANSLIT | sed -r s/[^a-zA-Z0-9]+/-/g | sed -r s/^-+\|-+$//g | tr A-Z a-z; }
cnt=0; gh gist list --limit 3 | cut -f1,2 | tac | while read id name; do ((cnt++)); gh gist clone $id $cnt-`slugify "$name"`; done
"""
cmd4 = ba.execute_print_capture(scriptexec)
print(cmd4.stdout_lines)
bashscr = """
#!/bin/bash
# set the STRING variable
STRING="Hello World!"
# print the contents of the variable on screen
echo $STRING
"""
bashtofile = ba.exec_sh_to_file(bashscr, printoutput=False)
print(bashtofile.stdout_lines)
bashscr = r"""
#!/bin/bash
salary=1000
expenses=800
#Check if salary and expenses are equal
if [ $salary == $expenses ];
then
echo "Salary and expenses are equal"
#Check if salary and expenses are not equal
elif [ $salary != $expenses ];
then
echo "Salary and expenses are not equal"
fi
"""
bashtofile2 = ba.exec_sh_directly(bashscr, printoutput=True)
print(bashtofile2.stdout_lines)
scriptfile='c:\\bashscripttest.sh'
with open(scriptfile,mode='w', encoding='utf-8',newline='\n') as f:
f.write(bashscr)
ba.installapt() # install apt https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg
ba.apt_install( package='hunspell-de') # install a package with apt
ba.apt_remove( package='hunspell-de') # remove a package with apt
test2=ba.exec_sh_url( bashscr=r'https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg', printoutput=True)
test3=ba.exec_sh_file( bashscr=scriptfile, printoutput=True)
test4=ba.exec_sh_to_file( bashscr, printoutput=True)
test5=ba.exec_sh_directly( bashscr, printoutput=True)
test6=ba.get_list_of_files_no_stat( folder=r'C:\Python34') # windows and cyg path (escaped and unescaped)
test7=ba.get_list_of_files_with_stat( folder=r'C:\Python34') # windows and cyg path (escaped and unescaped)
....
# b'-rwxr-x---+ 1 Administrators hansc 25749 Mar 31 23:52 trayinfo.py.bak\n'
# b'-rwxr-x---+ 1 Administrators hansc 671 Apr 1 09:50 trayinfotest.py\n'
# b'-rwxr-x---+ 1 Administrators hansc 25878 Apr 1 09:34 traymenuxxxxxxxxxx.py\n'
# b'-rwxr-x---+ 1 Administrators hansc 4329 Apr 1 03:12 tw.py\n'
# b'-rwxr-x---+ 1 Administrators hansc 8095 Apr 1 08:48 win10ctypestoastxxxxxxxxxx.py\n'
# [b'drwxr-x---+ 1 hansc hansc 0 Apr 1 13:01 1-pyde-py\n', b'drwxr-x---+ 1 hansc hansc 0 Apr 1 13:14 1-stra-py\n', b'drwxr-x---+ 1 hansc hansc 0 Apr 1 13:14 10-cygwinco-py\n', b'drwxr-x---+ 1 hansc hansc 0 Apr 1 13:01 10-tracealllines-py\n', b'drwxr-x---+ 1 hansc hansc 0 Apr 1 13:01 2-stra-py\n']
# b"Cloning into '1-tracealllines-py'...\n"
# b"Cloning into '2-cygwinco-py'...\n"
# b"Cloning into '3-cygwinco-py'...\n"
# []
# [b'Hello World!\n']
# b'Salary and expenses are not equal\n'
# [b'Salary and expenses are not equal\n']
....
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/cygsubprocess",
"name": "cygsubprocess",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "bash,cygwin,sh,linux",
"author": "Johannes Fischer",
"author_email": "<aulasparticularesdealemaosp@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/8d/9b/da8f4052bcecccd4401fba567404d013395d9a2996d8410e36cee7fa2f63/cygsubprocess-0.12.tar.gz",
"platform": null,
"description": "\r\n# Executes Cygwin bash scripts/commands in Python, captures and prints the output\r\n\r\n\r\n\r\n## pip install cygsubprocess\r\n\r\n\r\n\r\n### Tested against Windows 10 / Python 3.10 / Anaconda / cygcheck (cygwin) 3.4.6\r\n\r\n\r\n\r\n\r\n\r\n```python\r\n\r\n\r\n\r\nfrom cygsubprocess import Bashsubprocess\r\n\r\n\r\n\r\nba = Bashsubprocess(\r\n\r\n cygfolder=r'C:\\cygwin',mirrorsite=None,addtopath=True\r\n\r\n) # If cygwin is not installed, it will be installed and added to the PATH\r\n\r\nba.convert_path_cyg2win(\"/cygdrive/c/Users/hansc/Downloads/ClipAngel 2.09\")\r\n\r\n# r'C:\\Users\\hansc\\Downloads\\ClipAngel 2.09'\r\n\r\n\r\n\r\nba.convert_path_win2cyg(path=r\"C:\\Users\\hansc\\Downloads\\ClipAngel 2.09\")\r\n\r\n# '/cygdrive/c/Users/hansc/Downloads/ClipAngel 2.09'\r\n\r\ncmd1 = ba.execute_capture(\"ls -la\")\r\n\r\nprint(cmd1.stdout_lines[:5])\r\n\r\n\r\n\r\ncmd2 = ba.execute_capture([\"ls -la | grep 'py'\"])\r\n\r\nprint(cmd2.stdout_lines[:5])\r\n\r\n\r\n\r\n\r\n\r\ncmd1 = ba.execute_print_capture(\"ls -la\")\r\n\r\nprint(cmd1.stdout_lines[:5])\r\n\r\n\r\n\r\ncmd2 = ba.execute_print_capture([\"ls -la | grep 'py'\"])\r\n\r\nprint(cmd2.stdout_lines[:5])\r\n\r\n\r\n\r\n# gist download\r\n\r\nscriptexec = r\"\"\"\r\n\r\nslugify(){ echo \"$1\" | iconv -t ascii//TRANSLIT | sed -r s/[^a-zA-Z0-9]+/-/g | sed -r s/^-+\\|-+$//g | tr A-Z a-z; }\r\n\r\ncnt=0; gh gist list --limit 3 | cut -f1,2 | tac | while read id name; do ((cnt++)); gh gist clone $id $cnt-`slugify \"$name\"`; done\r\n\r\n\"\"\"\r\n\r\ncmd4 = ba.execute_print_capture(scriptexec)\r\n\r\nprint(cmd4.stdout_lines)\r\n\r\n\r\n\r\n\r\n\r\nbashscr = \"\"\"\r\n\r\n#!/bin/bash\r\n\r\n# set the STRING variable\r\n\r\nSTRING=\"Hello World!\"\r\n\r\n# print the contents of the variable on screen\r\n\r\necho $STRING\r\n\r\n\"\"\"\r\n\r\nbashtofile = ba.exec_sh_to_file(bashscr, printoutput=False)\r\n\r\nprint(bashtofile.stdout_lines)\r\n\r\n\r\n\r\nbashscr = r\"\"\"\r\n\r\n#!/bin/bash\r\n\r\nsalary=1000\r\n\r\nexpenses=800\r\n\r\n#Check if salary and expenses are equal\r\n\r\nif [ $salary == $expenses ];\r\n\r\nthen\r\n\r\n echo \"Salary and expenses are equal\"\r\n\r\n#Check if salary and expenses are not equal\r\n\r\nelif [ $salary != $expenses ];\r\n\r\nthen\r\n\r\n echo \"Salary and expenses are not equal\"\r\n\r\nfi\r\n\r\n\"\"\"\r\n\r\nbashtofile2 = ba.exec_sh_directly(bashscr, printoutput=True)\r\n\r\nprint(bashtofile2.stdout_lines)\r\n\r\n\r\n\r\nscriptfile='c:\\\\bashscripttest.sh'\r\n\r\nwith open(scriptfile,mode='w', encoding='utf-8',newline='\\n') as f:\r\n\r\n f.write(bashscr)\r\n\r\n\r\n\r\nba.installapt() # install apt https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg\r\n\r\nba.apt_install( package='hunspell-de') # install a package with apt\r\n\r\nba.apt_remove( package='hunspell-de') # remove a package with apt\r\n\r\n\r\n\r\ntest2=ba.exec_sh_url( bashscr=r'https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg', printoutput=True)\r\n\r\ntest3=ba.exec_sh_file( bashscr=scriptfile, printoutput=True)\r\n\r\ntest4=ba.exec_sh_to_file( bashscr, printoutput=True)\r\n\r\ntest5=ba.exec_sh_directly( bashscr, printoutput=True)\r\n\r\ntest6=ba.get_list_of_files_no_stat( folder=r'C:\\Python34') # windows and cyg path (escaped and unescaped)\r\n\r\ntest7=ba.get_list_of_files_with_stat( folder=r'C:\\Python34') # windows and cyg path (escaped and unescaped)\r\n\r\n\r\n\r\n....\r\n\r\n\r\n\r\n# b'-rwxr-x---+ 1 Administrators hansc 25749 Mar 31 23:52 trayinfo.py.bak\\n'\r\n\r\n# b'-rwxr-x---+ 1 Administrators hansc 671 Apr 1 09:50 trayinfotest.py\\n'\r\n\r\n# b'-rwxr-x---+ 1 Administrators hansc 25878 Apr 1 09:34 traymenuxxxxxxxxxx.py\\n'\r\n\r\n# b'-rwxr-x---+ 1 Administrators hansc 4329 Apr 1 03:12 tw.py\\n'\r\n\r\n# b'-rwxr-x---+ 1 Administrators hansc 8095 Apr 1 08:48 win10ctypestoastxxxxxxxxxx.py\\n'\r\n\r\n# [b'drwxr-x---+ 1 hansc hansc 0 Apr 1 13:01 1-pyde-py\\n', b'drwxr-x---+ 1 hansc hansc 0 Apr 1 13:14 1-stra-py\\n', b'drwxr-x---+ 1 hansc hansc 0 Apr 1 13:14 10-cygwinco-py\\n', b'drwxr-x---+ 1 hansc hansc 0 Apr 1 13:01 10-tracealllines-py\\n', b'drwxr-x---+ 1 hansc hansc 0 Apr 1 13:01 2-stra-py\\n']\r\n\r\n# b\"Cloning into '1-tracealllines-py'...\\n\"\r\n\r\n# b\"Cloning into '2-cygwinco-py'...\\n\"\r\n\r\n# b\"Cloning into '3-cygwinco-py'...\\n\"\r\n\r\n# []\r\n\r\n# [b'Hello World!\\n']\r\n\r\n# b'Salary and expenses are not equal\\n'\r\n\r\n# [b'Salary and expenses are not equal\\n']\r\n\r\n....\r\n\r\n```\r\n\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Executes Cygwin bash scripts/commands in Python, captures and prints the output",
"version": "0.12",
"split_keywords": [
"bash",
"cygwin",
"sh",
"linux"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "7a305083a52f5b9cad1a0cdba8c704ecc12131ef1f2a55c334025e061a1add46",
"md5": "cfc6c98657e38c8e7a6fb1bc9d763ed0",
"sha256": "6f964a91c8a449b8c0849ee40de37505fdef76190430eec484646bf916f8ae47"
},
"downloads": -1,
"filename": "cygsubprocess-0.12-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cfc6c98657e38c8e7a6fb1bc9d763ed0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 20450,
"upload_time": "2023-04-04T16:45:26",
"upload_time_iso_8601": "2023-04-04T16:45:26.616992Z",
"url": "https://files.pythonhosted.org/packages/7a/30/5083a52f5b9cad1a0cdba8c704ecc12131ef1f2a55c334025e061a1add46/cygsubprocess-0.12-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "8d9bda8f4052bcecccd4401fba567404d013395d9a2996d8410e36cee7fa2f63",
"md5": "e988e5bf4b883a2951d4c79991e16fd9",
"sha256": "f6d843f777c0e9ef31cc55588e0958898e174eb39c09fed7fea36dbf0e60b0b9"
},
"downloads": -1,
"filename": "cygsubprocess-0.12.tar.gz",
"has_sig": false,
"md5_digest": "e988e5bf4b883a2951d4c79991e16fd9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 18831,
"upload_time": "2023-04-04T16:45:28",
"upload_time_iso_8601": "2023-04-04T16:45:28.465807Z",
"url": "https://files.pythonhosted.org/packages/8d/9b/da8f4052bcecccd4401fba567404d013395d9a2996d8410e36cee7fa2f63/cygsubprocess-0.12.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-04 16:45:28",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "hansalemaos",
"github_project": "cygsubprocess",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "cygsubprocess"
}