My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

config-labels.py 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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
  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 = Path(input_examples_dir, subdir, filename)
  52. try:
  53. # UTF-8 because some files contain unicode chars
  54. with infilepath.open('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 = Path(output_examples_dir, subdir)
  106. outfilepath = 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 outfilepath.open("w", encoding="utf-8", newline='\n') as outfile:
  114. outfile.write("\n".join(outlines) + "\n")
  115. except Exception as e:
  116. print('Failed to write file: ' + str(e) )
  117. raise Exception
  118. else:
  119. print(' no change for ' + str(outfilepath))
  120. #----------
  121. def main():
  122. #----------
  123. global filenum
  124. global input_examples_dir
  125. global output_examples_dir
  126. filenum = 0
  127. #--------------------------------
  128. # Check for requirements
  129. #--------------------------------
  130. input_examples_dir = input_examples_dir.strip()
  131. input_examples_dir = input_examples_dir.rstrip('\\/')
  132. output_examples_dir = output_examples_dir.strip()
  133. output_examples_dir = output_examples_dir.rstrip('\\/')
  134. for dir in (input_examples_dir, output_examples_dir):
  135. if not Path(dir).exists():
  136. print('Directory not found: ' + dir)
  137. sys.exit(1)
  138. #--------------------------------
  139. # Copy tree if necessary.
  140. #--------------------------------
  141. # This includes files that are not otherwise included in the
  142. # insertion of the define statement.
  143. #
  144. if different_out_dir:
  145. print('Copying files to new directory: ' + output_examples_dir)
  146. try:
  147. copy_tree(input_examples_dir, output_examples_dir)
  148. except Exception as e:
  149. print('Failed to copy directory: ' + str(e) )
  150. raise Exception
  151. #-----------------------------
  152. # Find and process files
  153. #-----------------------------
  154. len_input_examples_dir = 1 + len(input_examples_dir)
  155. for filename in files_to_mod:
  156. input_path = Path(input_examples_dir)
  157. filepathlist = input_path.rglob(filename)
  158. for filepath in filepathlist:
  159. fulldirpath = str(filepath.parent)
  160. subdir = fulldirpath[len_input_examples_dir:]
  161. process_file(subdir, filename)
  162. #==============
  163. print('--- Starting config-labels ---')
  164. main()
  165. print('--- Done ---')