cd.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """Open a shell in the QMK Home directory
  2. """
  3. import sys
  4. import os
  5. import subprocess
  6. from milc import cli
  7. from qmk.path import under_qmk_firmware
  8. @cli.subcommand('Go to QMK Home')
  9. def cd(cli):
  10. """Go to QMK Home
  11. """
  12. if not sys.stdout.isatty():
  13. cli.log.error("This command is for interactive usage only. For non-interactive usage, 'cd $(qmk env QMK_HOME)' is more robust.")
  14. sys.exit(1)
  15. if not under_qmk_firmware():
  16. # Only do anything if the user is not under qmk_firmware already
  17. # in order to reduce the possibility of starting multiple shells
  18. cli.log.info("Spawning a subshell in your QMK_HOME directory.")
  19. cli.log.info("Type 'exit' to get back to the parent shell.")
  20. if not cli.platform.lower().startswith('windows'):
  21. # For Linux/Mac/etc
  22. # Check the user's login shell from 'passwd'
  23. # alternatively fall back to $SHELL env var
  24. # and finally to '/bin/bash'.
  25. import getpass
  26. import pwd
  27. shell = pwd.getpwnam(getpass.getuser()).pw_shell
  28. if not shell:
  29. shell = os.environ.get('SHELL', '/bin/bash')
  30. # Start the new subshell
  31. os.execl(shell, shell)
  32. else:
  33. # For Windows
  34. # Check the $SHELL env var
  35. # and fall back to '/usr/bin/bash'.
  36. qmk_env = os.environ.copy()
  37. # Set the prompt for the new shell
  38. qmk_env['MSYS2_PS1'] = qmk_env['PS1']
  39. # Start the new subshell
  40. subprocess.run([os.environ.get('SHELL', '/usr/bin/bash')], env=qmk_env)
  41. else:
  42. cli.log.info("Already within qmk_firmware directory.")