Add --offline option to docs/build.py to disable image downloads

develop
lethosor 2022-08-06 22:47:38 -04:00
parent 02a8f63ffc
commit 9098914ce4
No known key found for this signature in database
GPG Key ID: 76A269552F4F58C1
2 changed files with 25 additions and 1 deletions

@ -24,6 +24,22 @@ import sphinx
import sys 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` ------------------------------------ # -- Support :dfhack-keybind:`command` ------------------------------------
# this is a custom directive that pulls info from default keybindings # this is a custom directive that pulls info from default keybindings

@ -63,6 +63,8 @@ def parse_args(source_args):
help='Number of Sphinx threads to run [environment variable: JOBS; default: "auto"]') help='Number of Sphinx threads to run [environment variable: JOBS; default: "auto"]')
parser.add_argument('--debug', action='store_true', parser.add_argument('--debug', action='store_true',
help='Log commands that are run, etc.') 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) args, forward_args = _parse_known_args(parser, source_args)
# work around weirdness with list args # work around weirdness with list args
@ -79,6 +81,11 @@ if __name__ == '__main__':
exit(1) exit(1)
args, forward_args = parse_args(sys.argv[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: for format_name in args.format:
command = [args.sphinx] + OUTPUT_FORMATS[format_name].args + ['-j', args.jobs] command = [args.sphinx] + OUTPUT_FORMATS[format_name].args + ['-j', args.jobs]
if args.clean: if args.clean:
@ -88,5 +95,6 @@ if __name__ == '__main__':
if args.debug: if args.debug:
print('Building:', format_name) print('Building:', format_name)
print('Running:', command) print('Running:', command)
subprocess.call(command) subprocess.run(command, check=True, env=sphinx_env)
print('') print('')