Skip to content

Commit

Permalink
Replace generate_screenshot.sh with Python script
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasberbuer committed Jan 2, 2023
1 parent 2a979ff commit 37732e3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 32 deletions.
80 changes: 80 additions & 0 deletions docs/generate_screenshot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""
Generate screenshot of example usage with termshot.
Reference: https://github.com/homeport/termshot
"""

import sys
import tarfile
import urllib.request
from pathlib import Path
from shutil import which
from subprocess import check_call

HERE = Path(__file__).parent

URL_TERMSHOT = "https://github.com/homeport/termshot/releases/download/v0.2.5/termshot_0.2.5_linux_amd64.tar.gz" # noqa, pylint: disable=line-too-long

CONANFILE = """
[requires]
boost/1.79.0
catch2/3.2.0
fmt/8.0.0
nlohmann_json/3.10.0
"""


def download(url: str, timeout=10) -> bytes:
with urllib.request.urlopen(url, timeout=timeout) as response:
return response.read()


def filename_conan_check_updates() -> str:
for filename in ("conan-check-updates", "conan-check-updates.exe"):
if which(filename):
return filename
raise RuntimeError("Could not find conan-check-updates executable")


def main():
if sys.platform != "linux":
raise RuntimeError("Only Linux platform supported")

filename_archive = URL_TERMSHOT.rpartition("/")[-1]
filepath_archive = HERE / filename_archive

if not filepath_archive.exists():
print("Download archive")
data = download(URL_TERMSHOT)
filepath_archive.write_bytes(data)

filename_termshot = "termshot"
filepath_termshot = HERE / filename_termshot

if not filepath_termshot.exists():
print("Extract termshot from archive")
with tarfile.open(filepath_archive) as tar:
tar.extract(filename_termshot, path=HERE)
assert filepath_termshot.exists()

print("Generate conanfile.txt")
filepath_conanfile = HERE / "conanfile.txt"
filepath_conanfile.write_text(CONANFILE)

print("Run termshot")
check_call(
(
str(filepath_termshot),
"--show-cmd",
"--filename",
"screenshot.png",
"--",
filename_conan_check_updates(),
),
cwd=HERE,
timeout=10,
)


if __name__ == "__main__":
main()
32 changes: 0 additions & 32 deletions docs/generate_screenshot.sh

This file was deleted.

0 comments on commit 37732e3

Please sign in to comment.