Make some error strings more generic
[pgsql.git] / src / tools / install_files
blob08436c788d103a5ad6e936e1b0a45870e83682f1
1 #!/usr/bin/env python3
3 # Helper to install files that are not part of the default meson install
4 # target.
6 # This includes files that should only get installed into the temporary
7 # installation for tests and documentation.
9 import argparse
10 import os
11 import shutil
12 import sys
13 from pathlib import PurePath
15 parser = argparse.ArgumentParser()
17 parser.add_argument('--destdir', type=str,
18                     default=os.environ.get('DESTDIR', None))
19 parser.add_argument('--prefix', type=str)
20 parser.add_argument('--install', type=str, nargs='+',
21                     action='append', default=[])
22 parser.add_argument('--install-dirs', type=str, nargs='+',
23                     action='append', default=[])
24 parser.add_argument('--install-dir-contents', type=str, nargs='+',
25                     action='append', default=[])
27 args = parser.parse_args()
30 def error_exit(msg: str):
31     print(msg, file=sys.stderr)
32     exit(1)
35 def create_target_dir(prefix: str, destdir: str, targetdir: str):
36     if not os.path.isabs(targetdir):
37         targetdir = os.path.join(prefix, targetdir)
39     if destdir is not None:
40         # copy of meson's logic for joining destdir and install paths
41         targetdir = str(PurePath(destdir, *PurePath(targetdir).parts[1:]))
43     os.makedirs(targetdir, exist_ok=True)
45     return targetdir
48 def copy_files(targetdir: str, src_list: list):
49     for src in src_list:
50         shutil.copy2(src, targetdir)
53 def copy_dirs(targetdir: str, src_list: list, contents: bool):
54     for src in src_list:
55         if not os.path.isdir(src):
56             error_exit('{0} is not a directory'.format(src))
58         if contents:
59             target = targetdir
60         else:
61             target = os.path.join(targetdir, os.path.split(src)[1])
62         shutil.copytree(src, target, dirs_exist_ok=True)
65 for installs in args.install:
66     targetdir = create_target_dir(args.prefix, args.destdir, installs[0])
67     copy_files(targetdir, installs[1:])
69 for installs in args.install_dirs:
70     targetdir = create_target_dir(args.prefix, args.destdir, installs[0])
71     copy_dirs(targetdir, installs[1:], contents=False)
73 for installs in args.install_dir_contents:
74     targetdir = create_target_dir(args.prefix, args.destdir, installs[0])
75     copy_dirs(targetdir, installs[1:], contents=True)