Add working directory option (#619)

This commit is contained in:
Giovanni Barillari 2025-06-28 13:21:30 +02:00 committed by GitHub
parent 977b6fa97f
commit 7949c46045
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 14 additions and 3 deletions

View file

@ -291,6 +291,8 @@ Options:
should be invoked to build the actual target
[env var: GRANIAN_FACTORY; default:
(disabled)]
--working-dir DIRECTORY Set the working directory [env var:
GRANIAN_WORKING_DIR]
--env-files FILE Environment file(s) to load (requires
granian[dotenv] extra) [env var:
GRANIAN_ENV_FILES]

View file

@ -52,8 +52,8 @@ def load_module(module_name: str, raise_on_failure: bool = True) -> Optional[Mod
return sys.modules[module_name]
def load_target(target: str, factory: bool = False) -> Callable[..., None]:
sys.path.insert(0, '')
def load_target(target: str, wd: Optional[str] = None, factory: bool = False) -> Callable[..., None]:
sys.path.insert(0, wd or '')
path, name = get_import_components(target)
path = prepare_import(path) if path else None
name = name or 'app'

View file

@ -244,6 +244,11 @@ def option(*param_decls: str, cls: Optional[Type[click.Option]] = None, **attrs:
default=False,
help='Treat target as a factory function, that should be invoked to build the actual target',
)
@option(
'--working-dir',
type=click.Path(exists=True, file_okay=False, dir_okay=True, readable=True, path_type=pathlib.Path),
help='Set the working directory',
)
@option(
'--env-files',
type=click.Path(exists=True, file_okay=True, dir_okay=False, readable=True, path_type=pathlib.Path),
@ -368,6 +373,7 @@ def cli(
workers_lifetime: Optional[int],
workers_kill_timeout: Optional[int],
factory: bool,
working_dir: Optional[pathlib.Path],
env_files: Optional[List[pathlib.Path]],
static_path_route: str,
static_path_mount: Optional[pathlib.Path],
@ -442,6 +448,7 @@ def cli(
workers_lifetime=workers_lifetime,
workers_kill_timeout=workers_kill_timeout,
factory=factory,
working_dir=working_dir,
env_files=env_files,
static_path_route=static_path_route,
static_path_mount=static_path_mount,

View file

@ -110,6 +110,7 @@ class AbstractServer(Generic[WT]):
workers_lifetime: Optional[int] = None,
workers_kill_timeout: Optional[int] = None,
factory: bool = False,
working_dir: Optional[Path] = None,
env_files: Optional[Sequence[Path]] = None,
static_path_route: str = '/static',
static_path_mount: Optional[Path] = None,
@ -159,6 +160,7 @@ class AbstractServer(Generic[WT]):
self.workers_lifetime = workers_lifetime
self.workers_kill_timeout = workers_kill_timeout
self.factory = factory
self.working_dir = str(working_dir.resolve()) if working_dir else None
self.env_files = env_files or ()
self.static_path = (
(static_path_route, str(static_path_mount.resolve()), str(static_path_expires))
@ -504,7 +506,7 @@ class AbstractServer(Generic[WT]):
if wrap_loader:
target_loader = partial(target_loader, self.target)
else:
target_loader = partial(load_target, self.target, factory=self.factory)
target_loader = partial(load_target, self.target, wd=self.working_dir, factory=self.factory)
if not spawn_target:
spawn_target = default_spawners[self.interface]