Last active
July 8, 2020 21:09
-
-
Save dillera/c5b7ab0138a22dd821023bdc069d9135 to your computer and use it in GitHub Desktop.
Python script to fix IRIX idm files with symbolic links in them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Read in prepared list of symlinks from fs and | |
# a IDM file; write out new IDM file with symlinks | |
# pointing to their source as stdout | |
# v0.1 | |
LINK = "l" | |
########################################################### | |
# read in the specially prepared symlink file | |
# generate with | |
# find /usr/sgug_raw_selfhoster -type l -ls | awk '{print $11 " " $13}' | |
# then fix links with | |
# sed 's/sgug_raw_selfhoster/sgug/g' | |
# put each line into a dict with the value as the STREE path | |
# and the key as the DTREE target | |
# we will use the actual idm file STREE path to match the key | |
# and then take the value, pre-prend the right path for DTREE | |
# and write it out | |
# by default Software Packager just points symlinks at themselves! | |
symlinks = {} | |
with open("fs_link_input_file2.txt") as f: | |
for line in f: | |
(key, val) = line.split() | |
symlinks[(key)] = val | |
#for item in symlinks: | |
# print("Key : {} , Value : {}".format(item,symlinks[item])) | |
########################################################### | |
# read in the idb file to be corrected. | |
# the fixed file will be written out on stdout | |
with open("rse-b5-self-hoster_bad_links.idb") as file_in: | |
idb = [] | |
for line in file_in: | |
idb.append(line.strip()) | |
########################################################### | |
# main loop to fix the idm file, line by line looking for symlink lines | |
try: | |
# go over every line from the idm file, looking for symlink lines to fix | |
for line in idb: | |
splits = line.split() # split each line up | |
if splits[0] == "l": # this line from IDB is a link | |
thisLink = "/" + splits[4] # correct missing slash from the symlinks file | |
# go over each symlink item, I could not find a better way to get the key based on thisLink | |
for key, value in symlinks.items() : | |
if (key == thisLink): # matched key (path for symlink) | |
path = thisLink.split("/") # split the /usr/sgug/XXX path up | |
target_root = path[3] # take the element after sgug | |
# print out the new idb line, fixing the symlink to point to the actual file not itself, fix that path with target_root | |
print( splits[0] + " " + splits[1] + " " + splits[2] + " " + splits[3] + " " + splits[4] + " " + target_root + "/" + value + " " + splits[6] ) | |
# if this is not a symlink line then just spit it out as is | |
else: | |
print( splits[0] + " " + splits[1] + " " + splits[2] + " " + splits[3] + " " + splits[4] + " " + splits[5] + " " + splits[6] ) | |
except: | |
print("error") | |
pass | |
~ |
the trick is getting the real actual proper target of the symlink. since Software Packager had no idea, you need to run that find command with some awk to get just the path of the symlink and the real filename of the target.
Since the IDM file has as it's STREE the same thing as the full path of the symlink you found on the filesystem, you can use that as the key to the dict you created and the value is the real file. Then write out the line but put in the real target as the DTREE and you have it fixed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
before:
after script