Skip to content

Commit

Permalink
normalize project name before publish
Browse files Browse the repository at this point in the history
  • Loading branch information
werwty committed Jun 12, 2018
1 parent 4958835 commit 0d03043
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .pep8speaks.yml
@@ -0,0 +1,8 @@
pycodestyle:
max-line-length: 100 # Default is 79 in PEP8
ignore: # Errors and warnings to ignore
- E401 # multiple imports on one line
exclude:
- "./docs/*"
- "*/build/*"
- "*/migrations/*"
3 changes: 3 additions & 0 deletions plugins/pulp_python/plugins/distributors/steps.py
Expand Up @@ -11,6 +11,7 @@
from pulp_python.common import constants
from pulp_python.plugins import models
from pulp_python.plugins.distributors import configuration
from pulp_python.plugins.utils import sanitize_name


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -92,6 +93,7 @@ def write_simple_api(self, projects):
body = ElementTree.SubElement(html, 'body')
# Create a reference in index.html that points to the index for each project.
for project_name, packages in projects.items():
project_name = sanitize_name(project_name)
element = ElementTree.SubElement(body, 'a', {'href': project_name})
element.text = project_name
ElementTree.SubElement(body, 'br')
Expand Down Expand Up @@ -149,6 +151,7 @@ def write_json_api(self, projects):
"""
api_path = os.path.join(self.parent.web_working_dir, 'pypi')
for project_name, packages in projects.items():
project_name = sanitize_name(project_name)
project_metadata_path = os.path.join(api_path, project_name, 'json')
os.makedirs(project_metadata_path)
project_index_metadata_path = os.path.join(project_metadata_path, 'index.json')
Expand Down
13 changes: 13 additions & 0 deletions plugins/pulp_python/plugins/utils.py
@@ -0,0 +1,13 @@
import re


def sanitize_name(name):
"""
Strips out all non-alphanumeric characters, including underscores, and replaces them with
hyphens. Runs of multiple non-alphanumeric characters are replaced by only one hyphen.
This is to take a given python package name and create an iteration of it that can be
used as part of a url, e.g. for the simple api.
Args:
name (str): A project name to sanitize
"""
return re.sub('[^A-Za-z0-9]+', '-', name).lower()
2 changes: 1 addition & 1 deletion plugins/test/unit/plugins/distributors/test_steps.py
Expand Up @@ -158,7 +158,7 @@ def test_process_main(self, _create_project_index, makedirs, mock_package_qs, mo
mock_package_qs.packages_by_project.assert_called_once_with(conduit.repo_id)
makedirs.assert_has_calls([
mock.call(os.path.join(step.parent.web_working_dir, 'simple')),
mock.call(os.path.join(step.parent.web_working_dir, 'pypi', 'pulp_python_plugins',
mock.call(os.path.join(step.parent.web_working_dir, 'pypi', 'pulp-python-plugins',
'json')),
mock.call(os.path.join(step.parent.web_working_dir, 'pypi', 'nectar', 'json')),
])
Expand Down

0 comments on commit 0d03043

Please sign in to comment.