diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 67230b7..8655401 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -4,8 +4,6 @@ on: workflow_dispatch: pull_request: push: - branches: - main jobs: build: @@ -21,7 +19,7 @@ jobs: - name: Install markdown run: pip3 install markdown - name: Make resume - run: python3 resume.py + run: python3 resume.py --debug - name: Rename output if: ${{ matrix.os != 'windows-latest' }} run: | diff --git a/resume.py b/resume.py index 465a494..ecd40a2 100644 --- a/resume.py +++ b/resume.py @@ -120,7 +120,6 @@ def write_pdf(html: str, prefix: str = "resume", chrome: str = "") -> None: Write html to prefix.pdf """ chrome = chrome or guess_chrome_path() - html64 = base64.b64encode(html.encode("utf-8")) options = [ "--headless", @@ -132,9 +131,18 @@ def write_pdf(html: str, prefix: str = "resume", chrome: str = "") -> None: if sys.platform == "win32": options.append("--disable-gpu") - tmpdir = tempfile.TemporaryDirectory(prefix="resume.md_") - options.append(f"--crash-dumps-dir={tmpdir.name}") - options.append(f"--user-data-dir={tmpdir.name}") + # Ideally we'd use tempfile.TemporaryDirectory here. We can't because + # attempts to delete the tmpdir fail on Windows because Chrome creates a + # file the python process does not have permission to delete. See + # https://github.com/puppeteer/puppeteer/issues/2778, + # https://github.com/puppeteer/puppeteer/issues/298, and + # https://bugs.python.org/issue26660. If we ever drop Python 3.9 support we + # can use TemporaryDirectory with ignore_cleanup_errors=True as a context + # manager. + tmpdir = tempfile.mkdtemp(prefix="resume.md_") + options.append(f"--crash-dumps-dir={tmpdir}") + options.append(f"--user-data-dir={tmpdir}") + try: subprocess.run( [ @@ -155,14 +163,9 @@ def write_pdf(html: str, prefix: str = "resume", chrome: str = "") -> None: else: raise exc finally: - # We use this try-finally rather than TemporaryDirectory's context - # manager to be able to catch the exception caused by - # https://bugs.python.org/issue26660 on Windows - try: - shutil.rmtree(tmpdir.name) - except PermissionError as exc: - logging.warning(f"Could not delete {tmpdir.name}") - logging.info(exc) + shutil.rmtree(tmpdir, ignore_errors=True) + if os.path.isdir(tmpdir): + logging.debug(f"Could not delete {tmpdir}") if __name__ == "__main__": @@ -188,10 +191,13 @@ def write_pdf(html: str, prefix: str = "resume", chrome: str = "") -> None: help="Path to Chrome or Chromium executable", ) parser.add_argument("-q", "--quiet", action="store_true") + parser.add_argument("--debug", action="store_true") args = parser.parse_args() if args.quiet: logging.basicConfig(level=logging.WARN, format="%(message)s") + elif args.debug: + logging.basicConfig(level=logging.DEBUG, format="%(message)s") else: logging.basicConfig(level=logging.INFO, format="%(message)s")