try: from snakeoil.bash import read_bash_dict except ImportError: from snakeoil.fileutils import read_bash_dict from snakeoil.lists import stable_unique from snakeoil.currying import partial, post_curry from os.path import basename, dirname import re, operator x11_sub = post_curry(partial(re.compile("X11R6/+lib").sub, "lib"), 1) local_sub = post_curry(partial(re.compile("local/+lib").sub, "lib"), 1) pkgconfig1_sub = post_curry(partial( re.compile("usr/+lib[^/]*/+pkgconfig/+\.\./\.\.").sub, "usr"), 1) pkgconfig2_sub = post_curry(partial( re.compile("usr/+lib[^/]*/+pkgconfig/+\.\.").sub, "usr"), 1) flags_match = re.compile("-(?:mt|mthreads|kthread|Kthread|pthread" "|pthreads|-thread-safe|threads)").match class UnknownData(Exception): def __init__(self, line, token): self.token, self.line = token, line def __str__(self): return ("we don't know what to do with token %r in line dependency_libs " "line %r" % (self.token, self.line)) def rewrite_lafile(handle): data = read_bash_dict(handle) raw_dep_libs = data.get("dependency_libs", False) if not raw_dep_libs: return False, None original_libs = raw_dep_libs.split() rpaths, libs, libladirs, inherited_flags = [], [], [], [] original_inherited_flags = data.get("inherited_linker_flags", []) for item in stable_unique(original_libs): if item.startswith("-l"): libs.append(item) elif item.endswith(".la"): base = basename(item) if base.startswith("lib"): # convert to -l; punt .la, and 'lib' prefix libs.append("-l" + base[3:-3]) libladirs.append("-L" + dirname(item)) else: libs.append(item) elif item.startswith("-L"): # this is heinous, but is what the script did. entry = x11_sub(entry) entry = local_sub(entry) entry = pkgconfig1_sub(entry) entry = pkgconfig2_sub(entry) libladirs.append(entry) elif item.startswith("-R"): rpaths.append(item) elif flags_match(item): if inherited_flags: inherited_flags.append(item) else: libs.append(item) else: raise UnknownData(raw_dep_libs, item) libs = stable_unique(rpaths + libladirs + libs) inherited_flags = stable_unique(inherited_flags) if libs == original_libs and inherited_flags == original_inherited_flags: return False, None # must be prefixed with a space data["dependency_libs"] = ' ' + (' '.join(libs)) if inherited_flags: # must be prefixed with a space data["inherited_flags"] = ' ' + (' '.join(inherited_flags)) return True, "\n".join("%s='%s'" % (k, v) for k,v in sorted(data.iteritems())) def fix_path(location): from pkgcore.fs import livefs, fs for obj in livefs.iter_scan(location): if not fs.isreg(obj) or not obj.basename.endswith(".la"): continue updated, content = rewrite_lafile(open(obj.location, 'r')) if updated: open(obj.location, 'w').write(content)