]> git.bzium.org/ - embe/man-symlinker.git/commitdiff
Pierwsza wersja.
authorMichał Bartoszkiewicz <mbartoszkiewicz@gmail.com>
Wed, 5 Oct 2011 18:06:58 +0000 (20:06 +0200)
committerMichał Bartoszkiewicz <mbartoszkiewicz@gmail.com>
Wed, 5 Oct 2011 18:06:58 +0000 (20:06 +0200)
man-symlinker.py [new file with mode: 0755]

diff --git a/man-symlinker.py b/man-symlinker.py
new file mode 100755 (executable)
index 0000000..91a31ed
--- /dev/null
@@ -0,0 +1,61 @@
+#!/usr/bin/env python
+# encoding: UTF-8
+
+from __future__ import absolute_import, division
+
+import os
+import re
+import sys
+
+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)
+    if not m:
+        return False
+    category = m.group(1)
+    dir_name = 'man' + category
+    f = open(name, 'r')
+    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)
+    if not m:
+        return False
+    link = m.group(1)
+    if link.startswith('/') or link.count('/') > 1:
+        return False
+    if link.startswith(dir_name+'/'):
+        link = link[len(dir_name)+1:]
+    else:
+        link = '../' + link
+    os.unlink(name)
+    os.symlink(link, name)
+    print 'Converted {} into a symlink to {}'.format(name, link)
+    return True
+
+def main(args=[]):
+    try:
+        dashes = args.index('--')
+    except ValueError:
+        compressor = []
+        all_files = args
+    else:
+        compressor = args[:dashes]
+        all_files = args[dashes+1:]
+    files = []
+    for name in all_files:
+        if not process_file(name):
+            files.append(name)
+    if not compressor or not files:
+        return 0
+    os.execvp(compressor[0], compressor + files)
+    return 1
+
+if __name__ == '__main__':
+    sys.exit(main(sys.argv[1:]))