InstallSymlink.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. import os
  12. import pathlib # since Python 3.4
  13. def install_symlink(src, dst, install_dir, dst_is_dir=False, dir_mode=0o777):
  14. if not install_dir.exists():
  15. install_dir.mkdir(mode=dir_mode, parents=True, exist_ok=True)
  16. if not install_dir.is_dir():
  17. raise NotADirectoryError(install_dir)
  18. new_dst = install_dir.joinpath(dst)
  19. if new_dst.is_symlink() and os.readlink(new_dst) == src:
  20. print('File exists: {!r} -> {!r}'.format(new_dst, src))
  21. return
  22. print('Installing symlink {!r} -> {!r}'.format(new_dst, src))
  23. new_dst.symlink_to(src, target_is_directory=dst_is_dir)
  24. def main():
  25. import argparse
  26. parser = argparse.ArgumentParser(description='Install a symlink',
  27. usage='{0} [-h] [-d] [-m MODE] source dest install_dir\n\n'
  28. 'example:\n'
  29. ' {0} dash sh /bin'.format(pathlib.Path(__file__).name))
  30. parser.add_argument('source', help='target to link')
  31. parser.add_argument('dest', help='link name')
  32. parser.add_argument('install_dir', help='installation directory')
  33. parser.add_argument('-d', '--isdir',
  34. action='store_true',
  35. help='dest is a directory')
  36. parser.add_argument('-m', '--mode',
  37. help='directory mode on creating if not exist',
  38. default='0o755')
  39. args = parser.parse_args()
  40. dir_mode = int(args.mode, 8)
  41. meson_destdir = os.environ.get('MESON_INSTALL_DESTDIR_PREFIX', default='')
  42. install_dir = pathlib.Path(meson_destdir, args.install_dir)
  43. install_symlink(args.source, args.dest, install_dir, args.isdir, dir_mode)
  44. if __name__ == '__main__':
  45. main()