Advertisement
misdocumeno

Untitled

Nov 19th, 2020
925
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.23 KB | None | 0 0
  1. compile_queue = Queue(maxsize=0)
  2.  
  3. total_plugins = 0
  4. total_compiled = 0
  5. total_warnings = 0
  6. total_errors= 0
  7.  
  8. separator_printed = False
  9.  
  10. def compile(source, output, includes):
  11.     global total_plugins, total_compiled, total_errors, total_warnings, separator_printed
  12.     errors, warnings, compiled = False, False, False
  13.     # run the compiler
  14.     params = ['spcomp', source, '-o', output] + includes + ['-v', '0']
  15.     process = subprocess.Popen(params, stdout=subprocess.PIPE)
  16.     compiled = process.wait() == 0
  17.     # update the json
  18.     compile_json['sourceFiles'][source]['updateDate'] = os.path.getmtime(source)
  19.     compile_json['sourceFiles'][source]['successfullyCompiled'] = process.wait() == 0
  20.     # this is what we will print (after adding colors ofc)
  21.     logs = []
  22.     for line in process.stdout:
  23.         line = line.decode('utf-8').rstrip()
  24.         # add only necessary lines
  25.         if not line.startswith(('Code size:', 'Data size:', 'Stack/heap size:', 'Total requirements:', 'Compilation aborted.')):
  26.             if not re.match(r'[0-9]+ Warnings?\.', line) and not re.match(r'[0-9]+ Errors?\.', line) and re.match(r'\w+', line):
  27.                 logs.append(line)
  28.         # check for errors or warnings
  29.         if not errors and re.search(' : (fatal )?error [0-9]+:', line):
  30.             errors = True
  31.         elif not warnings and re.search(' : warning [0-9]+:', line):
  32.             warnings = True
  33.     # add colors and print
  34.     if not warnings and not errors:
  35.         print(source + ':', c.gn + 'OK' + c.df)
  36.         separator_printed = False
  37.     else:
  38.         if not separator_printed:
  39.             print(c.gy + ("─" * 100) + c.df)
  40.         if errors:
  41.             print(source + ':', c.rd + 'ERROR' + c.df)
  42.         else:
  43.             print(source + ':', c.yw + 'WARNING' + c.df)
  44.         # check if there is any errors or warnings in other files than the main plugin sp. if so, we have to specify the file
  45.         other_files_log = False
  46.         for line in logs:
  47.             if not line.startswith(source):
  48.                 other_files_log = True
  49.                 break
  50.         # print each line
  51.         for line in logs:
  52.             # get each part
  53.             filename = re.findall(r'(.*?)\(', line)[0]
  54.             linenum = re.findall(r'.*?\(([0-9]+)\)', line)[0]
  55.             errcode = re.findall(r'.*?\([0-9]+\) : ((?:warning|error|fatal error) [0-9]+): ', line)[0]
  56.             message = re.findall(r'.*? :.*?: (.*)', line)[0]
  57.             # add color
  58.             filename = filename.replace(script_path, '') if filename.endswith('.inc') else filename
  59.             message = c.df + re.sub(r'"(.*?)"', c.cy + r'\1' + c.df, message)
  60.             errcode = (c.rd if 'error' in errcode else c.yw) + errcode
  61.             print(((filename + ': ') if other_files_log else '') + c.lb + 'line ' + linenum + ' ' + errcode + ': ' + message)
  62.         # print separator
  63.         print(c.gy + ("─" * 100) + c.df)
  64.         separator_printed = True
  65.     # add to global counter
  66.     total_plugins += 1
  67.     if compiled:
  68.         total_compiled += 1
  69.     if errors:
  70.         total_errors += 1
  71.     elif warnings:
  72.         total_warnings += 1
  73.  
  74.  
  75. def compiler_thread():
  76.     # print copyright and stuff
  77.     header = subprocess.Popen(['spcomp'], stdout=subprocess.PIPE)
  78.     for line in header.stdout:
  79.         line = (line.decode('utf-8')).rstrip()
  80.         if line.startswith('S') or line.startswith('C'):
  81.             print(c.lb + line + c.df)
  82.     print('\n')
  83.  
  84.     # constantly check for new plugins to compile
  85.     while True:
  86.         # we have something to compile
  87.         if not compile_queue.empty():
  88.             plugin_src = compile_queue.get()
  89.             # check if we reach the end of the list
  90.             if plugin_src is None:
  91.                 compile_queue.task_done()
  92.                 return
  93.             # it's a plugin to compile
  94.             else:
  95.                 plugin_out = plugin_src.replace(input_folder, output_folder, 1)
  96.                 plugin_out = (plugin_out[:-3] if plugin_src.endswith('.sp') else plugin_out) + '.smx'
  97.                 # include folders
  98.                 include_folders = []
  99.                 # if there is a folder called 'include' in the same location, include it
  100.                 input_parent = str(pathlib.Path(plugin_src).parent)
  101.                 if os.path.isdir(os.path.join(input_parent, 'include')):
  102.                     include_folders.append(os.path.join(input_parent, 'include'))
  103.                 include_folders += included_folders
  104.                 for i in range(len(include_folders)):
  105.                     include_folders.insert(i*2, '-i')
  106.                 # create destination folder in case it doesn't exist
  107.                 output_parent = str(pathlib.Path(plugin_out).parent)
  108.                 if not os.path.isdir(output_parent):
  109.                     os.makedirs(output_parent, exist_ok=True)
  110.                 # compile and check for more to compile
  111.                 compile(plugin_src, plugin_out, include_folders)
  112.                 compile_queue.task_done()
  113.  
  114. compiler = Thread(target=compiler_thread)
  115. compiler.start()
  116.  
  117. ###############################
  118. # check source code to compile
  119. ###############################
  120. for src in all_src_files:
  121.     compile_queue.put(src)
  122.  
  123. compile_queue.put(None)
  124. # check launch params
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement