You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.3 KiB

  1. #!/usr/bin/env python3
  2. """CLI wrapper for running QMK commands.
  3. """
  4. import os
  5. import subprocess
  6. import sys
  7. from glob import glob
  8. from time import strftime
  9. from importlib import import_module
  10. from importlib.util import find_spec
  11. # Add the QMK python libs to our path
  12. script_dir = os.path.dirname(os.path.realpath(__file__))
  13. qmk_dir = os.path.abspath(os.path.join(script_dir, '..'))
  14. python_lib_dir = os.path.abspath(os.path.join(qmk_dir, 'lib', 'python'))
  15. sys.path.append(python_lib_dir)
  16. # Change to the root of our checkout
  17. os.environ['ORIG_CWD'] = os.getcwd()
  18. os.chdir(qmk_dir)
  19. # Make sure our modules have been setup
  20. with open('requirements.txt', 'r') as fd:
  21. for line in fd.readlines():
  22. line = line.strip().replace('<', '=').replace('>', '=')
  23. if line[0] == '#':
  24. continue
  25. if '#' in line:
  26. line = line.split('#')[0]
  27. module = line.split('=')[0] if '=' in line else line
  28. if not find_spec(module):
  29. print('Your QMK build environment is not fully setup!\n')
  30. print('Please run `./util/qmk_install.sh` to setup QMK.')
  31. exit(255)
  32. # Figure out our version
  33. command = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags']
  34. result = subprocess.run(command, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  35. if result.returncode == 0:
  36. os.environ['QMK_VERSION'] = 'QMK ' + result.stdout.strip()
  37. else:
  38. os.environ['QMK_VERSION'] = 'QMK ' + strftime('%Y-%m-%d-%H:%M:%S')
  39. # Setup the CLI
  40. import milc
  41. milc.EMOJI_LOGLEVELS['INFO'] = '{fg_blue}Ψ{style_reset_all}'
  42. # If we were invoked as `qmk <cmd>` massage sys.argv into `qmk-<cmd>`.
  43. # This means we can't accept arguments to the qmk script itself.
  44. script_name = os.path.basename(sys.argv[0])
  45. if script_name == 'qmk':
  46. if len(sys.argv) == 1:
  47. milc.cli.log.error('No subcommand specified!\n')
  48. if len(sys.argv) == 1 or sys.argv[1] in ['-h', '--help']:
  49. milc.cli.echo('usage: qmk <subcommand> [...]')
  50. milc.cli.echo('\nsubcommands:')
  51. subcommands = glob(os.path.join(qmk_dir, 'bin', 'qmk-*'))
  52. for subcommand in sorted(subcommands):
  53. subcommand = os.path.basename(subcommand).split('-', 1)[1]
  54. milc.cli.echo('\t%s', subcommand)
  55. milc.cli.echo('\nqmk <subcommand> --help for more information')
  56. exit(1)
  57. if sys.argv[1] in ['-V', '--version']:
  58. milc.cli.echo(os.environ['QMK_VERSION'])
  59. exit(0)
  60. sys.argv[0] = script_name = '-'.join((script_name, sys.argv[1]))
  61. del sys.argv[1]
  62. # Look for which module to import
  63. if script_name == 'qmk':
  64. milc.cli.print_help()
  65. exit(0)
  66. elif not script_name.startswith('qmk-'):
  67. milc.cli.log.error('Invalid symlink, must start with "qmk-": %s', script_name)
  68. else:
  69. subcommand = script_name.replace('-', '.').replace('_', '.').split('.')
  70. subcommand.insert(1, 'cli')
  71. subcommand = '.'.join(subcommand)
  72. try:
  73. import_module(subcommand)
  74. except ModuleNotFoundError as e:
  75. if e.__class__.__name__ != subcommand:
  76. raise
  77. milc.cli.log.error('Invalid subcommand! Could not import %s.', subcommand)
  78. exit(1)
  79. if __name__ == '__main__':
  80. return_code = milc.cli()
  81. if return_code is False:
  82. exit(1)
  83. elif return_code is not True and isinstance(return_code, int) and return_code < 256:
  84. exit(return_code)
  85. else:
  86. exit(0)