Manpage generation now works with python3

This commit is contained in:
Adam 2012-06-05 13:05:53 +02:00 committed by ridiculousfish
parent b187538339
commit 85f808130d
2 changed files with 34 additions and 27 deletions

View file

@ -1,4 +1,4 @@
#!/usr/bin/python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Run me like this: ./create_manpage_completions.py /usr/share/man/man1/* > man_completions.fish # Run me like this: ./create_manpage_completions.py /usr/share/man/man1/* > man_completions.fish
@ -575,7 +575,7 @@ class TypeDeroffManParser(ManParser):
lines.pop(0) lines.pop(0)
# Look for BUGS and stop there # Look for BUGS and stop there
for idx in xrange(len(lines)): for idx in range(len(lines)):
line = lines[idx] line = lines[idx]
if line.startswith('BUGS'): if line.startswith('BUGS'):
# Drop remaining elements # Drop remaining elements
@ -630,8 +630,8 @@ def file_missing_or_overwritable(path):
file.close() file.close()
return result return result
except IOError as (err, strerror): except IOError as err:
if err == errno.ENOENT: if err == 2:
# File does not exist, full steam ahead # File does not exist, full steam ahead
return True return True
else: else:
@ -661,8 +661,12 @@ def parse_manpage_at_path(manpage_path, output_directory):
fd = gzip.open(manpage_path, 'r') fd = gzip.open(manpage_path, 'r')
else: else:
fd = open(manpage_path, 'r') fd = open(manpage_path, 'r')
try:
manpage = fd.read() manpage = fd.read()
except UnicodeDecodeError:
return
fd.close() fd.close()
manpage = str(manpage)
# Get the "base" command, e.g. gcc.1.gz -> gcc # Get the "base" command, e.g. gcc.1.gz -> gcc
cmd_base = CMDNAME.split('.', 1)[0] cmd_base = CMDNAME.split('.', 1)[0]
@ -708,8 +712,8 @@ def parse_manpage_at_path(manpage_path, output_directory):
else: else:
add_diagnostic("Not overwriting the file at '%s'" % fullpath) add_diagnostic("Not overwriting the file at '%s'" % fullpath)
except IOError as (errno, strerror): except IOError as err:
add_diagnostic("Unable to open file '%s': error(%d): %s" % (fullpath, errno, strerror)) add_diagnostic("Unable to open file '%s': error(%d): %s" % (fullpath, err.errno, err.strerror))
return False return False
built_command_output.insert(0, "# %s: %s" % (CMDNAME, parser.name())) built_command_output.insert(0, "# %s: %s" % (CMDNAME, parser.name()))
@ -729,28 +733,24 @@ def parse_manpage_at_path(manpage_path, output_directory):
add_diagnostic('%s contains no options or is unparsable (tried parser %s)' % (manpage_path, parser_names), BRIEF_VERBOSE) add_diagnostic('%s contains no options or is unparsable (tried parser %s)' % (manpage_path, parser_names), BRIEF_VERBOSE)
return success return success
def compare_paths(a, b):
""" Compare two paths by their base name, case insensitive """
return cmp(os.path.basename(a).lower(), os.path.basename(b).lower())
def parse_and_output_man_pages(paths, output_directory, show_progress): def parse_and_output_man_pages(paths, output_directory, show_progress):
global diagnostic_indent global diagnostic_indent
paths.sort(compare_paths) paths.sort()
total_count = len(paths) total_count = len(paths)
successful_count, index = 0, 0 successful_count, index = 0, 0
padding_len = len(str(total_count)) padding_len = len(str(total_count))
last_progress_string_length = 0 last_progress_string_length = 0
if show_progress and not WRITE_TO_STDOUT: if show_progress and not WRITE_TO_STDOUT:
print "Parsing man pages and writing completions to", output_directory print("Parsing man pages and writing completions to {0}".format(output_directory))
for manpage_path in paths: for manpage_path in paths:
index += 1 index += 1
if show_progress: if show_progress:
filename = os.path.basename(manpage_path).split('.', 1)[0] filename = os.path.basename(manpage_path).split('.', 1)[0]
progress_str = ' %s / %d : %s' % (str(index).rjust(padding_len), total_count, filename) progress_str = ' {0} / {1} : {2}'.format((str(index).rjust(padding_len)), total_count, filename)
# Pad on the right with spaces so we overwrite whatever we wrote last time # Pad on the right with spaces so we overwrite whatever we wrote last time
padded_progress_str = progress_str.ljust(last_progress_string_length) padded_progress_str = progress_str.ljust(last_progress_string_length)
last_progress_string_length = len(progress_str) last_progress_string_length = len(progress_str)
print padded_progress_str, chr(27) + '[A' sys.stdout.write("\r\r{0} {1}".format(padded_progress_str, chr(27)))
try: try:
if parse_manpage_at_path(manpage_path, output_directory): if parse_manpage_at_path(manpage_path, output_directory):
successful_count += 1 successful_count += 1
@ -764,6 +764,7 @@ def parse_and_output_man_pages(paths, output_directory, show_progress):
flush_diagnostics(sys.stderr) flush_diagnostics(sys.stderr)
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
flush_diagnostics(sys.stderr) flush_diagnostics(sys.stderr)
print("") #Newline after loop
add_diagnostic("Successfully parsed %d / %d pages" % (successful_count, total_count), BRIEF_VERBOSE) add_diagnostic("Successfully parsed %d / %d pages" % (successful_count, total_count), BRIEF_VERBOSE)
flush_diagnostics(sys.stderr) flush_diagnostics(sys.stderr)
@ -772,16 +773,16 @@ def get_paths_from_manpath():
import subprocess, os import subprocess, os
proc = subprocess.Popen(['man', '--path'], stdout=subprocess.PIPE) proc = subprocess.Popen(['man', '--path'], stdout=subprocess.PIPE)
manpath, err_data = proc.communicate() manpath, err_data = proc.communicate()
parent_paths = manpath.strip().split(':') parent_paths = manpath.decode().strip().split(':')
if not parent_paths: if not parent_paths:
print >> sys.stderr, "Unable to get the manpath (tried man --path)" sys.stderr.write("Unable to get the manpath (tried man --path)\n")
sys.exit(-1) sys.exit(-1)
result = [] result = []
for parent_path in parent_paths: for parent_path in parent_paths:
directory_path = os.path.join(parent_path, 'man1') directory_path = os.path.join(parent_path, 'man1')
try: try:
names = os.listdir(directory_path) names = os.listdir(directory_path)
except OSError, e: except OSError as e:
names = [] names = []
names.sort() names.sort()
for name in names: for name in names:
@ -791,22 +792,22 @@ def get_paths_from_manpath():
def usage(script_name): def usage(script_name):
print "Usage: %s [-v, --verbose] [-s, --stdout] [-d, --directory] [-p, --progress] files..." % script_name print("Usage: {0} [-v, --verbose] [-s, --stdout] [-d, --directory] [-p, --progress] files...".format(script_name))
print """Command options are: print("""Command options are:
-h, --help\t\tShow this help message -h, --help\t\tShow this help message
-v, --verbose\tShow debugging output to stderr -v, --verbose\tShow debugging output to stderr
-s, --stdout\tWrite all completions to stdout (trumps the --directory option) -s, --stdout\tWrite all completions to stdout (trumps the --directory option)
-d, --directory\tWrite all completions to the given directory, instead of to ~/.config/fish/completions -d, --directory\tWrite all completions to the given directory, instead of to ~/.config/fish/completions
-m, --manpath\tProcess all man1 files available in the manpath (as determined by man --path) -m, --manpath\tProcess all man1 files available in the manpath (as determined by man --path)
-p, --progress\tShow progress -p, --progress\tShow progress
""" """)
if __name__ == "__main__": if __name__ == "__main__":
script_name = sys.argv[0] script_name = sys.argv[0]
try: try:
opts, file_paths = getopt.gnu_getopt(sys.argv[1:], 'vsd:hmp', ['verbose', 'stdout', 'directory=', 'help', 'manpath', 'progress']) opts, file_paths = getopt.gnu_getopt(sys.argv[1:], 'vsd:hmp', ['verbose', 'stdout', 'directory=', 'help', 'manpath', 'progress'])
except getopt.GetoptError, err: except getopt.GetoptError as err:
print str(err) # will print something like "option -a not recognized" print(err.strerror) # will print something like "option -a not recognized"
usage(script_name) usage(script_name)
sys.exit(2) sys.exit(2)
@ -834,7 +835,7 @@ if __name__ == "__main__":
file_paths.extend(get_paths_from_manpath()) file_paths.extend(get_paths_from_manpath())
if not file_paths: if not file_paths:
print "No paths specified" print("No paths specified")
sys.exit(0) sys.exit(0)
if not WRITE_TO_STDOUT and not output_directory: if not WRITE_TO_STDOUT and not output_directory:
@ -843,7 +844,7 @@ if __name__ == "__main__":
output_directory = os.path.expanduser('~/.config/fish/completions/') output_directory = os.path.expanduser('~/.config/fish/completions/')
try: try:
os.makedirs(output_directory) os.makedirs(output_directory)
except OSError, e: except OSError as e:
if e.errno != errno.EEXIST: if e.errno != errno.EEXIST:
raise raise

View file

@ -782,7 +782,10 @@ class Deroffer:
self.tr_to += ns self.tr_to += ns
# Update our table, then swap in the slower tr-savvy condputs # Update our table, then swap in the slower tr-savvy condputs
try: #Python2
self.tr = string.maketrans(self.tr_from, self.tr_to) self.tr = string.maketrans(self.tr_from, self.tr_to)
except AttributeError: #Python3
self.tr = "".maketrans(self.tr_from, self.tr_to)
self.condputs = self.condputs_tr self.condputs = self.condputs_tr
return True return True
@ -961,7 +964,10 @@ class Deroffer:
self.tr_to += ns self.tr_to += ns
# Update our table, then swap in the slower tr-savvy condputs # Update our table, then swap in the slower tr-savvy condputs
try: #Python2
self.tr = string.maketrans(self.tr_from, self.tr_to) self.tr = string.maketrans(self.tr_from, self.tr_to)
except AttributeError: #Python3
self.tr = "".maketrans(self.tr_from, self.tr_to)
self.condputs = self.condputs_tr self.condputs = self.condputs_tr
return True return True