qmk 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. # Add the QMK python libs to our path
  8. script_dir = os.path.dirname(os.path.realpath(__file__))
  9. qmk_dir = os.path.abspath(os.path.join(script_dir, '..'))
  10. python_lib_dir = os.path.abspath(os.path.join(qmk_dir, 'lib', 'python'))
  11. sys.path.append(python_lib_dir)
  12. # Make sure our modules have been setup
  13. with open(os.path.join(qmk_dir, 'requirements.txt'), 'r') as fd:
  14. for line in fd.readlines():
  15. line = line.strip().replace('<', '=').replace('>', '=')
  16. if line[0] == '#':
  17. continue
  18. if '#' in line:
  19. line = line.split('#')[0]
  20. module = line.split('=')[0] if '=' in line else line
  21. if module in ['pep8-naming']:
  22. # Not every module is importable by its own name.
  23. continue
  24. if not find_spec(module):
  25. print('Could not find module %s!' % module)
  26. print('Please run `pip3 install -r requirements.txt` to install the python dependencies.')
  27. exit(255)
  28. # Setup the CLI
  29. import milc # noqa
  30. milc.EMOJI_LOGLEVELS['INFO'] = '{fg_blue}Ψ{style_reset_all}'
  31. @milc.cli.entrypoint('QMK Helper Script')
  32. def qmk_main(cli):
  33. """The function that gets run when no subcommand is provided.
  34. """
  35. cli.print_help()
  36. def main():
  37. """Setup our environment and then call the CLI entrypoint.
  38. """
  39. # Change to the root of our checkout
  40. os.environ['ORIG_CWD'] = os.getcwd()
  41. os.chdir(qmk_dir)
  42. # Import the subcommands
  43. import qmk.cli # noqa
  44. # Execute
  45. return_code = milc.cli()
  46. if return_code is False:
  47. exit(1)
  48. elif return_code is not True and isinstance(return_code, int):
  49. if return_code < 0 or return_code > 255:
  50. milc.cli.log.error('Invalid return_code: %d', return_code)
  51. exit(255)
  52. exit(return_code)
  53. exit(0)
  54. if __name__ == '__main__':
  55. main()