sysroot-relativelinks.py 1012 B

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