qmk 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python3
  2. """CLI wrapper for running QMK commands.
  3. """
  4. import os
  5. import sys
  6. from importlib.util import find_spec
  7. from time import strftime
  8. from pathlib import Path
  9. # Add the QMK python libs to our path
  10. script_dir = Path(os.path.realpath(__file__)).parent
  11. qmk_dir = script_dir.parent
  12. python_lib_dir = Path(qmk_dir / 'lib' / 'python').resolve()
  13. sys.path.append(str(python_lib_dir))
  14. # Setup the CLI
  15. import milc # noqa
  16. def _check_modules(requirements):
  17. """ Check if the modules in the given requirements.txt are available.
  18. """
  19. with Path(qmk_dir / requirements).open() as fd:
  20. for line in fd.readlines():
  21. line = line.strip().replace('<', '=').replace('>', '=')
  22. if len(line) == 0 or line[0] == '#' or '-r' in line:
  23. continue
  24. if '#' in line:
  25. line = line.split('#')[0]
  26. module = dict()
  27. module['name'] = module['import'] = line.split('=')[0] if '=' in line else line
  28. # Not every module is importable by its own name.
  29. if module['name'] == "pep8-naming":
  30. module['import'] = "pep8ext_naming"
  31. if not find_spec(module['import']):
  32. print('Could not find module %s!' % module['name'])
  33. if developer:
  34. print('Please run `pip3 install -r requirements-dev.txt` to install the python development dependencies or turn off developer mode with `qmk config user.developer=None`.')
  35. print()
  36. else:
  37. print('Please run `pip3 install -r requirements.txt` to install the python dependencies.')
  38. print()
  39. exit(255)
  40. developer = False
  41. # Make sure our modules have been setup
  42. _check_modules('requirements.txt')
  43. # For developers additional modules are needed
  44. if milc.cli.config.user.developer:
  45. developer = True
  46. _check_modules('requirements-dev.txt')
  47. milc.EMOJI_LOGLEVELS['INFO'] = '{fg_blue}Ψ{style_reset_all}'
  48. @milc.cli.entrypoint('QMK Helper Script')
  49. def qmk_main(cli):
  50. """The function that gets run when no subcommand is provided.
  51. """
  52. cli.print_help()
  53. def main():
  54. """Setup our environment and then call the CLI entrypoint.
  55. """
  56. # Change to the root of our checkout
  57. os.environ['ORIG_CWD'] = os.getcwd()
  58. os.chdir(qmk_dir)
  59. # Import the subcommands
  60. import qmk.cli # noqa
  61. # Execute
  62. return_code = milc.cli()
  63. if return_code is False:
  64. exit(1)
  65. elif return_code is not True and isinstance(return_code, int):
  66. if return_code < 0 or return_code > 255:
  67. milc.cli.log.error('Invalid return_code: %d', return_code)
  68. exit(255)
  69. exit(return_code)
  70. exit(0)
  71. if __name__ == '__main__':
  72. main()