2018-11-28 15:31:16 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# #############################################################################
|
2018-12-13 07:57:08 +00:00
|
|
|
# Copyright (c) 2018-present lzutao <taolzu(at)gmail.com>
|
2018-11-28 15:31:16 +00:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# This source code is licensed under both the BSD-style license (found in the
|
|
|
|
# LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
# in the COPYING file in the root directory of this source tree).
|
|
|
|
# #############################################################################
|
2018-12-13 07:57:08 +00:00
|
|
|
# This file should be synced with https://github.com/lzutao/meson-symlink
|
2018-11-28 15:31:16 +00:00
|
|
|
|
2018-12-03 15:13:29 +00:00
|
|
|
import os
|
|
|
|
import pathlib # since Python 3.4
|
2018-11-28 15:31:16 +00:00
|
|
|
|
|
|
|
|
2018-12-02 15:48:11 +00:00
|
|
|
def install_symlink(src, dst, install_dir, dst_is_dir=False, dir_mode=0o777):
|
2018-12-03 15:13:29 +00:00
|
|
|
if not install_dir.exists():
|
|
|
|
install_dir.mkdir(mode=dir_mode, parents=True, exist_ok=True)
|
|
|
|
if not install_dir.is_dir():
|
|
|
|
raise NotADirectoryError(install_dir)
|
2018-11-28 15:31:16 +00:00
|
|
|
|
2018-12-03 15:13:29 +00:00
|
|
|
new_dst = install_dir.joinpath(dst)
|
|
|
|
if new_dst.is_symlink() and os.readlink(new_dst) == src:
|
2018-12-03 04:02:42 +00:00
|
|
|
print('File exists: {!r} -> {!r}'.format(new_dst, src))
|
2018-11-28 15:31:16 +00:00
|
|
|
return
|
2018-12-03 04:02:42 +00:00
|
|
|
print('Installing symlink {!r} -> {!r}'.format(new_dst, src))
|
2018-12-03 15:13:29 +00:00
|
|
|
new_dst.symlink_to(src, target_is_directory=dst_is_dir)
|
2018-11-28 15:31:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
import argparse
|
2018-12-03 04:02:42 +00:00
|
|
|
parser = argparse.ArgumentParser(description='Install a symlink',
|
2018-12-13 07:57:08 +00:00
|
|
|
usage='{0} [-h] [-d] [-m MODE] source dest install_dir\n\n'
|
2018-11-28 15:31:16 +00:00
|
|
|
'example:\n'
|
2018-12-13 07:57:08 +00:00
|
|
|
' {0} dash sh /bin'.format(pathlib.Path(__file__).name))
|
|
|
|
parser.add_argument('source', help='target to link')
|
|
|
|
parser.add_argument('dest', help='link name')
|
2018-11-28 15:31:16 +00:00
|
|
|
parser.add_argument('install_dir', help='installation directory')
|
|
|
|
parser.add_argument('-d', '--isdir',
|
|
|
|
action='store_true',
|
2018-12-13 07:57:08 +00:00
|
|
|
help='dest is a directory')
|
2018-11-28 15:31:16 +00:00
|
|
|
parser.add_argument('-m', '--mode',
|
|
|
|
help='directory mode on creating if not exist',
|
2018-12-13 07:57:08 +00:00
|
|
|
default='0o755')
|
2018-11-28 15:31:16 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
dir_mode = int(args.mode, 8)
|
|
|
|
|
2018-12-13 11:07:01 +00:00
|
|
|
meson_destdir = os.environ.get('MESON_INSTALL_DESTDIR_PREFIX', default='')
|
|
|
|
install_dir = pathlib.Path(meson_destdir, args.install_dir)
|
2018-12-13 07:57:08 +00:00
|
|
|
install_symlink(args.source, args.dest, install_dir, args.isdir, dir_mode)
|
2018-11-28 15:31:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|