2012-06-05 19:05:53 +08:00
|
|
|
#!/usr/bin/env python
|
2012-04-08 16:43:30 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-01-29 14:25:45 +08:00
|
|
|
|
2012-04-08 16:43:30 +08:00
|
|
|
# Run me like this: ./create_manpage_completions.py /usr/share/man/man1/* > man_completions.fish
|
2012-04-05 03:43:12 +08:00
|
|
|
|
2012-01-29 15:32:40 +08:00
|
|
|
"""
|
2012-11-18 18:23:22 +08:00
|
|
|
<OWNER> = Siteshwar Vashisht
|
|
|
|
<YEAR> = 2012
|
2012-01-29 15:32:40 +08:00
|
|
|
|
2012-11-18 18:23:22 +08:00
|
|
|
Copyright (c) 2012, Siteshwar Vashisht
|
2012-01-29 15:32:40 +08:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
"""
|
|
|
|
|
2012-12-02 22:31:44 +08:00
|
|
|
import string, sys, re, os.path, gzip, traceback, getopt, errno, codecs
|
2012-04-08 16:43:30 +08:00
|
|
|
from deroff import Deroffer
|
2012-03-16 18:03:43 +08:00
|
|
|
|
2012-06-19 04:59:07 +08:00
|
|
|
# Whether we're Python 3
|
|
|
|
IS_PY3 = sys.version_info[0] >= 3
|
|
|
|
|
2012-01-29 14:25:45 +08:00
|
|
|
# This gets set to the name of the command that we are currently executing
|
|
|
|
CMDNAME = ""
|
|
|
|
|
2012-06-19 04:59:07 +08:00
|
|
|
# Information used to track which of our parsers were successful
|
|
|
|
PARSER_INFO = {}
|
|
|
|
|
2012-03-31 01:00:01 +08:00
|
|
|
# builtcommand writes into this global variable, yuck
|
|
|
|
built_command_output = []
|
2012-01-29 14:25:45 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
# Diagnostic output
|
|
|
|
diagnostic_output = []
|
|
|
|
diagnostic_indent = 0
|
|
|
|
|
2012-04-12 10:05:43 +08:00
|
|
|
# Three diagnostic verbosity levels
|
|
|
|
VERY_VERBOSE, BRIEF_VERBOSE, NOT_VERBOSE = 2, 1, 0
|
2012-04-05 03:43:12 +08:00
|
|
|
|
2012-04-12 10:05:43 +08:00
|
|
|
# Pick some reasonable default values for settings
|
2012-06-19 04:59:07 +08:00
|
|
|
global VERBOSITY, WRITE_TO_STDOUT, DEROFF_ONLY
|
|
|
|
VERBOSITY, WRITE_TO_STDOUT, DEROFF_ONLY = NOT_VERBOSE, False, False
|
2012-04-12 10:05:43 +08:00
|
|
|
|
|
|
|
def add_diagnostic(dgn, msg_verbosity = VERY_VERBOSE):
|
|
|
|
# Add a diagnostic message, if msg_verbosity <= VERBOSITY
|
|
|
|
if msg_verbosity <= VERBOSITY:
|
2012-04-05 03:43:12 +08:00
|
|
|
diagnostic_output.append(' '*diagnostic_indent + dgn)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
def flush_diagnostics(where):
|
|
|
|
if diagnostic_output:
|
|
|
|
output_str = '\n'.join(diagnostic_output) + '\n'
|
|
|
|
where.write(output_str)
|
|
|
|
diagnostic_output[:] = []
|
|
|
|
|
|
|
|
# Make sure we don't output the same completion multiple times, which can happen
|
|
|
|
# For example, xsubpp.1.gz and xsubpp5.10.1.gz
|
|
|
|
# This maps commands to lists of completions
|
|
|
|
already_output_completions = {}
|
|
|
|
|
2012-01-29 14:25:45 +08:00
|
|
|
def compileAndSearch(regex, input):
|
2012-04-04 09:38:25 +08:00
|
|
|
options_section_regex = re.compile(regex , re.DOTALL)
|
|
|
|
options_section_matched = re.search( options_section_regex, input)
|
|
|
|
return options_section_matched
|
2012-01-29 14:25:45 +08:00
|
|
|
|
|
|
|
def unquoteDoubleQuotes(data):
|
2012-04-04 09:38:25 +08:00
|
|
|
if (len(data) < 2):
|
|
|
|
return data
|
|
|
|
if data[0] == '"' and data[len(data)-1] == '"':
|
|
|
|
data = data[1:len(data)-1]
|
|
|
|
return data
|
2012-01-29 14:25:45 +08:00
|
|
|
|
|
|
|
def unquoteSingleQuotes(data):
|
2012-04-04 09:38:25 +08:00
|
|
|
if (len(data) < 2):
|
|
|
|
return data
|
|
|
|
if data[0] == '`' and data[len(data)-1] == '\'':
|
|
|
|
data = data[1:len(data)-1]
|
|
|
|
return data
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
|
|
|
|
# Make a string of characters that are deemed safe in fish without needing to be escaped
|
|
|
|
# Note that space is not included
|
2012-04-09 14:26:26 +08:00
|
|
|
g_fish_safe_chars = frozenset(string.ascii_letters + string.digits + '_+-|/:=@~')
|
2012-04-04 09:38:25 +08:00
|
|
|
|
|
|
|
def fish_escape_single_quote(str):
|
|
|
|
# Escape a string if necessary so that it can be put in single quotes
|
|
|
|
# If it has no non-safe chars, there's nothing to do
|
|
|
|
if g_fish_safe_chars.issuperset(str):
|
|
|
|
return str
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
str = str.replace('\\', '\\\\') # Replace one backslash with two
|
|
|
|
str = str.replace("'", "\\'") # Replace one single quote with a backslash-single-quote
|
|
|
|
return "'" + str + "'"
|
2012-01-29 14:25:45 +08:00
|
|
|
|
2012-03-31 01:00:01 +08:00
|
|
|
def output_complete_command(cmdname, args, description, output_list):
|
2012-04-04 09:38:25 +08:00
|
|
|
comps = ['complete -c', cmdname]
|
|
|
|
comps.extend(args)
|
|
|
|
if description:
|
|
|
|
comps.append('--description')
|
|
|
|
comps.append(description)
|
|
|
|
output_list.append(' '.join(comps))
|
2012-01-29 14:25:45 +08:00
|
|
|
|
|
|
|
def builtcommand(options, description):
|
2012-04-04 09:38:25 +08:00
|
|
|
# print "Options are: ", options
|
|
|
|
man_optionlist = re.split(" |,|\"|=|[|]", options)
|
|
|
|
fish_options = []
|
|
|
|
for option in man_optionlist:
|
2012-04-09 14:26:26 +08:00
|
|
|
option = option.strip()
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-09 14:26:26 +08:00
|
|
|
# Skip some problematic cases
|
|
|
|
if option in ['-', '--']: continue
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
if option.startswith('--'):
|
|
|
|
# New style long option (--recursive)
|
|
|
|
fish_options.append('-l ' + fish_escape_single_quote(option[2:]))
|
|
|
|
elif option.startswith('-') and len(option) == 2:
|
|
|
|
# New style short option (-r)
|
|
|
|
fish_options.append('-s ' + fish_escape_single_quote(option[1:]))
|
|
|
|
elif option.startswith('-') and len(option) > 2:
|
|
|
|
# Old style long option (-recursive)
|
|
|
|
fish_options.append('-o ' + fish_escape_single_quote(option[1:]))
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
# Determine which options are new (not already in existing_options)
|
|
|
|
# Then add those to the existing options
|
|
|
|
existing_options = already_output_completions.setdefault(CMDNAME, set())
|
|
|
|
fish_options = [opt for opt in fish_options if opt not in existing_options]
|
|
|
|
existing_options.update(fish_options)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
# Maybe it's all for naught
|
|
|
|
if not fish_options: return
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-23 12:32:19 +08:00
|
|
|
# Here's what we'll use to truncate if necessary
|
|
|
|
max_description_width = 63
|
2013-01-09 06:57:48 +08:00
|
|
|
if IS_PY3:
|
|
|
|
truncation_suffix = '… [See Man Page]'
|
|
|
|
else:
|
|
|
|
truncation_suffix = u'… [See Man Page]'
|
2012-11-23 12:32:19 +08:00
|
|
|
|
|
|
|
# Try to include as many whole sentences as will fit
|
|
|
|
sentences = description.split('.')
|
|
|
|
truncated_description = sentences[0] + '.'
|
|
|
|
for line in sentences[1:]:
|
|
|
|
if not line: continue
|
|
|
|
proposed_description = truncated_description + ' ' + line + '.'
|
|
|
|
if len(proposed_description) <= max_description_width:
|
|
|
|
# It fits
|
|
|
|
truncated_description = proposed_description
|
|
|
|
else:
|
|
|
|
# No fit
|
|
|
|
break
|
|
|
|
|
|
|
|
# If the first sentence does not fit, truncate if necessary
|
|
|
|
if len(truncated_description) > max_description_width:
|
|
|
|
prefix_len = max_description_width - len(truncation_suffix)
|
|
|
|
truncated_description = truncated_description[:prefix_len] + truncation_suffix
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
# Escape some more things
|
2012-11-23 12:32:19 +08:00
|
|
|
truncated_description = fish_escape_single_quote(truncated_description)
|
2012-04-04 09:38:25 +08:00
|
|
|
escaped_cmd = fish_escape_single_quote(CMDNAME)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-23 12:32:19 +08:00
|
|
|
output_complete_command(escaped_cmd, fish_options, truncated_description, built_command_output)
|
2012-04-04 09:38:25 +08:00
|
|
|
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-01-29 14:25:45 +08:00
|
|
|
|
|
|
|
def removeGroffFormatting(data):
|
2012-04-04 09:38:25 +08:00
|
|
|
# data = data.replace("\fI","")
|
|
|
|
# data = data.replace("\fP","")
|
|
|
|
data = data.replace("\\fI","")
|
|
|
|
data = data.replace("\\fP","")
|
|
|
|
data = data.replace("\\f1","")
|
|
|
|
data = data.replace("\\fB","")
|
|
|
|
data = data.replace("\\fR","")
|
|
|
|
data = data.replace("\\e","")
|
|
|
|
data = re.sub(".PD( \d+)","",data)
|
|
|
|
data = data.replace(".BI","")
|
|
|
|
data = data.replace(".BR","")
|
|
|
|
data = data.replace("0.5i","")
|
|
|
|
data = data.replace(".rb","")
|
|
|
|
data = data.replace("\\^","")
|
|
|
|
data = data.replace("{ ","")
|
|
|
|
data = data.replace(" }","")
|
|
|
|
data = data.replace("\ ","")
|
|
|
|
data = data.replace("\-","-")
|
|
|
|
data = data.replace("\&","")
|
|
|
|
data = data.replace(".B","")
|
|
|
|
data = data.replace("\-","-")
|
|
|
|
data = data.replace(".I","")
|
|
|
|
data = data.replace("\f","")
|
|
|
|
return data
|
2012-01-29 14:25:45 +08:00
|
|
|
|
|
|
|
class ManParser:
|
2012-04-04 09:38:25 +08:00
|
|
|
def isMyType(self, manpage):
|
|
|
|
return False
|
2012-01-29 14:25:45 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
def parseManPage(self, manpage):
|
|
|
|
return False
|
2012-01-29 14:25:45 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
def name(self):
|
|
|
|
return "no-name"
|
2012-01-29 14:25:45 +08:00
|
|
|
|
|
|
|
class Type1ManParser(ManParser):
|
2012-04-04 09:38:25 +08:00
|
|
|
def isMyType(self, manpage):
|
|
|
|
# print manpage
|
|
|
|
options_section_matched = compileAndSearch("\.SH \"OPTIONS\"(.*?)", manpage)
|
|
|
|
|
|
|
|
if options_section_matched == None:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def parseManPage(self, manpage):
|
|
|
|
options_section_regex = re.compile( "\.SH \"OPTIONS\"(.*?)(\.SH|\Z)", re.DOTALL)
|
|
|
|
options_section_matched = re.search( options_section_regex, manpage)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
options_section = options_section_matched.group(0)
|
|
|
|
# print options_section
|
|
|
|
options_parts_regex = re.compile("\.PP(.*?)\.RE", re.DOTALL)
|
|
|
|
options_matched = re.search(options_parts_regex, options_section)
|
|
|
|
# print options_matched
|
|
|
|
add_diagnostic('Command is ' + CMDNAME)
|
|
|
|
|
|
|
|
if options_matched == None:
|
|
|
|
add_diagnostic('Unable to find options')
|
|
|
|
if( self.fallback(options_section) ):
|
|
|
|
return True
|
|
|
|
elif (self.fallback2(options_section) ):
|
|
|
|
return True
|
|
|
|
return False
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
while (options_matched != None):
|
|
|
|
# print len(options_matched.groups())
|
|
|
|
# print options_matched.group()
|
|
|
|
data = options_matched.group(1)
|
|
|
|
last_dotpp_index = data.rfind(".PP")
|
|
|
|
if (last_dotpp_index != -1):
|
|
|
|
data = data[last_dotpp_index+3:]
|
|
|
|
|
|
|
|
data = removeGroffFormatting(data)
|
|
|
|
data = data.split(".RS 4")
|
|
|
|
# print data
|
|
|
|
if (len (data) > 1): #and len(data[1]) <= 300):
|
|
|
|
optionName = data[0].strip()
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
if ( optionName.find("-") == -1):
|
|
|
|
add_diagnostic(optionName + " doesn't contain - ")
|
|
|
|
# return False
|
|
|
|
else:
|
|
|
|
optionName = unquoteDoubleQuotes(optionName)
|
|
|
|
optionName = unquoteSingleQuotes(optionName)
|
|
|
|
optionDescription = data[1].strip().replace("\n"," ")
|
|
|
|
# print >> sys.stderr, "Option: ", optionName," Description: ", optionDescription , '\n'
|
|
|
|
builtcommand(optionName, optionDescription)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
else:
|
|
|
|
add_diagnostic('Unable to split option from description')
|
|
|
|
return False
|
|
|
|
|
|
|
|
options_section = options_section[options_matched.end()-3:]
|
|
|
|
options_matched = re.search(options_parts_regex, options_section)
|
|
|
|
|
|
|
|
def fallback(self, options_section):
|
|
|
|
add_diagnostic('Falling Back')
|
|
|
|
options_parts_regex = re.compile("\.TP( \d+)?(.*?)\.TP", re.DOTALL)
|
|
|
|
options_matched = re.search(options_parts_regex, options_section)
|
|
|
|
if options_matched == None:
|
|
|
|
add_diagnostic('Still not found')
|
|
|
|
return False
|
|
|
|
while options_matched != None:
|
|
|
|
data = options_matched.group(2)
|
|
|
|
data = removeGroffFormatting(data)
|
|
|
|
data = data.strip()
|
|
|
|
data = data.split("\n",1)
|
|
|
|
if (len(data)>1 and len(data[1].strip())>0): # and len(data[1])<400):
|
|
|
|
optionName = data[0].strip()
|
|
|
|
if ( optionName.find("-") == -1):
|
|
|
|
add_diagnostic(optionName + "doesn't contains -")
|
|
|
|
else:
|
|
|
|
optionName = unquoteDoubleQuotes(optionName)
|
|
|
|
optionName = unquoteSingleQuotes(optionName)
|
|
|
|
optionDescription = data[1].strip().replace("\n"," ")
|
|
|
|
# print "Option: ", optionName," Description: ", optionDescription , '\n'
|
|
|
|
builtcommand(optionName, optionDescription)
|
|
|
|
else:
|
|
|
|
add_diagnostic('Unable to split option from description')
|
|
|
|
return False
|
|
|
|
|
|
|
|
options_section = options_section[options_matched.end()-3:]
|
|
|
|
options_matched = re.search(options_parts_regex, options_section)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def fallback2(self, options_section):
|
|
|
|
add_diagnostic('Falling Back2')
|
|
|
|
ix_remover_regex = re.compile("\.IX.*")
|
|
|
|
trailing_num_regex = re.compile('\\d+$')
|
|
|
|
options_parts_regex = re.compile("\.IP (.*?)\.IP", re.DOTALL)
|
|
|
|
|
|
|
|
options_section = re.sub(ix_remover_regex, "", options_section)
|
|
|
|
options_matched = re.search(options_parts_regex, options_section)
|
|
|
|
if options_matched == None:
|
|
|
|
add_diagnostic('Still not found2')
|
|
|
|
return False
|
|
|
|
while options_matched != None:
|
|
|
|
data = options_matched.group(1)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
# print "Data is : ", data
|
|
|
|
data = removeGroffFormatting(data)
|
|
|
|
data = data.strip()
|
|
|
|
data = data.split("\n",1)
|
|
|
|
if (len(data)>1 and len(data[1].strip())>0): # and len(data[1])<400):
|
|
|
|
# print "Data[0] is: ", data[0]
|
|
|
|
|
|
|
|
# data = re.sub(trailing_num_regex, "", data)
|
|
|
|
optionName = re.sub(trailing_num_regex, "", data[0].strip())
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
if ('-' not in optionName):
|
|
|
|
add_diagnostic(optionName + " doesn't contain -")
|
|
|
|
else:
|
|
|
|
optionName = optionName.strip()
|
|
|
|
optionName = unquoteDoubleQuotes(optionName)
|
|
|
|
optionName = unquoteSingleQuotes(optionName)
|
|
|
|
optionDescription = data[1].strip().replace("\n"," ")
|
|
|
|
# print "Option: ", optionName," Description: ", optionDescription , '\n'
|
|
|
|
builtcommand(optionName, optionDescription)
|
|
|
|
else:
|
|
|
|
# print data
|
|
|
|
add_diagnostic('Unable to split option from description')
|
|
|
|
return False
|
|
|
|
|
|
|
|
options_section = options_section[options_matched.end()-3:]
|
|
|
|
options_matched = re.search(options_parts_regex, options_section)
|
|
|
|
return True
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
def name(self):
|
|
|
|
return "Type1"
|
2012-01-29 14:25:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Type2ManParser(ManParser):
|
2012-04-04 09:38:25 +08:00
|
|
|
def isMyType(self, manpage):
|
|
|
|
options_section_matched = compileAndSearch("\.SH OPTIONS(.*?)", manpage)
|
|
|
|
|
|
|
|
if options_section_matched == None:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def parseManPage(self, manpage):
|
|
|
|
options_section_regex = re.compile( "\.SH OPTIONS(.*?)(\.SH|\Z)", re.DOTALL)
|
|
|
|
options_section_matched = re.search( options_section_regex, manpage)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
# if (options_section_matched == None):
|
|
|
|
# print "Falling Back"
|
|
|
|
# options_section_regex = re.compile( "\.SH OPTIONS(.*?)$", re.DOTALL)
|
|
|
|
# options_section_matched = re.search( options_section_regex, manpage)
|
|
|
|
# print manpage
|
|
|
|
options_section = options_section_matched.group(1)
|
|
|
|
# print options_section
|
|
|
|
# print options_section
|
|
|
|
# sys.exit(1)
|
|
|
|
|
|
|
|
# options_parts_regex = re.compile("\.TP(.*?)\.TP", re.DOTALL)
|
|
|
|
options_parts_regex = re.compile("\.[I|T]P( \d+(\.\d)?i?)?(.*?)\.[I|T]P", re.DOTALL)
|
|
|
|
# options_parts_regex = re.compile("\.TP(.*?)[(\.TP)|(\.SH)]", re.DOTALL)
|
|
|
|
options_matched = re.search(options_parts_regex, options_section)
|
|
|
|
add_diagnostic('Command is ' + CMDNAME)
|
|
|
|
|
|
|
|
if options_matched == None:
|
|
|
|
add_diagnostic(self.name() + ': Unable to find options')
|
|
|
|
return False
|
|
|
|
|
|
|
|
while (options_matched != None):
|
|
|
|
# print len(options_matched.groups())
|
|
|
|
data = options_matched.group(3)
|
|
|
|
|
|
|
|
data = removeGroffFormatting(data)
|
|
|
|
|
|
|
|
data = data.strip()
|
|
|
|
|
|
|
|
data = data.split("\n",1)
|
|
|
|
# print >> sys.stderr, data
|
|
|
|
if (len(data)>1 and len(data[1].strip())>0): # and len(data[1])<400):
|
|
|
|
optionName = data[0].strip()
|
|
|
|
if '-' not in optionName:
|
|
|
|
add_diagnostic(optionName + " doesn't contain -")
|
|
|
|
else:
|
|
|
|
optionName = unquoteDoubleQuotes(optionName)
|
|
|
|
optionName = unquoteSingleQuotes(optionName)
|
|
|
|
optionDescription = data[1].strip().replace("\n"," ")
|
|
|
|
# print "Option: ", optionName," Description: ", optionDescription , '\n'
|
|
|
|
builtcommand(optionName, optionDescription)
|
|
|
|
else:
|
|
|
|
# print >> sys.stderr, data
|
|
|
|
add_diagnostic('Unable to split option from description')
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
# return False
|
|
|
|
|
|
|
|
options_section = options_section[options_matched.end()-3:]
|
|
|
|
options_matched = re.search(options_parts_regex, options_section)
|
|
|
|
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
|
|
|
|
def name(self):
|
|
|
|
return "Type2"
|
2012-01-29 14:25:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Type3ManParser(ManParser):
|
2012-04-04 09:38:25 +08:00
|
|
|
def isMyType(self, manpage):
|
|
|
|
options_section_matched = compileAndSearch("\.SH DESCRIPTION(.*?)", manpage)
|
2012-01-29 14:25:45 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
if options_section_matched == None:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2012-01-29 14:25:45 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
def parseManPage(self, manpage):
|
|
|
|
options_section_regex = re.compile( "\.SH DESCRIPTION(.*?)(\.SH|\Z)", re.DOTALL)
|
|
|
|
options_section_matched = re.search( options_section_regex, manpage)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
options_section = options_section_matched.group(1)
|
|
|
|
# print options_section
|
|
|
|
# sys.exit(1)
|
|
|
|
options_parts_regex = re.compile("\.TP(.*?)\.TP", re.DOTALL)
|
|
|
|
options_matched = re.search(options_parts_regex, options_section)
|
|
|
|
add_diagnostic('Command is ' + CMDNAME)
|
2012-01-29 14:25:45 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
if options_matched == None:
|
|
|
|
add_diagnostic('Unable to find options section')
|
|
|
|
return False
|
2012-01-29 14:25:45 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
while (options_matched != None):
|
|
|
|
# print len(options_matched.groups())
|
|
|
|
data = options_matched.group(1)
|
2012-01-29 14:25:45 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
data = removeGroffFormatting(data)
|
|
|
|
data = data.strip()
|
|
|
|
data = data.split("\n",1)
|
2012-01-29 14:25:45 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
if (len(data)>1): # and len(data[1])<400):
|
|
|
|
optionName = data[0].strip()
|
|
|
|
if ( optionName.find("-") == -1):
|
|
|
|
add_diagnostic(optionName + "doesn't contain -")
|
|
|
|
else:
|
|
|
|
optionName = unquoteDoubleQuotes(optionName)
|
|
|
|
optionName = unquoteSingleQuotes(optionName)
|
|
|
|
optionDescription = data[1].strip().replace("\n"," ")
|
|
|
|
# print >> sys.stderr, "Option: ", optionName," Description: ", optionDescription , '\n'
|
|
|
|
builtcommand(optionName, optionDescription)
|
2012-01-29 14:25:45 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
else:
|
|
|
|
add_diagnostic('Unable to split option from description')
|
|
|
|
return False
|
2012-01-29 14:25:45 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
options_section = options_section[options_matched.end()-3:]
|
|
|
|
options_matched = re.search(options_parts_regex, options_section)
|
2012-01-29 14:25:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
def name(self):
|
|
|
|
return "Type3"
|
2012-01-29 14:25:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Type4ManParser(ManParser):
|
2012-04-04 09:38:25 +08:00
|
|
|
def isMyType(self, manpage):
|
|
|
|
options_section_matched = compileAndSearch("\.SH FUNCTION LETTERS(.*?)", manpage)
|
|
|
|
|
|
|
|
if options_section_matched == None:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def parseManPage(self, manpage):
|
|
|
|
options_section_regex = re.compile( "\.SH FUNCTION LETTERS(.*?)(\.SH|\Z)", re.DOTALL)
|
|
|
|
options_section_matched = re.search( options_section_regex, manpage)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
options_section = options_section_matched.group(1)
|
|
|
|
# print options_section
|
|
|
|
# sys.exit(1)
|
|
|
|
options_parts_regex = re.compile("\.TP(.*?)\.TP", re.DOTALL)
|
|
|
|
options_matched = re.search(options_parts_regex, options_section)
|
2012-06-07 23:18:54 +08:00
|
|
|
add_diagnostic('Command is ' + CMDNAME)
|
2012-04-04 09:38:25 +08:00
|
|
|
|
|
|
|
if options_matched == None:
|
|
|
|
print >> sys.stderr, "Unable to find options section"
|
|
|
|
return False
|
|
|
|
|
|
|
|
while (options_matched != None):
|
|
|
|
# print len(options_matched.groups())
|
|
|
|
data = options_matched.group(1)
|
|
|
|
|
|
|
|
data = removeGroffFormatting(data)
|
|
|
|
data = data.strip()
|
|
|
|
data = data.split("\n",1)
|
|
|
|
|
|
|
|
if (len(data)>1): # and len(data[1])<400):
|
|
|
|
optionName = data[0].strip()
|
|
|
|
if ( optionName.find("-") == -1):
|
2012-06-07 23:18:54 +08:00
|
|
|
add_diagnostic(optionName + " doesn't contain - ")
|
2012-04-04 09:38:25 +08:00
|
|
|
else:
|
|
|
|
optionName = unquoteDoubleQuotes(optionName)
|
|
|
|
optionName = unquoteSingleQuotes(optionName)
|
|
|
|
optionDescription = data[1].strip().replace("\n"," ")
|
|
|
|
# print "Option: ", optionName," Description: ", optionDescription , '\n'
|
|
|
|
builtcommand(optionName, optionDescription)
|
|
|
|
|
|
|
|
else:
|
|
|
|
add_diagnostic('Unable to split option from description')
|
|
|
|
return False
|
|
|
|
|
|
|
|
options_section = options_section[options_matched.end()-3:]
|
|
|
|
options_matched = re.search(options_parts_regex, options_section)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def name(self):
|
|
|
|
return "Type4"
|
2012-01-29 14:25:45 +08:00
|
|
|
|
2012-04-08 16:43:30 +08:00
|
|
|
class TypeDarwinManParser(ManParser):
|
2012-04-04 09:38:25 +08:00
|
|
|
def isMyType(self, manpage):
|
2012-04-05 03:43:12 +08:00
|
|
|
options_section_matched = compileAndSearch("\.S[hH] DESCRIPTION", manpage)
|
2012-04-04 09:38:25 +08:00
|
|
|
return options_section_matched != None
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
def trim_groff(self, line):
|
|
|
|
# Remove initial period
|
|
|
|
if line.startswith('.'):
|
|
|
|
line = line[1:]
|
|
|
|
# Skip leading groff crud
|
|
|
|
while re.match('[A-Z][a-z]\s', line):
|
|
|
|
line = line[3:]
|
2012-11-23 12:32:19 +08:00
|
|
|
|
|
|
|
# If the line ends with a space and then a period or comma, then erase the space
|
|
|
|
# This hack handles lines of the form '.Ar projectname .'
|
|
|
|
if line.endswith(' ,') or line.endswith(' .'):
|
|
|
|
line = line[:-2] + line[-1]
|
2012-04-04 09:38:25 +08:00
|
|
|
return line
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-23 12:32:19 +08:00
|
|
|
def count_argument_dashes(self, line):
|
|
|
|
# Determine how many dashes the line has using the following regex hack
|
|
|
|
# Look for the start of a line, followed by a dot, then a sequence of
|
|
|
|
# one or more dashes ('Fl')
|
|
|
|
result = 0
|
|
|
|
if line.startswith('.'):
|
|
|
|
line = line[4:]
|
|
|
|
while line.startswith('Fl '):
|
|
|
|
result = result + 1
|
|
|
|
line = line[3:]
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-18 09:22:57 +08:00
|
|
|
# Replace some groff escapes. There's a lot we don't bother to handle.
|
|
|
|
def groff_replace_escapes(self, line):
|
2012-11-23 12:32:19 +08:00
|
|
|
line = line.replace('.Nm', CMDNAME)
|
2012-10-18 09:22:57 +08:00
|
|
|
line = line.replace('\\ ', ' ')
|
|
|
|
line = line.replace('\& ', '')
|
2012-11-23 12:32:19 +08:00
|
|
|
line = line.replace(r'.\"', '')
|
2012-10-18 09:22:57 +08:00
|
|
|
return line
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
def is_option(self, line):
|
|
|
|
return line.startswith('.It Fl')
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
def parseManPage(self, manpage):
|
2012-04-05 03:43:12 +08:00
|
|
|
got_something = False
|
2012-04-04 09:38:25 +08:00
|
|
|
lines = manpage.splitlines()
|
|
|
|
# Discard lines until we get to ".sh Description"
|
2012-04-05 03:43:12 +08:00
|
|
|
while lines and not (lines[0].startswith('.Sh DESCRIPTION') or lines[0].startswith('.SH DESCRIPTION')):
|
2012-04-04 09:38:25 +08:00
|
|
|
lines.pop(0)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
while lines:
|
|
|
|
# Pop until we get to the next option
|
|
|
|
while lines and not self.is_option(lines[0]):
|
|
|
|
lines.pop(0)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
if not lines:
|
|
|
|
continue
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-10-18 09:22:57 +08:00
|
|
|
# Get the line and clean it up
|
|
|
|
line = lines.pop(0)
|
2012-11-23 12:32:19 +08:00
|
|
|
|
|
|
|
# Try to guess how many dashes this argument has
|
|
|
|
dash_count = self.count_argument_dashes(line)
|
|
|
|
|
2012-10-18 09:22:57 +08:00
|
|
|
line = self.groff_replace_escapes(line)
|
|
|
|
line = self.trim_groff(line)
|
|
|
|
line = line.strip()
|
|
|
|
if not line: continue
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
# Extract the name
|
2012-10-18 09:22:57 +08:00
|
|
|
name = line.split(None, 2)[0]
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
# Extract the description
|
2012-11-23 12:32:19 +08:00
|
|
|
desc_lines = []
|
2012-04-04 09:38:25 +08:00
|
|
|
while lines and not self.is_option(lines[0]):
|
2012-11-23 12:32:19 +08:00
|
|
|
line = lines.pop(0).strip()
|
|
|
|
if line.startswith('.'):
|
|
|
|
line = self.groff_replace_escapes(line)
|
|
|
|
line = self.trim_groff(line).strip()
|
|
|
|
if line:
|
|
|
|
desc_lines.append(line)
|
|
|
|
desc = ' '.join(desc_lines)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-12 09:26:26 +08:00
|
|
|
# print "name: ", name
|
2012-11-23 12:32:19 +08:00
|
|
|
# print "desc: ", desc
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
if name == '-':
|
|
|
|
# Skip double -- arguments
|
|
|
|
continue
|
|
|
|
elif len(name) > 1:
|
|
|
|
# Output the command
|
2012-11-23 12:32:19 +08:00
|
|
|
builtcommand(('-' * dash_count) + name, desc)
|
2012-04-05 03:43:12 +08:00
|
|
|
got_something = True
|
2012-04-04 09:38:25 +08:00
|
|
|
elif len(name) == 1:
|
|
|
|
builtcommand('-' + name, desc)
|
2012-04-05 03:43:12 +08:00
|
|
|
got_something = True
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-05 03:43:12 +08:00
|
|
|
return got_something
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
def name(self):
|
|
|
|
return "Darwin man parser"
|
2012-04-08 16:43:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TypeDeroffManParser(ManParser):
|
|
|
|
def isMyType(self, manpage):
|
|
|
|
return True # We're optimists
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-08 16:43:30 +08:00
|
|
|
def is_option(self, line):
|
|
|
|
return line.startswith('-')
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-08 16:43:30 +08:00
|
|
|
def could_be_description(self, line):
|
|
|
|
return len(line) > 0 and not line.startswith('-')
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-08 16:43:30 +08:00
|
|
|
def parseManPage(self, manpage):
|
|
|
|
d = Deroffer()
|
|
|
|
d.deroff(manpage)
|
|
|
|
output = d.get_output()
|
|
|
|
lines = output.split('\n')
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-08 16:43:30 +08:00
|
|
|
got_something = False
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-08 16:43:30 +08:00
|
|
|
# Discard lines until we get to DESCRIPTION or OPTIONS
|
|
|
|
while lines and not (lines[0].startswith('DESCRIPTION') or lines[0].startswith('OPTIONS') or lines[0].startswith('COMMAND OPTIONS')):
|
|
|
|
lines.pop(0)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-09 14:26:26 +08:00
|
|
|
# Look for BUGS and stop there
|
2012-06-05 19:05:53 +08:00
|
|
|
for idx in range(len(lines)):
|
2012-04-09 14:26:26 +08:00
|
|
|
line = lines[idx]
|
|
|
|
if line.startswith('BUGS'):
|
|
|
|
# Drop remaining elements
|
|
|
|
lines[idx:] = []
|
|
|
|
break
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-08 16:43:30 +08:00
|
|
|
while lines:
|
|
|
|
# Pop until we get to the next option
|
|
|
|
while lines and not self.is_option(lines[0]):
|
2012-04-09 14:26:26 +08:00
|
|
|
line = lines.pop(0)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-08 16:43:30 +08:00
|
|
|
if not lines:
|
|
|
|
continue
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-08 16:43:30 +08:00
|
|
|
options = lines.pop(0)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-08 16:43:30 +08:00
|
|
|
# Pop until we get to either an empty line or a line starting with -
|
|
|
|
description = ''
|
|
|
|
while lines and self.could_be_description(lines[0]):
|
|
|
|
if description: description += ' '
|
|
|
|
description += lines.pop(0)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-08 16:43:30 +08:00
|
|
|
builtcommand(options, description)
|
|
|
|
got_something = True
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-08 16:43:30 +08:00
|
|
|
return got_something
|
2012-11-18 18:23:22 +08:00
|
|
|
|
|
|
|
|
2012-04-08 16:43:30 +08:00
|
|
|
def name(self):
|
|
|
|
return "Deroffing man parser"
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-06-19 04:59:07 +08:00
|
|
|
# Return whether the file at the given path is overwritable
|
|
|
|
# Raises IOError if it cannot be opened
|
|
|
|
def file_is_overwritable(path):
|
|
|
|
result = False
|
|
|
|
file = open(path, 'r')
|
|
|
|
for line in file:
|
|
|
|
# Skip leading empty lines
|
|
|
|
line = line.strip()
|
|
|
|
if not line:
|
|
|
|
continue
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-06-19 04:59:07 +08:00
|
|
|
# We look in the initial run of lines that start with #
|
|
|
|
if not line.startswith('#'):
|
|
|
|
break
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-06-19 04:59:07 +08:00
|
|
|
# See if this contains the magic word
|
|
|
|
if 'Autogenerated' in line:
|
|
|
|
result = True
|
|
|
|
break
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-06-19 04:59:07 +08:00
|
|
|
file.close()
|
|
|
|
return result
|
|
|
|
|
2012-01-29 14:25:45 +08:00
|
|
|
|
2012-04-12 09:26:26 +08:00
|
|
|
# Return whether the file at the given path either does not exist, or exists but appears to be a file we output (and hence can overwrite)
|
|
|
|
def file_missing_or_overwritable(path):
|
|
|
|
try:
|
2012-06-19 04:59:07 +08:00
|
|
|
return file_is_overwritable(path)
|
2012-06-05 19:05:53 +08:00
|
|
|
except IOError as err:
|
2012-06-07 23:18:54 +08:00
|
|
|
if err.errno == 2:
|
2012-04-12 09:26:26 +08:00
|
|
|
# File does not exist, full steam ahead
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
# Something else happened
|
|
|
|
return False
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-06-19 04:59:07 +08:00
|
|
|
# Delete the file if it is autogenerated
|
|
|
|
def cleanup_autogenerated_file(path):
|
|
|
|
try:
|
|
|
|
if file_is_overwritable(path):
|
|
|
|
os.remove(path)
|
|
|
|
except (OSError, IOError):
|
|
|
|
pass
|
2012-04-12 09:26:26 +08:00
|
|
|
|
2012-06-19 04:59:07 +08:00
|
|
|
def parse_manpage_at_path(manpage_path, yield_to_dirs, output_directory):
|
2012-04-04 09:38:25 +08:00
|
|
|
filename = os.path.basename(manpage_path)
|
|
|
|
|
|
|
|
# Clear diagnostics
|
|
|
|
global diagnostic_indent
|
|
|
|
diagnostic_output[:] = []
|
|
|
|
diagnostic_indent = 0
|
|
|
|
|
|
|
|
# Set up some diagnostics
|
|
|
|
add_diagnostic('Considering ' + manpage_path)
|
|
|
|
diagnostic_indent += 1
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
if manpage_path.endswith('.gz'):
|
|
|
|
fd = gzip.open(manpage_path, 'r')
|
2012-06-05 19:22:29 +08:00
|
|
|
manpage = fd.read()
|
2012-06-19 04:59:07 +08:00
|
|
|
if IS_PY3: manpage = manpage.decode('latin-1')
|
|
|
|
else:
|
|
|
|
if IS_PY3:
|
|
|
|
fd = open(manpage_path, 'r', encoding='latin-1')
|
|
|
|
else:
|
|
|
|
fd = open(manpage_path, 'r')
|
2012-06-05 19:05:53 +08:00
|
|
|
manpage = fd.read()
|
2012-04-04 09:38:25 +08:00
|
|
|
fd.close()
|
2012-06-05 19:22:29 +08:00
|
|
|
|
2012-06-05 19:05:53 +08:00
|
|
|
manpage = str(manpage)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
# Get the "base" command, e.g. gcc.1.gz -> gcc
|
|
|
|
cmd_base = CMDNAME.split('.', 1)[0]
|
|
|
|
ignoredcommands = ["cc", "g++", "gcc", "c++", "cpp", "emacs", "gprof", "wget", "ld", "awk"]
|
|
|
|
if cmd_base in ignoredcommands:
|
|
|
|
return
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-08 16:43:30 +08:00
|
|
|
# Ignore perl's gazillion man pages
|
|
|
|
ignored_prefixes = ['perl', 'zsh']
|
|
|
|
for prefix in ignored_prefixes:
|
|
|
|
if cmd_base.startswith(prefix):
|
|
|
|
return
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-08 16:43:30 +08:00
|
|
|
# Ignore the millions of links to BUILTIN(1)
|
|
|
|
if manpage.find('BUILTIN 1') != -1:
|
|
|
|
return
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
# Clear the output list
|
|
|
|
built_command_output[:] = []
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-06-19 04:59:07 +08:00
|
|
|
if DEROFF_ONLY:
|
|
|
|
parsers = [TypeDeroffManParser()]
|
|
|
|
else:
|
|
|
|
parsers = [Type1ManParser(), Type2ManParser(), Type4ManParser(), Type3ManParser(), TypeDarwinManParser(), TypeDeroffManParser()]
|
2012-04-04 09:38:25 +08:00
|
|
|
parsersToTry = [p for p in parsers if p.isMyType(manpage)]
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-05 03:43:12 +08:00
|
|
|
success = False
|
2012-04-04 09:38:25 +08:00
|
|
|
if not parsersToTry:
|
|
|
|
add_diagnostic(manpage_path + ": Not supported")
|
|
|
|
else:
|
|
|
|
for parser in parsersToTry:
|
2012-06-19 04:59:07 +08:00
|
|
|
parser_name = parser.name()
|
|
|
|
add_diagnostic('Trying parser ' + parser_name)
|
2012-04-04 09:38:25 +08:00
|
|
|
diagnostic_indent += 1
|
|
|
|
success = parser.parseManPage(manpage)
|
|
|
|
diagnostic_indent -= 1
|
2013-01-09 06:57:48 +08:00
|
|
|
# Make sure empty files aren't reported as success
|
|
|
|
if not built_command_output:
|
|
|
|
success = False
|
2012-06-19 04:59:07 +08:00
|
|
|
if success:
|
|
|
|
PARSER_INFO.setdefault(parser_name, []).append(CMDNAME)
|
|
|
|
break
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
if success:
|
2012-04-12 09:26:26 +08:00
|
|
|
if WRITE_TO_STDOUT:
|
|
|
|
output_file = sys.stdout
|
|
|
|
else:
|
2012-06-19 04:59:07 +08:00
|
|
|
fullpath = os.path.join(output_directory, CMDNAME + '.fish')
|
2012-04-12 09:26:26 +08:00
|
|
|
try:
|
|
|
|
if file_missing_or_overwritable(fullpath):
|
2012-12-02 22:31:44 +08:00
|
|
|
output_file = codecs.open(fullpath, "w", encoding="utf-8");
|
2012-04-12 09:26:26 +08:00
|
|
|
else:
|
|
|
|
add_diagnostic("Not overwriting the file at '%s'" % fullpath)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-06-05 19:05:53 +08:00
|
|
|
except IOError as err:
|
|
|
|
add_diagnostic("Unable to open file '%s': error(%d): %s" % (fullpath, err.errno, err.strerror))
|
2012-04-12 09:26:26 +08:00
|
|
|
return False
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-23 12:32:19 +08:00
|
|
|
built_command_output.insert(0, "# " + CMDNAME)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-12 09:26:26 +08:00
|
|
|
# Output the magic word Autogenerated so we can tell if we can overwrite this
|
2012-11-23 12:32:19 +08:00
|
|
|
built_command_output.insert(1, "# Autogenerated from man page " + manpage_path)
|
2012-10-18 09:22:57 +08:00
|
|
|
built_command_output.insert(2, "# using " + parser_name)
|
2012-04-04 09:38:25 +08:00
|
|
|
for line in built_command_output:
|
2012-04-12 09:26:26 +08:00
|
|
|
output_file.write(line)
|
|
|
|
output_file.write('\n')
|
|
|
|
output_file.write('\n')
|
2012-04-04 09:38:25 +08:00
|
|
|
add_diagnostic(manpage_path + ' parsed successfully')
|
2012-04-12 09:26:26 +08:00
|
|
|
if output_file != sys.stdout:
|
|
|
|
output_file.close()
|
2012-04-04 09:38:25 +08:00
|
|
|
else:
|
|
|
|
parser_names = ', '.join(p.name() for p in parsersToTry)
|
2012-04-12 10:05:43 +08:00
|
|
|
#add_diagnostic('%s contains no options or is unparsable' % manpage_path, BRIEF_VERBOSE)
|
|
|
|
add_diagnostic('%s contains no options or is unparsable (tried parser %s)' % (manpage_path, parser_names), BRIEF_VERBOSE)
|
2013-01-09 06:57:48 +08:00
|
|
|
# Make sure we delete any old completion
|
|
|
|
if not WRITE_TO_STDOUT:
|
|
|
|
fullpath = os.path.join(output_directory, CMDNAME + '.fish')
|
|
|
|
try:
|
|
|
|
os.remove(fullpath)
|
|
|
|
except (OSError, IOError):
|
|
|
|
# Ignore failure
|
|
|
|
pass
|
|
|
|
|
2012-04-05 03:43:12 +08:00
|
|
|
return success
|
2012-01-29 14:25:45 +08:00
|
|
|
|
2012-06-19 04:59:07 +08:00
|
|
|
# Indicates whether the given filename has a presence in one of the yield-to directories
|
|
|
|
# If so, there's a bespoke completion and we should not generate one
|
|
|
|
def file_in_yield_directory(filename, yield_to_dirs):
|
|
|
|
for yield_dir in yield_to_dirs:
|
|
|
|
test_path = os.path.join(yield_dir, filename)
|
|
|
|
if os.path.isfile(test_path):
|
|
|
|
# Yield to the existing file
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def parse_and_output_man_pages(paths, output_directory, yield_to_dirs, show_progress):
|
|
|
|
global diagnostic_indent, CMDNAME
|
2012-06-05 19:05:53 +08:00
|
|
|
paths.sort()
|
2012-04-05 03:43:12 +08:00
|
|
|
total_count = len(paths)
|
2012-04-12 10:05:43 +08:00
|
|
|
successful_count, index = 0, 0
|
|
|
|
padding_len = len(str(total_count))
|
|
|
|
last_progress_string_length = 0
|
|
|
|
if show_progress and not WRITE_TO_STDOUT:
|
2012-06-05 19:05:53 +08:00
|
|
|
print("Parsing man pages and writing completions to {0}".format(output_directory))
|
2012-04-04 09:38:25 +08:00
|
|
|
for manpage_path in paths:
|
2012-04-12 10:05:43 +08:00
|
|
|
index += 1
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-06-19 04:59:07 +08:00
|
|
|
# Get the "base" command, e.g. gcc.1.gz -> gcc
|
|
|
|
man_file_name = os.path.basename(manpage_path)
|
|
|
|
CMDNAME = man_file_name.split('.', 1)[0]
|
|
|
|
output_file_name = CMDNAME + '.fish'
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-06-19 04:59:07 +08:00
|
|
|
# Show progress if we're doing that
|
2012-04-12 10:05:43 +08:00
|
|
|
if show_progress:
|
2012-06-19 04:59:07 +08:00
|
|
|
progress_str = ' {0} / {1} : {2}'.format((str(index).rjust(padding_len)), total_count, man_file_name)
|
2012-04-12 10:05:43 +08:00
|
|
|
# Pad on the right with spaces so we overwrite whatever we wrote last time
|
|
|
|
padded_progress_str = progress_str.ljust(last_progress_string_length)
|
|
|
|
last_progress_string_length = len(progress_str)
|
2012-07-09 08:35:07 +08:00
|
|
|
sys.stdout.write("\r{0}\r".format(padded_progress_str))
|
2012-06-19 04:59:07 +08:00
|
|
|
sys.stdout.flush()
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-06-19 04:59:07 +08:00
|
|
|
# Maybe we want to skip this item
|
|
|
|
skip = False
|
|
|
|
if not WRITE_TO_STDOUT:
|
|
|
|
# Compute the path that we would write to
|
|
|
|
output_path = os.path.join(output_directory, output_file_name)
|
|
|
|
|
|
|
|
if file_in_yield_directory(output_file_name, yield_to_dirs):
|
|
|
|
# We're duplicating a bespoke completion - delete any existing completion
|
|
|
|
skip = True
|
|
|
|
cleanup_autogenerated_file(output_path)
|
|
|
|
elif not file_missing_or_overwritable(output_path):
|
|
|
|
# Don't overwrite a user-created completion
|
|
|
|
skip = True
|
|
|
|
|
|
|
|
# Now skip if requested
|
|
|
|
if skip:
|
|
|
|
continue
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-04 09:38:25 +08:00
|
|
|
try:
|
2012-06-19 04:59:07 +08:00
|
|
|
if parse_manpage_at_path(manpage_path, yield_to_dirs, output_directory):
|
2012-04-05 03:43:12 +08:00
|
|
|
successful_count += 1
|
2012-04-04 09:38:25 +08:00
|
|
|
except IOError:
|
|
|
|
diagnostic_indent = 0
|
|
|
|
add_diagnostic('Cannot open ' + manpage_path)
|
2012-04-08 16:43:30 +08:00
|
|
|
except (KeyboardInterrupt, SystemExit):
|
|
|
|
raise
|
2012-04-04 09:38:25 +08:00
|
|
|
except:
|
2012-04-12 10:05:43 +08:00
|
|
|
add_diagnostic('Error parsing %s: %s' % (manpage_path, sys.exc_info()[0]), BRIEF_VERBOSE)
|
2012-04-04 09:38:25 +08:00
|
|
|
flush_diagnostics(sys.stderr)
|
|
|
|
traceback.print_exc(file=sys.stderr)
|
|
|
|
flush_diagnostics(sys.stderr)
|
2012-06-05 19:05:53 +08:00
|
|
|
print("") #Newline after loop
|
2012-04-12 10:05:43 +08:00
|
|
|
add_diagnostic("Successfully parsed %d / %d pages" % (successful_count, total_count), BRIEF_VERBOSE)
|
2012-04-05 03:43:12 +08:00
|
|
|
flush_diagnostics(sys.stderr)
|
2012-04-12 09:26:26 +08:00
|
|
|
|
|
|
|
def get_paths_from_manpath():
|
|
|
|
# Return all the paths to man(1) files in the manpath
|
|
|
|
import subprocess, os
|
2012-06-07 02:30:43 +08:00
|
|
|
proc = subprocess.Popen(['manpath'], stdout=subprocess.PIPE)
|
2012-04-16 10:45:44 +08:00
|
|
|
manpath, err_data = proc.communicate()
|
2012-06-05 19:05:53 +08:00
|
|
|
parent_paths = manpath.decode().strip().split(':')
|
2012-04-12 09:26:26 +08:00
|
|
|
if not parent_paths:
|
2012-06-07 02:30:43 +08:00
|
|
|
sys.stderr.write("Unable to get the manpath (tried manpath)\n")
|
2012-04-12 09:26:26 +08:00
|
|
|
sys.exit(-1)
|
|
|
|
result = []
|
|
|
|
for parent_path in parent_paths:
|
|
|
|
directory_path = os.path.join(parent_path, 'man1')
|
|
|
|
try:
|
|
|
|
names = os.listdir(directory_path)
|
2012-06-05 19:05:53 +08:00
|
|
|
except OSError as e:
|
2012-04-12 09:26:26 +08:00
|
|
|
names = []
|
|
|
|
names.sort()
|
|
|
|
for name in names:
|
|
|
|
result.append(os.path.join(directory_path, name))
|
|
|
|
return result
|
2012-11-18 18:23:22 +08:00
|
|
|
|
|
|
|
|
2012-04-12 09:26:26 +08:00
|
|
|
|
|
|
|
def usage(script_name):
|
2012-06-05 19:05:53 +08:00
|
|
|
print("Usage: {0} [-v, --verbose] [-s, --stdout] [-d, --directory] [-p, --progress] files...".format(script_name))
|
|
|
|
print("""Command options are:
|
2012-04-12 09:26:26 +08:00
|
|
|
-h, --help\t\tShow this help message
|
2012-06-19 04:59:07 +08:00
|
|
|
-v, --verbose [0, 1, 2]\tShow debugging output to stderr. Larger is more verbose.
|
2012-04-12 09:26:26 +08:00
|
|
|
-s, --stdout\tWrite all completions to stdout (trumps the --directory option)
|
2012-06-19 04:59:07 +08:00
|
|
|
-d, --directory [dir]\tWrite all completions to the given directory, instead of to ~/.config/fish/completions
|
|
|
|
-y, --yield-to [dir]\tSkip completions that are already present in the given directory
|
2012-06-07 02:30:43 +08:00
|
|
|
-m, --manpath\tProcess all man1 files available in the manpath (as determined by manpath)
|
2012-04-12 10:05:43 +08:00
|
|
|
-p, --progress\tShow progress
|
2012-06-05 19:05:53 +08:00
|
|
|
""")
|
2012-03-31 01:00:01 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2012-04-12 09:26:26 +08:00
|
|
|
script_name = sys.argv[0]
|
|
|
|
try:
|
2012-06-19 04:59:07 +08:00
|
|
|
opts, file_paths = getopt.gnu_getopt(sys.argv[1:], 'v:sd:hmpy:z', ['verbose=', 'stdout', 'directory=', 'help', 'manpath', 'progress', 'yield-to='])
|
2012-06-05 19:05:53 +08:00
|
|
|
except getopt.GetoptError as err:
|
2012-07-09 08:35:07 +08:00
|
|
|
print(err.msg) # will print something like "option -a not recognized"
|
2012-04-12 09:26:26 +08:00
|
|
|
usage(script_name)
|
|
|
|
sys.exit(2)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-06-19 04:59:07 +08:00
|
|
|
# If a completion already exists in one of the yield-to directories, then don't overwrite it
|
|
|
|
# And even delete an existing autogenerated one
|
|
|
|
yield_to_dirs = []
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-12 10:05:43 +08:00
|
|
|
use_manpath, show_progress, custom_dir = False, False, False
|
2012-04-12 09:26:26 +08:00
|
|
|
output_directory = ''
|
|
|
|
for opt, value in opts:
|
|
|
|
if opt in ('-v', '--verbose'):
|
2012-06-19 04:59:07 +08:00
|
|
|
VERBOSITY = int(value)
|
2012-04-12 09:26:26 +08:00
|
|
|
elif opt in ('-s', '--stdout'):
|
|
|
|
WRITE_TO_STDOUT = True
|
|
|
|
elif opt in ('-d', '--directory'):
|
|
|
|
output_directory = value
|
|
|
|
elif opt in ('-h', '--help'):
|
|
|
|
usage(script_name)
|
|
|
|
sys.exit(0)
|
|
|
|
elif opt in ('-m', '--manpath'):
|
|
|
|
use_manpath = True
|
2012-04-12 10:05:43 +08:00
|
|
|
elif opt in ('-p', '--progress'):
|
|
|
|
show_progress = True
|
2012-06-19 04:59:07 +08:00
|
|
|
elif opt in ('-y', '--yield-to'):
|
|
|
|
yield_to_dirs.append(value)
|
|
|
|
if not os.path.isdir(value):
|
|
|
|
sys.stderr.write("Warning: yield-to directory does not exist: '{0}'\n".format(value))
|
|
|
|
elif opt in ('-z'):
|
|
|
|
DEROFF_ONLY = True
|
2012-04-12 09:26:26 +08:00
|
|
|
else:
|
|
|
|
assert False, "unhandled option"
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-12 09:26:26 +08:00
|
|
|
if use_manpath:
|
|
|
|
# Fetch all man1 files from the manpath
|
|
|
|
file_paths.extend(get_paths_from_manpath())
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-12 09:26:26 +08:00
|
|
|
if not file_paths:
|
2012-06-05 19:05:53 +08:00
|
|
|
print("No paths specified")
|
2012-04-12 09:26:26 +08:00
|
|
|
sys.exit(0)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-12 09:26:26 +08:00
|
|
|
if not WRITE_TO_STDOUT and not output_directory:
|
|
|
|
# Default to ~/.config/fish/completions/
|
|
|
|
# Create it if it doesn't exist
|
|
|
|
output_directory = os.path.expanduser('~/.config/fish/completions/')
|
|
|
|
try:
|
|
|
|
os.makedirs(output_directory)
|
2012-06-05 19:05:53 +08:00
|
|
|
except OSError as e:
|
2012-04-12 09:26:26 +08:00
|
|
|
if e.errno != errno.EEXIST:
|
|
|
|
raise
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-09 14:26:26 +08:00
|
|
|
if True:
|
2012-06-19 04:59:07 +08:00
|
|
|
parse_and_output_man_pages(file_paths, output_directory, yield_to_dirs, show_progress)
|
2012-04-08 16:43:30 +08:00
|
|
|
else:
|
|
|
|
# Profiling code
|
|
|
|
import cProfile, pstats
|
2012-06-19 04:59:07 +08:00
|
|
|
cProfile.run('parse_and_output_man_pages(file_paths, output_directory, yield_to_dirs, show_progress)', 'fooprof')
|
2012-04-08 16:43:30 +08:00
|
|
|
p = pstats.Stats('fooprof')
|
|
|
|
p.sort_stats('cumulative').print_stats(100)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-06-19 04:59:07 +08:00
|
|
|
# Here we can write out all the parser infos
|
|
|
|
if False:
|
|
|
|
for name in PARSER_INFO:
|
|
|
|
print('Parser ' + name + ':')
|
|
|
|
print('\t' + ', '.join(PARSER_INFO[name]))
|
|
|
|
print('')
|