| 
									
										
										
										
											2021-09-13 20:52:13 +01:00
										 |  |  | import sys | 
					
						
							|  |  |  | import os.path | 
					
						
							| 
									
										
										
										
											2021-09-13 21:38:18 +01:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											2021-09-13 20:52:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | err = False | 
					
						
							|  |  |  | paths = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # check arguments | 
					
						
							|  |  |  | argc = len(sys.argv) | 
					
						
							|  |  |  | if argc < 3: | 
					
						
							|  |  |  |     print_usage() | 
					
						
							|  |  |  |     sys.exit(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # figure out src dir from root argument | 
					
						
							|  |  |  | sep="/" | 
					
						
							| 
									
										
										
										
											2021-09-13 21:42:19 +01:00
										 |  |  | src_dir = sep.join(sys.argv[2].split(sep)[:-1]) | 
					
						
							| 
									
										
										
										
											2021-09-13 20:52:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 21:38:18 +01:00
										 |  |  | # regex patterns | 
					
						
							|  |  |  | p_var = re.compile(r'(#.+?\.css)') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 20:52:13 +01:00
										 |  |  | def print_usage(): | 
					
						
							|  |  |  |     print("\nusage: python ppp.py ROOT TEMPLATES [...]") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def preprocess_file(path): | 
					
						
							|  |  |  |     lines=0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with open(path) as f: | 
					
						
							|  |  |  |         content = f.readlines() | 
					
						
							|  |  |  |         content = [x.strip() for x in content] | 
					
						
							|  |  |  |         lines=len(content) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for l in content: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 21:38:18 +01:00
										 |  |  |             # replace lines that start with #include with the contents of a file | 
					
						
							| 
									
										
										
										
											2021-09-13 20:52:13 +01:00
										 |  |  |             if l.startswith("#include"): | 
					
						
							|  |  |  |                 include_path = l.split(" ")[1] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 # prepend directory | 
					
						
							|  |  |  |                 include_path = "/".join([src_dir, include_path]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 preprocess_file(include_path) | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 21:38:18 +01:00
										 |  |  |             # inline replace any occurrence of #filename with the contents of that file | 
					
						
							|  |  |  |             match = re.search(p_var, l) | 
					
						
							|  |  |  |             if match: | 
					
						
							|  |  |  |                 path = "/".join([src_dir, match.group(0)[1:]]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 with open(path) as var_file: | 
					
						
							|  |  |  |                     var_content = var_file.read().strip() | 
					
						
							|  |  |  |                     l = re.sub(p_var, var_content, l) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 20:52:13 +01:00
										 |  |  |             print(l) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | for i in range(1,argc): | 
					
						
							|  |  |  |     path = sys.argv[i] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if not os.path.isfile(path): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         print(path + " is not a file") | 
					
						
							|  |  |  |         err = True | 
					
						
							|  |  |  |         continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if path in paths: | 
					
						
							|  |  |  |         # ignore duplicates | 
					
						
							|  |  |  |         continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     paths.append(path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if err: | 
					
						
							|  |  |  |     print_usage() | 
					
						
							|  |  |  |     sys.exit(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | preprocess_file(sys.argv[1]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | sys.exit(0) |