logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

commands.py (3827B)


  1. """Helper functions for commands.
  2. """
  3. import os
  4. import sys
  5. import shutil
  6. from pathlib import Path
  7. from milc import cli
  8. import jsonschema
  9. from qmk.constants import QMK_USERSPACE, HAS_QMK_USERSPACE
  10. from qmk.json_schema import json_load, validate
  11. from qmk.keyboard import keyboard_alias_definitions
  12. from qmk.util import maybe_exit
  13. from qmk.path import unix_style_path
  14. def find_make():
  15. """Returns the correct make command for this environment.
  16. """
  17. make_cmd = os.environ.get('MAKE')
  18. if not make_cmd:
  19. make_cmd = 'gmake' if shutil.which('gmake') else 'make'
  20. return make_cmd
  21. def get_make_parallel_args(parallel=1):
  22. """Returns the arguments for running the specified number of parallel jobs.
  23. """
  24. parallel_args = []
  25. if int(parallel) <= 0:
  26. # 0 or -1 means -j without argument (unlimited jobs)
  27. parallel_args.append('--jobs')
  28. elif int(parallel) > 1:
  29. parallel_args.append('--jobs=' + str(parallel))
  30. if int(parallel) != 1:
  31. # If more than 1 job is used, synchronize parallel output by target
  32. parallel_args.append('--output-sync=target')
  33. return parallel_args
  34. def parse_configurator_json(configurator_file):
  35. """Open and parse a configurator json export
  36. """
  37. user_keymap = json_load(configurator_file)
  38. # Validate against the jsonschema
  39. try:
  40. validate(user_keymap, 'qmk.keymap.v1')
  41. except jsonschema.ValidationError as e:
  42. cli.log.error(f'Invalid JSON keymap: {configurator_file} : {e.message}')
  43. maybe_exit(1)
  44. keyboard = user_keymap.get('keyboard', None)
  45. aliases = keyboard_alias_definitions()
  46. while keyboard in aliases:
  47. last_keyboard = keyboard
  48. keyboard = aliases[keyboard].get('target', keyboard)
  49. if keyboard == last_keyboard:
  50. break
  51. user_keymap['keyboard'] = keyboard
  52. return user_keymap
  53. def parse_env_vars(args):
  54. """Common processing for cli.args.env
  55. """
  56. envs = {}
  57. for env in args:
  58. if '=' in env:
  59. key, value = env.split('=', 1)
  60. envs[key] = value
  61. else:
  62. cli.log.warning('Invalid environment variable: %s', env)
  63. return envs
  64. def build_environment(args):
  65. envs = parse_env_vars(args)
  66. if HAS_QMK_USERSPACE:
  67. envs['QMK_USERSPACE'] = unix_style_path(Path(QMK_USERSPACE).resolve())
  68. return envs
  69. def in_virtualenv():
  70. """Check if running inside a virtualenv.
  71. Based on https://stackoverflow.com/a/1883251
  72. """
  73. active_prefix = getattr(sys, "base_prefix", None) or getattr(sys, "real_prefix", None) or sys.prefix
  74. return active_prefix != sys.prefix
  75. def dump_lines(output_file, lines, quiet=True, remove_repeated_newlines=False):
  76. """Handle dumping to stdout or file
  77. Creates parent folders if required
  78. """
  79. generated = '\n'.join(lines) + '\n'
  80. if remove_repeated_newlines:
  81. while '\n\n\n' in generated:
  82. generated = generated.replace('\n\n\n', '\n\n')
  83. if output_file and output_file.name != '-':
  84. output_file.parent.mkdir(parents=True, exist_ok=True)
  85. if output_file.exists():
  86. with open(output_file, 'r', encoding='utf-8', newline='\n') as f:
  87. existing = f.read()
  88. if existing == generated:
  89. if not quiet:
  90. cli.log.info(f'No changes to {output_file.name}.')
  91. return
  92. output_file.replace(output_file.parent / (output_file.name + '.bak'))
  93. with open(output_file, 'w', encoding='utf-8', newline='\n') as f:
  94. f.write(generated)
  95. # output_file.write_text(generated, encoding='utf-8', newline='\n') # `newline` needs Python 3.10
  96. if not quiet:
  97. cli.log.info(f'Wrote {output_file.name} to {output_file}.')
  98. else:
  99. print(generated)