From 9d5b2559274819e3b47230da85d4d306807bb4bf Mon Sep 17 00:00:00 2001 From: Simon Cornish <7t9jna402@sneakemail.com> Date: Tue, 16 Aug 2022 15:48:01 -0700 Subject: [PATCH] Use top-level name property as project name if user doesn't set one explicitly Signed-off-by: Simon Cornish <7t9jna402@sneakemail.com> --- podman_compose.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/podman_compose.py b/podman_compose.py index d77e734..8cadedc 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -1424,17 +1424,6 @@ class PodmanCompose: # TODO: remove next line os.chdir(dirname) - if not project_name: - # More strict then actually needed for simplicity: podman requires [a-zA-Z0-9][a-zA-Z0-9_.-]* - project_name = ( - os.environ.get("COMPOSE_PROJECT_NAME", None) or dir_basename.lower() - ) - project_name = norm_re.sub("", project_name) - if not project_name: - raise RuntimeError(f"Project name [{dir_basename}] normalized to empty") - - self.project_name = project_name - dotenv_path = os.path.join(dirname, args.env_file) dotenv_dict = dotenv_to_dict(dotenv_path) os.environ.update( @@ -1452,7 +1441,6 @@ class PodmanCompose: { "COMPOSE_PROJECT_DIR": dirname, "COMPOSE_FILE": pathsep.join(relative_files), - "COMPOSE_PROJECT_NAME": self.project_name, "COMPOSE_PATH_SEPARATOR": pathsep, } ) @@ -1479,6 +1467,23 @@ class PodmanCompose: if len(files) > 1: log(" ** merged:\n", json.dumps(compose, indent=2)) # ver = compose.get('version', None) + + if not project_name: + project_name = compose.get("name", None) + if project_name is None: + # More strict then actually needed for simplicity: podman requires [a-zA-Z0-9][a-zA-Z0-9_.-]* + project_name = ( + os.environ.get("COMPOSE_PROJECT_NAME", None) or dir_basename.lower() + ) + project_name = norm_re.sub("", project_name) + if not project_name: + raise RuntimeError( + f"Project name [{dir_basename}] normalized to empty" + ) + + self.project_name = project_name + self.environ.update({"COMPOSE_PROJECT_NAME": self.project_name}) + services = compose.get("services", None) if services is None: services = {}