-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* reserver custom basic error * add basic implementation of package parameters customization * add customizable package parameter + validation regex for `email` & `url` * add `user_parameters` field to upload function * docstring added * fulfill docstring * updated * add CLI Handler for reserver * add `reserver_help` * add OVERVIEW constant * `autopep8.sh` applied * remove blank line after docstring * update overview * update `--test` help * add art to the requirements * add `remove_dir` function * use `remove_dir` instead of `shutil.rmtree` * add CLI usage * autopep8.sh applied * `entry_points` added * Smoke test added to `test.yml` * enhance CLI * refactor hardcoded params into `reserver_param.py` * prioritize -v command --------- Co-authored-by: AHReccese <[email protected]>
- Loading branch information
Showing
11 changed files
with
180 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
art==6.2 | ||
requests==2.32.3 | ||
setuptools==70.0.0 | ||
wheel==0.43.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
art>=5.3 | ||
requests>=1.0.0 | ||
setuptools>=40.8.0 | ||
wheel>=0.35.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Reserver main.""" | ||
import argparse | ||
from art import tprint | ||
from .reserver_param import RESERVER_VERSION | ||
from .reserver_func import reserver_help | ||
from .reserver_obj import PyPIUploader | ||
|
||
|
||
def main(): | ||
""" | ||
CLI main function. | ||
:return: None | ||
""" | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
'--name', | ||
nargs='?', | ||
type=str, | ||
metavar="PACKAGE_NAME", | ||
help='Name(s) to get reserved', | ||
) | ||
parser.add_argument( | ||
'--token', | ||
nargs='?', | ||
type=str, | ||
metavar="(TEST.PYPI|PYPI)_TOKEN", | ||
help='The token for (main|test) PyPI account', | ||
) | ||
parser.add_argument('--test', action='store_true', help='test PyPI (test.pypi.org)') | ||
parser.add_argument('--version', help="version", action='store_true', default=False) | ||
parser.add_argument('-v', help="version", action='store_true', default=False) | ||
args = parser.parse_known_args()[0] | ||
if args.version or args.v: | ||
print(RESERVER_VERSION) | ||
return | ||
names = args.name | ||
test_pypi = args.test | ||
pypi_token = args.token | ||
if names and pypi_token: | ||
PyPIUploader(pypi_token, test_pypi).batch_upload(names) | ||
else: | ||
tprint("Reserver") | ||
tprint("V:" + RESERVER_VERSION) | ||
reserver_help() | ||
parser.print_help() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Reserver errors.""" | ||
|
||
|
||
class ReserverBaseError(Exception): | ||
"""Reserver base error class.""" | ||
|
||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Reserver functions.""" | ||
import re | ||
import requests | ||
import requests.adapters | ||
from .reserver_param import PYPI_TEST_URL, PYPI_MAIN_URL | ||
import requests.adapters | ||
from .reserver_param import PYPI_TEST_URL, PYPI_MAIN_URL, PACKAGE_PARAMETERS, VALIDATIONS, OVERVIEW | ||
from .reserver_param import INVALID_PACKAGE_PARAMETER_NAME_ERROR, INVALID_PACKAGE_PARAMETER_VALUE_ERROR | ||
from .reserver_errors import ReserverBaseError | ||
from hashlib import sha256 | ||
from time import time | ||
from os import mkdir, rmdir | ||
|
@@ -38,8 +41,8 @@ def does_package_exist(suggested_name, test_pypi): | |
retries = requests.adapters.Retry( | ||
total=5, | ||
backoff_factor=0.1, | ||
status_forcelist=[ 500, 502, 503, 504 ] | ||
) | ||
status_forcelist=[500, 502, 503, 504] | ||
) | ||
|
||
s.mount('http://', requests.adapters.HTTPAdapter(max_retries=retries)) | ||
s.mount('https://', requests.adapters.HTTPAdapter(max_retries=retries)) | ||
|
@@ -48,7 +51,32 @@ def does_package_exist(suggested_name, test_pypi): | |
return not response.status_code == 404 | ||
|
||
|
||
def generate_template_setup_py(package_name): | ||
def get_package_parameter(parameter, user_parameters, regex=None): | ||
""" | ||
Get the value for the associated package parameter. | ||
:param parameter: one of the customizable package parameters | ||
:type parameter: str | ||
:param user_parameters: user-customized package parameters | ||
:type user_parameters: dict | ||
:param regex: name of the regex to get applied | ||
:type regex: str | ||
:return: value of the associated parameter | ||
""" | ||
if not user_parameters or parameter not in user_parameters: | ||
if parameter in PACKAGE_PARAMETERS: | ||
return PACKAGE_PARAMETERS[parameter] | ||
else: | ||
raise ReserverBaseError(INVALID_PACKAGE_PARAMETER_NAME_ERROR) | ||
if regex: | ||
if re.match(VALIDATIONS[regex], user_parameters[parameter]): | ||
return user_parameters[parameter] | ||
else: | ||
raise ReserverBaseError(INVALID_PACKAGE_PARAMETER_VALUE_ERROR.format(parameter=parameter, regex=regex)) | ||
return user_parameters[parameter] | ||
|
||
|
||
def generate_template_setup_py(package_name, user_parameters): | ||
""" | ||
Generate a template `setup.py` file for given package name. | ||
|
@@ -73,18 +101,18 @@ def generate_template_setup_py(package_name): | |
name =""" + "\"" + package_name + "\"" + """, | ||
packages=[""" + "\"" + package_name + "\"" + "," + """], | ||
version='0.0.0', | ||
description='This name has been reserved using Reserver', | ||
description=""" + "\"" + get_package_parameter("description", user_parameters) + "\"" + """, | ||
long_description=\"\"\" | ||
This name has been reserved using [Reserver](https://github.com/openscilab/reserver). | ||
\"\"\", | ||
long_description_content_type='text/markdown', | ||
author='Development Team', | ||
author_email='[email protected]', | ||
url='https://url.com', | ||
download_url='https://download_url.com', | ||
author=""" + "\"" + get_package_parameter("author", user_parameters) + "\"" + """, | ||
author_email=""" + "\"" + get_package_parameter("author_email", user_parameters, "email") + "\"" + """, | ||
url=""" + "\"" + get_package_parameter("url", user_parameters, "url") + "\"" + """, | ||
download_url=""" + "\"" + get_package_parameter("download_url", user_parameters, "url") + "\"" + """, | ||
keywords="python3 python reserve reserver reserved", | ||
project_urls={ | ||
'Source': 'https://github.com/source', | ||
'Source':""" + "\"" + get_package_parameter("source", user_parameters, "url") + "\"" + """, | ||
}, | ||
install_requires="", | ||
python_requires='>=3.6', | ||
|
@@ -98,7 +126,7 @@ def generate_template_setup_py(package_name): | |
\'Programming Language :: Python :: 3.11\', | ||
\'Programming Language :: Python :: 3.12\', | ||
], | ||
license='MIT', | ||
license=""" + "\"" + get_package_parameter("license", user_parameters) + "\"" + """, | ||
) | ||
""" | ||
|
@@ -113,3 +141,14 @@ def generate_template_setup_py(package_name): | |
with open(package_name + "/__init__.py", "w") as f: | ||
f.write("# -*- coding: utf-8 -*-\n") | ||
f.write("\"\"\"" + package_name + " modules." + "\"\"\"") | ||
|
||
|
||
def reserver_help(): | ||
""" | ||
Print Reserver details. | ||
:return: None | ||
""" | ||
print(OVERVIEW) | ||
print("Repo : https://github.com/openscilab/reserver") | ||
print("Webpage : https://openscilab.com/\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,24 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Parameters and constants.""" | ||
OVERVIEW = """ | ||
Reserver is an open source Python package that offers the ability to quickly reserve a PyPI package name. Got a notion? Before it's taken, immediately reserve the product name! | ||
""" | ||
RESERVER_VERSION = "0.1" | ||
RESERVER_NAME = "reserver" | ||
|
||
PYPI_TEST_URL = "https://test.pypi.org/project" | ||
PYPI_MAIN_URL = "https://pypi.org/project" | ||
PACKAGE_PARAMETERS = { | ||
"description": "This name has been reserved using Reserver", | ||
"author": "Development Team", | ||
"author_email": "[email protected]", | ||
"url": "https://url.com", | ||
"download_url": "https://download_url.com", | ||
"source": "https://github.com/source", | ||
"license": "MIT", | ||
} | ||
VALIDATIONS = { | ||
"email": r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', | ||
"url": r'^(http|https)://[a-zA-Z0-9.-_]+\.[a-zA-Z]{2,}(/\S*)?$', | ||
} | ||
INVALID_PACKAGE_PARAMETER_NAME_ERROR = "Given parameter doesn't exist among the supported user allowed parameters." | ||
INVALID_PACKAGE_PARAMETER_VALUE_ERROR = "Invalid value for {parameter} that should be a valid {regex}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters