Remove dependency on help2man. Also significantly speds up man page generation

This commit is contained in:
Kovid Goyal 2009-03-08 12:30:50 -07:00
parent 0f1414679e
commit 05eea1baf8
5 changed files with 86 additions and 38 deletions

View File

@ -2,11 +2,15 @@
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2008, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import sys, os, re, logging, time, subprocess, atexit, mimetypes
import sys, os, re, logging, time, subprocess, atexit, mimetypes, warnings
from htmlentitydefs import name2codepoint from htmlentitydefs import name2codepoint
from math import floor from math import floor
from logging import Formatter from logging import Formatter
warnings.simplefilter('ignore', DeprecationWarning)
from PyQt4.QtCore import QUrl from PyQt4.QtCore import QUrl
from PyQt4.QtGui import QDesktopServices from PyQt4.QtGui import QDesktopServices
from calibre.startup import plugins, winutil, winutilerror from calibre.startup import plugins, winutil, winutilerror

View File

@ -410,45 +410,27 @@ def option_parser():
help='Save a manifest of all installed files to the specified location') help='Save a manifest of all installed files to the specified location')
return parser return parser
def install_man_pages(fatal_errors): def install_man_pages(fatal_errors, use_destdir=False):
from bz2 import compress from calibre.utils.help2man import create_man_page
import subprocess prefix = os.environ.get('DESTDIR', '/') if use_destdir else '/'
manpath = os.path.join(prefix, 'usr/share/man/man1')
if not os.path.exists(manpath):
os.makedirs(manpath)
print 'Installing MAN pages...' print 'Installing MAN pages...'
manpath = '/usr/share/man/man1'
f = NamedTemporaryFile()
f.write('[see also]\nhttp://%s.kovidgoyal.net\n'%__appname__)
f.flush()
manifest = [] manifest = []
os.environ['PATH'] += ':'+os.path.expanduser('~/bin')
for src in entry_points['console_scripts']: for src in entry_points['console_scripts']:
prog = src[:src.index('=')].strip() prog, right = src.split('=')
if prog in ('prs500', 'pdf-meta', 'epub-meta', 'lit-meta', prog = prog.strip()
'markdown-calibre', 'calibre-debug', 'fb2-meta', module = __import__(right.split(':')[0].strip(), fromlist=['a'])
'calibre-fontconfig', 'calibre-parallel', 'odt-meta', parser = getattr(module, 'option_parser', None)
'rb-meta', 'imp-meta', 'mobi-meta'): if parser is None:
continue continue
parser = parser()
help2man = ('help2man', prog, '--name', 'part of %s'%__appname__, raw = create_man_page(prog, parser)
'--section', '1', '--no-info', '--include',
f.name, '--manual', __appname__)
manfile = os.path.join(manpath, prog+'.1'+__appname__+'.bz2') manfile = os.path.join(manpath, prog+'.1'+__appname__+'.bz2')
print '\tInstalling MAN page for', prog print '\tInstalling MAN page for', prog
try: open(manfile, 'wb').write(raw)
p = subprocess.Popen(help2man, stdout=subprocess.PIPE) manifest.append(manfile)
except OSError, err:
import errno
if err.errno != errno.ENOENT:
raise
print 'Failed to install MAN pages as help2man is missing from your system'
break
o = p.stdout.read()
raw = re.compile(r'^\.IP\s*^([A-Z :]+)$', re.MULTILINE).sub(r'.SS\n\1', o)
if not raw.strip():
print 'Unable to create MAN page for', prog
continue
f2 = open_file(manfile)
manifest.append(f2.name)
f2.write(compress(raw))
return manifest return manifest
def post_install(): def post_install():
@ -460,9 +442,9 @@ def post_install():
manifest = [] manifest = []
setup_desktop_integration(opts.fatal_errors) setup_desktop_integration(opts.fatal_errors)
if opts.no_root or os.geteuid() == 0: if opts.no_root or os.geteuid() == 0:
manifest += install_man_pages(opts.fatal_errors, use_destdir)
manifest += setup_udev_rules(opts.group_file, not opts.dont_reload, opts.fatal_errors) manifest += setup_udev_rules(opts.group_file, not opts.dont_reload, opts.fatal_errors)
manifest += setup_completion(opts.fatal_errors) manifest += setup_completion(opts.fatal_errors)
manifest += install_man_pages(opts.fatal_errors)
else: else:
print "Skipping udev, completion, and man-page install for non-root user." print "Skipping udev, completion, and man-page install for non-root user."

View File

@ -18,7 +18,6 @@ DEPENDENCIES = [
('lxml', '2.1.5', 'lxml', 'python-lxml', 'python-lxml'), ('lxml', '2.1.5', 'lxml', 'python-lxml', 'python-lxml'),
('python-dateutil', '1.4.1', 'python-dateutil', 'python-dateutil', 'python-dateutil'), ('python-dateutil', '1.4.1', 'python-dateutil', 'python-dateutil', 'python-dateutil'),
('BeautifulSoup', '3.0.5', 'beautifulsoup', 'python-beautifulsoup', 'python-BeautifulSoup'), ('BeautifulSoup', '3.0.5', 'beautifulsoup', 'python-beautifulsoup', 'python-BeautifulSoup'),
('help2man', '1.36.4', 'help2man', 'help2man', 'help2man'),
] ]

View File

@ -88,7 +88,7 @@ sudo python -c "import urllib2; exec urllib2.urlopen('http://calibre.kovidgoyal.
be ignored. be ignored.
</li> </li>
<li> <li>
You must have help2man and xdg-utils installed You must have xdg-utils installed
on your system before running the installer. on your system before running the installer.
</li> </li>
<li> <li>

View File

@ -0,0 +1,63 @@
from __future__ import with_statement
__license__ = 'GPL 3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import time, bz2
from calibre.constants import __version__, __appname__, __author__
def create_man_page(prog, parser):
usage = parser.usage.splitlines()
for i, line in enumerate(list(usage)):
if not line.strip():
usage[i] = '.PP'
else:
usage[i] = line.replace('%prog', prog)
lines = [
'.TH ' + prog.upper() + ' "1" ' + time.strftime('"%B %Y"') +
' "%s (%s %s)" "%s"'%(prog, __appname__, __version__, __appname__),
'.SH NAME',
prog + r' \- part of '+__appname__,
'.SH SYNOPSIS',
'.B "%s"'%prog + r'\fR '+' '.join(usage[0].split()[1:]),
'.SH DESCRIPTION',
]
lines += usage[1:]
lines += [
'.SH OPTIONS'
]
def format_option(opt):
ans = ['.TP']
opts = []
opts += opt._short_opts
opts.append(opt.get_opt_string())
opts = [r'\fB'+x.replace('-', r'\-')+r'\fR' for x in opts]
ans.append(', '.join(opts))
help = opt.help if opt.help else ''
ans.append(help.replace('%prog', prog).replace('%default', str(opt.default)))
return ans
for opt in parser.option_list:
lines.extend(format_option(opt))
for group in parser.option_groups:
lines.append('.SS '+group.title)
for opt in group.option_list:
lines.extend(format_option(opt))
lines += ['.SH SEE ALSO',
'The User Manual is available at '
'http://calibre.kovidgoyal.net/user_manual',
'.PP', '.B Created by '+__author__]
return bz2.compress('\n'.join(lines))
def main():
from calibre.ebooks.epub.from_any import option_parser
open('/tmp/any2epub.1calibre.bz2', 'w').write(create_man_page(
'any2epub', option_parser()))
if __name__ == '__main__':
main()