My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

config-labels.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/usr/bin/env python3
  2. #
  3. # for python3.5 or higher
  4. #-----------------------------------
  5. # Within Marlin project MarlinFirmware/Configurations, this program visits all folders
  6. # under .../config/examples/*, processing each Configuration.h, Configuration_adv.h,
  7. # _Bootscreen.h, and _Statusscreen.h, to insert:
  8. # #define CONFIG_EXAMPLES_DIR "examples/<style>/<vendor>/<model>"
  9. # ... or similar path leading to this file.
  10. #
  11. # Warning: The program modifies files in place, so be sure to back them up first if needed.
  12. # Can be run multiple times if needed. Only modifies files which don't have
  13. # correct #define CONFIG_EXAMPLES_DIR line.
  14. #
  15. # Invocation:
  16. #-------------
  17. # 1. Change directory to your MarlinFirmware/Configurations working copy
  18. # 2. python3 config-labels.py
  19. #
  20. #-----------------------------------
  21. # 2020-05-10 GMW original
  22. # 2020-06-05 SRL style tweaks
  23. #-----------------------------------
  24. #
  25. import sys,os
  26. from pathlib import Path
  27. from distutils.dir_util import copy_tree # for copy_tree, because shutil.copytree can't handle existing files, dirs
  28. # Modify input_examples_dir and output_examples_dir for your installation
  29. # No trailing slash
  30. # Setting output_examples_dir = input_examples_dir causes the program to insert into the existing files.
  31. input_examples_dir = r'config/examples'
  32. # output_examples_dir = input_examples_dir
  33. output_examples_dir = r'config/examples'
  34. #-------------------------------------
  35. files_to_mod = ['Configuration.h', 'Configuration_adv.h', '_Bootscreen.h', '_Statusscreen.h']
  36. macro_name = 'CONFIG_EXAMPLES_DIR'
  37. def_macro_name = '#define ' + macro_name
  38. filenum = 0
  39. different_out_dir = not (output_examples_dir == input_examples_dir)
  40. #----------------------------------------------
  41. def process_file(subdir: str, filename: str):
  42. #----------------------------------------------
  43. global filenum
  44. filenum += 1
  45. print(str(filenum) + ' ' + filename + ': ' + subdir)
  46. def_line = (def_macro_name + ' "' + subdir.replace('\\', '/') + '"')
  47. #------------------------
  48. # Read file
  49. #------------------------
  50. lines = []
  51. infilepath = os.path.join(input_examples_dir, subdir, filename)
  52. try:
  53. # UTF-8 because some files contain unicode chars
  54. with open(infilepath, 'rt', encoding="utf-8") as infile:
  55. lines = infile.readlines()
  56. except Exception as e:
  57. print('Failed to read file: ' + str(e) )
  58. raise Exception
  59. lines = [line.rstrip('\r\n') for line in lines]
  60. #------------------------
  61. # Process lines
  62. #------------------------
  63. file_modified = False
  64. # region state machine
  65. # -1 = before pragma once;
  66. # 0 = region to place define;
  67. # 1 = past region to place define
  68. region = -1
  69. outlines = []
  70. for line in lines:
  71. outline = line
  72. if (region == -1) and (def_macro_name in line):
  73. outline = None
  74. file_modified = True
  75. elif (region == -1) and ('pragma once' in line):
  76. region = 0
  77. elif (region == 0):
  78. if (line.strip() == ''):
  79. pass
  80. elif (def_macro_name in line):
  81. region = 1
  82. if line == def_line: # leave it as is
  83. pass
  84. else:
  85. outline = def_line
  86. file_modified = True
  87. else: # some other string
  88. outlines.append(def_line)
  89. outlines.append('')
  90. region = 1
  91. file_modified = True
  92. elif (region == 1):
  93. if (def_macro_name in line):
  94. outline = None
  95. file_modified = True
  96. else:
  97. pass
  98. # end if
  99. if outline is not None:
  100. outlines.append(outline)
  101. # end for
  102. #-------------------------
  103. # Output file
  104. #-------------------------
  105. outdir = os.path.join(output_examples_dir, subdir)
  106. outfilepath = os.path.join(outdir, filename)
  107. if file_modified:
  108. # Note: no need to create output dirs, as the initial copy_tree
  109. # will do that.
  110. print(' writing ' + str(outfilepath))
  111. try:
  112. # Preserve unicode chars; Avoid CR-LF on Windows.
  113. with open(outfilepath, "w", encoding="utf-8", newline='\n') as outfile:
  114. outfile.write("\n".join(outlines))
  115. outfile.write("\n")
  116. except Exception as e:
  117. print('Failed to write file: ' + str(e) )
  118. raise Exception
  119. else:
  120. print(' no change for ' + str(outfilepath))
  121. #----------
  122. def main():
  123. #----------
  124. global filenum
  125. global input_examples_dir
  126. global output_examples_dir
  127. filenum = 0
  128. #--------------------------------
  129. # Check for requirements
  130. #--------------------------------
  131. input_examples_dir = input_examples_dir.strip()
  132. input_examples_dir = input_examples_dir.rstrip('\\/')
  133. output_examples_dir = output_examples_dir.strip()
  134. output_examples_dir = output_examples_dir.rstrip('\\/')
  135. for dir in [input_examples_dir, output_examples_dir]:
  136. if not (os.path.exists(dir)):
  137. print('Directory not found: ' + dir)
  138. sys.exit(1)
  139. #--------------------------------
  140. # Copy tree if necessary.
  141. #--------------------------------
  142. # This includes files that are not otherwise included in the
  143. # insertion of the define statement.
  144. #
  145. if different_out_dir:
  146. print('Copying files to new directory: ' + output_examples_dir)
  147. try:
  148. copy_tree(input_examples_dir, output_examples_dir)
  149. except Exception as e:
  150. print('Failed to copy directory: ' + str(e) )
  151. raise Exception
  152. #-----------------------------
  153. # Find and process files
  154. #-----------------------------
  155. len_input_examples_dir = len(input_examples_dir);
  156. len_input_examples_dir += 1
  157. for filename in files_to_mod:
  158. input_path = Path(input_examples_dir)
  159. filepathlist = input_path.rglob(filename)
  160. for filepath in filepathlist:
  161. fulldirpath = str(filepath.parent)
  162. subdir = fulldirpath[len_input_examples_dir:]
  163. process_file(subdir, filename)
  164. #==============
  165. print('--- Starting config-labels ---')
  166. main()
  167. print('--- Done ---')