commands.py 2.8 KB

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