Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align our subprocess usage with current best practices. #12940

Merged
merged 6 commits into from
May 19, 2021

Conversation

skullydazed
Copy link
Member

Description

This switches most of our subprocess usage to MILC's cli.run(). There are still a couple places that use subprocess.run which I didn't want to change just now.

I added stdin=DEVNULL to most commands because this fixes a bug on windows where input is no longer accepted after a subprocess is run.

Types of Changes

  • Core
  • Bugfix

Checklist

  • My code follows the code style of this project: C, Python
  • I have read the PR Checklist document and have made the appropriate changes.
  • I have read the CONTRIBUTING document.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

@github-actions github-actions bot added cli qmk cli command python labels May 18, 2021
@skullydazed skullydazed requested a review from a team May 18, 2021 22:22
Copy link
Member

@fauxpark fauxpark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest on MSYS2 fails with:

======================================================================
FAIL: qmk.tests.test_cli_commands.test_c2json
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:/msys64/home/fauxpark/git/qmk_firmware_develop/lib/python/qmk/tests/test_cli_commands.py", line 198, in test_c2json
    check_returncode(result)
  File "C:/msys64/home/fauxpark/git/qmk_firmware_develop/lib/python/qmk/tests/test_cli_commands.py", line 31, in check_returncode
    assert result.returncode in expected
AssertionError
qmk.tests.test_cli_commands.test_c2json ... DEBUG:MILC:Running command: ['C:/msys64/usr/bin/zsh', '-c', 'qmk c2json -kb handwired/pytest/has_template -km default keyboards/handwired/pytest/has_template/keymaps/default/keymap.c']
`C:/msys64/usr/bin/zsh -c qmk c2json -kb handwired/pytest/has_template -km default keyboards/handwired/pytest/has_template/keymaps/default/keymap.c` stdout:
<class 'TypeError'>
ERROR expected string or bytes-like object
Traceback (most recent call last):
  File "C:/msys64/mingw64/lib/python3.8/site-packages/milc/milc.py", line 467, in __call__
    return self.__call__()
  File "C:/msys64/mingw64/lib/python3.8/site-packages/milc/milc.py", line 472, in __call__
    return self._entrypoint(self)
  File "C:/msys64/home/fauxpark/git/qmk_firmware_develop/lib/python/qmk/cli/c2json.py", line 40, in c2json
    keymap_json = qmk.keymap.c2json(cli.args.keyboard, cli.args.keymap, cli.args.filename, use_cpp=cli.args.no_cpp)
  File "C:/msys64/home/fauxpark/git/qmk_firmware_develop/lib/python/qmk/keymap.py", line 545, in c2json
    keymap_json = parse_keymap_c(keymap_file, use_cpp)
  File "C:/msys64/home/fauxpark/git/qmk_firmware_develop/lib/python/qmk/keymap.py", line 519, in parse_keymap_c
    keymap_file = _c_preprocess(keymap_file)
  File "C:/msys64/home/fauxpark/git/qmk_firmware_develop/lib/python/qmk/keymap.py", line 374, in _c_preprocess
    pre_processed_keymap = cli.run(cmd, stdin=stdin)
  File "C:/msys64/mingw64/lib/python3.8/site-packages/milc/milc.py", line 125, in run
    safecmd = ' '.join(safecmd)
  File "C:/msys64/mingw64/lib/python3.8/shlex.py", line 325, in quote
    if _find_unsafe(s) is None:
TypeError: expected string or bytes-like object

returncode: 255
FAIL

(I'm not sure whether it's related, though)

lib/python/qmk/os_helpers/__init__.py Outdated Show resolved Hide resolved
lib/python/qmk/os_helpers/__init__.py Outdated Show resolved Hide resolved
lib/python/qmk/keymap.py Show resolved Hide resolved
skullydazed and others added 2 commits May 18, 2021 19:03
Co-authored-by: Ryan <fauxpark@gmail.com>
@skullydazed skullydazed requested a review from a team May 19, 2021 02:04
lib/python/qmk/keymap.py Outdated Show resolved Hide resolved
@github-actions github-actions bot added the core label May 19, 2021
@skullydazed skullydazed requested review from fauxpark and a team May 19, 2021 16:01
@@ -371,7 +370,9 @@ def _c_preprocess(path, stdin=None):
Returns:
the stdout of the pre-processor
"""
pre_processed_keymap = qmk.commands.run(['cpp', path] if path else ['cpp'], stdin=stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
cmd = ['cpp', str(path)] if path else ['cpp']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If path is always an object of type Path then I think this should use .as_posix() instead, otherwise LGTM 👍

@skullydazed skullydazed merged commit db1eacd into master May 19, 2021
@skullydazed skullydazed deleted the fix_doctor_on_windows branch May 19, 2021 22:24
leland-kwong pushed a commit to leland-kwong/qmk_firmware that referenced this pull request May 23, 2021
* Align our subprocess usage with current best practices.

* remove unused import

* Apply suggestions from code review

Co-authored-by: Ryan <fauxpark@gmail.com>

* fix the cpp invocation for older python

* allow for unprompted installation

* make sure qmk new-keyboard works on windows

Co-authored-by: Ryan <fauxpark@gmail.com>
@sigprof sigprof mentioned this pull request May 31, 2021
14 tasks
HokieGeek pushed a commit to HokieGeek/qmk_firmware that referenced this pull request Jul 11, 2021
* Align our subprocess usage with current best practices.

* remove unused import

* Apply suggestions from code review

Co-authored-by: Ryan <fauxpark@gmail.com>

* fix the cpp invocation for older python

* allow for unprompted installation

* make sure qmk new-keyboard works on windows

Co-authored-by: Ryan <fauxpark@gmail.com>
nhongooi pushed a commit to nhongooi/qmk_firmware that referenced this pull request Dec 5, 2021
* Align our subprocess usage with current best practices.

* remove unused import

* Apply suggestions from code review

Co-authored-by: Ryan <fauxpark@gmail.com>

* fix the cpp invocation for older python

* allow for unprompted installation

* make sure qmk new-keyboard works on windows

Co-authored-by: Ryan <fauxpark@gmail.com>
BorisTestov pushed a commit to BorisTestov/qmk_firmware that referenced this pull request May 23, 2024
* Align our subprocess usage with current best practices.

* remove unused import

* Apply suggestions from code review

Co-authored-by: Ryan <fauxpark@gmail.com>

* fix the cpp invocation for older python

* allow for unprompted installation

* make sure qmk new-keyboard works on windows

Co-authored-by: Ryan <fauxpark@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli qmk cli command core python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants