mc-apply.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python
  2. #
  3. # mc-apply.py
  4. #
  5. # Apply firmware configuration from a JSON file (marlin_config.json).
  6. #
  7. # usage: mc-apply.py [-h] [--opt] [config_file]
  8. #
  9. # Process Marlin firmware configuration.
  10. #
  11. # positional arguments:
  12. # config_file Path to the configuration file.
  13. #
  14. # optional arguments:
  15. # -h, --help show this help message and exit
  16. # --opt Output as an option setting script.
  17. #
  18. import json, sys, os
  19. import config
  20. import argparse
  21. def report_version(conf):
  22. if 'VERSION' in conf:
  23. for k, v in sorted(conf['VERSION'].items()):
  24. print(k + ': ' + v)
  25. def write_opt_file(conf, outpath='Marlin/apply_config.sh'):
  26. with open(outpath, 'w') as outfile:
  27. for key, val in conf.items():
  28. if key in ('__INITIAL_HASH', 'VERSION'): continue
  29. # Other keys are assumed to be configs
  30. if not type(val) is dict:
  31. continue
  32. # Write config commands to the script file
  33. lines = []
  34. for k, v in sorted(val.items()):
  35. if v != '':
  36. v.replace('"', '\\"').replace("'", "\\'").replace(' ', '\\ ')
  37. lines += [f'opt_set {k} {v}']
  38. else:
  39. lines += [f'opt_enable {k}']
  40. outfile.write('\n'.join(lines))
  41. print('Config script written to: ' + outpath)
  42. def back_up_config(name):
  43. # Back up the existing file before modifying it
  44. conf_path = 'Marlin/' + name
  45. with open(conf_path, 'r') as f:
  46. # Write a filename.bak#.ext retaining the original extension
  47. parts = conf_path.split('.')
  48. nr = ''
  49. while True:
  50. bak_path = '.'.join(parts[:-1]) + f'.bak{nr}.' + parts[-1]
  51. if os.path.exists(bak_path):
  52. nr = 1 if nr == '' else nr + 1
  53. continue
  54. with open(bak_path, 'w') as b:
  55. b.writelines(f.readlines())
  56. break
  57. def apply_config(conf):
  58. for key in conf:
  59. if key in ('__INITIAL_HASH', 'VERSION'): continue
  60. back_up_config(key)
  61. for k, v in conf[key].items():
  62. if v:
  63. config.set('Marlin/' + key, k, v)
  64. else:
  65. config.enable('Marlin/' + key, k)
  66. def main():
  67. parser = argparse.ArgumentParser(description='Process Marlin firmware configuration.')
  68. parser.add_argument('--opt', action='store_true', help='Output as an option setting script.')
  69. parser.add_argument('config_file', nargs='?', default='marlin_config.json', help='Path to the configuration file.')
  70. args = parser.parse_args()
  71. try:
  72. infile = open(args.config_file, 'r')
  73. except:
  74. print(f'No {args.config_file} found.')
  75. sys.exit(1)
  76. conf = json.load(infile)
  77. report_version(conf)
  78. if args.opt:
  79. write_opt_file(conf)
  80. else:
  81. apply_config(conf)
  82. if __name__ == '__main__':
  83. main()