diff options
| -rw-r--r-- | .github/workflows/ci.yml | 3 | ||||
| -rw-r--r-- | .pylintrc | 3 | ||||
| -rw-r--r-- | README.md | 20 | ||||
| -rw-r--r-- | poetry.lock | 181 | ||||
| -rw-r--r-- | pyproject.toml | 17 | ||||
| -rw-r--r-- | taskipy/exceptions.py | 9 | ||||
| -rw-r--r-- | taskipy/help.py | 33 | ||||
| -rw-r--r-- | taskipy/pyproject.py | 26 | ||||
| -rw-r--r-- | taskipy/task.py | 85 | ||||
| -rw-r--r-- | taskipy/task_runner.py | 118 | ||||
| -rw-r--r-- | tests/test_taskipy.py | 75 |
11 files changed, 363 insertions, 207 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2dada89..0a7b7cc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,8 +19,7 @@ jobs: - 3.6 - 3.7 - 3.8 - # need to figure out some type annotation issues that only occur on 3.9 - # - 3.9 + - 3.9 steps: - uses: actions/checkout@v2 @@ -7,7 +7,8 @@ disable= R0903, R0915, W0703, - W0707 + W0707, + R1732 [FORMAT] max-line-length=1000 @@ -47,7 +47,7 @@ In your `pyproject.toml` file, add a new section called `[tool.taskipy.tasks]`. The section is a key-value map, from the names of the task to the actual command that should be run in the shell. -Example: +There are two ways to define tasks. The easy way is to simply write the command down as a string: __pyproject.toml__ ```toml @@ -56,6 +56,17 @@ test = "python -m unittest tests/test_*.py" lint = "pylint tests taskipy" ``` +Alternatively, you can define tasks more explicitly by declaring both the command and a helpful description using an inline table: + +__pyproject.toml__ +```toml +[tool.taskipy.tasks] +test = { cmd = "python -m unittest tests/test_*.py", help = "runs all unit tests" } +lint = { cmd = "pylint tests taskipy", help = "confirms code style using pylint" } +``` + +The explicit notation is more verbose, but provides better context to anyone who uses the task. + ### Running Tasks In order to run a task, run the following command in your terminal: ```bash @@ -69,6 +80,13 @@ test python -m unittest tests/test_*.py lint pylint tests taskipy ``` +If you declared your task explicitly, you will see the description of the task by the side of the task's name: +```bash +$ poetry run task --list +test runs all unit tests +lint confirms code style using pylint +``` + ### Passing Command Line Args to Tasks If you want to pass command line arguments to tasks (positional or named), simply append them to the end of the task command. diff --git a/poetry.lock b/poetry.lock index 182dea7..6f001da 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,10 +1,10 @@ [[package]] name = "astroid" -version = "2.5.1" +version = "2.5.6" description = "An abstract syntax tree for Python with inference support." category = "dev" optional = false -python-versions = ">=3.6" +python-versions = "~=3.6" [package.dependencies] lazy-object-proxy = ">=1.4.0" @@ -15,7 +15,7 @@ wrapt = ">=1.11,<1.13" name = "colorama" version = "0.4.4" description = "Cross-platform colored terminal text." -category = "dev" +category = "main" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" @@ -34,11 +34,11 @@ colors = ["colorama (>=0.4.3,<0.5.0)"] [[package]] name = "lazy-object-proxy" -version = "1.5.2" +version = "1.6.0" description = "A fast and thorough lazy object proxy." category = "dev" optional = false -python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*" [[package]] name = "mccabe" @@ -58,7 +58,7 @@ python-versions = ">=3.5" [[package]] name = "mypy" -version = "0.761" +version = "0.812" description = "Optional static typing for Python" category = "dev" optional = false @@ -93,22 +93,19 @@ test = ["ipaddress", "mock", "unittest2", "enum34", "pywin32", "wmi"] [[package]] name = "pylint" -version = "2.7.2" +version = "2.8.2" description = "python code static checker" category = "dev" optional = false python-versions = "~=3.6" [package.dependencies] -astroid = ">=2.5.1,<2.6" +astroid = ">=2.5.6,<2.7" colorama = {version = "*", markers = "sys_platform == \"win32\""} isort = ">=4.2.5,<6" mccabe = ">=0.6,<0.7" toml = ">=0.7.1" -[package.extras] -docs = ["sphinx (==3.5.1)", "python-docs-theme (==2020.12)"] - [[package]] name = "rope" version = "0.14.0" @@ -127,7 +124,7 @@ python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*" [[package]] name = "typed-ast" -version = "1.4.2" +version = "1.4.3" description = "a fork of Python 2 and 3 ast modules with type comment support" category = "dev" optional = false @@ -135,7 +132,7 @@ python-versions = "*" [[package]] name = "typing-extensions" -version = "3.7.4.3" +version = "3.10.0.0" description = "Backported and Experimental Type Hints for Python 3.5+" category = "dev" optional = false @@ -152,12 +149,12 @@ python-versions = "*" [metadata] lock-version = "1.1" python-versions = "^3.6" -content-hash = "3750f1e08c9f4d634811bdd89c8d1e004df324cfbe38fb0caab6a8f7713fcf33" +content-hash = "788a82707e70e2638ef6e96717e0c3159de334d4f1c2ae9262138e404fafe8c4" [metadata.files] astroid = [ - {file = "astroid-2.5.1-py3-none-any.whl", hash = "sha256:21d735aab248253531bb0f1e1e6d068f0ee23533e18ae8a6171ff892b98297cf"}, - {file = "astroid-2.5.1.tar.gz", hash = "sha256:cfc35498ee64017be059ceffab0a25bedf7548ab76f2bea691c5565896e7128d"}, + {file = "astroid-2.5.6-py3-none-any.whl", hash = "sha256:4db03ab5fc3340cf619dbc25e42c2cc3755154ce6009469766d7143d1fc2ee4e"}, + {file = "astroid-2.5.6.tar.gz", hash = "sha256:8a398dfce302c13f14bab13e2b14fe385d32b73f4e4853b9bdfb64598baa1975"}, ] colorama = [ {file = "colorama-0.4.4-py2.py3-none-any.whl", hash = "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2"}, @@ -168,30 +165,28 @@ isort = [ {file = "isort-5.8.0.tar.gz", hash = "sha256:0a943902919f65c5684ac4e0154b1ad4fac6dcaa5d9f3426b732f1c8b5419be6"}, ] lazy-object-proxy = [ - {file = "lazy-object-proxy-1.5.2.tar.gz", hash = "sha256:5944a9b95e97de1980c65f03b79b356f30a43de48682b8bdd90aa5089f0ec1f4"}, - {file = "lazy_object_proxy-1.5.2-cp27-cp27m-macosx_10_14_x86_64.whl", hash = "sha256:e960e8be509e8d6d618300a6c189555c24efde63e85acaf0b14b2cd1ac743315"}, - {file = "lazy_object_proxy-1.5.2-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:522b7c94b524389f4a4094c4bf04c2b02228454ddd17c1a9b2801fac1d754871"}, - {file = "lazy_object_proxy-1.5.2-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:3782931963dc89e0e9a0ae4348b44762e868ea280e4f8c233b537852a8996ab9"}, - {file = "lazy_object_proxy-1.5.2-cp35-cp35m-manylinux2014_aarch64.whl", hash = "sha256:429c4d1862f3fc37cd56304d880f2eae5bd0da83bdef889f3bd66458aac49128"}, - {file = "lazy_object_proxy-1.5.2-cp35-cp35m-win32.whl", hash = "sha256:cd1bdace1a8762534e9a36c073cd54e97d517a17d69a17985961265be6d22847"}, - {file = "lazy_object_proxy-1.5.2-cp35-cp35m-win_amd64.whl", hash = "sha256:ddbdcd10eb999d7ab292677f588b658372aadb9a52790f82484a37127a390108"}, - {file = "lazy_object_proxy-1.5.2-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:ecb5dd5990cec6e7f5c9c1124a37cb2c710c6d69b0c1a5c4aa4b35eba0ada068"}, - {file = "lazy_object_proxy-1.5.2-cp36-cp36m-manylinux2014_aarch64.whl", hash = "sha256:b6577f15d5516d7d209c1a8cde23062c0f10625f19e8dc9fb59268859778d7d7"}, - {file = "lazy_object_proxy-1.5.2-cp36-cp36m-win32.whl", hash = "sha256:c8fe2d6ff0ff583784039d0255ea7da076efd08507f2be6f68583b0da32e3afb"}, - {file = "lazy_object_proxy-1.5.2-cp36-cp36m-win_amd64.whl", hash = "sha256:fa5b2dee0e231fa4ad117be114251bdfe6afe39213bd629d43deb117b6a6c40a"}, - {file = "lazy_object_proxy-1.5.2-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:1d33d6f789697f401b75ce08e73b1de567b947740f768376631079290118ad39"}, - {file = "lazy_object_proxy-1.5.2-cp37-cp37m-manylinux2014_aarch64.whl", hash = "sha256:57fb5c5504ddd45ed420b5b6461a78f58cbb0c1b0cbd9cd5a43ad30a4a3ee4d0"}, - {file = "lazy_object_proxy-1.5.2-cp37-cp37m-win32.whl", hash = "sha256:e7273c64bccfd9310e9601b8f4511d84730239516bada26a0c9846c9697617ef"}, - {file = "lazy_object_proxy-1.5.2-cp37-cp37m-win_amd64.whl", hash = "sha256:6f4e5e68b7af950ed7fdb594b3f19a0014a3ace0fedb86acb896e140ffb24302"}, - {file = "lazy_object_proxy-1.5.2-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:cadfa2c2cf54d35d13dc8d231253b7985b97d629ab9ca6e7d672c35539d38163"}, - {file = "lazy_object_proxy-1.5.2-cp38-cp38-manylinux2014_aarch64.whl", hash = "sha256:e7428977763150b4cf83255625a80a23dfdc94d43be7791ce90799d446b4e26f"}, - {file = "lazy_object_proxy-1.5.2-cp38-cp38-win32.whl", hash = "sha256:2f2de8f8ac0be3e40d17730e0600619d35c78c13a099ea91ef7fb4ad944ce694"}, - {file = "lazy_object_proxy-1.5.2-cp38-cp38-win_amd64.whl", hash = "sha256:38c3865bd220bd983fcaa9aa11462619e84a71233bafd9c880f7b1cb753ca7fa"}, - {file = "lazy_object_proxy-1.5.2-cp39-cp39-macosx_10_14_x86_64.whl", hash = "sha256:8a44e9901c0555f95ac401377032f6e6af66d8fc1fbfad77a7a8b1a826e0b93c"}, - {file = "lazy_object_proxy-1.5.2-cp39-cp39-manylinux1_x86_64.whl", hash = "sha256:fa7fb7973c622b9e725bee1db569d2c2ee64d2f9a089201c5e8185d482c7352d"}, - {file = "lazy_object_proxy-1.5.2-cp39-cp39-manylinux2014_aarch64.whl", hash = "sha256:71a1ef23f22fa8437974b2d60fedb947c99a957ad625f83f43fd3de70f77f458"}, - {file = "lazy_object_proxy-1.5.2-cp39-cp39-win32.whl", hash = "sha256:ef3f5e288aa57b73b034ce9c1f1ac753d968f9069cd0742d1d69c698a0167166"}, - {file = "lazy_object_proxy-1.5.2-cp39-cp39-win_amd64.whl", hash = "sha256:37d9c34b96cca6787fe014aeb651217944a967a5b165e2cacb6b858d2997ab84"}, + {file = "lazy-object-proxy-1.6.0.tar.gz", hash = "sha256:489000d368377571c6f982fba6497f2aa13c6d1facc40660963da62f5c379726"}, + {file = "lazy_object_proxy-1.6.0-cp27-cp27m-macosx_10_14_x86_64.whl", hash = "sha256:c6938967f8528b3668622a9ed3b31d145fab161a32f5891ea7b84f6b790be05b"}, + {file = "lazy_object_proxy-1.6.0-cp27-cp27m-win32.whl", hash = "sha256:ebfd274dcd5133e0afae738e6d9da4323c3eb021b3e13052d8cbd0e457b1256e"}, + {file = "lazy_object_proxy-1.6.0-cp27-cp27m-win_amd64.whl", hash = "sha256:ed361bb83436f117f9917d282a456f9e5009ea12fd6de8742d1a4752c3017e93"}, + {file = "lazy_object_proxy-1.6.0-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:d900d949b707778696fdf01036f58c9876a0d8bfe116e8d220cfd4b15f14e741"}, + {file = "lazy_object_proxy-1.6.0-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:5743a5ab42ae40caa8421b320ebf3a998f89c85cdc8376d6b2e00bd12bd1b587"}, + {file = "lazy_object_proxy-1.6.0-cp36-cp36m-manylinux2014_aarch64.whl", hash = "sha256:bf34e368e8dd976423396555078def5cfc3039ebc6fc06d1ae2c5a65eebbcde4"}, + {file = "lazy_object_proxy-1.6.0-cp36-cp36m-win32.whl", hash = "sha256:b579f8acbf2bdd9ea200b1d5dea36abd93cabf56cf626ab9c744a432e15c815f"}, + {file = "lazy_object_proxy-1.6.0-cp36-cp36m-win_amd64.whl", hash = "sha256:4f60460e9f1eb632584c9685bccea152f4ac2130e299784dbaf9fae9f49891b3"}, + {file = "lazy_object_proxy-1.6.0-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:d7124f52f3bd259f510651450e18e0fd081ed82f3c08541dffc7b94b883aa981"}, + {file = "lazy_object_proxy-1.6.0-cp37-cp37m-manylinux2014_aarch64.whl", hash = "sha256:22ddd618cefe54305df49e4c069fa65715be4ad0e78e8d252a33debf00f6ede2"}, + {file = "lazy_object_proxy-1.6.0-cp37-cp37m-win32.whl", hash = "sha256:9d397bf41caad3f489e10774667310d73cb9c4258e9aed94b9ec734b34b495fd"}, + {file = "lazy_object_proxy-1.6.0-cp37-cp37m-win_amd64.whl", hash = "sha256:24a5045889cc2729033b3e604d496c2b6f588c754f7a62027ad4437a7ecc4837"}, + {file = "lazy_object_proxy-1.6.0-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:17e0967ba374fc24141738c69736da90e94419338fd4c7c7bef01ee26b339653"}, + {file = "lazy_object_proxy-1.6.0-cp38-cp38-manylinux2014_aarch64.whl", hash = "sha256:410283732af311b51b837894fa2f24f2c0039aa7f220135192b38fcc42bd43d3"}, + {file = "lazy_object_proxy-1.6.0-cp38-cp38-win32.whl", hash = "sha256:85fb7608121fd5621cc4377a8961d0b32ccf84a7285b4f1d21988b2eae2868e8"}, + {file = "lazy_object_proxy-1.6.0-cp38-cp38-win_amd64.whl", hash = "sha256:d1c2676e3d840852a2de7c7d5d76407c772927addff8d742b9808fe0afccebdf"}, + {file = "lazy_object_proxy-1.6.0-cp39-cp39-macosx_10_14_x86_64.whl", hash = "sha256:b865b01a2e7f96db0c5d12cfea590f98d8c5ba64ad222300d93ce6ff9138bcad"}, + {file = "lazy_object_proxy-1.6.0-cp39-cp39-manylinux1_x86_64.whl", hash = "sha256:4732c765372bd78a2d6b2150a6e99d00a78ec963375f236979c0626b97ed8e43"}, + {file = "lazy_object_proxy-1.6.0-cp39-cp39-manylinux2014_aarch64.whl", hash = "sha256:9698110e36e2df951c7c36b6729e96429c9c32b3331989ef19976592c5f3c77a"}, + {file = "lazy_object_proxy-1.6.0-cp39-cp39-win32.whl", hash = "sha256:1fee665d2638491f4d6e55bd483e15ef21f6c8c2095f235fef72601021e64f61"}, + {file = "lazy_object_proxy-1.6.0-cp39-cp39-win_amd64.whl", hash = "sha256:f5144c75445ae3ca2057faac03fda5a902eff196702b0a24daf1d6ce0650514b"}, ] mccabe = [ {file = "mccabe-0.6.1-py2.py3-none-any.whl", hash = "sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42"}, @@ -202,20 +197,28 @@ mslex = [ {file = "mslex-0.3.0.tar.gz", hash = "sha256:4a1ac3f25025cad78ad2fe499dd16d42759f7a3801645399cce5c404415daa97"}, ] mypy = [ - {file = "mypy-0.761-cp35-cp35m-macosx_10_6_x86_64.whl", hash = "sha256:7f672d02fffcbace4db2b05369142e0506cdcde20cea0e07c7c2171c4fd11dd6"}, - {file = "mypy-0.761-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:87c556fb85d709dacd4b4cb6167eecc5bbb4f0a9864b69136a0d4640fdc76a36"}, - {file = "mypy-0.761-cp35-cp35m-win_amd64.whl", hash = "sha256:c6d27bd20c3ba60d5b02f20bd28e20091d6286a699174dfad515636cb09b5a72"}, - {file = "mypy-0.761-cp36-cp36m-macosx_10_6_x86_64.whl", hash = "sha256:4b9365ade157794cef9685791032521233729cb00ce76b0ddc78749abea463d2"}, - {file = "mypy-0.761-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:634aef60b4ff0f650d3e59d4374626ca6153fcaff96ec075b215b568e6ee3cb0"}, - {file = "mypy-0.761-cp36-cp36m-win_amd64.whl", hash = "sha256:53ea810ae3f83f9c9b452582261ea859828a9ed666f2e1ca840300b69322c474"}, - {file = "mypy-0.761-cp37-cp37m-macosx_10_6_x86_64.whl", hash = "sha256:0a9a45157e532da06fe56adcfef8a74629566b607fa2c1ac0122d1ff995c748a"}, - {file = "mypy-0.761-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:7eadc91af8270455e0d73565b8964da1642fe226665dd5c9560067cd64d56749"}, - {file = "mypy-0.761-cp37-cp37m-win_amd64.whl", hash = "sha256:e2bb577d10d09a2d8822a042a23b8d62bc3b269667c9eb8e60a6edfa000211b1"}, - {file = "mypy-0.761-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:2c35cae79ceb20d47facfad51f952df16c2ae9f45db6cb38405a3da1cf8fc0a7"}, - {file = "mypy-0.761-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:f97a605d7c8bc2c6d1172c2f0d5a65b24142e11a58de689046e62c2d632ca8c1"}, - {file = "mypy-0.761-cp38-cp38-win_amd64.whl", hash = "sha256:a6bd44efee4dc8c3324c13785a9dc3519b3ee3a92cada42d2b57762b7053b49b"}, - {file = "mypy-0.761-py3-none-any.whl", hash = "sha256:7e396ce53cacd5596ff6d191b47ab0ea18f8e0ec04e15d69728d530e86d4c217"}, - {file = "mypy-0.761.tar.gz", hash = "sha256:85baab8d74ec601e86134afe2bcccd87820f79d2f8d5798c889507d1088287bf"}, + {file = "mypy-0.812-cp35-cp35m-macosx_10_9_x86_64.whl", hash = "sha256:a26f8ec704e5a7423c8824d425086705e381b4f1dfdef6e3a1edab7ba174ec49"}, + {file = "mypy-0.812-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:28fb5479c494b1bab244620685e2eb3c3f988d71fd5d64cc753195e8ed53df7c"}, + {file = "mypy-0.812-cp35-cp35m-manylinux2010_x86_64.whl", hash = "sha256:9743c91088d396c1a5a3c9978354b61b0382b4e3c440ce83cf77994a43e8c521"}, + {file = "mypy-0.812-cp35-cp35m-win_amd64.whl", hash = "sha256:d7da2e1d5f558c37d6e8c1246f1aec1e7349e4913d8fb3cb289a35de573fe2eb"}, + {file = "mypy-0.812-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:4eec37370483331d13514c3f55f446fc5248d6373e7029a29ecb7b7494851e7a"}, + {file = "mypy-0.812-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:d65cc1df038ef55a99e617431f0553cd77763869eebdf9042403e16089fe746c"}, + {file = "mypy-0.812-cp36-cp36m-manylinux2010_x86_64.whl", hash = "sha256:61a3d5b97955422964be6b3baf05ff2ce7f26f52c85dd88db11d5e03e146a3a6"}, + {file = "mypy-0.812-cp36-cp36m-win_amd64.whl", hash = "sha256:25adde9b862f8f9aac9d2d11971f226bd4c8fbaa89fb76bdadb267ef22d10064"}, + {file = "mypy-0.812-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:552a815579aa1e995f39fd05dde6cd378e191b063f031f2acfe73ce9fb7f9e56"}, + {file = "mypy-0.812-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:499c798053cdebcaa916eef8cd733e5584b5909f789de856b482cd7d069bdad8"}, + {file = "mypy-0.812-cp37-cp37m-manylinux2010_x86_64.whl", hash = "sha256:5873888fff1c7cf5b71efbe80e0e73153fe9212fafdf8e44adfe4c20ec9f82d7"}, + {file = "mypy-0.812-cp37-cp37m-win_amd64.whl", hash = "sha256:9f94aac67a2045ec719ffe6111df543bac7874cee01f41928f6969756e030564"}, + {file = "mypy-0.812-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:d23e0ea196702d918b60c8288561e722bf437d82cb7ef2edcd98cfa38905d506"}, + {file = "mypy-0.812-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:674e822aa665b9fd75130c6c5f5ed9564a38c6cea6a6432ce47eafb68ee578c5"}, + {file = "mypy-0.812-cp38-cp38-manylinux2010_x86_64.whl", hash = "sha256:abf7e0c3cf117c44d9285cc6128856106183938c68fd4944763003decdcfeb66"}, + {file = "mypy-0.812-cp38-cp38-win_amd64.whl", hash = "sha256:0d0a87c0e7e3a9becdfbe936c981d32e5ee0ccda3e0f07e1ef2c3d1a817cf73e"}, + {file = "mypy-0.812-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:7ce3175801d0ae5fdfa79b4f0cfed08807af4d075b402b7e294e6aa72af9aa2a"}, + {file = "mypy-0.812-cp39-cp39-manylinux1_x86_64.whl", hash = "sha256:b09669bcda124e83708f34a94606e01b614fa71931d356c1f1a5297ba11f110a"}, + {file = "mypy-0.812-cp39-cp39-manylinux2010_x86_64.whl", hash = "sha256:33f159443db0829d16f0a8d83d94df3109bb6dd801975fe86bacb9bf71628e97"}, + {file = "mypy-0.812-cp39-cp39-win_amd64.whl", hash = "sha256:3f2aca7f68580dc2508289c729bd49ee929a436208d2b2b6aab15745a70a57df"}, + {file = "mypy-0.812-py3-none-any.whl", hash = "sha256:2f9b3407c58347a452fc0736861593e105139b905cca7d097e413453a1d650b4"}, + {file = "mypy-0.812.tar.gz", hash = "sha256:cd07039aa5df222037005b08fbbfd69b3ab0b0bd7a07d7906de75ae52c4e3119"}, ] mypy-extensions = [ {file = "mypy_extensions-0.4.3-py2.py3-none-any.whl", hash = "sha256:090fedd75945a69ae91ce1303b5824f428daf5a028d2f6ab8a299250a846f15d"}, @@ -252,8 +255,8 @@ psutil = [ {file = "psutil-5.8.0.tar.gz", hash = "sha256:0c9ccb99ab76025f2f0bbecf341d4656e9c1351db8cc8a03ccd62e318ab4b5c6"}, ] pylint = [ - {file = "pylint-2.7.2-py3-none-any.whl", hash = "sha256:d09b0b07ba06bcdff463958f53f23df25e740ecd81895f7d2699ec04bbd8dc3b"}, - {file = "pylint-2.7.2.tar.gz", hash = "sha256:0e21d3b80b96740909d77206d741aa3ce0b06b41be375d92e1f3244a274c1f8a"}, + {file = "pylint-2.8.2-py3-none-any.whl", hash = "sha256:f7e2072654a6b6afdf5e2fb38147d3e2d2d43c89f648637baab63e026481279b"}, + {file = "pylint-2.8.2.tar.gz", hash = "sha256:586d8fa9b1891f4b725f587ef267abe2a1bad89d6b184520c7f07a253dd6e217"}, ] rope = [ {file = "rope-0.14.0-py2-none-any.whl", hash = "sha256:6b728fdc3e98a83446c27a91fc5d56808a004f8beab7a31ab1d7224cecc7d969"}, @@ -265,41 +268,41 @@ toml = [ {file = "toml-0.10.2.tar.gz", hash = "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f"}, ] typed-ast = [ - {file = "typed_ast-1.4.2-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:7703620125e4fb79b64aa52427ec192822e9f45d37d4b6625ab37ef403e1df70"}, - {file = "typed_ast-1.4.2-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:c9aadc4924d4b5799112837b226160428524a9a45f830e0d0f184b19e4090487"}, - {file = "typed_ast-1.4.2-cp35-cp35m-manylinux2014_aarch64.whl", hash = "sha256:9ec45db0c766f196ae629e509f059ff05fc3148f9ffd28f3cfe75d4afb485412"}, - {file = "typed_ast-1.4.2-cp35-cp35m-win32.whl", hash = "sha256:85f95aa97a35bdb2f2f7d10ec5bbdac0aeb9dafdaf88e17492da0504de2e6400"}, - {file = "typed_ast-1.4.2-cp35-cp35m-win_amd64.whl", hash = "sha256:9044ef2df88d7f33692ae3f18d3be63dec69c4fb1b5a4a9ac950f9b4ba571606"}, - {file = "typed_ast-1.4.2-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:c1c876fd795b36126f773db9cbb393f19808edd2637e00fd6caba0e25f2c7b64"}, - {file = "typed_ast-1.4.2-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:5dcfc2e264bd8a1db8b11a892bd1647154ce03eeba94b461effe68790d8b8e07"}, - {file = "typed_ast-1.4.2-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:8db0e856712f79c45956da0c9a40ca4246abc3485ae0d7ecc86a20f5e4c09abc"}, - {file = "typed_ast-1.4.2-cp36-cp36m-manylinux2014_aarch64.whl", hash = "sha256:d003156bb6a59cda9050e983441b7fa2487f7800d76bdc065566b7d728b4581a"}, - {file = "typed_ast-1.4.2-cp36-cp36m-win32.whl", hash = "sha256:4c790331247081ea7c632a76d5b2a265e6d325ecd3179d06e9cf8d46d90dd151"}, - {file = "typed_ast-1.4.2-cp36-cp36m-win_amd64.whl", hash = "sha256:d175297e9533d8d37437abc14e8a83cbc68af93cc9c1c59c2c292ec59a0697a3"}, - {file = "typed_ast-1.4.2-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:cf54cfa843f297991b7388c281cb3855d911137223c6b6d2dd82a47ae5125a41"}, - {file = "typed_ast-1.4.2-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:b4fcdcfa302538f70929eb7b392f536a237cbe2ed9cba88e3bf5027b39f5f77f"}, - {file = "typed_ast-1.4.2-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:987f15737aba2ab5f3928c617ccf1ce412e2e321c77ab16ca5a293e7bbffd581"}, - {file = "typed_ast-1.4.2-cp37-cp37m-manylinux2014_aarch64.whl", hash = "sha256:37f48d46d733d57cc70fd5f30572d11ab8ed92da6e6b28e024e4a3edfb456e37"}, - {file = "typed_ast-1.4.2-cp37-cp37m-win32.whl", hash = "sha256:36d829b31ab67d6fcb30e185ec996e1f72b892255a745d3a82138c97d21ed1cd"}, - {file = "typed_ast-1.4.2-cp37-cp37m-win_amd64.whl", hash = "sha256:8368f83e93c7156ccd40e49a783a6a6850ca25b556c0fa0240ed0f659d2fe496"}, - {file = "typed_ast-1.4.2-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:963c80b583b0661918718b095e02303d8078950b26cc00b5e5ea9ababe0de1fc"}, - {file = "typed_ast-1.4.2-cp38-cp38-manylinux1_i686.whl", hash = "sha256:e683e409e5c45d5c9082dc1daf13f6374300806240719f95dc783d1fc942af10"}, - {file = "typed_ast-1.4.2-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:84aa6223d71012c68d577c83f4e7db50d11d6b1399a9c779046d75e24bed74ea"}, - {file = "typed_ast-1.4.2-cp38-cp38-manylinux2014_aarch64.whl", hash = "sha256:a38878a223bdd37c9709d07cd357bb79f4c760b29210e14ad0fb395294583787"}, - {file = "typed_ast-1.4.2-cp38-cp38-win32.whl", hash = "sha256:a2c927c49f2029291fbabd673d51a2180038f8cd5a5b2f290f78c4516be48be2"}, - {file = "typed_ast-1.4.2-cp38-cp38-win_amd64.whl", hash = "sha256:c0c74e5579af4b977c8b932f40a5464764b2f86681327410aa028a22d2f54937"}, - {file = "typed_ast-1.4.2-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:07d49388d5bf7e863f7fa2f124b1b1d89d8aa0e2f7812faff0a5658c01c59aa1"}, - {file = "typed_ast-1.4.2-cp39-cp39-manylinux1_i686.whl", hash = "sha256:240296b27397e4e37874abb1df2a608a92df85cf3e2a04d0d4d61055c8305ba6"}, - {file = "typed_ast-1.4.2-cp39-cp39-manylinux1_x86_64.whl", hash = "sha256:d746a437cdbca200622385305aedd9aef68e8a645e385cc483bdc5e488f07166"}, - {file = "typed_ast-1.4.2-cp39-cp39-manylinux2014_aarch64.whl", hash = "sha256:14bf1522cdee369e8f5581238edac09150c765ec1cb33615855889cf33dcb92d"}, - {file = "typed_ast-1.4.2-cp39-cp39-win32.whl", hash = "sha256:cc7b98bf58167b7f2db91a4327da24fb93368838eb84a44c472283778fc2446b"}, - {file = "typed_ast-1.4.2-cp39-cp39-win_amd64.whl", hash = "sha256:7147e2a76c75f0f64c4319886e7639e490fee87c9d25cb1d4faef1d8cf83a440"}, - {file = "typed_ast-1.4.2.tar.gz", hash = "sha256:9fc0b3cb5d1720e7141d103cf4819aea239f7d136acf9ee4a69b047b7986175a"}, + {file = "typed_ast-1.4.3-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:2068531575a125b87a41802130fa7e29f26c09a2833fea68d9a40cf33902eba6"}, + {file = "typed_ast-1.4.3-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:c907f561b1e83e93fad565bac5ba9c22d96a54e7ea0267c708bffe863cbe4075"}, + {file = "typed_ast-1.4.3-cp35-cp35m-manylinux2014_aarch64.whl", hash = "sha256:1b3ead4a96c9101bef08f9f7d1217c096f31667617b58de957f690c92378b528"}, + {file = "typed_ast-1.4.3-cp35-cp35m-win32.whl", hash = "sha256:dde816ca9dac1d9c01dd504ea5967821606f02e510438120091b84e852367428"}, + {file = "typed_ast-1.4.3-cp35-cp35m-win_amd64.whl", hash = "sha256:777a26c84bea6cd934422ac2e3b78863a37017618b6e5c08f92ef69853e765d3"}, + {file = "typed_ast-1.4.3-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:f8afcf15cc511ada719a88e013cec87c11aff7b91f019295eb4530f96fe5ef2f"}, + {file = "typed_ast-1.4.3-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:52b1eb8c83f178ab787f3a4283f68258525f8d70f778a2f6dd54d3b5e5fb4341"}, + {file = "typed_ast-1.4.3-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:01ae5f73431d21eead5015997ab41afa53aa1fbe252f9da060be5dad2c730ace"}, + {file = "typed_ast-1.4.3-cp36-cp36m-manylinux2014_aarch64.whl", hash = "sha256:c190f0899e9f9f8b6b7863debfb739abcb21a5c054f911ca3596d12b8a4c4c7f"}, + {file = "typed_ast-1.4.3-cp36-cp36m-win32.whl", hash = "sha256:398e44cd480f4d2b7ee8d98385ca104e35c81525dd98c519acff1b79bdaac363"}, + {file = "typed_ast-1.4.3-cp36-cp36m-win_amd64.whl", hash = "sha256:bff6ad71c81b3bba8fa35f0f1921fb24ff4476235a6e94a26ada2e54370e6da7"}, + {file = "typed_ast-1.4.3-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:0fb71b8c643187d7492c1f8352f2c15b4c4af3f6338f21681d3681b3dc31a266"}, + {file = "typed_ast-1.4.3-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:760ad187b1041a154f0e4d0f6aae3e40fdb51d6de16e5c99aedadd9246450e9e"}, + {file = "typed_ast-1.4.3-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:5feca99c17af94057417d744607b82dd0a664fd5e4ca98061480fd8b14b18d04"}, + {file = "typed_ast-1.4.3-cp37-cp37m-manylinux2014_aarch64.whl", hash = "sha256:95431a26309a21874005845c21118c83991c63ea800dd44843e42a916aec5899"}, + {file = "typed_ast-1.4.3-cp37-cp37m-win32.whl", hash = "sha256:aee0c1256be6c07bd3e1263ff920c325b59849dc95392a05f258bb9b259cf39c"}, + {file = "typed_ast-1.4.3-cp37-cp37m-win_amd64.whl", hash = "sha256:9ad2c92ec681e02baf81fdfa056fe0d818645efa9af1f1cd5fd6f1bd2bdfd805"}, + {file = "typed_ast-1.4.3-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:b36b4f3920103a25e1d5d024d155c504080959582b928e91cb608a65c3a49e1a"}, + {file = "typed_ast-1.4.3-cp38-cp38-manylinux1_i686.whl", hash = "sha256:067a74454df670dcaa4e59349a2e5c81e567d8d65458d480a5b3dfecec08c5ff"}, + {file = "typed_ast-1.4.3-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:7538e495704e2ccda9b234b82423a4038f324f3a10c43bc088a1636180f11a41"}, + {file = "typed_ast-1.4.3-cp38-cp38-manylinux2014_aarch64.whl", hash = "sha256:af3d4a73793725138d6b334d9d247ce7e5f084d96284ed23f22ee626a7b88e39"}, + {file = "typed_ast-1.4.3-cp38-cp38-win32.whl", hash = "sha256:f2362f3cb0f3172c42938946dbc5b7843c2a28aec307c49100c8b38764eb6927"}, + {file = "typed_ast-1.4.3-cp38-cp38-win_amd64.whl", hash = "sha256:dd4a21253f42b8d2b48410cb31fe501d32f8b9fbeb1f55063ad102fe9c425e40"}, + {file = "typed_ast-1.4.3-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:f328adcfebed9f11301eaedfa48e15bdece9b519fb27e6a8c01aa52a17ec31b3"}, + {file = "typed_ast-1.4.3-cp39-cp39-manylinux1_i686.whl", hash = "sha256:2c726c276d09fc5c414693a2de063f521052d9ea7c240ce553316f70656c84d4"}, + {file = "typed_ast-1.4.3-cp39-cp39-manylinux1_x86_64.whl", hash = "sha256:cae53c389825d3b46fb37538441f75d6aecc4174f615d048321b716df2757fb0"}, + {file = "typed_ast-1.4.3-cp39-cp39-manylinux2014_aarch64.whl", hash = "sha256:b9574c6f03f685070d859e75c7f9eeca02d6933273b5e69572e5ff9d5e3931c3"}, + {file = "typed_ast-1.4.3-cp39-cp39-win32.whl", hash = "sha256:209596a4ec71d990d71d5e0d312ac935d86930e6eecff6ccc7007fe54d703808"}, + {file = "typed_ast-1.4.3-cp39-cp39-win_amd64.whl", hash = "sha256:9c6d1a54552b5330bc657b7ef0eae25d00ba7ffe85d9ea8ae6540d2197a3788c"}, + {file = "typed_ast-1.4.3.tar.gz", hash = "sha256:fb1bbeac803adea29cedd70781399c99138358c26d05fcbd23c13016b7f5ec65"}, ] typing-extensions = [ - {file = "typing_extensions-3.7.4.3-py2-none-any.whl", hash = "sha256:dafc7639cde7f1b6e1acc0f457842a83e722ccca8eef5270af2d74792619a89f"}, - {file = "typing_extensions-3.7.4.3-py3-none-any.whl", hash = "sha256:7cb407020f00f7bfc3cb3e7881628838e69d8f3fcab2f64742a5e76b2f841918"}, - {file = "typing_extensions-3.7.4.3.tar.gz", hash = "sha256:99d4073b617d30288f569d3f13d2bd7548c3a7e4c8de87db09a9d29bb3a4a60c"}, + {file = "typing_extensions-3.10.0.0-py2-none-any.whl", hash = "sha256:0ac0f89795dd19de6b97debb0c6af1c70987fd80a2d62d1958f7e56fcc31b497"}, + {file = "typing_extensions-3.10.0.0-py3-none-any.whl", hash = "sha256:779383f6086d90c99ae41cf0ff39aac8a7937a9283ce0a414e5dd782f4c94a84"}, + {file = "typing_extensions-3.10.0.0.tar.gz", hash = "sha256:50b6f157849174217d0656f99dc82fe932884fb250826c18350e159ec6cdf342"}, ] wrapt = [ {file = "wrapt-1.12.1.tar.gz", hash = "sha256:b62ffa81fb85f4332a4f609cab4ac40709470da05643a082ec1eb88e6d9b97d7"}, diff --git a/pyproject.toml b/pyproject.toml index 30ad74a..1ba2b84 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,33 +24,34 @@ python = "^3.6" toml = "^0.10.0" psutil = "^5.7.2" mslex = "^0.3.0" +colorama = "^0.4.4" [tool.poetry.dev-dependencies] pylint = "^2.4.4" -mypy = "^0.761" +mypy = "^0.812" rope = "^0.14.0" psutil = "^5.6.7" [tool.taskipy.tasks] -test = "python -m unittest -v tests/test_*.py" +test = { cmd = "python -m unittest -v tests/test_*.py", help = "runs all tests" } post_test = "./task lint" -run_unittest_as_windows_sanity = "python -m unittest -v" +run_unittest_as_windows_sanity = { cmd = "python -m unittest -v", help = "this is a command that is run by our Windows CI on Github" } -lint = "./task lint_pylint && ./task lint_mypy" +lint = { cmd = "./task lint_pylint && ./task lint_mypy", help = "lint project with pylint for style and mypy for typings" } lint_pylint = "pylint tests taskipy" lint_mypy = "mypy tests taskipy" -publish = "poetry publish --build" +publish = { cmd = "poetry publish --build", help = "publishes taskipy to PyPI as is. do not use directly" } post_publish = "./task make_release_commit" -make_release_commit = "python ./.hooks/make_release_commit.py" +make_release_commit = { cmd = "python ./.hooks/make_release_commit.py", help = "creates a tagged commit for the release. do not use directly" } pre_publish_patch = "./task test" -publish_patch = "poetry version patch && ./task publish" +publish_patch = { cmd = "poetry version patch && ./task publish", help = "publishes a patch version, that has only fixes but no new features (x.x.PATCH)" } pre_publish_minor = "./task test" -publish_minor = "poetry version minor && ./task publish" +publish_minor = { cmd = "poetry version minor && ./task publish", help = "publishes a minor version, which has new fetures (x.MINOR.x)" } [build-system] requires = ["poetry>=0.12"] diff --git a/taskipy/exceptions.py b/taskipy/exceptions.py index 16d1e58..573967e 100644 --- a/taskipy/exceptions.py +++ b/taskipy/exceptions.py @@ -32,6 +32,15 @@ class TaskNotFoundError(TaskipyError): def __str__(self): return f'could not find task "{self.task}"' +class MalformedTaskError(TaskipyError): + def __init__(self, task_name: str, reason: str): + super().__init__() + self.task = task_name + self.reason = reason + + def __str__(self): + return f'the task "{self.task}" in the pyproject.toml file is malformed. reason: {self.reason}' + class InvalidUsageError(TaskipyError): exit_code = 127 diff --git a/taskipy/help.py b/taskipy/help.py new file mode 100644 index 0000000..77e3645 --- /dev/null +++ b/taskipy/help.py @@ -0,0 +1,33 @@ +import shutil +import textwrap +import colorama # type: ignore +from typing import List + +from taskipy.task import Task + + +class HelpFormatter: + def __init__(self, tasks: List[Task]): + self.__tasks = tasks + + def print(self, line_width=shutil.get_terminal_size().columns): + colorama.init() + + tasks_col = [task.name for task in self.__tasks] + longest_item_in_tasks_col = len(max(tasks_col, key=len)) + + desc_col_wrap_indent = ' ' * (longest_item_in_tasks_col + 1) + desc_col_width = line_width - len(desc_col_wrap_indent) + + for task in self.__tasks: + name_text = task.name + desc_text = task.description or task.command + + tasks_col_text = f'{name_text:<{longest_item_in_tasks_col}}' + desc_col_text = '\n'.join(textwrap.wrap(desc_text, + width=desc_col_width, + subsequent_indent=desc_col_wrap_indent)) + print(f'{self.__highlight(tasks_col_text)} {desc_col_text}') + + def __highlight(self, text: str): + return f'{colorama.Fore.CYAN}{text}{colorama.Style.RESET_ALL}' diff --git a/taskipy/pyproject.py b/taskipy/pyproject.py index 4bb599b..8486cb6 100644 --- a/taskipy/pyproject.py +++ b/taskipy/pyproject.py @@ -1,9 +1,11 @@ -from pathlib import Path -from typing import Any, MutableMapping, Union - import toml +from pathlib import Path +from typing import Any, Dict, MutableMapping, Optional, Union + +from taskipy.task import Task from taskipy.exceptions import ( + InvalidRunnerTypeError, MalformedPyProjectError, MissingPyProjectFileError, MissingTaskipyTasksSectionError, @@ -17,9 +19,11 @@ class PyProject: self.__items = PyProject.__load_toml_file(pyproject_path) @property - def tasks(self) -> dict: + def tasks(self) -> Dict[str, Task]: try: - return self.__items['tool']['taskipy']['tasks'] + return { + task_name: Task(task_name, task_toml_contents) for + task_name, task_toml_contents in self.__items['tool']['taskipy']['tasks'].items() } except KeyError: raise MissingTaskipyTasksSectionError() @@ -30,6 +34,18 @@ class PyProject: except KeyError: raise MissingTaskipySettingsSectionError() + @property + def runner(self) -> Optional[str]: + try: + runner = self.settings['runner'] + + if not isinstance(runner, str): + raise InvalidRunnerTypeError() + + return runner.strip() + except (KeyError, MissingTaskipySettingsSectionError): + return None + @staticmethod def __load_toml_file(file_path: Union[str, Path]) -> MutableMapping[str, Any]: try: diff --git a/taskipy/task.py b/taskipy/task.py index 05e6329..c357b98 100644 --- a/taskipy/task.py +++ b/taskipy/task.py @@ -1,71 +1,44 @@ -import platform -import shlex -import mslex # type: ignore -from typing import Optional, List - -from taskipy.exceptions import ( - InvalidRunnerTypeError, - TaskNotFoundError, - MissingTaskipySettingsSectionError -) -from taskipy.pyproject import PyProject +from taskipy.exceptions import MalformedTaskError class Task: - def __init__(self, task_name: str, args: List[str], project: PyProject): - self.project = project - self.name = task_name - self.args = args - - def __str__(self) -> str: - return self.name + def __init__(self, task_name: str, task_toml_contents: object): + self.__task_name = task_name + self.__task_command = self.__extract_task_command(task_toml_contents) + self.__task_description = self.__extract_task_description(task_toml_contents) @property - def pre_task(self) -> Optional[str]: - return self.__find_hooks('pre') + def name(self): + return self.__task_name @property - def post_task(self) -> Optional[str]: - return self.__find_hooks('post') + def command(self): + return self.__task_command @property - def commands(self) -> List[str]: - try: - command = self.project.tasks[self.name] - commands = [] - commands.append( - ' '.join([command] + [self.__quote_arg(arg) for arg in self.args]) - ) - - if self.pre_task: - commands.insert(0, self.pre_task) + def description(self): + return self.__task_description - if self.post_task: - commands.append(self.post_task) + def __extract_task_command(self, task_toml_contents: object) -> str: + if isinstance(task_toml_contents, str): + return task_toml_contents - return commands - except KeyError: - raise TaskNotFoundError(self.name) - - @property - def runner(self) -> Optional[str]: - try: - runner = self.project.settings['runner'] + if isinstance(task_toml_contents, dict): + try: + return task_toml_contents['cmd'] + except KeyError: + raise MalformedTaskError(self.__task_name, 'the task item does not have the "cmd" property') - if not isinstance(runner, str): - raise InvalidRunnerTypeError() + raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, help }') - return runner.strip() - except (KeyError, MissingTaskipySettingsSectionError): - return None + def __extract_task_description(self, task_toml_contents: object) -> str: + if isinstance(task_toml_contents, str): + return '' - def __find_hooks(self, hook_type: str) -> Optional[str]: - try: - return self.project.tasks[f'{hook_type}_{self.name}'] - except KeyError: - return None + if isinstance(task_toml_contents, dict): + try: + return task_toml_contents['help'] + except KeyError: + return '' - def __quote_arg(self, arg: str) -> str: - if platform.system() == 'Windows': - return mslex.quote(arg) - return shlex.quote(arg) + raise MalformedTaskError(self.__task_name, 'tasks must be strings, or dicts that contain { cmd, help }') diff --git a/taskipy/task_runner.py b/taskipy/task_runner.py index 444adbd..0aa3aa1 100644 --- a/taskipy/task_runner.py +++ b/taskipy/task_runner.py @@ -1,59 +1,101 @@ import sys - +import platform import psutil # type: ignore import signal import subprocess +import shlex +import mslex # type: ignore from pathlib import Path -from typing import List, Union +from typing import List, Union, Optional from taskipy.pyproject import PyProject +from taskipy.exceptions import TaskNotFoundError from taskipy.task import Task +from taskipy.help import HelpFormatter class TaskRunner: def __init__(self, cwd: Union[str, Path]): working_dir = cwd if isinstance(cwd, Path) else Path(cwd) self.__working_dir = working_dir - self.project = PyProject(working_dir) + self.__project = PyProject(working_dir) def list(self): """lists tasks to stdout""" - longest = len(max(self.project.tasks, key=len)) - for k, v in self.project.tasks.items(): - print(f'{k:<{longest}} {v}') + formatter = HelpFormatter(self.__project.tasks.values()) + formatter.print() def run(self, task_name: str, args: List[str]) -> int: - task = Task(task_name, args, self.project) - - for command in task.commands: - if task.runner is not None: - command = f'{task.runner} {command}' - process = subprocess.Popen( - command, shell=True, cwd=self.__working_dir - ) - - def send_signal_to_task_process(signum: int, frame) -> None: # pylint: disable=unused-argument - # pylint: disable=W0640 - psutil_process_wrapper = psutil.Process(process.pid) - is_direct_subprocess_a_shell_process = sys.platform != 'darwin' # pylint: disable=C0103 - - if is_direct_subprocess_a_shell_process: - # A shell is created because of Popen(..., shell=True) on linux only - # We want here to kill shell's child - sub_processes_of_taskipy_shell = psutil_process_wrapper.children() - for child_process in sub_processes_of_taskipy_shell: - child_process.send_signal(signum) - else: - process.send_signal(signum) - - signal.signal(signal.SIGTERM, send_signal_to_task_process) - - try: - process.wait() - except KeyboardInterrupt: - pass - - if process.returncode != 0: - return process.returncode + try: + task = self.__project.tasks[task_name] + except KeyError: + raise TaskNotFoundError(task_name) + + pre_task = self.__pre_task(task_name) + if pre_task is not None: + exit_code = self.__run_command_and_return_exit_code(pre_task.command) + if exit_code != 0: + return exit_code + + exit_code = self.__run_command_and_return_exit_code(task.command, args) + if exit_code != 0: + return exit_code + + post_task = self.__post_task(task_name) + if post_task is not None: + exit_code = self.__run_command_and_return_exit_code(post_task.command) + if exit_code != 0: + return exit_code return 0 + + def __run_command_and_return_exit_code(self, command: str, args: List[str] = None) -> int: + if args is None: + args = [] + + if self.__project.runner is not None: + command = f'{self.__project.runner} {command}' + + def quote_arg(arg: str) -> str: + if platform.system() == 'Windows': + return mslex.quote(arg) + return shlex.quote(arg) + + def send_signal_to_task_process(signum: int, _frame) -> None: + # pylint: disable=W0640 + psutil_process_wrapper = psutil.Process(process.pid) + is_direct_subprocess_a_shell_process = sys.platform != 'darwin' # pylint: disable=C0103 + + if is_direct_subprocess_a_shell_process: + # A shell is created because of Popen(..., shell=True) on linux only + # We want here to kill shell's child + sub_processes_of_taskipy_shell = psutil_process_wrapper.children() + for child_process in sub_processes_of_taskipy_shell: + child_process.send_signal(signum) + else: + process.send_signal(signum) + + command_with_args = ' '.join([command] + [quote_arg(arg) for arg in args]) + process = subprocess.Popen(command_with_args, + shell=True, + cwd=self.__working_dir) + signal.signal(signal.SIGTERM, send_signal_to_task_process) + + try: + process.wait() + except KeyboardInterrupt: + pass + + return process.returncode + + def __pre_task(self, task_name: str): + return self.__find_hooks('pre', task_name) + + def __post_task(self, task_name: str): + return self.__find_hooks('post', task_name) + + def __find_hooks(self, hook_type: str, task_name: str) -> Optional[Task]: + try: + return self.__project.tasks[f'{hook_type}_{task_name}'] + except KeyError: + return None diff --git a/tests/test_taskipy.py b/tests/test_taskipy.py index cac0809..84b767a 100644 --- a/tests/test_taskipy.py +++ b/tests/test_taskipy.py @@ -65,6 +65,11 @@ class TaskipyTestCase(unittest.TestCase): full_string.find(substr_b), msg=f'Expected \n "{substr_a}"\nto appear before\n "{substr_b}"\nin\n "{full_string}"') + def assertTerminalTextEqual(self, expected: str, actual: str): + expected_without_ansi_chars = expected.encode('ascii', 'ignore') + actual_without_ansi_chars = actual.encode('ascii', 'ignore') + self.assertEqual(expected_without_ansi_chars, actual_without_ansi_chars) + class RunTaskTestCase(TaskipyTestCase): def test_running_task(self): @@ -94,6 +99,7 @@ class RunTaskTestCase(TaskipyTestCase): self.assertSubstr('hello stderr', stderr) + class TaskPrePostHooksTestCase(TaskipyTestCase): def test_running_pre_task_hook(self): cwd = self.create_test_dir_from_fixture('project_with_pre_post_task_hooks') @@ -177,23 +183,23 @@ class PassArgumentsTestCase(TaskipyTestCase): class ListTasksTestCase(TaskipyTestCase): project_tasks_output = "\n".join([ - "one echo first task", - "two echo second task", - "three echo third task", + "one echo first task", + "two echo second task", + "three echo third task", ]) def test_running_task_list(self): cwd = self.create_test_dir_from_fixture('project_with_tasks_to_list') exit_code, stdout, _ = self.run_task('--list', cwd=cwd) - self.assertEqual(self.project_tasks_output, stdout.strip()) + self.assertTerminalTextEqual(self.project_tasks_output, stdout.strip()) self.assertEqual(exit_code, 0) def test_running_task_list_with_shorthand(self): cwd = self.create_test_dir_from_fixture('project_with_tasks_to_list') exit_code, stdout, _ = self.run_task('-l', cwd=cwd) - self.assertEqual(self.project_tasks_output, stdout.strip()) + self.assertTerminalTextEqual(self.project_tasks_output, stdout.strip()) self.assertEqual(exit_code, 0) def test_running_task_list_before_name(self): @@ -201,7 +207,7 @@ class ListTasksTestCase(TaskipyTestCase): # anything following the flag should be ignored exit_code, stdout, _ = self.run_task('--list', ['one'], cwd=cwd) - self.assertEqual(self.project_tasks_output, stdout.strip()) + self.assertTerminalTextEqual(self.project_tasks_output, stdout.strip()) self.assertEqual(exit_code, 0) def test_running_task_list_with_arg(self): @@ -210,8 +216,63 @@ class ListTasksTestCase(TaskipyTestCase): exit_code, stdout, _ = self.run_task('one', ['--list'], cwd=cwd) expected = "first task --list" - self.assertEqual(expected, stdout.strip()) + self.assertTerminalTextEqual(expected, stdout.strip()) + self.assertEqual(exit_code, 0) + + +class TaskDescriptionTestCase(TaskipyTestCase): + def test_running_task_with_description(self): + py_project_toml = ''' + [tool.taskipy.tasks] + print_age = { cmd = "echo age is 29", help = "prints the age" } + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + _, stdout, _ = self.run_task('print_age', cwd=cwd) + + self.assertSubstr('age is 29', stdout) + + def test_listing_task_with_description(self): + py_project_toml = ''' + [tool.taskipy.tasks] + print_age = { cmd = "echo age is 29", help = "prints the age" } + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + _, stdout, _ = self.run_task('--list', cwd=cwd) + + self.assertSubstr('prints the age', stdout) + + def test_reject_task_for_not_having_cmd(self): + py_project_toml = ''' + [tool.taskipy.tasks] + print_age = { help = "prints the age" } + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('print_age', cwd=cwd) + + self.assertEqual(exit_code, 1) + self.assertSubstr('the task item does not have the "cmd" property', stdout) + + def test_allow_task_to_not_have_help(self): + py_project_toml = ''' + [tool.taskipy.tasks] + print_age = { cmd = "echo age is 29" } + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('print_age', cwd=cwd) + self.assertEqual(exit_code, 0) + self.assertSubstr('age is 29', stdout) + + def test_reject_task_that_is_not_string_nor_object(self): + py_project_toml = ''' + [tool.taskipy.tasks] + print_age = 5 + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('print_age', cwd=cwd) + + self.assertEqual(exit_code, 1) + self.assertSubstr('tasks must be strings, or dicts that contain { cmd, help }', stdout) class TaskRunFailTestCase(TaskipyTestCase): |