1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2024-12-23 08:16:54 +00:00

move usage generation to setup.py

this is an unfortunate rewrite of the manpage creation code mentionned
in #208. ideally, this would be rewritten into a class that can
generate both man pages and .rst files.
This commit is contained in:
Antoine Beaupré 2015-10-07 21:07:12 -04:00
parent 4787424a66
commit 13d3568548
3 changed files with 79 additions and 38 deletions

View file

@ -548,24 +548,8 @@ def preprocess_args(self, args):
print(warning) print(warning)
return args return args
def run(self, args=None): def build_parser(self, args=None, prog=None):
check_extension_modules() common_parser = argparse.ArgumentParser(add_help=False, prog=prog)
keys_dir = get_keys_dir()
if not os.path.exists(keys_dir):
os.makedirs(keys_dir)
os.chmod(keys_dir, stat.S_IRWXU)
cache_dir = get_cache_dir()
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
os.chmod(cache_dir, stat.S_IRWXU)
with open(os.path.join(cache_dir, 'CACHEDIR.TAG'), 'w') as fd:
fd.write(textwrap.dedent("""
Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by Borg.
# For information about cache directory tags, see:
# http://www.brynosaurus.com/cachedir/
""").lstrip())
common_parser = argparse.ArgumentParser(add_help=False)
common_parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', common_parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
default=False, default=False,
help='verbose output') help='verbose output')
@ -576,11 +560,7 @@ def run(self, args=None):
common_parser.add_argument('--remote-path', dest='remote_path', default=RemoteRepository.remote_path, metavar='PATH', common_parser.add_argument('--remote-path', dest='remote_path', default=RemoteRepository.remote_path, metavar='PATH',
help='set remote path to executable (default: "%(default)s")') help='set remote path to executable (default: "%(default)s")')
# We can't use argparse for "serve" since we don't want it to show up in "Available commands" parser = argparse.ArgumentParser(prog=prog, description='Borg %s - Deduplicated Backups' % __version__)
if args:
args = self.preprocess_args(args)
parser = argparse.ArgumentParser(description='Borg %s - Deduplicated Backups' % __version__)
subparsers = parser.add_subparsers(title='Available commands') subparsers = parser.add_subparsers(title='Available commands')
serve_epilog = textwrap.dedent(""" serve_epilog = textwrap.dedent("""
@ -976,6 +956,30 @@ def run(self, args=None):
subparser.set_defaults(func=functools.partial(self.do_help, parser, subparsers.choices)) subparser.set_defaults(func=functools.partial(self.do_help, parser, subparsers.choices))
subparser.add_argument('topic', metavar='TOPIC', type=str, nargs='?', subparser.add_argument('topic', metavar='TOPIC', type=str, nargs='?',
help='additional help on TOPIC') help='additional help on TOPIC')
return parser
def run(self, args=None):
check_extension_modules()
keys_dir = get_keys_dir()
if not os.path.exists(keys_dir):
os.makedirs(keys_dir)
os.chmod(keys_dir, stat.S_IRWXU)
cache_dir = get_cache_dir()
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
os.chmod(cache_dir, stat.S_IRWXU)
with open(os.path.join(cache_dir, 'CACHEDIR.TAG'), 'w') as fd:
fd.write(textwrap.dedent("""
Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by Borg.
# For information about cache directory tags, see:
# http://www.brynosaurus.com/cachedir/
""").lstrip())
# We can't use argparse for "serve" since we don't want it to show up in "Available commands"
if args:
args = self.preprocess_args(args)
parser = self.build_parser(args)
args = parser.parse_args(args or ['-h']) args = parser.parse_args(args or ['-h'])
self.verbose = args.verbose self.verbose = args.verbose

View file

@ -36,7 +36,7 @@ help:
clean: clean:
-rm -rf $(BUILDDIR)/* -rm -rf $(BUILDDIR)/*
html: usage html:
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
@echo @echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html." @echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
@ -139,17 +139,3 @@ gh-io: html
inotify: html inotify: html
while inotifywait -r . --exclude usage.rst --exclude '_build/*' ; do make html ; done while inotifywait -r . --exclude usage.rst --exclude '_build/*' ; do make html ; done
# generate list of targets
usage: $(shell borg help | grep -A1 "Available commands:" | tail -1 | sed 's/[{} ]//g;s/,\|^/.rst.inc usage\//g;s/^.rst.inc//;s/usage\/help//')
# generate help file based on usage
usage/%.rst.inc: ../borg/archiver.py
@echo generating usage for $*
@printf ".. _borg_$*:\n\n" > $@
@printf "borg $*\n" >> $@
@echo -n borg $* | tr 'a-z- ' '-' >> $@
@printf "\n::\n\n" >> $@
@borg help $* --usage-only | sed -e 's/^/ /' >> $@
@printf "\nDescription\n~~~~~~~~~~~\n" >> $@
@borg help $* --epilog-only >> $@

View file

@ -1,5 +1,6 @@
# -*- encoding: utf-8 *-* # -*- encoding: utf-8 *-*
import os import os
import re
import sys import sys
from glob import glob from glob import glob
@ -119,6 +120,54 @@ def detect_lz4(prefixes):
with open('README.rst', 'r') as fd: with open('README.rst', 'r') as fd:
long_description = fd.read() long_description = fd.read()
class build_usage(Command):
description = "generate usage for each command"
user_options = [
('output=', 'O', 'output directory'),
]
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
import pdb
print('generating usage docs')
from borg.archiver import Archiver
parser = Archiver().build_parser(prog='borg')
choices = {}
for action in parser._actions:
if action.choices is not None:
choices.update(action.choices)
print('found commands: %s' % list(choices.keys()))
if not os.path.exists('docs/usage'):
os.mkdir('docs/usage')
for command, parser in choices.items():
if command is 'help':
continue
with open('docs/usage/%s.rst.inc' % command, 'w') as cmdfile:
print('generating help for %s' % command)
cmdfile.write(""".. _borg_{command}:
borg {command}
{underline}
::
""".format(**{"command": command,
"underline": '-' * len('borg ' + command)}))
epilog = parser.epilog
parser.epilog = None
cmdfile.write(re.sub("^", " ", parser.format_help(), flags=re.M))
cmdfile.write("""
Description
~~~~~~~~~~~
""")
cmdfile.write(epilog)
class build_api(Command): class build_api(Command):
description = "generate a basic api.rst file based on the modules available" description = "generate a basic api.rst file based on the modules available"
@ -149,10 +198,12 @@ def run(self):
# (function, predicate), see http://docs.python.org/2/distutils/apiref.html#distutils.cmd.Command.sub_commands # (function, predicate), see http://docs.python.org/2/distutils/apiref.html#distutils.cmd.Command.sub_commands
build.sub_commands.append(('build_api', None)) build.sub_commands.append(('build_api', None))
build.sub_commands.append(('build_usage', None))
cmdclass = { cmdclass = {
'build_ext': build_ext, 'build_ext': build_ext,
'build_api': build_api, 'build_api': build_api,
'build_usage': build_usage,
'sdist': Sdist 'sdist': Sdist
} }