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_files_to_dir.py | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'build/scripts/copy_files_to_dir.py')
-rw-r--r-- | build/scripts/copy_files_to_dir.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/build/scripts/copy_files_to_dir.py b/build/scripts/copy_files_to_dir.py new file mode 100644 index 0000000000..ead57ba16e --- /dev/null +++ b/build/scripts/copy_files_to_dir.py @@ -0,0 +1,59 @@ +import argparse +import errno +import os +import process_command_files as pcf +import shutil +import sys + + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument('--dest-dir', required=True) + parser.add_argument('--existing', choices=('skip', 'overwrite'), default='overwrite') + parser.add_argument('--flat', action='store_true') + parser.add_argument('--skip-prefix', dest='skip_prefixes', action='append', default=[]) + parser.add_argument('files', nargs='*') + return parser.parse_args(pcf.get_args(sys.argv[1:])) + + +def makedirs(dirname): + try: + os.makedirs(dirname) + except OSError as e: + if e.errno == errno.EEXIST and os.path.isdir(dirname): + pass + else: + raise + + +def main(): + args = parse_args() + + dest_dir = os.path.normpath(args.dest_dir) + os.pathsep + makedirs(dest_dir) + + prefixes = ['{}{}'.format(os.path.normpath(p), os.path.sep) for p in args.skip_prefixes] + + for src in args.files: + src = os.path.normpath(src) + assert os.path.isfile(src) + if args.flat: + rel_dst = os.path.basename(src) + else: + rel_dst = src + for prefix in prefixes: + if src.startswith(prefix): + rel_dst = src[len(prefix):] + break + assert not os.path.isabs(rel_dst) + dst = os.path.join(args.dest_dir, rel_dst) + if os.path.isfile(dst) and args.existing == 'skip': + break + + makedirs(os.path.dirname(dst)) + + shutil.copyfile(src, dst) + + +if __name__ == '__main__': + main() |