Browse Source

Add a script to id configs

Scott Lahteine 4 years ago
parent
commit
bf25cc8412
1 changed files with 201 additions and 0 deletions
  1. 201
    0
      buildroot/share/scripts/config-labels.py

+ 201
- 0
buildroot/share/scripts/config-labels.py View File

@@ -0,0 +1,201 @@
1
+#!/usr/bin/env python
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
+import os
27
+from pathlib import Path
28
+from distutils.dir_util import copy_tree  # for copy_tree, because shutil.copytree can't handle existing files, dirs
29
+
30
+# Modify input_examples_dir and output_examples_dir for your installation
31
+# No trailing slash
32
+# Setting output_examples_dir = input_examples_dir causes the program to insert into the existing files.
33
+
34
+input_examples_dir = r'config/examples'
35
+# output_examples_dir   = input_examples_dir
36
+output_examples_dir = r'config/examples'
37
+
38
+#-------------------------------------
39
+
40
+files_to_mod = ['Configuration.h', 'Configuration_adv.h', '_Bootscreen.h', '_Statusscreen.h']
41
+
42
+macro_name     = 'CONFIG_EXAMPLES_DIR'
43
+def_macro_name = '#define ' + macro_name
44
+
45
+filenum = 0
46
+different_out_dir = not (output_examples_dir == input_examples_dir)
47
+
48
+#----------------------------------------------
49
+def process_file(subdir: str, filename: str):
50
+#----------------------------------------------
51
+	global filenum
52
+	filenum += 1
53
+
54
+	print(str(filenum) + '  ' + filename + ':  ' + subdir)
55
+
56
+	def_line = (def_macro_name + ' "'  + subdir.replace('\\', '/')  + '"')
57
+
58
+	#------------------------
59
+	# Read file
60
+	#------------------------
61
+	lines = []
62
+	infilepath = os.path.join(input_examples_dir, subdir, filename)
63
+	try:
64
+		# UTF-8 because some files contain unicode chars
65
+		with open(infilepath, 'rt', encoding="utf-8") as infile:
66
+			lines = infile.readlines()
67
+
68
+	except Exception as e:
69
+		print('Failed to read file: ' + str(e) )
70
+		raise Exception
71
+
72
+	lines = [line.rstrip('\r\n') for line in lines]
73
+
74
+	#------------------------
75
+	# Process lines
76
+	#------------------------
77
+	file_modified    = False
78
+
79
+	# region state machine
80
+	# -1 = before pragma once;
81
+	# 0 = region to place define;
82
+	# 1 = past region to place define
83
+	region      = -1
84
+
85
+	outlines = []
86
+	for line in lines:
87
+		outline = line
88
+
89
+		if (region == -1) and (def_macro_name in line):
90
+			outline       = None
91
+			file_modified = True
92
+
93
+		elif (region == -1) and ('pragma once' in line):
94
+			region = 0
95
+
96
+		elif (region == 0):
97
+			if (line.strip() == ''):
98
+				pass
99
+			elif (def_macro_name in line):
100
+				region = 1
101
+				if line == def_line:   # leave it as is
102
+					pass
103
+				else:
104
+					outline       = def_line
105
+					file_modified = True
106
+			else: # some other string
107
+				outlines.append(def_line)
108
+				outlines.append('')
109
+				region = 1
110
+				file_modified = True
111
+
112
+		elif (region == 1):
113
+			if (def_macro_name in line):
114
+				outline       = None
115
+				file_modified = True
116
+			else:
117
+				pass
118
+
119
+		# end if
120
+		if outline is not None:
121
+			outlines.append(outline)
122
+	# end for
123
+
124
+	#-------------------------
125
+	#     Output file
126
+	#-------------------------
127
+	outdir      = os.path.join(output_examples_dir, subdir)
128
+	outfilepath = os.path.join(outdir, filename)
129
+
130
+	if file_modified:
131
+		# Note: no need to create output dirs, as the initial copy_tree
132
+		# will do that.
133
+
134
+		print('  writing ' + str(outfilepath))
135
+		try:
136
+			# Preserve unicode chars; Avoid CR-LF on Windows.
137
+			with open(outfilepath, "w", encoding="utf-8", newline='\n') as outfile:
138
+				outfile.write("\n".join(outlines))
139
+				outfile.write("\n")
140
+
141
+		except Exception as e:
142
+			print('Failed to write file: ' + str(e) )
143
+			raise Exception
144
+	else:
145
+		print('  no change for ' + str(outfilepath))
146
+
147
+#----------
148
+def main():
149
+#----------
150
+	global filenum
151
+	global input_examples_dir
152
+	global output_examples_dir
153
+	filenum = 0
154
+
155
+	#--------------------------------
156
+	# Check for requirements
157
+	#--------------------------------
158
+	input_examples_dir  = input_examples_dir.strip()
159
+	input_examples_dir  = input_examples_dir.rstrip('\\/')
160
+	output_examples_dir = output_examples_dir.strip()
161
+	output_examples_dir = output_examples_dir.rstrip('\\/')
162
+
163
+	for dir in [input_examples_dir, output_examples_dir]:
164
+		if not (os.path.exists(dir)):
165
+			print('Directory not found: ' + dir)
166
+			sys.exit(1)
167
+
168
+	#--------------------------------
169
+	# Copy tree if necessary.
170
+	#--------------------------------
171
+	# This includes files that are not otherwise included in the
172
+	# insertion of the define statement.
173
+	#
174
+	if different_out_dir:
175
+		print('Copying files to new directory: ' + output_examples_dir)
176
+		try:
177
+			copy_tree(input_examples_dir, output_examples_dir)
178
+		except Exception as e:
179
+			print('Failed to copy directory: ' + str(e) )
180
+			raise Exception
181
+
182
+	#-----------------------------
183
+	# Find and process files
184
+	#-----------------------------
185
+	len_input_examples_dir = len(input_examples_dir);
186
+	len_input_examples_dir += 1
187
+
188
+	for filename in files_to_mod:
189
+		input_path = Path(input_examples_dir)
190
+		filepathlist = input_path.rglob(filename)
191
+
192
+		for filepath in filepathlist:
193
+			fulldirpath = str(filepath.parent)
194
+			subdir      = fulldirpath[len_input_examples_dir:]
195
+
196
+			process_file(subdir, filename)
197
+
198
+#==============
199
+print('--- Starting config-labels ---')
200
+main()
201
+print('--- Done ---')

Loading…
Cancel
Save