sysroot-relativelinks.py 959 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env python
  2. import sys
  3. import os
  4. # Take a sysroot directory and turn all the abolute symlinks and turn them into
  5. # relative ones such that the sysroot is usable within another system.
  6. if len(sys.argv) != 2:
  7. print("Usage is " + sys.argv[0] + "<directory>")
  8. sys.exit(1)
  9. topdir = sys.argv[1]
  10. topdir = os.path.abspath(topdir)
  11. def handlelink(filep, subdir):
  12. link = os.readlink(filep)
  13. if link[0] != "/":
  14. return
  15. if link.startswith(topdir):
  16. return
  17. #print("Replacing %s with %s for %s" % (link, topdir+link, filep))
  18. print("Replacing %s with %s for %s" % (link, os.path.relpath(topdir+link, subdir), filep))
  19. os.unlink(filep)
  20. os.symlink(os.path.relpath(topdir+link, subdir), filep)
  21. for subdir, dirs, files in os.walk(topdir):
  22. for f in files:
  23. filep = os.path.join(subdir, f)
  24. if os.path.islink(filep):
  25. #print("Considering %s" % filep)
  26. handlelink(filep, subdir)