diff --git a/ets2panda/driver/build_system/build_build_system.py b/ets2panda/driver/build_system/build_build_system.py index 70ac97d99d2aeb5adf675e27c41aeefc147786df..4b2e3921f47ec96ed0cd46bbafc3455fa2661764 100755 --- a/ets2panda/driver/build_system/build_build_system.py +++ b/ets2panda/driver/build_system/build_build_system.py @@ -19,6 +19,18 @@ import shutil import subprocess import sys import tarfile +import fcntl + + +def ensure_lock_dir_exists(lock_path): + lock_dir = os.path.dirname(lock_path) + if lock_dir and not os.path.exists(lock_dir): + try: + os.makedirs(lock_dir, exist_ok=True) + return True + except OSError as e: + return False + return True def copy_files(source_path, dest_path, is_file=False): @@ -71,9 +83,22 @@ def parse_args(): def main(): options = parse_args() + lock_path = options.source_path + '.lock' + if not ensure_lock_dir_exists(lock_path): + raise Exception(f"Create lock dir failed: {lock_path}") - build(options) - copy_output(options) + try: + lock_file = open(lock_path, 'w') + fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX) + build(options) + copy_output(options) + except Exception as err: + raise Exception("main run failed. Error: " + str(err)) from err + finally: + if lock_file: + fcntl.flock(lock_file.fileno(), fcntl.LOCK_UN) + lock_file.close() + if __name__ == '__main__':