blob: 3f2998c647ab74e3eb4b82bffa721adb82a848bf [file] [log] [blame]
Lzu Taoef2e7612018-11-28 22:31:16 +07001#!/usr/bin/env python3
2# #############################################################################
Lzu Taoabfde032018-12-13 14:57:08 +07003# Copyright (c) 2018-present lzutao <taolzu(at)gmail.com>
Lzu Taoef2e7612018-11-28 22:31:16 +07004# 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# #############################################################################
Lzu Taoabfde032018-12-13 14:57:08 +070010# This file should be synced with https://github.com/lzutao/meson-symlink
Lzu Tao67babb62018-12-03 22:13:29 +070011
Lzu Taoef2e7612018-11-28 22:31:16 +070012import os
Lzu Tao67babb62018-12-03 22:13:29 +070013import pathlib # since Python 3.4
Lzu Taoef2e7612018-11-28 22:31:16 +070014
15
Lzu Tao437ec5f2018-12-02 22:48:11 +070016def install_symlink(src, dst, install_dir, dst_is_dir=False, dir_mode=0o777):
Lzu Tao67babb62018-12-03 22:13:29 +070017 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)
Lzu Taoef2e7612018-11-28 22:31:16 +070021
Lzu Tao67babb62018-12-03 22:13:29 +070022 new_dst = install_dir.joinpath(dst)
23 if new_dst.is_symlink() and os.readlink(new_dst) == src:
Lzu Taof8975232018-12-03 11:02:42 +070024 print('File exists: {!r} -> {!r}'.format(new_dst, src))
Lzu Taoef2e7612018-11-28 22:31:16 +070025 return
Lzu Taof8975232018-12-03 11:02:42 +070026 print('Installing symlink {!r} -> {!r}'.format(new_dst, src))
Lzu Tao67babb62018-12-03 22:13:29 +070027 new_dst.symlink_to(src, target_is_directory=dst_is_dir)
Lzu Taoef2e7612018-11-28 22:31:16 +070028
29
30def main():
31 import argparse
Lzu Taof8975232018-12-03 11:02:42 +070032 parser = argparse.ArgumentParser(description='Install a symlink',
Lzu Taoabfde032018-12-13 14:57:08 +070033 usage='{0} [-h] [-d] [-m MODE] source dest install_dir\n\n'
Lzu Taoef2e7612018-11-28 22:31:16 +070034 'example:\n'
Lzu Taoabfde032018-12-13 14:57:08 +070035 ' {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')
Lzu Taoef2e7612018-11-28 22:31:16 +070038 parser.add_argument('install_dir', help='installation directory')
39 parser.add_argument('-d', '--isdir',
40 action='store_true',
Lzu Taoabfde032018-12-13 14:57:08 +070041 help='dest is a directory')
Lzu Taoef2e7612018-11-28 22:31:16 +070042 parser.add_argument('-m', '--mode',
43 help='directory mode on creating if not exist',
Lzu Taoabfde032018-12-13 14:57:08 +070044 default='0o755')
Lzu Taoef2e7612018-11-28 22:31:16 +070045 args = parser.parse_args()
46
Lzu Taoef2e7612018-11-28 22:31:16 +070047 dir_mode = int(args.mode, 8)
48
Lzu Tao3ee55042018-12-13 18:07:01 +070049 meson_destdir = os.environ.get('MESON_INSTALL_DESTDIR_PREFIX', default='')
50 install_dir = pathlib.Path(meson_destdir, args.install_dir)
Lzu Taoabfde032018-12-13 14:57:08 +070051 install_symlink(args.source, args.dest, install_dir, args.isdir, dir_mode)
Lzu Taoef2e7612018-11-28 22:31:16 +070052
53
54if __name__ == '__main__':
55 main()