From 9098914ce47471d8a368e07ae46ccad2074959b5 Mon Sep 17 00:00:00 2001 From: lethosor Date: Sat, 6 Aug 2022 22:47:38 -0400 Subject: [PATCH] Add --offline option to docs/build.py to disable image downloads --- conf.py | 16 ++++++++++++++++ docs/build.py | 10 +++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/conf.py b/conf.py index 08c2e6bd8..dd7be9792 100644 --- a/conf.py +++ b/conf.py @@ -24,6 +24,22 @@ import sphinx import sys +if os.environ.get('DFHACK_DOCS_BUILD_OFFLINE'): + # block attempted image downloads, particularly for the PDF builder + def request_disabled(*args, **kwargs): + raise RuntimeError('Offline build - network request blocked') + + import urllib3.util + urllib3.util.create_connection = request_disabled + + import urllib3.connection + urllib3.connection.HTTPConnection.connect = request_disabled + + import requests + requests.request = request_disabled + requests.get = request_disabled + + # -- Support :dfhack-keybind:`command` ------------------------------------ # this is a custom directive that pulls info from default keybindings diff --git a/docs/build.py b/docs/build.py index 582279bcb..6a63ed59e 100755 --- a/docs/build.py +++ b/docs/build.py @@ -63,6 +63,8 @@ def parse_args(source_args): help='Number of Sphinx threads to run [environment variable: JOBS; default: "auto"]') parser.add_argument('--debug', action='store_true', help='Log commands that are run, etc.') + parser.add_argument('--offline', action='store_true', + help='Disable network connections') args, forward_args = _parse_known_args(parser, source_args) # work around weirdness with list args @@ -79,6 +81,11 @@ if __name__ == '__main__': exit(1) args, forward_args = parse_args(sys.argv[1:]) + + sphinx_env = os.environ.copy() + if args.offline: + sphinx_env['DFHACK_DOCS_BUILD_OFFLINE'] = '1' + for format_name in args.format: command = [args.sphinx] + OUTPUT_FORMATS[format_name].args + ['-j', args.jobs] if args.clean: @@ -88,5 +95,6 @@ if __name__ == '__main__': if args.debug: print('Building:', format_name) print('Running:', command) - subprocess.call(command) + subprocess.run(command, check=True, env=sphinx_env) + print('')