sysroot-relativelinks.py 1.0 KB

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