Skip to content

Commit

Permalink
Enhance/errors (#45)
Browse files Browse the repository at this point in the history
* add chardet to requirements

* re-order imports and add chardet

* replace `error.__str__` with `error.output`

* handle package vs module conflict cases

* check encodings on different OSes

* rollback to `utf-8` encoding when exception get raised in encoding detection

* add test case to check conflict with standard python library modules

* add test case to check conflict with non-standard python library modules + fixing a typo

* update env variable name

* add main pypi account credentials

* `CHANGELOG.md` updated

* main pypi testcases updated

* fix `chardet` in `dev-requirements.txt`

* add an empty line at the end

* update error message
  • Loading branch information
AHReccese authored Aug 3, 2024
1 parent 41f128d commit 2e7cee4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ jobs:
env:
TWINE_TEST_USERNAME: ${{ secrets.TEST_PYPI_USERNAME }}
TWINE_TEST_PASSWORD: ${{ secrets.TEST_PYPI_PASSWORD }}
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python -m pytest . --cov=reserver --cov-report=term
- name: Upload coverage to Codecov
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]
### Added
- more testcases in conflict cases
### Changed
- `upload` method in `reserver_obj.py`
## [0.2] - 2024-06-17
### Added
- `CLI` handler
Expand Down
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ bandit>=1.5.1
pydocstyle>=3.0.0
pytest>=4.3.1
pytest-cov>=2.6.1
chardet==4.0.0
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ requests>=1.0.0
setuptools>=40.8.0
wheel>=0.35.0
twine>=3.5.0
chardet>=4.0.0
20 changes: 14 additions & 6 deletions reserver/reserver_obj.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# -*- coding: utf-8 -*-
"""Reserver modules."""
from .reserver_func import does_package_exist, generate_template_setup_py
from .util import has_named_parameter, remove_dir
from os import environ, path, getcwd, remove
import chardet
from re import sub
from sys import executable
from os import environ, path, getcwd, remove
from .util import has_named_parameter, remove_dir
from subprocess import check_output, CalledProcessError
from re import sub
from .reserver_func import does_package_exist, generate_template_setup_py


class PyPIUploader:
Expand Down Expand Up @@ -101,15 +102,22 @@ def upload(self, package_name, user_parameters=None):
check_output(command, shell=True)
except CalledProcessError as e:
publish_failed = True
error = e.__str__()
error = e.output
try:
error = error.decode(chardet.detect(error)['encoding'])
except:
error = error.decode('utf-8')
if command == commands[-2]:
if "403" in error and "Invalid or non-existent authentication information" in error:
error = "Invalid or non-existent authentication information(PyPI API Key)."
if "400" in error and "too similar to an existing project" in error:
error = "Given package name is too similar to an existing project in PyPI."
if "400" in error and "isn't allowed." in error:
error = "Given package name has conflict with the module name of a previously taken package."
if "400" in error and "isn't allowed (conflict with Python Standard Library" in error:
error = "Given package name has conflict with Python Standard Library module name."
break

# todo remove env variable
if "TWINE_USERNAME" in environ:
environ.pop("TWINE_USERNAME")
if "TWINE_PASSWORD" in environ:
Expand Down
16 changes: 14 additions & 2 deletions tests/test_reserver.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
from reserver import PyPIUploader
from reserver.reserver_func import get_random_name
import os
test_pypi_token = os.environ.get("TEST_PYPI_PASSWORD")

test_pypi_token = os.environ.get("TWINE_TEST_PASSWORD")
pypi_token = os.environ.get("TWINE_PASSWORD")

def test_package_exists():
# test reserved name
uploader = PyPIUploader(test_pypi_token, test_pypi=True)
assert uploader.upload("numpy") == False

def test_standard_module_conflict():
# try to reserve a standard python library module
uploader = PyPIUploader(test_pypi_token, test_pypi=True)
assert uploader.upload("os") == False

def test_batch_packages_names():
# test batch of package names
uploader = PyPIUploader(test_pypi_token, test_pypi=True)
Expand All @@ -22,5 +29,10 @@ def test_valid_package_invalid_credentials():
def test_valid_package_valid_credentials():
# test not reserved name -> correct credentials
# uploader = PyPIUploader(test_pypi_token, test_pypi=True)
# uploader.upload_to_pypi(get_random_name())
# uploader.upload(get_random_name())
assert True == True

def test_module_conflict():
# try to reserve a name which conflicts with the module name of a previously taken package (the taken package itself has a different name, but it's module name has conflict)."
uploader = PyPIUploader(pypi_token, test_pypi=False)
assert uploader.upload("freeze") == False

0 comments on commit 2e7cee4

Please sign in to comment.