-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathman.py
93 lines (74 loc) · 2.92 KB
/
man.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
"""
Ghetto man replacement for GnuWin32.
"""
import os
import sys
import string
from subprocess import call, Popen, PIPE
from optparse import OptionParser
__author__ = 'setrofim'
__version__ = '0.1'
tp = Popen('uname', stdout=PIPE, stderr=PIPE, shell=True)
out, err = tp.communicate()
if out.startswith('MINGW32'):
os.sep = '/'
GNUWIN32 = os.getenv('GNUWIN32') or r'C:\Apps\GnuWin32'
DEFAULT_MAN_PAGES_PATH = os.path.join(GNUWIN32, 'man')
FIND = os.path.join(GNUWIN32, 'bin', 'find.exe')
MAN_PAGES_PATH = os.getenv('MAN_PAGES_PATH') or DEFAULT_MAN_PAGES_PATH
PAGER = os.getenv('PAGER') or os.path.join(GNUWIN32, 'bin', 'less_.exe')
def check_environment():
for path in [GNUWIN32, DEFAULT_MAN_PAGES_PATH, FIND]:
if not os.path.exists(path):
raise Exception("Can't find {0}; is GNUWIN32 set correctly?".format(path))
def error(message):
sys.stderr.write('ERROR: {0}\n'.format(message))
parser.print_help()
sys.exit(1)
def process_arguments(parser):
opts, args = parser.parse_args()
if len(args) == 2:
category = args[0]
if not(len(category) <= 2 and category[0] in string.digits):
raise Exception('Invalid category: {0}'.format(category))
item = args[1]
elif len(args) == 1:
category = '*'
item = args[0]
else:
raise Exception('Incorrect number of arguments.')
return category, item, opts
if __name__ == '__main__':
parser = OptionParser(usage='Usage: man.py [options] [CATEGORY] PROGRAM')
parser.add_option('-a', dest='all', action='store_true',
help='Display all matching entries (by default, only the first one).')
parser.add_option('-i', dest='nocase', action='store_true',
help='Case insensitive search.')
try:
check_environment()
category, item, opts = process_arguments(parser)
except Exception, e:
error(str(e))
namepred = opts.nocase and 'iname' or 'name'
command = '{0} {4}{5}cat{1} -{2} {3}.{1}.txt'.format(FIND,
category,
namepred,
item,
MAN_PAGES_PATH,
os.sep)
p = Popen(command, stdout=PIPE, stderr=PIPE, shell=True)
out, err = p.communicate()
if err:
error(err)
paths = [ p.rstrip('\r\n') for p in out.split('\n') if p.rstrip('\r\n')]
if paths:
if opts.all:
args = [PAGER]
args.extend(paths)
call(args, shell=True)
else:
call([PAGER, paths[0]], shell=True)
else:
sys.stderr.write('Could not find {0}.\n'.format(item))
sys.exit(2)