2 # #############################################################################
3 # Copyright (c) 2018-present lzutao <taolzu(at)gmail.com>
6 # This source code is licensed under both the BSD-style license (found in the
7 # LICENSE file in the root directory of this source tree) and the GPLv2 (found
8 # in the COPYING file in the root directory of this source tree).
9 # #############################################################################
10 # This file should be synced with https://github.com/lzutao/meson-symlink
13 import pathlib # since Python 3.4
16 def install_symlink(src, dst, install_dir, dst_is_dir=False, dir_mode=0o777):
17 if not install_dir.exists():
18 install_dir.mkdir(mode=dir_mode, parents=True, exist_ok=True)
19 if not install_dir.is_dir():
20 raise NotADirectoryError(install_dir)
22 new_dst = install_dir.joinpath(dst)
23 if new_dst.is_symlink() and os.readlink(new_dst) == src:
24 print('File exists: {!r} -> {!r}'.format(new_dst, src))
26 print('Installing symlink {!r} -> {!r}'.format(new_dst, src))
27 new_dst.symlink_to(src, target_is_directory=dst_is_dir)
32 parser = argparse.ArgumentParser(description='Install a symlink',
33 usage='{0} [-h] [-d] [-m MODE] source dest install_dir\n\n'
35 ' {0} dash sh /bin'.format(pathlib.Path(__file__).name))
36 parser.add_argument('source', help='target to link')
37 parser.add_argument('dest', help='link name')
38 parser.add_argument('install_dir', help='installation directory')
39 parser.add_argument('-d', '--isdir',
41 help='dest is a directory')
42 parser.add_argument('-m', '--mode',
43 help='directory mode on creating if not exist',
45 args = parser.parse_args()
47 dir_mode = int(args.mode, 8)
49 meson_destdir = os.environ.get('MESON_INSTALL_DESTDIR_PREFIX', default='')
50 install_dir = pathlib.Path(meson_destdir, args.install_dir)
51 install_symlink(args.source, args.dest, install_dir, args.isdir, dir_mode)
54 if __name__ == '__main__':