meson: Update usage of InstallSymlink helper
authorLzu Tao <taolzu@gmail.com>
Mon, 3 Dec 2018 04:02:56 +0000 (11:02 +0700)
committerLzu Tao <taolzu@gmail.com>
Mon, 3 Dec 2018 15:48:37 +0000 (22:48 +0700)
contrib/meson/InstallSymlink.py

index d7b1e5a..a0858eb 100644 (file)
@@ -7,42 +7,49 @@
 # 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).
 # #############################################################################
-import errno
+
 import os
+import pathlib  # since Python 3.4
+
 
+def prepend_destdir(path):
+  """prepend_destdir(path) -> Path
 
-def mkdir_p(path, dir_mode=0o777):
-  try:
-    os.makedirs(path, mode=dir_mode)
-  except OSError as exc:  # Python >2.5
-    if exc.errno == errno.EEXIST and os.path.isdir(path):
-      pass
-    else:
-      raise
+  Needed because pathlib.Path.joinpath() discards the first path if the
+  second one is absolute, which is usually the case here.
+  """
+  path = pathlib.Path(path)
+  DESTDIR = os.environ.get('DESTDIR')
+
+  if DESTDIR:
+    if not path.is_absolute():
+      raise Exception('{!r} must be an absolute path when DESTDIR is set'.format(path))
 
+    path = pathlib.Path(DESTDIR).joinpath(*path.resolve().parts[1:])
+  return path
 
-def InstallSymlink(src, dst, install_dir, dst_is_dir=False, dir_mode=0o777):
-  if not os.path.exists(install_dir):
-      mkdir_p(install_dir, dir_mode)
-  if not os.path.isdir(install_dir):
-      raise NotADirectoryError(install_dir)
 
-  new_dst = os.path.join(install_dir, dst)
-  if os.path.islink(new_dst) and os.readlink(new_dst) == src:
-    print('File exists: %r -> %r' % (dst, src))
+def install_symlink(src, dst, install_dir, dst_is_dir=False, dir_mode=0o777):
+  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)
+
+  new_dst = install_dir.joinpath(dst)
+  if new_dst.is_symlink() and os.readlink(new_dst) == src:
+    print('File exists: {!r} -> {!r}'.format(new_dst, src))
     return
-  print('Installing symlink %r -> %r' % (new_dst, src))
-  os.symlink(src, new_dst, dst_is_dir)
+  print('Installing symlink {!r} -> {!r}'.format(new_dst, src))
+  new_dst.symlink_to(src, target_is_directory=dst_is_dir)
 
 
 def main():
   import argparse
-  parser = argparse.ArgumentParser(description='Install a symlink.\n',
-      usage='usage: InstallSymlink.py [-h] [-d] [-m MODE] src dst '
-            'install_dir\n\n'
+  parser = argparse.ArgumentParser(description='Install a symlink',
+      usage='InstallSymlink.py [-h] [-d] [-m MODE] src dst install_dir\n\n'
             'example:\n'
-            '\tInstallSymlink.py libcrypto.so.1.0.0 libcrypt.so '
-            '/usr/lib/x86_64-linux-gnu False')
+            '\tInstallSymlink.py dash sh /bin\n'
+            '\tDESTDIR=./staging InstallSymlink.py dash sh /bin')
   parser.add_argument('src', help='target to link')
   parser.add_argument('dst', help='link name')
   parser.add_argument('install_dir', help='installation directory')
@@ -56,16 +63,11 @@ def main():
 
   src = args.src
   dst = args.dst
-  install_dir = args.install_dir
   dst_is_dir = args.isdir
   dir_mode = int(args.mode, 8)
 
-  DESTDIR = os.environ.get('DESTDIR')
-  if DESTDIR:
-      install_dir = DESTDIR + install_dir if os.path.isabs(install_dir) \
-               else os.path.join(DESTDIR, install_dir)
-
-  InstallSymlink(src, dst, install_dir, dst_is_dir, dir_mode)
+  install_dir = prepend_destdir(args.install_dir)
+  install_symlink(src, dst, install_dir, dst_is_dir, dir_mode)
 
 
 if __name__ == '__main__':