Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
https://stackoverflow.com/a/16493707/4651668
Command modeled after:
git --work-tree=webpage checkout upstream/gh-pages -- v
"""
if not args.checkout:
return
command = [
"git",
f"--work-tree={args.webpage_directory}",
"checkout",
args.checkout,
"--",
"v",
]
logging.info(
f"Attempting checkout with the following command:\n{shlex_join(command)}"
)
process = subprocess.run(command, stderr=subprocess.PIPE)
if process.returncode == 0:
# Addresses an odd behavior where git checkout stages v/* files that don't actually exist
subprocess.run(["git", "add", "v"])
else:
stderr = process.stderr.decode()
message = (
f"Checkout returned a nonzero exit status. See stderr:\n{stderr.rstrip()}"
)
if "pathspec" in stderr:
message += (
"\nManubot note: if there are no preexisting webpage versions (like for a newly created manuscript), "
"the pathspec error above is expected and can be safely ignored."
) # see https://github.com/manubot/rootstock/issues/183
logging.warning(message)
args.extend(["--to", "docx"])
elif format == "html":
args.extend(["--to", "html"])
elif format == "plain":
args.extend(["--to", "plain", "--wrap", "none"])
if info["pandoc version"] >= (2,):
# Do not use ALL_CAPS for bold & underscores for italics
# https://github.com/jgm/pandoc/issues/4834#issuecomment-412972008
filter_path = (
pathlib.Path(__file__)
.joinpath("..", "plain-pandoc-filter.lua")
.resolve()
)
assert filter_path.exists()
args.extend(["--lua-filter", str(filter_path)])
logging.info("call_pandoc subprocess args:\n" + shlex_join(args))
process = subprocess.run(
args=args,
input=metadata_block.encode(),
stdout=subprocess.PIPE if path else sys.stdout,
stderr=sys.stderr,
)
process.check_returncode()
process = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if process.returncode == 0:
logging.info(
"get_rootstock_commit added a `rootstock` remote to the git repository."
)
# find most recent common ancestor commit
try:
args = ["git", "fetch", "rootstock", "master"]
subprocess.check_output(args, stderr=subprocess.PIPE, universal_newlines=True)
args = ["git", "merge-base", "master", "rootstock/master"]
output = subprocess.check_output(
args, stderr=subprocess.PIPE, universal_newlines=True
)
except subprocess.CalledProcessError as error:
logging.warning(
f"get_rootstock_commit: {shlex_join(error.cmd)!r} returned exit code {error.returncode} "
f"with the following stdout:\n{error.stdout}\n"
f"And the following stderr:\n{error.stderr}"
)
return None
rootstock_commit = output.strip()
return rootstock_commit
if not (use_text ^ use_path):
raise ValueError("load_bibliography: specify either path or text but not both.")
if not get_pandoc_info()["pandoc-citeproc"]:
logging.error(
"pandoc-citeproc not found on system: manubot.pandoc.bibliography.load_bibliography returning empty CSL JSON"
)
return []
args = ["pandoc-citeproc", "--bib2json"]
if input_format:
args.extend(["--format", input_format])
run_kwargs = {}
if use_path:
args.append(str(path))
if use_text:
run_kwargs["input"] = text
logging.info("call_pandoc subprocess args:\n>>> " + shlex_join(args))
process = subprocess.run(
args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
**run_kwargs,
)
logging.info(f"captured stderr:\n{process.stderr}")
process.check_returncode()
try:
csl_json = json.loads(process.stdout)
except Exception:
logging.exception(f"Error parsing bib2json output as JSON:\n{process.stdout}")
csl_json = []
return csl_json
def ots_stamp(path):
"""
Timestamp a file using OpenTimestamps.
This function calls `ots stamp path`.
If `path` does not exist, this function does nothing.
"""
process_args = ["ots", "stamp", str(path)]
process = subprocess.run(
process_args, stderr=subprocess.PIPE, universal_newlines=True
)
if process.returncode != 0:
logging.warning(
f"OpenTimestamp command returned nonzero code ({process.returncode}).\n"
f">>> {shlex_join(process.args)}\n"
"""
ots_paths = list()
for version in get_versions(args):
ots_paths.extend(args.versions_directory.joinpath(version).glob("**/*.ots"))
ots_paths.sort()
for ots_path in ots_paths:
process_args = ["ots"]
if args.no_ots_cache:
process_args.append("--no-cache")
else:
process_args.extend(["--cache", str(args.ots_cache)])
process_args.extend(["upgrade", str(ots_path)])
process = subprocess.run(
process_args, stderr=subprocess.PIPE, universal_newlines=True
)
message = f">>> {shlex_join(process.args)}\n{process.stderr}"
if process.returncode != 0:
logging.warning(
f"OpenTimestamp upgrade failed with exit code {process.returncode}.\n{message}"
)
elif not process.stderr.strip() == "Success! Timestamp complete":
logging.info(message)
backup_path = ots_path.with_suffix(".ots.bak")
if backup_path.exists():
if process.returncode == 0:
backup_path.unlink()
else:
# Restore original timestamp if failure
backup_path.rename(ots_path)