def get_long_description(cleanup=True): from sphinx.application import Sphinx from sphinx.util.osutil import abspath import tempfile import shutil from doc.conf import extensions from sphinxcontrib.writers.rst import RstTranslator from sphinx.ext.graphviz import text_visit_graphviz RstTranslator.visit_dsp = text_visit_graphviz outdir = tempfile.mkdtemp(prefix='setup-', dir='.') exclude_patterns = os.listdir(mydir or '.') exclude_patterns.remove('pypi.rst') app = Sphinx(abspath(mydir), osp.join(mydir, 'doc/'), outdir, outdir + '/.doctree', 'rst', confoverrides={ 'exclude_patterns': exclude_patterns, 'master_doc': 'pypi', 'dispatchers_out_dir': abspath(outdir + '/_dispatchers'), 'extensions': extensions + ['sphinxcontrib.restbuilder'] }, status=None, warning=None) app.build(filenames=[osp.join(app.srcdir, 'pypi.rst')]) with open(outdir + '/pypi.rst') as file: res = file.read() if cleanup: shutil.rmtree(outdir) return res
def get_long_description(cleanup=True): from sphinx.application import Sphinx from sphinx.util.osutil import abspath import tempfile import shutil from sphinxcontrib.writers.rst import RstTranslator outdir = tempfile.mkdtemp(prefix='setup-', dir='.') exclude_patterns = os.listdir(mydir or '.') exclude_patterns.remove('pypi.rst') app = Sphinx(abspath(mydir), './doc/', outdir, outdir + '/.doctree', 'text', confoverrides={ 'exclude_patterns': exclude_patterns, 'master_doc': 'pypi', 'dispatchers_out_dir': abspath(outdir + '/_dispatchers') }, status=None, warning=None) app.builder.translator_class = RstTranslator app.build(filenames=[osp.join(app.srcdir, 'pypi.rst')]) res = open(outdir + '/pypi.txt').read() if cleanup: shutil.rmtree(outdir) return res
def main(argv): if not color_terminal(): # Windows' poor cmd box doesn't understand ANSI sequences nocolor() try: opts, args = getopt.getopt(argv[1:], "ab:t:d:c:CD:A:nNEqQWw:PThvj:", ["help", "version"]) allopts = set(opt[0] for opt in opts) if "-h" in allopts or "--help" in allopts: usage(argv) print >> sys.stderr print >> sys.stderr, "For more information, see " "<http://sphinx-doc.org/>." return 0 if "--version" in allopts: print "Sphinx (sphinx-build) %s" % __version__ return 0 srcdir = confdir = abspath(args[0]) if not path.isdir(srcdir): print >> sys.stderr, "Error: Cannot find source directory `%s'." % (srcdir,) return 1 if not path.isfile(path.join(srcdir, "conf.py")) and "-c" not in allopts and "-C" not in allopts: print >> sys.stderr, ("Error: Source directory doesn't " "contain conf.py file.") return 1 outdir = abspath(args[1]) except getopt.error, err: usage(argv, "Error: %s" % err) return 1
def finalize_options(self): # type: () -> None if self.source_dir is None: self.source_dir = self._guess_source_dir() self.announce('Using source directory %s' % self.source_dir) # type: ignore self.ensure_dirname('source_dir') # type: ignore if self.source_dir is None: self.source_dir = os.curdir self.source_dir = abspath(self.source_dir) if self.config_dir is None: self.config_dir = self.source_dir self.config_dir = abspath(self.config_dir) self.ensure_string_list('builder') # type: ignore if self.build_dir is None: build = self.get_finalized_command('build') # type: ignore self.build_dir = os.path.join(abspath(build.build_base), 'sphinx') self.mkpath(self.build_dir) # type: ignore self.build_dir = abspath(self.build_dir) self.doctree_dir = os.path.join(self.build_dir, 'doctrees') self.mkpath(self.doctree_dir) # type: ignore self.builder_target_dirs = [ (builder, os.path.join(self.build_dir, builder)) for builder in self.builder ] # type: List[Tuple[str, unicode]] for _, builder_target_dir in self.builder_target_dirs: self.mkpath(builder_target_dir) # type: ignore
def finalize_options(self): # type: () -> None if self.source_dir is None: self.source_dir = self._guess_source_dir() self.announce('Using source directory %s' % self.source_dir) self.ensure_dirname('source_dir') if self.source_dir is None: self.source_dir = os.curdir self.source_dir = abspath(self.source_dir) if self.config_dir is None: self.config_dir = self.source_dir self.config_dir = abspath(self.config_dir) self.ensure_string_list('builder') if self.build_dir is None: build = self.get_finalized_command('build') self.build_dir = os.path.join(abspath(build.build_base), 'sphinx') # type: ignore self.mkpath(self.build_dir) # type: ignore self.build_dir = abspath(self.build_dir) self.doctree_dir = os.path.join(self.build_dir, 'doctrees') self.mkpath(self.doctree_dir) # type: ignore self.builder_target_dirs = [ (builder, os.path.join(self.build_dir, builder)) for builder in self.builder] # type: List[Tuple[str, unicode]] for _, builder_target_dir in self.builder_target_dirs: self.mkpath(builder_target_dir) # type: ignore
def get_node_location(node: Node) -> Optional[str]: (source, line) = get_source_line(node) if source: source = abspath(source) if source and line: return "%s:%s" % (source, line) elif source: return "%s:" % source elif line: return "<unknown>:%s" % line else: return None
def finalize_options(self): if self.source_dir is None: self.source_dir = self._guess_source_dir() self.announce("Using source directory %s" % self.source_dir) self.ensure_dirname("source_dir") if self.source_dir is None: self.source_dir = os.curdir self.source_dir = abspath(self.source_dir) if self.config_dir is None: self.config_dir = self.source_dir self.config_dir = abspath(self.config_dir) if self.build_dir is None: build = self.get_finalized_command("build") self.build_dir = os.path.join(abspath(build.build_base), "sphinx") self.mkpath(self.build_dir) self.build_dir = abspath(self.build_dir) self.doctree_dir = os.path.join(self.build_dir, "doctrees") self.mkpath(self.doctree_dir) self.builder_target_dir = os.path.join(self.build_dir, self.builder) self.mkpath(self.builder_target_dir)
def finalize_options(self): if self.source_dir is None: self.source_dir = self._guess_source_dir() self.announce('Using source directory %s' % self.source_dir) self.ensure_dirname('source_dir') if self.source_dir is None: self.source_dir = os.curdir self.source_dir = abspath(self.source_dir) if self.config_dir is None: self.config_dir = self.source_dir self.config_dir = abspath(self.config_dir) if self.build_dir is None: build = self.get_finalized_command('build') self.build_dir = os.path.join(abspath(build.build_base), 'sphinx') self.mkpath(self.build_dir) self.build_dir = abspath(self.build_dir) self.doctree_dir = os.path.join(self.build_dir, 'doctrees') self.mkpath(self.doctree_dir) self.builder_target_dir = os.path.join(self.build_dir, self.builder) self.mkpath(self.builder_target_dir)
def test_get_node_location_abspath(): # Ensure that node locations are reported as an absolute path, # even if the source attribute is a relative path. relative_filename = os.path.join('relative', 'path.txt') absolute_filename = osutil.abspath(relative_filename) n = nodes.Node() n.source = relative_filename location = logging.get_node_location(n) assert location == absolute_filename + ':'
def finalize_options(self) -> None: self.ensure_string_list('builder') if self.source_dir is None: self.source_dir = self._guess_source_dir() self.announce('Using source directory %s' % self.source_dir) self.ensure_dirname('source_dir') if self.config_dir is None: self.config_dir = self.source_dir if self.build_dir is None: build = self.get_finalized_command('build') self.build_dir = os.path.join(abspath(build.build_base), 'sphinx') self.doctree_dir = os.path.join(self.build_dir, 'doctrees') self.builder_target_dirs = [(builder, os.path.join(self.build_dir, builder)) for builder in self.builder]
def main(argv): # type: (List[unicode]) -> int parser = optparse.OptionParser(USAGE, epilog=EPILOG, formatter=MyFormatter()) parser.add_option('--version', action='store_true', dest='version', help='show version information and exit') group = parser.add_option_group('General options') group.add_option('-b', metavar='BUILDER', dest='builder', default='html', help='builder to use; default is html') group.add_option('-a', action='store_true', dest='force_all', help='write all files; default is to only write new and ' 'changed files') group.add_option('-E', action='store_true', dest='freshenv', help='don\'t use a saved environment, always read ' 'all files') group.add_option('-d', metavar='PATH', default=None, dest='doctreedir', help='path for the cached environment and doctree files ' '(default: outdir/.doctrees)') group.add_option('-j', metavar='N', default=1, type='int', dest='jobs', help='build in parallel with N processes where possible') # this option never gets through to this point (it is intercepted earlier) # group.add_option('-M', metavar='BUILDER', dest='make_mode', # help='"make" mode -- as used by Makefile, like ' # '"sphinx-build -M html"') group = parser.add_option_group('Build configuration options') group.add_option('-c', metavar='PATH', dest='confdir', help='path where configuration file (conf.py) is located ' '(default: same as sourcedir)') group.add_option('-C', action='store_true', dest='noconfig', help='use no config file at all, only -D options') group.add_option('-D', metavar='setting=value', action='append', dest='define', default=[], help='override a setting in configuration file') group.add_option('-A', metavar='name=value', action='append', dest='htmldefine', default=[], help='pass a value into HTML templates') group.add_option('-t', metavar='TAG', action='append', dest='tags', default=[], help='define tag: include "only" blocks with TAG') group.add_option('-n', action='store_true', dest='nitpicky', help='nit-picky mode, warn about all missing references') group = parser.add_option_group('Console output options') group.add_option('-v', action='count', dest='verbosity', default=0, help='increase verbosity (can be repeated)') group.add_option('-q', action='store_true', dest='quiet', help='no output on stdout, just warnings on stderr') group.add_option('-Q', action='store_true', dest='really_quiet', help='no output at all, not even warnings') group.add_option('--color', dest='color', action='store_const', const='yes', default='auto', help='Do emit colored output (default: auto-detect)') group.add_option('-N', '--no-color', dest='color', action='store_const', const='no', help='Do not emit colored output (default: auot-detect)') group.add_option('-w', metavar='FILE', dest='warnfile', help='write warnings (and errors) to given file') group.add_option('-W', action='store_true', dest='warningiserror', help='turn warnings into errors') group.add_option('-T', action='store_true', dest='traceback', help='show full traceback on exception') group.add_option('-P', action='store_true', dest='pdb', help='run Pdb on exception') # parse options try: opts, args = parser.parse_args(list(argv[1:])) except SystemExit as err: return err.code # handle basic options if opts.version: print('Sphinx (sphinx-build) %s' % __display_version__) return 0 # get paths (first and second positional argument) try: srcdir = abspath(args[0]) confdir = abspath(opts.confdir or srcdir) if opts.noconfig: confdir = None if not path.isdir(srcdir): print('Error: Cannot find source directory `%s\'.' % srcdir, file=sys.stderr) return 1 if not opts.noconfig and not path.isfile(path.join(confdir, 'conf.py')): print('Error: Config directory doesn\'t contain a conf.py file.', file=sys.stderr) return 1 outdir = abspath(args[1]) if srcdir == outdir: print( 'Error: source directory and destination directory are same.', file=sys.stderr) return 1 except IndexError: parser.print_help() return 1 except UnicodeError: print('Error: Multibyte filename not supported on this filesystem ' 'encoding (%r).' % fs_encoding, file=sys.stderr) return 1 # handle remaining filename arguments filenames = args[2:] err = 0 # type: ignore for filename in filenames: if not path.isfile(filename): print('Error: Cannot find file %r.' % filename, file=sys.stderr) err = 1 # type: ignore if err: return 1 # likely encoding used for command-line arguments try: locale = __import__('locale') # due to submodule of the same name likely_encoding = locale.getpreferredencoding() except Exception: likely_encoding = None if opts.force_all and filenames: print('Error: Cannot combine -a option and filenames.', file=sys.stderr) return 1 if opts.color == 'no' or (opts.color == 'auto' and not color_terminal()): nocolor() doctreedir = abspath(opts.doctreedir or path.join(outdir, '.doctrees')) status = sys.stdout warning = sys.stderr error = sys.stderr if opts.quiet: status = None if opts.really_quiet: status = warning = None if warning and opts.warnfile: try: warnfp = open(opts.warnfile, 'w') except Exception as exc: print('Error: Cannot open warning file %r: %s' % (opts.warnfile, exc), file=sys.stderr) sys.exit(1) warning = Tee(warning, warnfp) # type: ignore error = warning confoverrides = {} for val in opts.define: try: key, val = val.split('=', 1) except ValueError: print('Error: -D option argument must be in the form name=value.', file=sys.stderr) return 1 if likely_encoding and isinstance(val, binary_type): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides[key] = val for val in opts.htmldefine: try: key, val = val.split('=') except ValueError: print('Error: -A option argument must be in the form name=value.', file=sys.stderr) return 1 try: val = int(val) except ValueError: if likely_encoding and isinstance(val, binary_type): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides['html_context.%s' % key] = val if opts.nitpicky: confoverrides['nitpicky'] = True app = None try: with docutils_namespace(): app = Sphinx(srcdir, confdir, outdir, doctreedir, opts.builder, confoverrides, status, warning, opts.freshenv, opts.warningiserror, opts.tags, opts.verbosity, opts.jobs) app.build(opts.force_all, filenames) return app.statuscode except (Exception, KeyboardInterrupt) as exc: handle_exception(app, opts, exc, error) return 1
def build_main(argv: List[str] = sys.argv[1:]) -> int: """Sphinx build "main" command-line entry.""" parser = get_parser() args = parser.parse_args(argv) if args.noconfig: args.confdir = None elif not args.confdir: args.confdir = args.sourcedir if not args.doctreedir: args.doctreedir = os.path.join(args.outputdir, '.doctrees') # handle remaining filename arguments filenames = args.filenames missing_files = [] for filename in filenames: if not os.path.isfile(filename): missing_files.append(filename) if missing_files: parser.error(__('cannot find files %r') % missing_files) if args.force_all and filenames: parser.error(__('cannot combine -a option and filenames')) if args.color == 'no' or (args.color == 'auto' and not color_terminal()): nocolor() status = sys.stdout warning = sys.stderr error = sys.stderr if args.quiet: status = None if args.really_quiet: status = warning = None if warning and args.warnfile: try: warnfile = abspath(args.warnfile) ensuredir(path.dirname(warnfile)) warnfp = open(args.warnfile, 'w', encoding="utf-8") except Exception as exc: parser.error( __('cannot open warning file %r: %s') % (args.warnfile, exc)) warning = Tee(warning, warnfp) # type: ignore error = warning confoverrides = {} for val in args.define: try: key, val = val.split('=', 1) except ValueError: parser.error( __('-D option argument must be in the form name=value')) confoverrides[key] = val for val in args.htmldefine: try: key, val = val.split('=') except ValueError: parser.error( __('-A option argument must be in the form name=value')) try: val = int(val) except ValueError: pass confoverrides['html_context.%s' % key] = val if args.nitpicky: confoverrides['nitpicky'] = True app = None try: confdir = args.confdir or args.sourcedir with patch_docutils(confdir), docutils_namespace(): app = Sphinx(args.sourcedir, args.confdir, args.outputdir, args.doctreedir, args.builder, confoverrides, status, warning, args.freshenv, args.warningiserror, args.tags, args.verbosity, args.jobs, args.keep_going, args.pdb) app.build(args.force_all, filenames) return app.statuscode except (Exception, KeyboardInterrupt) as exc: handle_exception(app, args, exc, error) return 2
def main(argv=sys.argv[1:]): # type: ignore # type: (List[unicode]) -> int parser = get_parser() # parse options try: args = parser.parse_args(argv) except SystemExit as err: return err.code # get paths (first and second positional argument) try: srcdir = abspath(args.sourcedir) confdir = abspath(args.confdir or srcdir) if args.noconfig: confdir = None if not path.isdir(srcdir): print('Error: Cannot find source directory `%s\'.' % srcdir, file=sys.stderr) return 1 if not args.noconfig and not path.isfile(path.join(confdir, 'conf.py')): print('Error: Config directory doesn\'t contain a conf.py file.', file=sys.stderr) return 1 outdir = abspath(args.outputdir) if srcdir == outdir: print('Error: source directory and destination directory are same.', file=sys.stderr) return 1 except UnicodeError: print( 'Error: Multibyte filename not supported on this filesystem ' 'encoding (%r).' % fs_encoding, file=sys.stderr) return 1 # handle remaining filename arguments filenames = args.filenames errored = False for filename in filenames: if not path.isfile(filename): print('Error: Cannot find file %r.' % filename, file=sys.stderr) errored = True if errored: return 1 # likely encoding used for command-line arguments try: locale = __import__('locale') # due to submodule of the same name likely_encoding = locale.getpreferredencoding() except Exception: likely_encoding = None if args.force_all and filenames: print('Error: Cannot combine -a option and filenames.', file=sys.stderr) return 1 if args.color == 'no' or (args.color == 'auto' and not color_terminal()): nocolor() doctreedir = abspath(args.doctreedir or path.join(outdir, '.doctrees')) status = sys.stdout warning = sys.stderr error = sys.stderr if args.quiet: status = None if args.really_quiet: status = warning = None if warning and args.warnfile: try: warnfp = open(args.warnfile, 'w') except Exception as exc: print('Error: Cannot open warning file %r: %s' % (args.warnfile, exc), file=sys.stderr) sys.exit(1) warning = Tee(warning, warnfp) # type: ignore error = warning confoverrides = {} for val in args.define: try: key, val = val.split('=', 1) except ValueError: print('Error: -D option argument must be in the form name=value.', file=sys.stderr) return 1 if likely_encoding and isinstance(val, binary_type): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides[key] = val for val in args.htmldefine: try: key, val = val.split('=') except ValueError: print('Error: -A option argument must be in the form name=value.', file=sys.stderr) return 1 try: val = int(val) except ValueError: if likely_encoding and isinstance(val, binary_type): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides['html_context.%s' % key] = val if args.nitpicky: confoverrides['nitpicky'] = True app = None try: with patch_docutils(), docutils_namespace(): app = Sphinx(srcdir, confdir, outdir, doctreedir, args.builder, confoverrides, status, warning, args.freshenv, args.warningiserror, args.tags, args.verbosity, args.jobs) app.build(args.force_all, filenames) return app.statuscode except (Exception, KeyboardInterrupt) as exc: handle_exception(app, args, exc, error) return 1
def main(argv): # type: (List[unicode]) -> int parser = optparse.OptionParser(USAGE, epilog=EPILOG, formatter=MyFormatter()) parser.add_option('--version', action='store_true', dest='version', help='show version information and exit') group = parser.add_option_group('General options') group.add_option('-b', metavar='BUILDER', dest='builder', default='html', help='builder to use; default is html') group.add_option('-a', action='store_true', dest='force_all', help='write all files; default is to only write new and ' 'changed files') group.add_option('-E', action='store_true', dest='freshenv', help='don\'t use a saved environment, always read ' 'all files') group.add_option('-d', metavar='PATH', default=None, dest='doctreedir', help='path for the cached environment and doctree files ' '(default: outdir/.doctrees)') group.add_option('-j', metavar='N', default=1, type='int', dest='jobs', help='build in parallel with N processes where possible') # this option never gets through to this point (it is intercepted earlier) # group.add_option('-M', metavar='BUILDER', dest='make_mode', # help='"make" mode -- as used by Makefile, like ' # '"sphinx-build -M html"') group = parser.add_option_group('Build configuration options') group.add_option('-c', metavar='PATH', dest='confdir', help='path where configuration file (conf.py) is located ' '(default: same as sourcedir)') group.add_option('-C', action='store_true', dest='noconfig', help='use no config file at all, only -D options') group.add_option('-D', metavar='setting=value', action='append', dest='define', default=[], help='override a setting in configuration file') group.add_option('-A', metavar='name=value', action='append', dest='htmldefine', default=[], help='pass a value into HTML templates') group.add_option('-t', metavar='TAG', action='append', dest='tags', default=[], help='define tag: include "only" blocks with TAG') group.add_option('-n', action='store_true', dest='nitpicky', help='nit-picky mode, warn about all missing references') group = parser.add_option_group('Console output options') group.add_option('-v', action='count', dest='verbosity', default=0, help='increase verbosity (can be repeated)') group.add_option('-q', action='store_true', dest='quiet', help='no output on stdout, just warnings on stderr') group.add_option('-Q', action='store_true', dest='really_quiet', help='no output at all, not even warnings') group.add_option('--color', dest='color', action='store_const', const='yes', default='auto', help='Do emit colored output (default: auto-detect)') group.add_option('-N', '--no-color', dest='color', action='store_const', const='no', help='Do not emit colored output (default: auot-detect)') group.add_option('-w', metavar='FILE', dest='warnfile', help='write warnings (and errors) to given file') group.add_option('-W', action='store_true', dest='warningiserror', help='turn warnings into errors') group.add_option('-T', action='store_true', dest='traceback', help='show full traceback on exception') group.add_option('-P', action='store_true', dest='pdb', help='run Pdb on exception') # parse options try: opts, args = parser.parse_args(list(argv[1:])) except SystemExit as err: return err.code # handle basic options if opts.version: print('Sphinx (sphinx-build) %s' % __display_version__) return 0 # get paths (first and second positional argument) try: srcdir = abspath(args[0]) confdir = abspath(opts.confdir or srcdir) if opts.noconfig: confdir = None if not path.isdir(srcdir): print('Error: Cannot find source directory `%s\'.' % srcdir, file=sys.stderr) return 1 if not opts.noconfig and not path.isfile(path.join(confdir, 'conf.py')): print('Error: Config directory doesn\'t contain a conf.py file.', file=sys.stderr) return 1 outdir = abspath(args[1]) if srcdir == outdir: print('Error: source directory and destination directory are same.', file=sys.stderr) return 1 except IndexError: parser.print_help() return 1 except UnicodeError: print( 'Error: Multibyte filename not supported on this filesystem ' 'encoding (%r).' % fs_encoding, file=sys.stderr) return 1 # handle remaining filename arguments filenames = args[2:] errored = False for filename in filenames: if not path.isfile(filename): print('Error: Cannot find file %r.' % filename, file=sys.stderr) errored = True if errored: return 1 # likely encoding used for command-line arguments try: locale = __import__('locale') # due to submodule of the same name likely_encoding = locale.getpreferredencoding() except Exception: likely_encoding = None if opts.force_all and filenames: print('Error: Cannot combine -a option and filenames.', file=sys.stderr) return 1 if opts.color == 'no' or (opts.color == 'auto' and not color_terminal()): nocolor() doctreedir = abspath(opts.doctreedir or path.join(outdir, '.doctrees')) status = sys.stdout warning = sys.stderr error = sys.stderr if opts.quiet: status = None if opts.really_quiet: status = warning = None if warning and opts.warnfile: try: warnfp = open(opts.warnfile, 'w') except Exception as exc: print('Error: Cannot open warning file %r: %s' % (opts.warnfile, exc), file=sys.stderr) sys.exit(1) warning = Tee(warning, warnfp) # type: ignore error = warning confoverrides = {} for val in opts.define: try: key, val = val.split('=', 1) except ValueError: print('Error: -D option argument must be in the form name=value.', file=sys.stderr) return 1 if likely_encoding and isinstance(val, binary_type): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides[key] = val for val in opts.htmldefine: try: key, val = val.split('=') except ValueError: print('Error: -A option argument must be in the form name=value.', file=sys.stderr) return 1 try: val = int(val) except ValueError: if likely_encoding and isinstance(val, binary_type): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides['html_context.%s' % key] = val if opts.nitpicky: confoverrides['nitpicky'] = True app = None try: with docutils_namespace(): app = Sphinx(srcdir, confdir, outdir, doctreedir, opts.builder, confoverrides, status, warning, opts.freshenv, opts.warningiserror, opts.tags, opts.verbosity, opts.jobs) app.build(opts.force_all, filenames) return app.statuscode except (Exception, KeyboardInterrupt) as exc: handle_exception(app, opts, exc, error) return 1
def main(argv): if not color_terminal(): nocolor() parser = optparse.OptionParser(USAGE, epilog=EPILOG, formatter=MyFormatter()) parser.add_option("--version", action="store_true", dest="version", help="show version information and exit") group = parser.add_option_group("General options") group.add_option("-b", metavar="BUILDER", dest="builder", default="html", help="builder to use; default is html") group.add_option( "-a", action="store_true", dest="force_all", help="write all files; default is to only write new and " "changed files", ) group.add_option( "-E", action="store_true", dest="freshenv", help="don't use a saved environment, always read " "all files" ) group.add_option( "-d", metavar="PATH", default=None, dest="doctreedir", help="path for the cached environment and doctree files " "(default: outdir/.doctrees)", ) group.add_option( "-j", metavar="N", default=1, type="int", dest="jobs", help="build in parallel with N processes where possible" ) # this option never gets through to this point (it is intercepted earlier) # group.add_option('-M', metavar='BUILDER', dest='make_mode', # help='"make" mode -- as used by Makefile, like ' # '"sphinx-build -M html"') group = parser.add_option_group("Build configuration options") group.add_option( "-c", metavar="PATH", dest="confdir", help="path where configuration file (conf.py) is located " "(default: same as sourcedir)", ) group.add_option("-C", action="store_true", dest="noconfig", help="use no config file at all, only -D options") group.add_option( "-D", metavar="setting=value", action="append", dest="define", default=[], help="override a setting in configuration file", ) group.add_option( "-A", metavar="name=value", action="append", dest="htmldefine", default=[], help="pass a value into HTML templates", ) group.add_option( "-t", metavar="TAG", action="append", dest="tags", default=[], help='define tag: include "only" blocks with TAG' ) group.add_option( "-n", action="store_true", dest="nitpicky", help="nit-picky mode, warn about all missing references" ) group = parser.add_option_group("Console output options") group.add_option("-v", action="count", dest="verbosity", default=0, help="increase verbosity (can be repeated)") group.add_option("-q", action="store_true", dest="quiet", help="no output on stdout, just warnings on stderr") group.add_option("-Q", action="store_true", dest="really_quiet", help="no output at all, not even warnings") group.add_option("-N", action="store_true", dest="nocolor", help="do not emit colored output") group.add_option("-w", metavar="FILE", dest="warnfile", help="write warnings (and errors) to given file") group.add_option("-W", action="store_true", dest="warningiserror", help="turn warnings into errors") group.add_option("-T", action="store_true", dest="traceback", help="show full traceback on exception") group.add_option("-P", action="store_true", dest="pdb", help="run Pdb on exception") # parse options try: opts, args = parser.parse_args(list(argv[1:])) except SystemExit as err: return err.code # handle basic options if opts.version: print("Sphinx (sphinx-build) %s" % __display_version__) return 0 # get paths (first and second positional argument) try: srcdir = abspath(args[0]) confdir = abspath(opts.confdir or srcdir) if opts.noconfig: confdir = None if not path.isdir(srcdir): print("Error: Cannot find source directory `%s'." % srcdir, file=sys.stderr) return 1 if not opts.noconfig and not path.isfile(path.join(confdir, "conf.py")): print("Error: Config directory doesn't contain a conf.py file.", file=sys.stderr) return 1 outdir = abspath(args[1]) if srcdir == outdir: print("Error: source directory and destination directory are same.", file=sys.stderr) return 1 except IndexError: parser.print_help() return 1 except UnicodeError: print( "Error: Multibyte filename not supported on this filesystem " "encoding (%r)." % fs_encoding, file=sys.stderr, ) return 1 # handle remaining filename arguments filenames = args[2:] err = 0 for filename in filenames: if not path.isfile(filename): print("Error: Cannot find file %r." % filename, file=sys.stderr) err = 1 if err: return 1 # likely encoding used for command-line arguments try: locale = __import__("locale") # due to submodule of the same name likely_encoding = locale.getpreferredencoding() except Exception: likely_encoding = None if opts.force_all and filenames: print("Error: Cannot combine -a option and filenames.", file=sys.stderr) return 1 if opts.nocolor: nocolor() doctreedir = abspath(opts.doctreedir or path.join(outdir, ".doctrees")) status = sys.stdout warning = sys.stderr error = sys.stderr if opts.quiet: status = None if opts.really_quiet: status = warning = None if warning and opts.warnfile: try: warnfp = open(opts.warnfile, "w") except Exception as exc: print("Error: Cannot open warning file %r: %s" % (opts.warnfile, exc), file=sys.stderr) sys.exit(1) warning = Tee(warning, warnfp) error = warning confoverrides = {} for val in opts.define: try: key, val = val.split("=") except ValueError: print("Error: -D option argument must be in the form name=value.", file=sys.stderr) return 1 if likely_encoding and isinstance(val, binary_type): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides[key] = val for val in opts.htmldefine: try: key, val = val.split("=") except ValueError: print("Error: -A option argument must be in the form name=value.", file=sys.stderr) return 1 try: val = int(val) except ValueError: if likely_encoding and isinstance(val, binary_type): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides["html_context.%s" % key] = val if opts.nitpicky: confoverrides["nitpicky"] = True app = None try: with docutils_namespace(): app = Sphinx( srcdir, confdir, outdir, doctreedir, opts.builder, confoverrides, status, warning, opts.freshenv, opts.warningiserror, opts.tags, opts.verbosity, opts.jobs, ) app.build(opts.force_all, filenames) return app.statuscode except (Exception, KeyboardInterrupt) as exc: handle_exception(app, opts, exc, error) return 1
warnfile = None confoverrides = {} tags = [] doctreedir = path.join(outdir, ".doctrees") for opt, val in opts: if opt == "-b": buildername = val elif opt == "-a": if filenames: usage(argv, "Error: Cannot combine -a option and filenames.") return 1 force_all = True elif opt == "-t": tags.append(val) elif opt == "-d": doctreedir = abspath(val) elif opt == "-c": confdir = abspath(val) if not path.isfile(path.join(confdir, "conf.py")): print >> sys.stderr, ("Error: Configuration directory " "doesn't contain conf.py file.") return 1 elif opt == "-C": confdir = None elif opt == "-D": try: key, val = val.split("=") except ValueError: print >> sys.stderr, ("Error: -D option argument must be " "in the form name=value.") return 1 try: val = int(val)
def main(argv): if not color_terminal(): nocolor() parser = optparse.OptionParser(USAGE, epilog=EPILOG, formatter=MyFormatter()) parser.add_option('--version', action='store_true', dest='version', help='show version information and exit') group = parser.add_option_group('General options') group.add_option('-b', metavar='BUILDER', dest='builder', default='html', help='builder to use; default is html') group.add_option('-a', action='store_true', dest='force_all', help='write all files; default is to only write new and ' 'changed files') group.add_option('-E', action='store_true', dest='freshenv', help='don\'t use a saved environment, always read ' 'all files') group.add_option('-d', metavar='PATH', default=None, dest='doctreedir', help='path for the cached environment and doctree files ' '(default: outdir/.doctrees)') group.add_option('-j', metavar='N', default=1, type='int', dest='jobs', help='build in parallel with N processes where possible') # this option never gets through to this point (it is intercepted earlier) # group.add_option('-M', metavar='BUILDER', dest='make_mode', # help='"make" mode -- as used by Makefile, like ' # '"sphinx-build -M html"') group = parser.add_option_group('Build configuration options') group.add_option('-c', metavar='PATH', dest='confdir', help='path where configuration file (conf.py) is located ' '(default: same as sourcedir)') group.add_option('-C', action='store_true', dest='noconfig', help='use no config file at all, only -D options') group.add_option('-D', metavar='setting=value', action='append', dest='define', default=[], help='override a setting in configuration file') group.add_option('-A', metavar='name=value', action='append', dest='htmldefine', default=[], help='pass a value into HTML templates') group.add_option('-t', metavar='TAG', action='append', dest='tags', default=[], help='define tag: include "only" blocks with TAG') group.add_option('-n', action='store_true', dest='nitpicky', help='nit-picky mode, warn about all missing references') group = parser.add_option_group('Console output options') group.add_option('-v', action='count', dest='verbosity', default=0, help='increase verbosity (can be repeated)') group.add_option('-q', action='store_true', dest='quiet', help='no output on stdout, just warnings on stderr') group.add_option('-Q', action='store_true', dest='really_quiet', help='no output at all, not even warnings') group.add_option('-N', action='store_true', dest='nocolor', help='do not emit colored output') group.add_option('-w', metavar='FILE', dest='warnfile', help='write warnings (and errors) to given file') group.add_option('-W', action='store_true', dest='warningiserror', help='turn warnings into errors') group.add_option('-T', action='store_true', dest='traceback', help='show full traceback on exception') group.add_option('-P', action='store_true', dest='pdb', help='run Pdb on exception') # parse options try: opts, args = parser.parse_args(argv[1:]) except SystemExit as err: return err.code # handle basic options if opts.version: print('Sphinx (sphinx-build) %s' % __display_version__) return 0 # get paths (first and second positional argument) try: srcdir = abspath(args[0]) confdir = abspath(opts.confdir or srcdir) if opts.noconfig: confdir = None if not path.isdir(srcdir): print('Error: Cannot find source directory `%s\'.' % srcdir, file=sys.stderr) return 1 if not opts.noconfig and not path.isfile(path.join(confdir, 'conf.py')): print('Error: Config directory doesn\'t contain a conf.py file.', file=sys.stderr) return 1 outdir = abspath(args[1]) except IndexError: parser.print_help() return 1 except UnicodeError: print('Error: Multibyte filename not supported on this filesystem ' 'encoding (%r).' % fs_encoding, file=sys.stderr) return 1 # handle remaining filename arguments filenames = args[2:] err = 0 for filename in filenames: if not path.isfile(filename): print('Error: Cannot find file %r.' % filename, file=sys.stderr) err = 1 if err: return 1 # likely encoding used for command-line arguments try: locale = __import__('locale') # due to submodule of the same name likely_encoding = locale.getpreferredencoding() except Exception: likely_encoding = None if opts.force_all and filenames: print('Error: Cannot combine -a option and filenames.', file=sys.stderr) return 1 if opts.nocolor: nocolor() doctreedir = abspath(opts.doctreedir or path.join(outdir, '.doctrees')) status = sys.stdout warning = sys.stderr error = sys.stderr if opts.quiet: status = None if opts.really_quiet: status = warning = None if warning and opts.warnfile: try: warnfp = open(opts.warnfile, 'w') except Exception as exc: print('Error: Cannot open warning file %r: %s' % (opts.warnfile, exc), file=sys.stderr) sys.exit(1) warning = Tee(warning, warnfp) error = warning confoverrides = {} for val in opts.define: try: key, val = val.split('=') except ValueError: print('Error: -D option argument must be in the form name=value.', file=sys.stderr) return 1 if likely_encoding and isinstance(val, binary_type): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides[key] = val for val in opts.htmldefine: try: key, val = val.split('=') except ValueError: print('Error: -A option argument must be in the form name=value.', file=sys.stderr) return 1 try: val = int(val) except ValueError: if likely_encoding and isinstance(val, binary_type): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides['html_context.%s' % key] = val if opts.nitpicky: confoverrides['nitpicky'] = True app = None try: app = Sphinx(srcdir, confdir, outdir, doctreedir, opts.builder, confoverrides, status, warning, opts.freshenv, opts.warningiserror, opts.tags, opts.verbosity, opts.jobs) app.build(opts.force_all, filenames) return app.statuscode except (Exception, KeyboardInterrupt) as err: if opts.pdb: import pdb print(red('Exception occurred while building, starting debugger:'), file=error) traceback.print_exc() pdb.post_mortem(sys.exc_info()[2]) else: print(file=error) if opts.verbosity or opts.traceback: traceback.print_exc(None, error) print(file=error) if isinstance(err, KeyboardInterrupt): print('interrupted!', file=error) elif isinstance(err, SystemMessage): print(red('reST markup error:'), file=error) print(terminal_safe(err.args[0]), file=error) elif isinstance(err, SphinxError): print(red('%s:' % err.category), file=error) print(terminal_safe(text_type(err)), file=error) elif isinstance(err, UnicodeError): print(red('Encoding error:'), file=error) print(terminal_safe(text_type(err)), file=error) tbpath = save_traceback(app) print(red( 'The full traceback has been saved in %s, if you want ' 'to report the issue to the developers.' % tbpath), file=error) else: print(red('Exception occurred:'), file=error) print(format_exception_cut_frames().rstrip(), file=error) tbpath = save_traceback(app) print(red('The full traceback has been saved in %s, if you ' 'want to report the issue to the developers.' % tbpath), file=error) print( 'Please also report this if it was a user error, so ' 'that a better error message can be provided next time.', file=error) print( 'A bug report can be filed in the tracker at ' '<https://github.com/sphinx-doc/sphinx/issues>. Thanks!', file=error) return 1
def main(argv): if not color_terminal(): # Windows' poor cmd box doesn't understand ANSI sequences nocolor() # parse options try: opts, args = getopt.getopt(argv[1:], 'ab:t:d:c:CD:A:nNEqQWw:PThvj:', ['help', 'version']) except getopt.error as err: usage(argv, 'Error: %s' % err) return 1 # handle basic options allopts = set(opt[0] for opt in opts) # help and version options if '-h' in allopts or '--help' in allopts: usage(argv) print(file=sys.stderr) print('For more information, see <http://sphinx-doc.org/>.', file=sys.stderr) return 0 if '--version' in allopts: print('Sphinx (sphinx-build) %s' % __version__) return 0 # get paths (first and second positional argument) try: srcdir = confdir = abspath(args[0]) if not path.isdir(srcdir): print('Error: Cannot find source directory `%s\'.' % srcdir, file=sys.stderr) return 1 if not path.isfile(path.join(srcdir, 'conf.py')) and \ '-c' not in allopts and '-C' not in allopts: print('Error: Source directory doesn\'t contain a conf.py file.', file=sys.stderr) return 1 outdir = abspath(args[1]) except IndexError: usage(argv, 'Error: Insufficient arguments.') return 1 except UnicodeError: print( 'Error: Multibyte filename not supported on this filesystem ' 'encoding (%r).' % fs_encoding, file=sys.stderr) return 1 # handle remaining filename arguments filenames = args[2:] err = 0 for filename in filenames: if not path.isfile(filename): print('Error: Cannot find file %r.' % filename, file=sys.stderr) err = 1 if err: return 1 # likely encoding used for command-line arguments try: locale = __import__('locale') # due to submodule of the same name likely_encoding = locale.getpreferredencoding() except Exception: likely_encoding = None buildername = None force_all = freshenv = warningiserror = use_pdb = False show_traceback = False verbosity = 0 parallel = 0 status = sys.stdout warning = sys.stderr error = sys.stderr warnfile = None confoverrides = {} tags = [] doctreedir = path.join(outdir, '.doctrees') for opt, val in opts: if opt == '-b': buildername = val elif opt == '-a': if filenames: usage(argv, 'Error: Cannot combine -a option and filenames.') return 1 force_all = True elif opt == '-t': tags.append(val) elif opt == '-d': doctreedir = abspath(val) elif opt == '-c': confdir = abspath(val) if not path.isfile(path.join(confdir, 'conf.py')): print('Error: Configuration directory doesn\'t contain conf.py file.', file=sys.stderr) return 1 elif opt == '-C': confdir = None elif opt == '-D': try: key, val = val.split('=') except ValueError: print('Error: -D option argument must be in the form name=value.', file=sys.stderr) return 1 if likely_encoding and isinstance(val, binary_type): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides[key] = val elif opt == '-A': try: key, val = val.split('=') except ValueError: print('Error: -A option argument must be in the form name=value.', file=sys.stderr) return 1 try: val = int(val) except ValueError: if likely_encoding and isinstance(val, binary_type): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides['html_context.%s' % key] = val elif opt == '-n': confoverrides['nitpicky'] = True elif opt == '-N': nocolor() elif opt == '-E': freshenv = True elif opt == '-q': status = None elif opt == '-Q': status = None warning = None elif opt == '-W': warningiserror = True elif opt == '-w': warnfile = val elif opt == '-P': use_pdb = True elif opt == '-T': show_traceback = True elif opt == '-v': verbosity += 1 show_traceback = True elif opt == '-j': try: parallel = int(val) except ValueError: print('Error: -j option argument must be an integer.', file=sys.stderr) return 1 if warning and warnfile: warnfp = open(warnfile, 'w') warning = Tee(warning, warnfp) error = warning if not path.isdir(outdir): if status: print('Making output directory...', file=status) os.makedirs(outdir) app = None try: app = Sphinx(srcdir, confdir, outdir, doctreedir, buildername, confoverrides, status, warning, freshenv, warningiserror, tags, verbosity, parallel) app.build(force_all, filenames) return app.statuscode except (Exception, KeyboardInterrupt) as err: if use_pdb: import pdb print(red('Exception occurred while building, starting debugger:'), file=error) traceback.print_exc() pdb.post_mortem(sys.exc_info()[2]) else: print(file=error) if show_traceback: traceback.print_exc(None, error) print(file=error) if isinstance(err, KeyboardInterrupt): print('interrupted!', file=error) elif isinstance(err, SystemMessage): print(red('reST markup error:'), file=error) print(terminal_safe(err.args[0]), file=error) elif isinstance(err, SphinxError): print(red('%s:' % err.category), file=error) print(terminal_safe(text_type(err)), file=error) elif isinstance(err, UnicodeError): print(red('Encoding error:'), file=error) print(terminal_safe(text_type(err)), file=error) tbpath = save_traceback(app) print(red('The full traceback has been saved in %s, if you want ' 'to report the issue to the developers.' % tbpath), file=error) else: print(red('Exception occurred:'), file=error) print(format_exception_cut_frames().rstrip(), file=error) tbpath = save_traceback(app) print(red('The full traceback has been saved in %s, if you ' 'want to report the issue to the developers.' % tbpath), file=error) print('Please also report this if it was a user error, so ' 'that a better error message can be provided next time.', file=error) print('A bug report can be filed in the tracker at ' '<https://bitbucket.org/birkenfeld/sphinx/issues/>. Thanks!', file=error) return 1
def __init__(self, srcdir, confdir, outdir, doctreedir, buildername, confoverrides=None, status=sys.stdout, warning=sys.stderr, freshenv=False, warningiserror=False, tags=None, verbosity=0, parallel=0, keep_going=False): # type: (str, str, str, str, str, Dict, IO, IO, bool, bool, List[str], int, int, bool) -> None # NOQA self.phase = BuildPhase.INITIALIZATION self.verbosity = verbosity self.extensions = {} # type: Dict[str, Extension] self.builder = None # type: Builder self.env = None # type: BuildEnvironment self.project = None # type: Project self.registry = SphinxComponentRegistry() self.html_themes = {} # type: Dict[str, str] # validate provided directories self.srcdir = abspath(srcdir) self.outdir = abspath(outdir) self.doctreedir = abspath(doctreedir) self.confdir = confdir if self.confdir: # confdir is optional self.confdir = abspath(self.confdir) if not path.isfile(path.join(self.confdir, 'conf.py')): raise ApplicationError(__("config directory doesn't contain a " "conf.py file (%s)") % confdir) if not path.isdir(self.srcdir): raise ApplicationError(__('Cannot find source directory (%s)') % self.srcdir) if self.srcdir == self.outdir: raise ApplicationError(__('Source directory and destination ' 'directory cannot be identical')) self.parallel = parallel if status is None: self._status = StringIO() # type: IO self.quiet = True else: self._status = status self.quiet = False if warning is None: self._warning = StringIO() # type: IO else: self._warning = warning self._warncount = 0 self.keep_going = warningiserror and keep_going if self.keep_going: self.warningiserror = False else: self.warningiserror = warningiserror logging.setup(self, self._status, self._warning) self.events = EventManager() # keep last few messages for traceback # This will be filled by sphinx.util.logging.LastMessagesWriter self.messagelog = deque(maxlen=10) # type: deque # say hello to the world logger.info(bold(__('Running Sphinx v%s') % sphinx.__display_version__)) # status code for command-line application self.statuscode = 0 # read config self.tags = Tags(tags) if self.confdir is None: self.config = Config({}, confoverrides or {}) else: self.config = Config.read(self.confdir, confoverrides or {}, self.tags) # initialize some limited config variables before initialize i18n and loading # extensions self.config.pre_init_values() # set up translation infrastructure self._init_i18n() # check the Sphinx version if requested if self.config.needs_sphinx and self.config.needs_sphinx > sphinx.__display_version__: raise VersionRequirementError( __('This project needs at least Sphinx v%s and therefore cannot ' 'be built with this version.') % self.config.needs_sphinx) # set confdir to srcdir if -C given (!= no confdir); a few pieces # of code expect a confdir to be set if self.confdir is None: self.confdir = self.srcdir # load all built-in extension modules for extension in builtin_extensions: self.setup_extension(extension) # load all user-given extension modules for extension in self.config.extensions: self.setup_extension(extension) # preload builder module (before init config values) self.preload_builder(buildername) if not path.isdir(outdir): with progress_message(__('making output directory')): ensuredir(outdir) # the config file itself can be an extension if self.config.setup: prefix = __('while setting up extension %s:') % "conf.py" with prefixed_warnings(prefix): if callable(self.config.setup): self.config.setup(self) else: raise ConfigError( __("'setup' as currently defined in conf.py isn't a Python callable. " "Please modify its definition to make it a callable function. " "This is needed for conf.py to behave as a Sphinx extension.") ) # now that we know all config values, collect them from conf.py self.config.init_values() self.emit('config-inited', self.config) # create the project self.project = Project(self.srcdir, self.config.source_suffix) # create the builder self.builder = self.create_builder(buildername) # set up the build environment self._init_env(freshenv) # set up the builder self._init_builder()
def main(argv=sys.argv[1:]): # type: ignore # type: (List[unicode]) -> int parser = get_parser() args = parser.parse_args(argv) # get paths (first and second positional argument) try: srcdir = abspath(args.sourcedir) confdir = abspath(args.confdir or srcdir) if args.noconfig: confdir = None if not path.isdir(srcdir): parser.error('cannot find source directory (%s)' % srcdir) if not args.noconfig and not path.isfile(path.join(confdir, 'conf.py')): parser.error("config directory doesn't contain a conf.py file " "(%s)" % confdir) outdir = abspath(args.outputdir) if srcdir == outdir: parser.error('source directory and destination directory are same') except UnicodeError: parser.error('multibyte filename not supported on this filesystem ' 'encoding (%r)' % fs_encoding) # handle remaining filename arguments filenames = args.filenames missing_files = [] for filename in filenames: if not path.isfile(filename): missing_files.append(filename) if missing_files: parser.error('cannot find files %r' % missing_files) # likely encoding used for command-line arguments try: locale = __import__('locale') # due to submodule of the same name likely_encoding = locale.getpreferredencoding() except Exception: likely_encoding = None if args.force_all and filenames: parser.error('cannot combine -a option and filenames') if args.color == 'no' or (args.color == 'auto' and not color_terminal()): nocolor() doctreedir = abspath(args.doctreedir or path.join(outdir, '.doctrees')) status = sys.stdout warning = sys.stderr error = sys.stderr if args.quiet: status = None if args.really_quiet: status = warning = None if warning and args.warnfile: try: warnfp = open(args.warnfile, 'w') except Exception as exc: parser.error('cannot open warning file %r: %s' % (args.warnfile, exc)) warning = Tee(warning, warnfp) # type: ignore error = warning confoverrides = {} for val in args.define: try: key, val = val.split('=', 1) except ValueError: parser.error('-D option argument must be in the form name=value') if likely_encoding and isinstance(val, binary_type): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides[key] = val for val in args.htmldefine: try: key, val = val.split('=') except ValueError: parser.error('-A option argument must be in the form name=value') try: val = int(val) except ValueError: if likely_encoding and isinstance(val, binary_type): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides['html_context.%s' % key] = val if args.nitpicky: confoverrides['nitpicky'] = True app = None try: with patch_docutils(), docutils_namespace(): app = Sphinx(srcdir, confdir, outdir, doctreedir, args.builder, confoverrides, status, warning, args.freshenv, args.warningiserror, args.tags, args.verbosity, args.jobs) app.build(args.force_all, filenames) return app.statuscode except (Exception, KeyboardInterrupt) as exc: handle_exception(app, args, exc, error) return 2
def __init__(self, srcdir, confdir, outdir, doctreedir, buildername, confoverrides=None, status=sys.stdout, warning=sys.stderr, freshenv=False, warningiserror=False, tags=None, verbosity=0, parallel=0): # type: (unicode, unicode, unicode, unicode, unicode, Dict, IO, IO, bool, bool, List[unicode], int, int) -> None # NOQA self.verbosity = verbosity self.extensions = {} # type: Dict[unicode, Extension] self._setting_up_extension = ['?'] # type: List[unicode] self.builder = None # type: Builder self.env = None # type: BuildEnvironment self.registry = SphinxComponentRegistry() self.enumerable_nodes = {} # type: Dict[nodes.Node, Tuple[unicode, Callable]] # NOQA self.html_themes = {} # type: Dict[unicode, unicode] # validate provided directories self.srcdir = abspath(srcdir) self.outdir = abspath(outdir) self.doctreedir = abspath(doctreedir) self.confdir = confdir if self.confdir: # confdir is optional self.confdir = abspath(self.confdir) if not path.isfile(path.join(self.confdir, 'conf.py')): raise ApplicationError("config directory doesn't contain a " "conf.py file (%s)" % confdir) if not path.isdir(self.srcdir): raise ApplicationError('Cannot find source directory (%s)' % self.srcdir) if self.srcdir == self.outdir: raise ApplicationError('Source directory and destination ' 'directory cannot be identical') self.parallel = parallel if status is None: self._status = cStringIO() # type: IO self.quiet = True else: self._status = status self.quiet = False if warning is None: self._warning = cStringIO() # type: IO else: self._warning = warning self._warncount = 0 self.warningiserror = warningiserror logging.setup(self, self._status, self._warning) self.events = EventManager() # keep last few messages for traceback # This will be filled by sphinx.util.logging.LastMessagesWriter self.messagelog = deque(maxlen=10) # type: deque # say hello to the world logger.info(bold('Running Sphinx v%s' % sphinx.__display_version__)) # status code for command-line application self.statuscode = 0 # read config self.tags = Tags(tags) self.config = Config(confdir, CONFIG_FILENAME, confoverrides or {}, self.tags) self.config.check_unicode() # defer checking types until i18n has been initialized # initialize some limited config variables before initialize i18n and loading # extensions self.config.pre_init_values() # set up translation infrastructure self._init_i18n() # check the Sphinx version if requested if self.config.needs_sphinx and self.config.needs_sphinx > sphinx.__display_version__: raise VersionRequirementError( __('This project needs at least Sphinx v%s and therefore cannot ' 'be built with this version.') % self.config.needs_sphinx) # set confdir to srcdir if -C given (!= no confdir); a few pieces # of code expect a confdir to be set if self.confdir is None: self.confdir = self.srcdir # load all built-in extension modules for extension in builtin_extensions: self.setup_extension(extension) # load all user-given extension modules for extension in self.config.extensions: self.setup_extension(extension) # preload builder module (before init config values) self.preload_builder(buildername) if not path.isdir(outdir): logger.info('making output directory...') ensuredir(outdir) # the config file itself can be an extension if self.config.setup: self._setting_up_extension = ['conf.py'] if callable(self.config.setup): self.config.setup(self) else: raise ConfigError( __("'setup' as currently defined in conf.py isn't a Python callable. " "Please modify its definition to make it a callable function. This is " "needed for conf.py to behave as a Sphinx extension.") ) # now that we know all config values, collect them from conf.py self.config.init_values() self.emit('config-inited', self.config) # check primary_domain if requested primary_domain = self.config.primary_domain if primary_domain and not self.registry.has_domain(primary_domain): logger.warning(__('primary_domain %r not found, ignored.'), primary_domain) # create the builder self.builder = self.create_builder(buildername) # check all configuration values for permissible types self.config.check_types() # set up the build environment self._init_env(freshenv) # set up the builder self._init_builder() # set up the enumerable nodes self._init_enumerable_nodes()
# handle basic options allopts = set(opt[0] for opt in opts) # help and version options if '-h' in allopts or '--help' in allopts: usage(argv) print(file=sys.stderr) print('For more information, see <http://sphinx-doc.org/>.', file=sys.stderr) return 0 if '--version' in allopts: print('Sphinx (sphinx-build) %s' % __version__) return 0 # get paths (first and second positional argument) try: srcdir = confdir = abspath(args[0]) if not path.isdir(srcdir): print('Error: Cannot find source directory `%s\'.' % srcdir, file=sys.stderr) return 1 if not path.isfile(path.join(srcdir, 'conf.py')) and \ '-c' not in allopts and '-C' not in allopts: print('Error: Source directory doesn\'t contain a conf.py file.', file=sys.stderr) return 1 outdir = abspath(args[1]) except IndexError: usage(argv, 'Error: Insufficient arguments.') return 1 except UnicodeError: print(
def main(argv): if not color_terminal(): nocolor() parser = optparse.OptionParser(USAGE, epilog=EPILOG, formatter=MyFormatter()) parser.add_option('--version', action='store_true', dest='version', help='show version information and exit') group = parser.add_option_group('General options') group.add_option('-b', metavar='BUILDER', dest='builder', default='html', help='builder to use; default is html') group.add_option('-a', action='store_true', dest='force_all', help='write all files; default is to only write new and ' 'changed files') group.add_option('-E', action='store_true', dest='freshenv', help='don\'t use a saved environment, always read ' 'all files') group.add_option('-d', metavar='PATH', default=None, dest='doctreedir', help='path for the cached environment and doctree files ' '(default: outdir/.doctrees)') group.add_option('-j', metavar='N', default=1, type='int', dest='jobs', help='build in parallel with N processes where possible') # this option never gets through to this point (it is intercepted earlier) # group.add_option('-M', metavar='BUILDER', dest='make_mode', # help='"make" mode -- as used by Makefile, like ' # '"sphinx-build -M html"') group = parser.add_option_group('Build configuration options') group.add_option('-c', metavar='PATH', dest='confdir', help='path where configuration file (conf.py) is located ' '(default: same as sourcedir)') group.add_option('-C', action='store_true', dest='noconfig', help='use no config file at all, only -D options') group.add_option('-D', metavar='setting=value', action='append', dest='define', default=[], help='override a setting in configuration file') group.add_option('-A', metavar='name=value', action='append', dest='htmldefine', default=[], help='pass a value into HTML templates') group.add_option('-t', metavar='TAG', action='append', dest='tags', default=[], help='define tag: include "only" blocks with TAG') group.add_option('-n', action='store_true', dest='nitpicky', help='nit-picky mode, warn about all missing references') group = parser.add_option_group('Console output options') group.add_option('-v', action='count', dest='verbosity', default=0, help='increase verbosity (can be repeated)') group.add_option('-q', action='store_true', dest='quiet', help='no output on stdout, just warnings on stderr') group.add_option('-Q', action='store_true', dest='really_quiet', help='no output at all, not even warnings') group.add_option('-N', action='store_true', dest='nocolor', help='do not emit colored output') group.add_option('-w', metavar='FILE', dest='warnfile', help='write warnings (and errors) to given file') group.add_option('-W', action='store_true', dest='warningiserror', help='turn warnings into errors') group.add_option('-T', action='store_true', dest='traceback', help='show full traceback on exception') group.add_option('-P', action='store_true', dest='pdb', help='run Pdb on exception') # parse options try: opts, args = parser.parse_args(argv[1:]) except SystemExit as err: return err.code # handle basic options if opts.version: print('Sphinx (sphinx-build) %s' % __version__) return 0 # get paths (first and second positional argument) try: srcdir = abspath(args[0]) confdir = abspath(opts.confdir or srcdir) if opts.noconfig: confdir = None if not path.isdir(srcdir): print('Error: Cannot find source directory `%s\'.' % srcdir, file=sys.stderr) return 1 if not opts.noconfig and not path.isfile(path.join(confdir, 'conf.py')): print('Error: Config directory doesn\'t contain a conf.py file.', file=sys.stderr) return 1 outdir = abspath(args[1]) except IndexError: usage(argv, 'Error: Insufficient arguments.') return 1 except UnicodeError: print( 'Error: Multibyte filename not supported on this filesystem ' 'encoding (%r).' % fs_encoding, file=sys.stderr) return 1 # handle remaining filename arguments filenames = args[2:] err = 0 for filename in filenames: if not path.isfile(filename): print('Error: Cannot find file %r.' % filename, file=sys.stderr) err = 1 if err: return 1 # likely encoding used for command-line arguments try: locale = __import__('locale') # due to submodule of the same name likely_encoding = locale.getpreferredencoding() except Exception: likely_encoding = None if opts.force_all and filenames: print('Error: Cannot combine -a option and filenames.', file=sys.stderr) return 1 if opts.nocolor: nocolor() doctreedir = abspath(opts.doctreedir or path.join(outdir, '.doctrees')) status = sys.stdout warning = sys.stderr error = sys.stderr if opts.quiet: status = None if opts.really_quiet: status = warning = None if warning and opts.warnfile: try: warnfp = open(opts.warnfile, 'w') except Exception as exc: print('Error: Cannot open warning file %r: %s' % (opts.warnfile, exc), file=sys.stderr) sys.exit(1) warning = Tee(warning, warnfp) error = warning confoverrides = {} for val in opts.define: try: key, val = val.split('=') except ValueError: print('Error: -D option argument must be in the form name=value.', file=sys.stderr) return 1 if likely_encoding and isinstance(val, binary_type): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides[key] = val for val in opts.htmldefine: try: key, val = val.split('=') except ValueError: print('Error: -A option argument must be in the form name=value.', file=sys.stderr) return 1 try: val = int(val) except ValueError: if likely_encoding and isinstance(val, binary_type): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides['html_context.%s' % key] = val if opts.nitpicky: confoverrides['nitpicky'] = True app = None try: app = Sphinx(srcdir, confdir, outdir, doctreedir, opts.builder, confoverrides, status, warning, opts.freshenv, opts.warningiserror, opts.tags, opts.verbosity, opts.jobs) app.build(opts.force_all, filenames) return app.statuscode except (Exception, KeyboardInterrupt) as err: if opts.pdb: import pdb print(red('Exception occurred while building, starting debugger:'), file=error) traceback.print_exc() pdb.post_mortem(sys.exc_info()[2]) else: print(file=error) if opts.verbosity or opts.traceback: traceback.print_exc(None, error) print(file=error) if isinstance(err, KeyboardInterrupt): print('interrupted!', file=error) elif isinstance(err, SystemMessage): print(red('reST markup error:'), file=error) print(terminal_safe(err.args[0]), file=error) elif isinstance(err, SphinxError): print(red('%s:' % err.category), file=error) print(terminal_safe(text_type(err)), file=error) elif isinstance(err, UnicodeError): print(red('Encoding error:'), file=error) print(terminal_safe(text_type(err)), file=error) tbpath = save_traceback(app) print(red('The full traceback has been saved in %s, if you want ' 'to report the issue to the developers.' % tbpath), file=error) else: print(red('Exception occurred:'), file=error) print(format_exception_cut_frames().rstrip(), file=error) tbpath = save_traceback(app) print(red('The full traceback has been saved in %s, if you ' 'want to report the issue to the developers.' % tbpath), file=error) print('Please also report this if it was a user error, so ' 'that a better error message can be provided next time.', file=error) print('A bug report can be filed in the tracker at ' '<https://bitbucket.org/birkenfeld/sphinx/issues/>. Thanks!', file=error) return 1
def main(argv): if not color_terminal(): # Windows' poor cmd box doesn't understand ANSI sequences nocolor() # parse options try: opts, args = getopt.getopt(argv[1:], 'ab:t:d:c:CD:A:nNEqQWw:PThvj:', ['help', 'version']) except getopt.error as err: usage(argv, 'Error: %s' % err) return 1 # handle basic options allopts = set(opt[0] for opt in opts) # help and version options if '-h' in allopts or '--help' in allopts: usage(argv) print(file=sys.stderr) print('For more information, see <http://sphinx-doc.org/>.', file=sys.stderr) return 0 if '--version' in allopts: print('Sphinx (sphinx-build) %s' % __version__) return 0 # get paths (first and second positional argument) try: srcdir = confdir = abspath(args[0]) if not path.isdir(srcdir): print('Error: Cannot find source directory `%s\'.' % (srcdir, ), file=sys.stderr) return 1 if not path.isfile(path.join(srcdir, 'conf.py')) and \ '-c' not in allopts and '-C' not in allopts: print(('Error: Source directory doesn\'t ' 'contain a conf.py file.'), file=sys.stderr) return 1 outdir = abspath(args[1]) except IndexError: usage(argv, 'Error: Insufficient arguments.') return 1 except UnicodeError: print(('Error: Multibyte filename not supported on this filesystem ' 'encoding (%r).' % fs_encoding), file=sys.stderr) return 1 # handle remaining filename arguments filenames = args[2:] err = 0 for filename in filenames: if not path.isfile(filename): print('Error: Cannot find file %r.' % filename, file=sys.stderr) err = 1 if err: return 1 # likely encoding used for command-line arguments try: locale = __import__('locale') # due to submodule of the same name likely_encoding = locale.getpreferredencoding() except Exception: likely_encoding = None buildername = None force_all = freshenv = warningiserror = use_pdb = False show_traceback = False verbosity = 0 parallel = 0 status = sys.stdout warning = sys.stderr error = sys.stderr warnfile = None confoverrides = {} tags = [] doctreedir = path.join(outdir, '.doctrees') for opt, val in opts: if opt == '-b': buildername = val elif opt == '-a': if filenames: usage(argv, 'Error: Cannot combine -a option and filenames.') return 1 force_all = True elif opt == '-t': tags.append(val) elif opt == '-d': doctreedir = abspath(val) elif opt == '-c': confdir = abspath(val) if not path.isfile(path.join(confdir, 'conf.py')): print(('Error: Configuration directory ' 'doesn\'t contain conf.py file.'), file=sys.stderr) return 1 elif opt == '-C': confdir = None elif opt == '-D': try: key, val = val.split('=') except ValueError: print(('Error: -D option argument must be ' 'in the form name=value.'), file=sys.stderr) return 1 try: val = int(val) except ValueError: if likely_encoding and isinstance(val, bytes): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides[key] = val elif opt == '-A': try: key, val = val.split('=') except ValueError: print(('Error: -A option argument must be ' 'in the form name=value.'), file=sys.stderr) return 1 try: val = int(val) except ValueError: if likely_encoding and isinstance(val, bytes): try: val = val.decode(likely_encoding) except UnicodeError: pass confoverrides['html_context.%s' % key] = val elif opt == '-n': confoverrides['nitpicky'] = True elif opt == '-N': nocolor() elif opt == '-E': freshenv = True elif opt == '-q': status = None elif opt == '-Q': status = None warning = None elif opt == '-W': warningiserror = True elif opt == '-w': warnfile = val elif opt == '-P': use_pdb = True elif opt == '-T': show_traceback = True elif opt == '-v': verbosity += 1 show_traceback = True elif opt == '-j': try: parallel = int(val) except ValueError: print(('Error: -j option argument must be an ' 'integer.'), file=sys.stderr) return 1 if warning and warnfile: warnfp = open(warnfile, 'w') warning = Tee(warning, warnfp) error = warning if not path.isdir(outdir): if status: print('Making output directory...', file=status) os.makedirs(outdir) app = None try: app = Sphinx(srcdir, confdir, outdir, doctreedir, buildername, confoverrides, status, warning, freshenv, warningiserror, tags, verbosity, parallel) app.build(force_all, filenames) return app.statuscode except (Exception, KeyboardInterrupt) as err: if use_pdb: import pdb print(red('Exception occurred while building, ' 'starting debugger:'), file=error) traceback.print_exc() pdb.post_mortem(sys.exc_info()[2]) else: print(file=error) if show_traceback: traceback.print_exc(None, error) print(file=error) if isinstance(err, KeyboardInterrupt): print('interrupted!', file=error) elif isinstance(err, SystemMessage): print(red('reST markup error:'), file=error) print(terminal_safe(err.args[0]), file=error) elif isinstance(err, SphinxError): print(red('%s:' % err.category), file=error) print(terminal_safe(str(err)), file=error) elif isinstance(err, UnicodeError): print(red('Encoding error:'), file=error) print(terminal_safe(str(err)), file=error) tbpath = save_traceback(app) print(red('The full traceback has been saved ' 'in %s, if you want to report the ' 'issue to the developers.' % tbpath), file=error) else: print(red('Exception occurred:'), file=error) print(format_exception_cut_frames().rstrip(), file=error) tbpath = save_traceback(app) print(red('The full traceback has been saved ' 'in %s, if you want to report the ' 'issue to the developers.' % tbpath), file=error) print(('Please also report this if it was a user ' 'error, so that a better error message ' 'can be provided next time.'), file=error) print(( 'Either send bugs to the mailing list at ' '<http://groups.google.com/group/sphinx-users/>,\n' 'or report them in the tracker at ' '<http://bitbucket.org/birkenfeld/sphinx/issues/>. Thanks!' ), file=error) return 1
def __init__(self, srcdir, confdir, outdir, doctreedir, buildername, confoverrides=None, status=sys.stdout, warning=sys.stderr, freshenv=False, warningiserror=False, tags=None, verbosity=0, parallel=0, keep_going=False): # type: (str, str, str, str, str, Dict, IO, IO, bool, bool, List[str], int, int, bool) -> None # NOQA self.phase = BuildPhase.INITIALIZATION self.verbosity = verbosity self.extensions = {} # type: Dict[str, Extension] self.builder = None # type: Builder self.env = None # type: BuildEnvironment self.project = None # type: Project self.registry = SphinxComponentRegistry() self.html_themes = {} # type: Dict[str, str] # validate provided directories self.srcdir = abspath(srcdir) self.outdir = abspath(outdir) self.doctreedir = abspath(doctreedir) self.confdir = confdir if self.confdir: # confdir is optional self.confdir = abspath(self.confdir) if not path.isfile(path.join(self.confdir, 'conf.py')): raise ApplicationError( __("config directory doesn't contain a " "conf.py file (%s)") % confdir) if not path.isdir(self.srcdir): raise ApplicationError( __('Cannot find source directory (%s)') % self.srcdir) if self.srcdir == self.outdir: raise ApplicationError( __('Source directory and destination ' 'directory cannot be identical')) self.parallel = parallel if status is None: self._status = StringIO() # type: IO self.quiet = True else: self._status = status self.quiet = False if warning is None: self._warning = StringIO() # type: IO else: self._warning = warning self._warncount = 0 self.keep_going = warningiserror and keep_going if self.keep_going: self.warningiserror = False else: self.warningiserror = warningiserror logging.setup(self, self._status, self._warning) self.events = EventManager(self) # keep last few messages for traceback # This will be filled by sphinx.util.logging.LastMessagesWriter self.messagelog = deque(maxlen=10) # type: deque # say hello to the world logger.info(bold( __('Running Sphinx v%s') % sphinx.__display_version__)) # notice for parallel build on macOS and py38+ if sys.version_info > ( 3, 8) and platform.system() == 'Darwin' and parallel > 1: logger.info( bold( __("For security reason, parallel mode is disabled on macOS and " "python3.8 and above. For more details, please read " "https://github.com/sphinx-doc/sphinx/issues/6803"))) # status code for command-line application self.statuscode = 0 # read config self.tags = Tags(tags) if self.confdir is None: self.config = Config({}, confoverrides or {}) else: self.config = Config.read(self.confdir, confoverrides or {}, self.tags) # initialize some limited config variables before initialize i18n and loading # extensions self.config.pre_init_values() # set up translation infrastructure self._init_i18n() # check the Sphinx version if requested if self.config.needs_sphinx and self.config.needs_sphinx > sphinx.__display_version__: raise VersionRequirementError( __('This project needs at least Sphinx v%s and therefore cannot ' 'be built with this version.') % self.config.needs_sphinx) # set confdir to srcdir if -C given (!= no confdir); a few pieces # of code expect a confdir to be set if self.confdir is None: self.confdir = self.srcdir # load all built-in extension modules for extension in builtin_extensions: self.setup_extension(extension) # load all user-given extension modules for extension in self.config.extensions: self.setup_extension(extension) # preload builder module (before init config values) self.preload_builder(buildername) if not path.isdir(outdir): with progress_message(__('making output directory')): ensuredir(outdir) # the config file itself can be an extension if self.config.setup: prefix = __('while setting up extension %s:') % "conf.py" with prefixed_warnings(prefix): if callable(self.config.setup): self.config.setup(self) else: raise ConfigError( __("'setup' as currently defined in conf.py isn't a Python callable. " "Please modify its definition to make it a callable function. " "This is needed for conf.py to behave as a Sphinx extension." )) # now that we know all config values, collect them from conf.py self.config.init_values() self.events.emit('config-inited', self.config) # create the project self.project = Project(self.srcdir, self.config.source_suffix) # create the builder self.builder = self.create_builder(buildername) # set up the build environment self._init_env(freshenv) # set up the builder self._init_builder()
# handle basic options allopts = set(opt[0] for opt in opts) # help and version options if '-h' in allopts or '--help' in allopts: usage(argv) print(file=sys.stderr) print('For more information, see <http://sphinx-doc.org/>.', file=sys.stderr) return 0 if '--version' in allopts: print('Sphinx (sphinx-build) %s' % __version__) return 0 # get paths (first and second positional argument) try: srcdir = confdir = abspath(args[0]) if not path.isdir(srcdir): print('Error: Cannot find source directory `%s\'.' % srcdir, file=sys.stderr) return 1 if not path.isfile(path.join(srcdir, 'conf.py')) and \ '-c' not in allopts and '-C' not in allopts: print('Error: Source directory doesn\'t contain a conf.py file.', file=sys.stderr) return 1 outdir = abspath(args[1]) except IndexError: usage(argv, 'Error: Insufficient arguments.') return 1 except UnicodeError: print('Error: Multibyte filename not supported on this filesystem '