in doc/bst2html.py [0:0]
def run_session(description, tempdir, source_cache, palette, config_file, force):
desc = _yaml.load(description, shortname=os.path.basename(description))
desc_dir = os.path.dirname(description)
# Preflight commands and check if we can skip this session
#
if not force:
needs_build = False
commands = desc.get_sequence('commands')
for command in commands:
output = command.get_str('output', default=None)
if output is not None and check_needs_build(desc_dir, output, force=False):
needs_build = True
break
if not needs_build:
click.echo("Skipping '{}' as no files need to be built".format(description), err=True)
return
# FIXME: Workaround a setuptools bug where the symlinks
# we store in git dont get carried into a release
# tarball. This workaround lets us build docs from
# a source distribution tarball.
#
symlinks = desc.get_mapping('workaround-symlinks', default={})
for symlink, target in symlinks.items():
target = target.as_str()
# Resolve real path to where symlink should be
symlink = os.path.join(desc_dir, symlink)
# Ensure dir exists
symlink_dir = os.path.dirname(symlink)
os.makedirs(symlink_dir, exist_ok=True)
click.echo("Generating symlink at: {} (target: {})".format(symlink, target), err=True)
# Generate a symlink
try:
os.symlink(target, symlink)
except FileExistsError:
# If the files exist, we're running from a git checkout and
# not a source distribution, no need to complain
pass
remove_files = desc.get_str_list('remove-files', default=[])
for remove_file in remove_files:
remove_file = os.path.join(desc_dir, remove_file)
remove_file = os.path.realpath(remove_file)
if os.path.isdir(remove_file):
utils._force_rmtree(remove_file)
else:
utils.safe_remove(remove_file)
# Run commands
#
commands = desc.get_sequence('commands')
for command in commands:
# Get the directory where this command should be run
directory = command.get_str('directory')
directory = os.path.join(desc_dir, directory)
directory = os.path.realpath(directory)
# Get the command string
command_str = command.get_str('command')
# Check whether this is a shell command and not a bst command
is_shell = command.get_bool('shell', default=False)
# Check if there is fake output
command_fake_output = command.get_str('fake-output', default=None)
# Run the command, or just use the fake output
if command_fake_output is None:
if is_shell:
command_out = run_shell_command(directory, command_str)
else:
command_out = run_bst_command(config_file, directory, command_str)
else:
command_out = command_fake_output
# Encode and save the output if that was asked for
output = command.get_str('output', default=None)
if output is not None:
# Convert / Generate a nice <div>
converted = generate_html(command_out, directory, config_file,
source_cache, tempdir, palette,
command_str, command_fake_output is not None)
# Save it
filename = os.path.join(desc_dir, output)
filename = os.path.realpath(filename)
output_dir = os.path.dirname(filename)
os.makedirs(output_dir, exist_ok=True)
with open(filename, 'wb') as f:
f.write(converted.encode('utf-8'))
click.echo("Saved session at '{}'".format(filename), err=True)