2019-07-16 01:14:27 +06:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""CLI wrapper for running QMK commands.
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from importlib.util import find_spec
|
2019-11-28 02:27:06 +06:00
|
|
|
from pathlib import Path
|
2019-07-16 01:14:27 +06:00
|
|
|
|
|
|
|
# Add the QMK python libs to our path
|
2019-11-28 02:27:06 +06:00
|
|
|
script_dir = Path(os.path.realpath(__file__)).parent
|
|
|
|
qmk_dir = script_dir.parent
|
|
|
|
python_lib_dir = Path(qmk_dir / 'lib' / 'python').resolve()
|
|
|
|
sys.path.append(str(python_lib_dir))
|
|
|
|
|
2019-07-16 01:14:27 +06:00
|
|
|
|
2019-11-28 02:27:06 +06:00
|
|
|
def _check_modules(requirements):
|
|
|
|
""" Check if the modules in the given requirements.txt are available.
|
|
|
|
"""
|
|
|
|
with Path(qmk_dir / requirements).open() as fd:
|
|
|
|
for line in fd.readlines():
|
|
|
|
line = line.strip().replace('<', '=').replace('>', '=')
|
2019-07-16 01:14:27 +06:00
|
|
|
|
2020-03-23 00:48:30 +06:00
|
|
|
if len(line) == 0 or line[0] == '#' or line.startswith('-r'):
|
2019-11-28 02:27:06 +06:00
|
|
|
continue
|
2019-07-16 01:14:27 +06:00
|
|
|
|
2019-11-28 02:27:06 +06:00
|
|
|
if '#' in line:
|
|
|
|
line = line.split('#')[0]
|
|
|
|
|
|
|
|
module = dict()
|
2021-02-01 02:46:00 +06:00
|
|
|
module['name'] = line.split('=')[0] if '=' in line else line
|
|
|
|
module['import'] = module['name'].replace('-', '_')
|
2019-11-24 00:42:39 +06:00
|
|
|
|
|
|
|
# Not every module is importable by its own name.
|
2019-11-28 02:27:06 +06:00
|
|
|
if module['name'] == "pep8-naming":
|
|
|
|
module['import'] = "pep8ext_naming"
|
2019-11-24 00:42:39 +06:00
|
|
|
|
2019-11-28 02:27:06 +06:00
|
|
|
if not find_spec(module['import']):
|
|
|
|
print('Could not find module %s!' % module['name'])
|
2020-04-19 02:00:56 +06:00
|
|
|
print('Please run `python3 -m pip install -r %s` to install required python dependencies.' % (qmk_dir / requirements,))
|
2019-11-28 02:27:06 +06:00
|
|
|
if developer:
|
2020-03-23 22:08:35 +06:00
|
|
|
print('You can also turn off developer mode: qmk config user.developer=None')
|
|
|
|
print()
|
|
|
|
exit(255)
|
2019-11-28 02:27:06 +06:00
|
|
|
|
|
|
|
|
|
|
|
developer = False
|
|
|
|
# Make sure our modules have been setup
|
|
|
|
_check_modules('requirements.txt')
|
|
|
|
|
2020-03-23 22:08:35 +06:00
|
|
|
# Setup the CLI
|
|
|
|
import milc # noqa
|
|
|
|
|
2019-11-28 02:27:06 +06:00
|
|
|
# For developers additional modules are needed
|
2020-02-20 06:10:56 +06:00
|
|
|
if milc.cli.config.user.developer:
|
2020-04-05 15:17:12 +06:00
|
|
|
# Do not run the check for 'config',
|
|
|
|
# so users can turn off developer mode
|
|
|
|
if len(sys.argv) == 1 or (len(sys.argv) > 1 and 'config' != sys.argv[1]):
|
|
|
|
developer = True
|
|
|
|
_check_modules('requirements-dev.txt')
|
2019-07-16 01:14:27 +06:00
|
|
|
|
2019-09-23 02:25:33 +06:00
|
|
|
milc.EMOJI_LOGLEVELS['INFO'] = '{fg_blue}Ψ{style_reset_all}'
|
2019-07-16 01:14:27 +06:00
|
|
|
|
|
|
|
|
2019-09-23 02:25:33 +06:00
|
|
|
@milc.cli.entrypoint('QMK Helper Script')
|
|
|
|
def qmk_main(cli):
|
|
|
|
"""The function that gets run when no subcommand is provided.
|
|
|
|
"""
|
|
|
|
cli.print_help()
|
2019-07-16 01:14:27 +06:00
|
|
|
|
|
|
|
|
2019-09-23 02:25:33 +06:00
|
|
|
def main():
|
|
|
|
"""Setup our environment and then call the CLI entrypoint.
|
|
|
|
"""
|
|
|
|
# Change to the root of our checkout
|
|
|
|
os.environ['ORIG_CWD'] = os.getcwd()
|
|
|
|
os.chdir(qmk_dir)
|
2019-07-16 01:14:27 +06:00
|
|
|
|
2019-09-23 02:25:33 +06:00
|
|
|
# Import the subcommands
|
2019-11-21 04:54:18 +06:00
|
|
|
import qmk.cli # noqa
|
2019-07-16 01:14:27 +06:00
|
|
|
|
2019-09-23 02:25:33 +06:00
|
|
|
# Execute
|
2019-08-22 12:40:24 +06:00
|
|
|
return_code = milc.cli()
|
2019-09-23 02:25:33 +06:00
|
|
|
|
2019-08-22 12:40:24 +06:00
|
|
|
if return_code is False:
|
|
|
|
exit(1)
|
2019-09-23 02:25:33 +06:00
|
|
|
|
|
|
|
elif return_code is not True and isinstance(return_code, int):
|
|
|
|
if return_code < 0 or return_code > 255:
|
|
|
|
milc.cli.log.error('Invalid return_code: %d', return_code)
|
|
|
|
exit(255)
|
|
|
|
|
2019-08-22 12:40:24 +06:00
|
|
|
exit(return_code)
|
2019-09-23 02:25:33 +06:00
|
|
|
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|