symlink-install-tree.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/env python3
  2. from pathlib import PurePath
  3. import errno
  4. import json
  5. import os
  6. import subprocess
  7. import sys
  8. def destdir_join(d1: str, d2: str) -> str:
  9. if not d1:
  10. return d2
  11. # c:\destdir + c:\prefix must produce c:\destdir\prefix
  12. return str(PurePath(d1, *PurePath(d2).parts[1:]))
  13. introspect = os.environ.get('MESONINTROSPECT')
  14. out = subprocess.run([*introspect.split(' '), '--installed'],
  15. stdout=subprocess.PIPE, check=True).stdout
  16. for source, dest in json.loads(out).items():
  17. bundle_dest = destdir_join('qemu-bundle', dest)
  18. path = os.path.dirname(bundle_dest)
  19. try:
  20. os.makedirs(path, exist_ok=True)
  21. except BaseException as e:
  22. print(f'error making directory {path}', file=sys.stderr)
  23. raise e
  24. try:
  25. os.symlink(source, bundle_dest)
  26. except BaseException as e:
  27. if not isinstance(e, OSError) or e.errno != errno.EEXIST:
  28. if os.name == 'nt':
  29. print('Please enable Developer Mode to support soft link '
  30. 'without Administrator permission')
  31. print(f'error making symbolic link {dest}', file=sys.stderr)
  32. raise e