104 lines
2.4 KiB
Python
104 lines
2.4 KiB
Python
#!/usr/bin/python3
|
|
import os
|
|
import sys
|
|
import re
|
|
from pathlib import PurePath, PurePosixPath
|
|
PP = PurePosixPath
|
|
|
|
def strip_part(part):
|
|
part = part.lower()
|
|
part = re.sub(r"[^a-z]+", "", part)
|
|
return part
|
|
|
|
def recursive_fuzzy_search(base, parts):
|
|
if not parts:
|
|
"done recursion"
|
|
return base
|
|
|
|
part = parts[0]
|
|
if os.path.exists(os.path.join(base, part)):
|
|
"perfect match"
|
|
base = os.path.join(base, part)
|
|
return recursive_fuzzy_search(base, parts[1:])
|
|
|
|
spart = strip_part(part)
|
|
basemap = {}
|
|
for bpart in os.listdir(base):
|
|
"""
|
|
if (len(parts) > 1) != (os.path.isdir(os.path.join(base, bpart))):
|
|
continue
|
|
"""
|
|
if not os.path.isdir(os.path.join(base, bpart)):
|
|
"we are only looking at subdirs for now"
|
|
"we don't link standalone files"
|
|
continue
|
|
sbpart = strip_part(bpart)
|
|
if sbpart in basemap:
|
|
"ambiguous match"
|
|
return None
|
|
basemap[sbpart] = bpart
|
|
#print(f"Searching {spart} in {basemap}")
|
|
if spart in basemap:
|
|
base = os.path.join(base, basemap[spart])
|
|
return recursive_fuzzy_search(base, parts[1:])
|
|
|
|
"no match"
|
|
return None
|
|
|
|
|
|
|
|
def testpath(tested, base, sub):
|
|
if tested:
|
|
return tested
|
|
testvar = os.path.join(base, sub)
|
|
if os.path.exists(testvar):
|
|
return testvar
|
|
|
|
if not os.path.exists(base):
|
|
return None
|
|
|
|
"now, it gets fuzzier... much, much fuzzier"
|
|
rv = recursive_fuzzy_search(base, PP(sub).parts)
|
|
#print(f"Recursive search for {PP(sub).parts} found {rv}")
|
|
|
|
return rv
|
|
|
|
def resolve_realpath(hint, realpath):
|
|
done = None
|
|
if hint == "link_appdata":
|
|
done = testpath(done, os.environ['HOME'], realpath)
|
|
return done
|
|
elif hint == "link_user":
|
|
done = testpath(done, os.environ['HOME'], realpath)
|
|
elif hint == "link_games":
|
|
done = testpath(done, os.path.join(os.environ['HOME'], 'Games'), realpath)
|
|
elif hint == "link_steam":
|
|
done = testpath(done, os.path.join(os.environ['HOME'], 'Games', 'Steam'), realpath)
|
|
|
|
return done
|
|
|
|
|
|
def install_link(hint, realpath, linkpath):
|
|
#os.symlink('linkpath', 'realpath')
|
|
realpath = realpath.replace('\\', '/')
|
|
print((hint, realpath))
|
|
reallyrealpath = resolve_realpath(hint, realpath)
|
|
if reallyrealpath:
|
|
print(reallyrealpath)
|
|
|
|
def main():
|
|
linklist = []
|
|
srcbat = "zz_subcall_hardlinks_install.bat"
|
|
matchre = re.compile('^call :([^ ]*) "([^"]*)" "([^"]*)"')
|
|
with open(srcbat, 'r') as fd:
|
|
for line in fd:
|
|
m = matchre.match(line)
|
|
if m:
|
|
install_link(m[1], m[2], m[3])
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|
|
|