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