Skip to content

Commit

Permalink
fix: validate the project name when running pdm init (pdm-project#2402)
Browse files Browse the repository at this point in the history
* Issues 2360 (pdm-project#1)

* bugfix: pdm-project#2360

* refactor: typo and code

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Issues 2360 (pdm-project#2)

* bugfix: pdm-project#2360

* docs: 2360.bugfix.md

* docs: 2360.bugfix.md

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
LeetaoGoooo and pre-commit-ci[bot] authored Nov 15, 2023
1 parent ec76487 commit 73651b7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions news/2360.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
prevent wrong project name (including space and illegal characters)
16 changes: 14 additions & 2 deletions src/pdm/cli/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pdm.models.python import PythonInfo
from pdm.models.specifiers import get_specifier
from pdm.models.venv import get_venv_python
from pdm.utils import get_user_email_from_git, package_installed
from pdm.utils import get_user_email_from_git, package_installed, sanitize_project_name, validate_project_name

if TYPE_CHECKING:
from pdm.project import Project
Expand Down Expand Up @@ -90,6 +90,18 @@ def ask(self, question: str, default: str) -> str:
return default
return termui.ask(question, default=default)

def ask_project(self, project: Project) -> str:
default = sanitize_project_name(project.root.name)
name = self.ask("Project name", default)
if default == name or validate_project_name(name):
return name
project.core.ui.echo(
"Project name is not valid, it should follow PEP 426",
err=True,
style="warning",
)
return self.ask_project(project)

def get_metadata_from_input(self, project: Project, options: argparse.Namespace) -> dict[str, Any]:
from pdm.formats.base import array_of_inline_tables, make_array, make_inline_table

Expand All @@ -102,7 +114,7 @@ def get_metadata_from_input(self, project: Project, options: argparse.Namespace)
)
build_backend: type[BuildBackend] | None = None
if is_library:
name = self.ask("Project name", project.root.name)
name = self.ask_project(project)
version = self.ask("Project version", "0.1.0")
description = self.ask("Project description", "")
if options.backend:
Expand Down
13 changes: 13 additions & 0 deletions src/pdm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,16 @@ def package_installed(package_name: str) -> bool:
return False
else:
return True


def validate_project_name(name: str) -> bool:
"""Check if the project name is valid or not"""

pattern = r"^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$"
return re.fullmatch(pattern, name, flags=re.IGNORECASE) is not None


def sanitize_project_name(name: str) -> str:
"""Sanitize the project name and remove all illegal characters"""
pattern = r"[^a-zA-Z0-9\-_\.]"
return re.sub(pattern, "", name)

0 comments on commit 73651b7

Please sign in to comment.