diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /build/scripts/copy_to_dir.py | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'build/scripts/copy_to_dir.py')
-rw-r--r-- | build/scripts/copy_to_dir.py | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/build/scripts/copy_to_dir.py b/build/scripts/copy_to_dir.py new file mode 100644 index 00000000000..9baeb5ffacf --- /dev/null +++ b/build/scripts/copy_to_dir.py @@ -0,0 +1,75 @@ +import errno +import sys +import os +import shutil +import optparse +import tarfile + + +def parse_args(): + parser = optparse.OptionParser() + parser.add_option('--build-root') + parser.add_option('--dest-dir') + parser.add_option('--dest-arch') + return parser.parse_args() + + +def ensure_dir_exists(path): + try: + os.makedirs(path) + except OSError as e: + if e.errno == errno.EEXIST and os.path.isdir(path): + pass + else: + raise + + +def hardlink_or_copy(src, dst): + if os.name == 'nt': + shutil.copy(src, dst) + else: + try: + os.link(src, dst) + except OSError as e: + if e.errno == errno.EEXIST: + return + elif e.errno == errno.EXDEV: + sys.stderr.write("Can't make cross-device hardlink - fallback to copy: {} -> {}\n".format(src, dst)) + shutil.copy(src, dst) + else: + raise + + +def main(): + opts, args = parse_args() + assert opts.build_root + assert opts.dest_dir + + dest_arch = None + if opts.dest_arch: + if opts.dest_arch.endswith('.tar'): + dest_arch = tarfile.open(opts.dest_arch, 'w', dereference=True) + elif opts.dest_arch.endswith('.tar.gz') or opts.dest_arch.endswith('.tgz'): + dest_arch = tarfile.open(opts.dest_arch, 'w:gz', dereference=True) + else: + # TODO: move check to graph generation stage + raise Exception('Unsopported archive type for {}. Use one of: tar, tar.gz, tgz.'.format(os.path.basename(opts.dest_arch))) + + for arg in args: + dst = arg + if dst.startswith(opts.build_root): + dst = dst[len(opts.build_root) + 1:] + + if dest_arch and not arg.endswith('.pkg.fake'): + dest_arch.add(arg, arcname=dst) + + dst = os.path.join(opts.dest_dir, dst) + ensure_dir_exists(os.path.dirname(dst)) + hardlink_or_copy(arg, dst) + + if dest_arch: + dest_arch.close() + + +if __name__ == '__main__': + sys.exit(main()) |