blob: 56e36f3ad5492a1c800a539eb93f8239ed1691ca [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Brad Bishopc342db32019-05-15 21:57:59 -04002#
3# SPDX-License-Identifier: GPL-2.0-only
4#
5
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006import sys
7import os
8
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
12if len(sys.argv) != 2:
13 print("Usage is " + sys.argv[0] + "<directory>")
14 sys.exit(1)
15
16topdir = sys.argv[1]
17topdir = os.path.abspath(topdir)
18
19def handlelink(filep, subdir):
20 link = os.readlink(filep)
21 if link[0] != "/":
22 return
23 if link.startswith(topdir):
24 return
25 #print("Replacing %s with %s for %s" % (link, topdir+link, filep))
26 print("Replacing %s with %s for %s" % (link, os.path.relpath(topdir+link, subdir), filep))
27 os.unlink(filep)
28 os.symlink(os.path.relpath(topdir+link, subdir), filep)
29
30for subdir, dirs, files in os.walk(topdir):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050031 for f in dirs + files:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032 filep = os.path.join(subdir, f)
33 if os.path.islink(filep):
34 #print("Considering %s" % filep)
35 handlelink(filep, subdir)