|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from os import path |
| 7 | + |
| 8 | + |
| 9 | +def run(xcframework_path: str): |
| 10 | + if not xcframework_path.endswith('.xcframework'): |
| 11 | + print('XCFramework path must end in *.xcframework', file=sys.stderr) |
| 12 | + sys.exit(1) |
| 13 | + if not path.exists(xcframework_path): |
| 14 | + print(f'Path {xcframework_path} does not exist', file=sys.stderr) |
| 15 | + sys.exit(1) |
| 16 | + if not path.isdir(xcframework_path): |
| 17 | + print('XCFramework must be a directory', file=sys.stderr) |
| 18 | + sys.exit(1) |
| 19 | + |
| 20 | + walked_paths = set() |
| 21 | + |
| 22 | + for current_root, current_subdirectories, current_files in os.walk(xcframework_path): |
| 23 | + |
| 24 | + if current_root in walked_paths: |
| 25 | + print('Skipping', current_root) |
| 26 | + |
| 27 | + for current_subdirectory in current_subdirectories: |
| 28 | + current_directory = path.join(current_root, current_subdirectory) |
| 29 | + is_symlink = path.islink(current_directory) |
| 30 | + if not is_symlink: |
| 31 | + continue |
| 32 | + |
| 33 | + walked_paths.add(current_directory) |
| 34 | + |
| 35 | + target = path.realpath(current_directory) |
| 36 | + print('Directory:', current_directory, '>', target) |
| 37 | + # remove current subdirectory |
| 38 | + print('Removing', current_directory) |
| 39 | + os.unlink(current_directory) |
| 40 | + # copy target into current directory |
| 41 | + target_contents = os.path.join(target, '.') |
| 42 | + print('Copying', target_contents, 'to', current_directory) |
| 43 | + subprocess.check_call(['cp', '-r', target_contents, current_directory]) |
| 44 | + |
| 45 | + for current_file in current_files: |
| 46 | + current_file_absolute = os.path.join(current_root, current_file) |
| 47 | + is_symlink = os.path.islink(current_file_absolute) |
| 48 | + if not is_symlink: |
| 49 | + continue |
| 50 | + target = path.realpath(current_file_absolute) |
| 51 | + print('File:', current_file_absolute, '>', target) |
| 52 | + # remove current file |
| 53 | + print('Removing', current_file_absolute) |
| 54 | + os.unlink(current_file_absolute) |
| 55 | + # copy target into current directory |
| 56 | + print('Copying', target, 'to', current_root) |
| 57 | + subprocess.check_call(['cp', target, current_root]) |
| 58 | + |
| 59 | + pass |
| 60 | + |
| 61 | +if __name__ == '__main__': |
| 62 | + parser = argparse.ArgumentParser(description='Resolve and replace symlinks in an xcframework') |
| 63 | + parser.add_argument('path', type=str, help='new checksum of LightningDevKit.xcframework.zip', default=None) |
| 64 | + args = parser.parse_args() |
| 65 | + run(xcframework_path=args.path) |
0 commit comments