Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- compile_queue = Queue(maxsize=0)
- total_plugins = 0
- total_compiled = 0
- total_warnings = 0
- total_errors= 0
- separator_printed = False
- def compile(source, output, includes):
- global total_plugins, total_compiled, total_errors, total_warnings, separator_printed
- errors, warnings, compiled = False, False, False
- # run the compiler
- params = ['spcomp', source, '-o', output] + includes + ['-v', '0']
- process = subprocess.Popen(params, stdout=subprocess.PIPE)
- compiled = process.wait() == 0
- # update the json
- compile_json['sourceFiles'][source]['updateDate'] = os.path.getmtime(source)
- compile_json['sourceFiles'][source]['successfullyCompiled'] = process.wait() == 0
- # this is what we will print (after adding colors ofc)
- logs = []
- for line in process.stdout:
- line = line.decode('utf-8').rstrip()
- # add only necessary lines
- if not line.startswith(('Code size:', 'Data size:', 'Stack/heap size:', 'Total requirements:', 'Compilation aborted.')):
- if not re.match(r'[0-9]+ Warnings?\.', line) and not re.match(r'[0-9]+ Errors?\.', line) and re.match(r'\w+', line):
- logs.append(line)
- # check for errors or warnings
- if not errors and re.search(' : (fatal )?error [0-9]+:', line):
- errors = True
- elif not warnings and re.search(' : warning [0-9]+:', line):
- warnings = True
- # add colors and print
- if not warnings and not errors:
- print(source + ':', c.gn + 'OK' + c.df)
- separator_printed = False
- else:
- if not separator_printed:
- print(c.gy + ("─" * 100) + c.df)
- if errors:
- print(source + ':', c.rd + 'ERROR' + c.df)
- else:
- print(source + ':', c.yw + 'WARNING' + c.df)
- # check if there is any errors or warnings in other files than the main plugin sp. if so, we have to specify the file
- other_files_log = False
- for line in logs:
- if not line.startswith(source):
- other_files_log = True
- break
- # print each line
- for line in logs:
- # get each part
- filename = re.findall(r'(.*?)\(', line)[0]
- linenum = re.findall(r'.*?\(([0-9]+)\)', line)[0]
- errcode = re.findall(r'.*?\([0-9]+\) : ((?:warning|error|fatal error) [0-9]+): ', line)[0]
- message = re.findall(r'.*? :.*?: (.*)', line)[0]
- # add color
- filename = filename.replace(script_path, '') if filename.endswith('.inc') else filename
- message = c.df + re.sub(r'"(.*?)"', c.cy + r'\1' + c.df, message)
- errcode = (c.rd if 'error' in errcode else c.yw) + errcode
- print(((filename + ': ') if other_files_log else '') + c.lb + 'line ' + linenum + ' ' + errcode + ': ' + message)
- # print separator
- print(c.gy + ("─" * 100) + c.df)
- separator_printed = True
- # add to global counter
- total_plugins += 1
- if compiled:
- total_compiled += 1
- if errors:
- total_errors += 1
- elif warnings:
- total_warnings += 1
- def compiler_thread():
- # print copyright and stuff
- header = subprocess.Popen(['spcomp'], stdout=subprocess.PIPE)
- for line in header.stdout:
- line = (line.decode('utf-8')).rstrip()
- if line.startswith('S') or line.startswith('C'):
- print(c.lb + line + c.df)
- print('\n')
- # constantly check for new plugins to compile
- while True:
- # we have something to compile
- if not compile_queue.empty():
- plugin_src = compile_queue.get()
- # check if we reach the end of the list
- if plugin_src is None:
- compile_queue.task_done()
- return
- # it's a plugin to compile
- else:
- plugin_out = plugin_src.replace(input_folder, output_folder, 1)
- plugin_out = (plugin_out[:-3] if plugin_src.endswith('.sp') else plugin_out) + '.smx'
- # include folders
- include_folders = []
- # if there is a folder called 'include' in the same location, include it
- input_parent = str(pathlib.Path(plugin_src).parent)
- if os.path.isdir(os.path.join(input_parent, 'include')):
- include_folders.append(os.path.join(input_parent, 'include'))
- include_folders += included_folders
- for i in range(len(include_folders)):
- include_folders.insert(i*2, '-i')
- # create destination folder in case it doesn't exist
- output_parent = str(pathlib.Path(plugin_out).parent)
- if not os.path.isdir(output_parent):
- os.makedirs(output_parent, exist_ok=True)
- # compile and check for more to compile
- compile(plugin_src, plugin_out, include_folders)
- compile_queue.task_done()
- compiler = Thread(target=compiler_thread)
- compiler.start()
- ###############################
- # check source code to compile
- ###############################
- for src in all_src_files:
- compile_queue.put(src)
- compile_queue.put(None)
- # check launch params
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement