qmk 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python3
  2. """CLI wrapper for running QMK commands.
  3. """
  4. import os
  5. import subprocess
  6. import sys
  7. from importlib.util import find_spec
  8. from time import strftime
  9. # Add the QMK python libs to our path
  10. script_dir = os.path.dirname(os.path.realpath(__file__))
  11. qmk_dir = os.path.abspath(os.path.join(script_dir, '..'))
  12. python_lib_dir = os.path.abspath(os.path.join(qmk_dir, 'lib', 'python'))
  13. sys.path.append(python_lib_dir)
  14. # Make sure our modules have been setup
  15. with open(os.path.join(qmk_dir, 'requirements.txt'), 'r') as fd:
  16. for line in fd.readlines():
  17. line = line.strip().replace('<', '=').replace('>', '=')
  18. if line[0] == '#':
  19. continue
  20. if '#' in line:
  21. line = line.split('#')[0]
  22. module = line.split('=')[0] if '=' in line else line
  23. if not find_spec(module):
  24. print('Could not find module %s!', module)
  25. print('Please run `pip3 install -r requirements.txt` to install the python dependencies.')
  26. exit(255)
  27. # Figure out our version
  28. # TODO(skullydazed/anyone): Find a method that doesn't involve git. This is slow in docker and on windows.
  29. command = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags']
  30. result = subprocess.run(command, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  31. if result.returncode == 0:
  32. os.environ['QMK_VERSION'] = result.stdout.strip()
  33. else:
  34. os.environ['QMK_VERSION'] = 'nogit-' + strftime('%Y-%m-%d-%H:%M:%S') + '-dirty'
  35. # Setup the CLI
  36. import milc
  37. milc.EMOJI_LOGLEVELS['INFO'] = '{fg_blue}Ψ{style_reset_all}'
  38. @milc.cli.entrypoint('QMK Helper Script')
  39. def qmk_main(cli):
  40. """The function that gets run when no subcommand is provided.
  41. """
  42. cli.print_help()
  43. def main():
  44. """Setup our environment and then call the CLI entrypoint.
  45. """
  46. # Change to the root of our checkout
  47. os.environ['ORIG_CWD'] = os.getcwd()
  48. os.chdir(qmk_dir)
  49. # Import the subcommands
  50. import qmk.cli
  51. # Execute
  52. return_code = milc.cli()
  53. if return_code is False:
  54. exit(1)
  55. elif return_code is not True and isinstance(return_code, int):
  56. if return_code < 0 or return_code > 255:
  57. milc.cli.log.error('Invalid return_code: %d', return_code)
  58. exit(255)
  59. exit(return_code)
  60. exit(0)
  61. if __name__ == '__main__':
  62. main()