1#!/usr/bin/env python3
2# #############################################################################
3# Copyright (c) 2018-present  lzutao <taolzu(at)gmail.com>
4# All rights reserved.
5#
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
11
12import os
13import pathlib  # since Python 3.4
14
15
16def 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)
21
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))
25    return
26  print('Installing symlink {!r} -> {!r}'.format(new_dst, src))
27  new_dst.symlink_to(src, target_is_directory=dst_is_dir)
28
29
30def main():
31  import argparse
32  parser = argparse.ArgumentParser(description='Install a symlink',
33      usage='{0} [-h] [-d] [-m MODE] source dest install_dir\n\n'
34            'example:\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',
40      action='store_true',
41      help='dest is a directory')
42  parser.add_argument('-m', '--mode',
43      help='directory mode on creating if not exist',
44      default='0o755')
45  args = parser.parse_args()
46
47  dir_mode = int(args.mode, 8)
48
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)
52
53
54if __name__ == '__main__':
55  main()
56