confmigrate.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """This script automates the copying of the default keymap into your own keymap.
  2. """
  3. import re
  4. import sys
  5. import os
  6. from qmk.constants import QMK_FIRMWARE
  7. from qmk.path import normpath
  8. from milc import cli
  9. def eprint(*args, **kwargs):
  10. print(*args, file=sys.stderr, **kwargs)
  11. file_header = """\
  12. /* Copyright 2020 QMK
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. */
  27. /*
  28. * This file was auto-generated by:
  29. * `qmk chibios-confmigrate -i {0} -r {1}`
  30. */
  31. #pragma once
  32. """
  33. def collect_defines(filepath):
  34. with open(filepath, 'r', encoding='utf-8') as f:
  35. content = f.read()
  36. define_search = re.compile(r'(?m)^#\s*define\s+(?:.*\\\r?\n)*.*$', re.MULTILINE)
  37. value_search = re.compile(r'^#\s*define\s+(?P<name>[a-zA-Z0-9_]+(\([^\)]*\))?)\s*(?P<value>.*)', re.DOTALL)
  38. define_matches = define_search.findall(content)
  39. defines = {"keys": [], "dict": {}}
  40. for define_match in define_matches:
  41. value_match = value_search.search(define_match)
  42. defines["keys"].append(value_match.group("name"))
  43. defines["dict"][value_match.group("name")] = value_match.group("value")
  44. return defines
  45. def check_diffs(input_defs, reference_defs):
  46. not_present_in_input = []
  47. not_present_in_reference = []
  48. to_override = []
  49. for key in reference_defs["keys"]:
  50. if key not in input_defs["dict"]:
  51. not_present_in_input.append(key)
  52. continue
  53. for key in input_defs["keys"]:
  54. if key not in input_defs["dict"]:
  55. not_present_in_input.append(key)
  56. continue
  57. for key in input_defs["keys"]:
  58. if key in reference_defs["keys"] and input_defs["dict"][key] != reference_defs["dict"][key]:
  59. to_override.append((key, input_defs["dict"][key]))
  60. return (to_override, not_present_in_input, not_present_in_reference)
  61. def migrate_chconf_h(to_override, outfile):
  62. print(file_header.format(cli.args.input.relative_to(QMK_FIRMWARE), cli.args.reference.relative_to(QMK_FIRMWARE)), file=outfile)
  63. for override in to_override:
  64. print("#define %s %s" % (override[0], override[1]), file=outfile)
  65. print("", file=outfile)
  66. print("#include_next <chconf.h>\n", file=outfile)
  67. def migrate_halconf_h(to_override, outfile):
  68. print(file_header.format(cli.args.input.relative_to(QMK_FIRMWARE), cli.args.reference.relative_to(QMK_FIRMWARE)), file=outfile)
  69. for override in to_override:
  70. print("#define %s %s" % (override[0], override[1]), file=outfile)
  71. print("", file=outfile)
  72. print("#include_next <halconf.h>\n", file=outfile)
  73. def migrate_mcuconf_h(to_override, outfile):
  74. print(file_header.format(cli.args.input.relative_to(QMK_FIRMWARE), cli.args.reference.relative_to(QMK_FIRMWARE)), file=outfile)
  75. print("#include_next <mcuconf.h>\n", file=outfile)
  76. for override in to_override:
  77. print("#undef %s" % (override[0]), file=outfile)
  78. print("#define %s %s" % (override[0], override[1]), file=outfile)
  79. print("", file=outfile)
  80. @cli.argument('-i', '--input', type=normpath, arg_only=True, help='Specify input config file.')
  81. @cli.argument('-r', '--reference', type=normpath, arg_only=True, help='Specify the reference file to compare against')
  82. @cli.argument('-o', '--overwrite', arg_only=True, action='store_true', help='Overwrites the input file during migration.')
  83. @cli.argument('-d', '--delete', arg_only=True, action='store_true', help='If the file has no overrides, migration will delete the input file.')
  84. @cli.argument('-f', '--force', arg_only=True, action='store_true', help='Re-migrates an already migrated file, even if it doesn\'t detect a full ChibiOS config.')
  85. @cli.subcommand('Generates a migrated ChibiOS configuration file, as a result of comparing the input against a reference')
  86. def chibios_confmigrate(cli):
  87. """Generates a usable ChibiOS replacement configuration file, based on a fully-defined conf and a reference config.
  88. """
  89. input_defs = collect_defines(cli.args.input)
  90. reference_defs = collect_defines(cli.args.reference)
  91. (to_override, not_present_in_input, not_present_in_reference) = check_diffs(input_defs, reference_defs)
  92. if len(not_present_in_input) > 0:
  93. eprint("Keys not in input, but present inside reference (potential manual migration required):")
  94. for key in not_present_in_input:
  95. eprint(" %s" % (key))
  96. if len(not_present_in_reference) > 0:
  97. eprint("Keys not in reference, but present inside input (potential manual migration required):")
  98. for key in not_present_in_reference:
  99. eprint(" %s" % (key))
  100. if len(to_override) == 0:
  101. eprint('No overrides found! If there were no missing keys above, it should be safe to delete the input file.')
  102. if cli.args.delete:
  103. os.remove(cli.args.input)
  104. else:
  105. eprint('Overrides found:')
  106. for override in to_override:
  107. eprint("%40s: %s -> %s" % (override[0], reference_defs["dict"][override[0]].encode('unicode_escape').decode("utf-8"), override[1].encode('unicode_escape').decode("utf-8")))
  108. eprint('--------------------------------------')
  109. if cli.args.input.name == "chconf.h" and ("CHCONF_H" in input_defs["dict"] or "_CHCONF_H_" in input_defs["dict"] or cli.args.force):
  110. migrate_chconf_h(to_override, outfile=sys.stdout)
  111. if cli.args.overwrite:
  112. with open(cli.args.input, "w", encoding='utf-8') as out_file:
  113. migrate_chconf_h(to_override, outfile=out_file)
  114. elif cli.args.input.name == "halconf.h" and ("HALCONF_H" in input_defs["dict"] or "_HALCONF_H_" in input_defs["dict"] or cli.args.force):
  115. migrate_halconf_h(to_override, outfile=sys.stdout)
  116. if cli.args.overwrite:
  117. with open(cli.args.input, "w", encoding='utf-8') as out_file:
  118. migrate_halconf_h(to_override, outfile=out_file)
  119. elif cli.args.input.name == "mcuconf.h" and ("MCUCONF_H" in input_defs["dict"] or "_MCUCONF_H_" in input_defs["dict"] or cli.args.force):
  120. migrate_mcuconf_h(to_override, outfile=sys.stdout)
  121. if cli.args.overwrite:
  122. with open(cli.args.input, "w", encoding='utf-8') as out_file:
  123. migrate_mcuconf_h(to_override, outfile=out_file)