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.

86 lines
2.2 KiB

  1. """Helper functions for commands.
  2. """
  3. import json
  4. import os
  5. import platform
  6. import subprocess
  7. import shlex
  8. import shutil
  9. import qmk.keymap
  10. def create_make_command(keyboard, keymap, target=None):
  11. """Create a make compile command
  12. Args:
  13. keyboard
  14. The path of the keyboard, for example 'plank'
  15. keymap
  16. The name of the keymap, for example 'algernon'
  17. target
  18. Usually a bootloader.
  19. Returns:
  20. A command that can be run to make the specified keyboard and keymap
  21. """
  22. make_args = [keyboard, keymap]
  23. make_cmd = 'gmake' if shutil.which('gmake') else 'make'
  24. if target:
  25. make_args.append(target)
  26. return [make_cmd, ':'.join(make_args)]
  27. def compile_configurator_json(user_keymap, bootloader=None):
  28. """Convert a configurator export JSON file into a C file
  29. Args:
  30. configurator_filename
  31. The configurator JSON export file
  32. bootloader
  33. A bootloader to flash
  34. Returns:
  35. A command to run to compile and flash the C file.
  36. """
  37. # Write the keymap C file
  38. qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers'])
  39. # Return a command that can be run to make the keymap and flash if given
  40. if bootloader is None:
  41. return create_make_command(user_keymap['keyboard'], user_keymap['keymap'])
  42. return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader)
  43. def parse_configurator_json(configurator_file):
  44. """Open and parse a configurator json export
  45. """
  46. # FIXME(skullydazed/anyone): Add validation here
  47. user_keymap = json.load(configurator_file)
  48. return user_keymap
  49. def run(command, *args, **kwargs):
  50. """Run a command with subprocess.run
  51. """
  52. platform_id = platform.platform().lower()
  53. if isinstance(command, str):
  54. raise TypeError('`command` must be a non-text sequence such as list or tuple.')
  55. if 'windows' in platform_id:
  56. safecmd = map(shlex.quote, command)
  57. safecmd = ' '.join(safecmd)
  58. command = [os.environ['SHELL'], '-c', safecmd]
  59. return subprocess.run(command, *args, **kwargs)