"""Setup owsrequest.""" import configparser import hashlib import os from setuptools import Command from setuptools import find_packages from setuptools import setup from owsrequest import __version__ class register(Command): """No-op: setup.py register was removed from setuptools.""" description = 'no-op register (removed in modern setuptools)' user_options = [('repository=', 'r', 'repository name / URL (ignored)')] def initialize_options(self): """No-op.""" self.repository = None def finalize_options(self): """No-op.""" def run(self): """No-op.""" class upload(Command): """Upload dist files to a PyPI server (replaces removed setuptools upload).""" description = 'upload dist files to a PyPI-compatible server' user_options = [('repository=', 'r', 'repository name from .pypirc')] def initialize_options(self): """Set default repository.""" self.repository = 'pypi' def finalize_options(self): """No-op.""" def run(self): """Read ~/.pypirc and POST each dist file to the repository.""" # Imported lazily so setup.py remains importable before requests is installed. import requests # Read repository URL and credentials from ~/.pypirc using the -r value. rc_path = os.path.expanduser('~/.pypirc') cfg = configparser.RawConfigParser() if not cfg.read(rc_path): raise SystemExit(f'upload: {rc_path} not found') try: repo_url = cfg.get(self.repository, 'repository') username = cfg.get(self.repository, 'username') password = cfg.get(self.repository, 'password') except configparser.NoSectionError: raise SystemExit( f'upload: section [{self.repository}] not found in {rc_path}' ) # dist_files is populated by the preceding sdist/bdist_wheel commands in # the same invocation (e.g. `setup.py sdist bdist_wheel upload`). dist_files = getattr(self.distribution, 'dist_files', []) if not dist_files: raise SystemExit( 'upload: no dist files found; run sdist and/or bdist_wheel first' ) for _cmd, _pyver, filename in dist_files: # Compute MD5 in chunks to avoid loading large artifacts into memory. digest = hashlib.md5() # noqa: S324 with open(filename, 'rb') as fh: for chunk in iter(lambda: fh.read(65536), b''): digest.update(chunk) # Stream the file directly to requests rather than buffering it. with open(filename, 'rb') as fh: resp = requests.post( repo_url, auth=(username, password), data={ ':action': 'file_upload', 'name': self.distribution.get_name(), 'version': self.distribution.get_version(), 'md5_digest': digest.hexdigest(), }, files={ 'content': ( os.path.basename(filename), fh, 'application/octet-stream', ) }, timeout=120, ) resp.raise_for_status() print(f'Uploaded {os.path.basename(filename)} to {repo_url}') setup( cmdclass={'register': register, 'upload': upload}, name='owsrequest', version=__version__, url='https://github.com/theorchard/python-owsrequest/', author='The Orchard', author_email='webdev@theorchard.com', description=('Request library.'), packages=find_packages(), include_package_data=True, install_requires=[ 'backoff~=2.1', 'boto3~=1.20', 'cachelib~=0.4', 'jwtauth>=1.0.0,<2.0.0', 'owsresponse~=3.0', 'requests~=2.26', 'retrying~=1.3', 'secrets_manager~=1.4', 'PyYAML>=5.4.1,<6.1', # see https://github.com/boto/botocore/blob/7abb99f25b5862ed34754a59249104630a820f17/setup.py#L28 # noqa: E501 'urllib3>=1.25.4,<1.27 ; python_version < "3.10"', 'urllib3>=1.25.4,!=2.2.0,<3 ; python_version >= "3.10"', ], zip_safe=False, entry_points={ 'pytest11': [ 'owsrequest = owsrequest.utils.fixture_plugin', ] }, classifiers=[ 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.11', 'Framework :: Pytest', ],)