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.

109 lines
3.0 KiB

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