summaryrefslogtreecommitdiffstats
path: root/build/scripts/bundle_output.py
blob: 8b99eaa21fe8e70f893c79db303104d5016d7657 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import argparse
import os
import shutil
import sys


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--name', required=True)
    parser.add_argument('--output', required=True)
    parser.add_argument('--result', required=True)
    return parser.parse_args(sys.argv[1:])


def main():
    args = parse_args()
    if os.path.basename(args.name) != args.name:
        print("Error: --name must be a base name without directory components", file=sys.stderr, flush=True)
        sys.exit(1)
    result_dir = os.path.dirname(args.result)
    src_path = os.path.join(result_dir, args.name)

    if not os.path.exists(src_path):
        print(f"Error: Source file '{src_path}' does not exist", file=sys.stderr, flush=True)
        sys.exit(1)
    if not os.path.isfile(src_path):
        print(f"Error: Source path '{src_path}' is not a file", file=sys.stderr, flush=True)
        sys.exit(1)
    if os.path.isdir(args.output):
        print(f"Error: Output path '{args.output}' is a directory", file=sys.stderr, flush=True)
        sys.exit(1)
    output_dir = os.path.dirname(args.output)
    if output_dir:
        os.makedirs(output_dir, exist_ok=True)

    try:
        shutil.move(src_path, args.output)
    except Exception as e:
        print(f"Error moving file: {str(e)}", file=sys.stderr, flush=True)
        sys.exit(1)


if __name__ == '__main__':
    main()