From 38c26b565c1c289a447ba8ab487679438b807146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Collonval?= Date: Fri, 4 Dec 2020 14:57:53 +0100 Subject: [PATCH] And support for windows --- README.md | 2 +- papermill_report/handlers.py | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 72ab5c3..02da8ae 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ This is needed to build the link to broken notebook. ## Notes -- The service must be run as `root` because report processes are executed through `su --login` command to +- On Unix platforms, the service must be run as `root` because report processes are executed through `su --login` command to impersonate the authenticated user and setting the environment variables afresh. ## Development diff --git a/papermill_report/handlers.py b/papermill_report/handlers.py index aeeeab7..0ff7e03 100644 --- a/papermill_report/handlers.py +++ b/papermill_report/handlers.py @@ -2,7 +2,6 @@ import json import logging import os -import pwd import re import stat import sys @@ -24,6 +23,11 @@ ANONYMOUS_USER = "anonymous_pm_report" +try: + import pwd +except ImportError: # None Unix + pwd = None + if os.environ.get("JUPYTERHUB_API_TOKEN"): from jupyterhub.services.auth import HubOAuthenticated else: @@ -323,7 +327,7 @@ def _save_broken_report(self, notebook_path: Path, username: str) -> Path: Returns: The copied broken report path """ - local_user = pwd.getpwnam(username) if username != ANONYMOUS_USER else None + local_user = pwd.getpwnam(username) if pwd is not None and username != ANONYMOUS_USER else None prefix = datetime.strftime(datetime.now(), "%Y-%m-%d") + "_broken_" broken_notebook = Path(self.broken_path) / (prefix + notebook_path.name) if not broken_notebook.parent.exists(): @@ -439,7 +443,7 @@ async def get(self, template_path: str): str(output_nb), ] try: - if username != ANONYMOUS_USER: + if pwd is not None and username != ANONYMOUS_USER: # Generate the report impersonating the authenticated user await _execute_command( ["su", username, "-l", "-c", " ".join(command)], cwd=tmp_folder @@ -466,7 +470,7 @@ async def get(self, template_path: str): str(output_nb), ] - if username != ANONYMOUS_USER: + if pwd is not None and username != ANONYMOUS_USER: await _execute_command( ["su", username, "-l", "-c", " ".join(command)], cwd=tmp_folder )