X-Git-Url: https://git.bzium.org/embe/man-symlinker.git/blobdiff_plain/c010ee2581fa29514389af2201ec82f11f6ae2b3..refs/heads/main:/man-symlinker diff --git a/man-symlinker b/man-symlinker index bb52842..d2d8e2f 100755 --- a/man-symlinker +++ b/man-symlinker @@ -1,4 +1,4 @@ -#!/usr/bin/env python2.7 +#!/usr/bin/env python # encoding: UTF-8 from __future__ import absolute_import, division @@ -11,8 +11,15 @@ MAN_NAME_RE = re.compile(r'/man/(?:[^/]+/)?man([^/]+)/[^/]+.\1$') MAX_LEN = 1024 SO_RE = re.compile(r'^\.so\s+(.*)$') -def process_file(name): - m = MAN_NAME_RE.search(name) +SUFFIXES = { + 'xz': '.xz', + 'bzip2': '.bz2', + 'gzip': '.gz', +} + +def process_file(name, suffix): + full_name = os.path.abspath(name) + m = MAN_NAME_RE.search(full_name) if not m: return False category = m.group(1) @@ -21,10 +28,10 @@ def process_file(name): s = f.read(MAX_LEN+1) if len(s) > MAX_LEN: return False - s = s.strip() - if '\n' in s: - return False - m = SO_RE.search(s) + lines = [line for line in s.rstrip().split('\n') if not line.startswith('.\\"')] + if len(lines) != 1: + return False + m = SO_RE.search(lines[0]) if not m: return False link = m.group(1) @@ -32,11 +39,11 @@ def process_file(name): return False if link.startswith(dir_name+'/'): link = link[len(dir_name)+1:] - else: + elif '/' in link: link = '../' + link os.unlink(name) - os.symlink(link, name) - print 'Converted {} into a symlink to {}'.format(name, link) + os.symlink(link + suffix, name + suffix) + print 'Converted {} into a symlink to {}'.format(full_name, link) return True def main(args=[]): @@ -48,9 +55,13 @@ def main(args=[]): else: compressor = args[:dashes] all_files = args[dashes+1:] + if compressor: + suffix = SUFFIXES.get(compressor[0], '') + else: + suffix = '' files = [] for name in all_files: - if not process_file(name): + if not process_file(name, suffix): files.append(name) if not compressor or not files: return 0