Fix single process build + code format

This commit is contained in:
Corentin 2022-10-02 10:41:13 +09:00
commit d683ab022f
2 changed files with 48 additions and 20 deletions

View file

@ -33,7 +33,8 @@ class Config:
class ConsoleColor:
"""Simple shortcut to use colors in console"""
"""Simple shortcut to use colors in console."""
HEADER = '\033[95m'
BLUE = '\033[94m'
GREEN = '\033[92m'
@ -45,8 +46,8 @@ class ConsoleColor:
def get_hash(path: Path) -> str:
with open(path, 'r') as hashing_file:
hash_obj = hashlib.md5()
hash_obj = hashlib.md5()
with open(path, 'r', encoding='utf-8') as hashing_file:
hash_obj.update(hashing_file.read().encode())
return hash_obj.hexdigest()
@ -66,7 +67,7 @@ def make(config: Config):
if config.PRE_COMPILE_FUNCTION is not None:
config.PRE_COMPILE_FUNCTION()
if 'IGNORE_APPS' in config.__dict__ :
if 'IGNORE_APPS' in config.__dict__:
config.IGNORE_APPS = []
# Update flags and directories for mode debug/release
@ -86,7 +87,7 @@ def make(config: Config):
hash_dict = {}
if (config.OBJECT_DIR / 'hash.json').exists():
with open(config.OBJECT_DIR / 'hash.json', 'r') as hash_file:
with open(config.OBJECT_DIR / 'hash.json', 'r', encoding='utf-8') as hash_file:
hash_dict = json.loads(hash_file.read())
# Get source dependencies
@ -101,9 +102,9 @@ def make(config: Config):
dependency_dict[str(source_path)] = job.stdout.split('.o: ')[1].replace('\n', '').replace('\\ ', '').split(' ')
if error_paths:
for error_path, error_text in error_paths:
print(ConsoleColor.RED + f'Error checking dependencies for {error_path}:\n'
+ ConsoleColor.ENDCOLOR + error_text)
with open(config.OBJECT_DIR / 'hash.json', 'w') as hash_file:
print(f'{ConsoleColor.RED}Error checking dependencies for {error_path}:'
f'\n{ConsoleColor.ENDCOLOR + error_text}')
with open(config.OBJECT_DIR / 'hash.json', 'w', encoding='utf-8') as hash_file:
hash_file.write(json.dumps(hash_dict, indent=1))
sys.exit(1)
@ -119,17 +120,17 @@ def make(config: Config):
dependency_hash = get_hash(dependency_path)
if str(dependency_path) not in hash_dict or hash_dict[str(dependency_path)] != dependency_hash:
dependency_changed = True
print(ConsoleColor.ORANGE + f'Dependency changed for {source_path} : {dependency_path}'
+ ConsoleColor.ENDCOLOR)
print(f'{ConsoleColor.ORANGE}Dependency changed for {source_path} :'
f' {dependency_path} {ConsoleColor.ENDCOLOR}')
break
if (dependency_changed or not object_path.exists()
or str(source_path) not in hash_dict or hash_dict[str(source_path)] != source_hash):
todo_list.append((source_path, cmd))
continue
if not todo_list and all([(config.BIN_DIR / app_path).exists() for app_path in config.APPS
if app_path not in config.IGNORE_APPS]):
print(ConsoleColor.GREEN + 'Nothing to do' + ConsoleColor.ENDCOLOR)
if not todo_list and all(
[(config.BIN_DIR / app_path).exists() for app_path in config.APPS if app_path not in config.IGNORE_APPS]):
print(f'{ConsoleColor.GREEN}Nothing to do{ConsoleColor.ENDCOLOR}')
return
# Running compilation processes
@ -151,6 +152,7 @@ def make(config: Config):
# Update hash if no error
for dependency_path in dependency_dict[str(job_path)]:
hash_dict[str(dependency_path)] = get_hash(dependency_path)
del source_path
for job_path, job in jobs: # Wait the last jobs to finish
job.wait()
if job.returncode != 0:
@ -160,7 +162,7 @@ def make(config: Config):
for dependency_path in dependency_dict[str(job_path)]:
hash_dict[str(dependency_path)] = get_hash(dependency_path)
else: # Single-process
for source_path, source_hash, cmd in todo_list:
for source_path, cmd in todo_list:
print(ConsoleColor.BLUE + cmd + ConsoleColor.ENDCOLOR)
job = subprocess.run(cmd, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True, shell=True)
@ -170,10 +172,11 @@ def make(config: Config):
# Update hash if no error
for dependency_path in dependency_dict[str(source_path)]:
hash_dict[str(dependency_path)] = get_hash(dependency_path)
del source_path
if error_paths:
for error_path, error_text in error_paths:
print(ConsoleColor.RED + f'Error compiling {error_path}:\n' + ConsoleColor.ENDCOLOR + error_text)
with open(config.OBJECT_DIR / 'hash.json', 'w') as hash_file:
with open(config.OBJECT_DIR / 'hash.json', 'w', encoding='utf-8') as hash_file:
hash_file.write(json.dumps(hash_dict, indent=1))
sys.exit(1)
@ -202,12 +205,12 @@ def make(config: Config):
app_path, oldest_job = jobs.pop()
oldest_job.wait()
if oldest_job.returncode != 0:
error_paths.append((source_path, oldest_job.stdout.read() + oldest_job.stderr.read()))
error_paths.append((app_path, oldest_job.stdout.read() + oldest_job.stderr.read()))
break
for job_path, job in jobs: # Wait the last jobs to finish
job.wait()
if job.returncode != 0:
error_paths.append((source_path, job.stdout.read() + job.stderr.read()))
error_paths.append((app_path, job.stdout.read() + job.stderr.read()))
else: # Single-process
for app_path, app_object_path in zip(config.APPS, all_app_objects):
if app_path in config.IGNORE_APPS:
@ -223,13 +226,13 @@ def make(config: Config):
job = subprocess.run(cmd, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True, shell=True)
if job.returncode != 0:
error_paths.append((source_path, job.stdout + job.stderr))
error_paths.append((app_path, job.stdout + job.stderr))
if error_paths:
for error_path, error_text in error_paths:
print(ConsoleColor.RED + f'Error linking {error_path}:\n' + ConsoleColor.ENDCOLOR + error_text)
with open(os.path.join(config.OBJECT_DIR, 'hash.json'), 'w') as hash_file:
with open(os.path.join(config.OBJECT_DIR, 'hash.json'), 'w', encoding='utf-8') as hash_file:
hash_file.write(json.dumps(hash_dict, indent=1))
sys.exit(1)
with open(config.OBJECT_DIR / 'hash.json', 'w') as hash_file:
with open(config.OBJECT_DIR / 'hash.json', 'w', encoding='utf-8') as hash_file:
hash_file.write(json.dumps(hash_dict, indent=1))