| 1 |
# Fix for older setuptools |
| 2 |
import logging |
| 3 |
import re |
| 4 |
import os |
| 5 |
|
| 6 |
from setuptools import setup, find_packages, Command |
| 7 |
|
| 8 |
|
| 9 |
def fpath(name): |
| 10 |
return os.path.join(os.path.dirname(__file__), name) |
| 11 |
|
| 12 |
|
| 13 |
def read(fname): |
| 14 |
return open(fpath(fname)).read() |
| 15 |
|
| 16 |
|
| 17 |
def desc(): |
| 18 |
return read('README.md') |
| 19 |
|
| 20 |
|
| 21 |
# grep flasgger/__init__.py since python 3.x cannot |
| 22 |
# import it before using 2to3 |
| 23 |
file_text = read(fpath('flasgger/__init__.py')) |
| 24 |
|
| 25 |
|
| 26 |
def grep(attrname): |
| 27 |
pattern = r"{0}\W*=\W*'([^']+)'".format(attrname) |
| 28 |
strval, = re.findall(pattern, file_text) |
| 29 |
return strval |
| 30 |
|
| 31 |
|
| 32 |
def download_swagger_ui(version: str = '3.52.5'): |
| 33 |
import tarfile |
| 34 |
import urllib.request |
| 35 |
url = f'https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-{version}.tgz' |
| 36 |
destination = 'flasgger/ui3/static' |
| 37 |
|
| 38 |
import certifi |
| 39 |
|
| 40 |
# Create an SSL context with the specified CA certificate file |
| 41 |
import ssl |
| 42 |
context = ssl.create_default_context(cafile=certifi.where()) |
| 43 |
|
| 44 |
with urllib.request.urlopen(url, context=context) as response: |
| 45 |
import tempfile |
| 46 |
with tempfile.NamedTemporaryFile(delete=True) as temp_file: |
| 47 |
temp_file.write(response.read()) |
| 48 |
|
| 49 |
with tarfile.open(temp_file.name, 'r:gz') as tar: |
| 50 |
|
| 51 |
file_mappings = [ |
| 52 |
('package/favicon-*', destination), |
| 53 |
('package/swagger-ui.css*', destination), |
| 54 |
('package/swagger-ui.js*', destination), |
| 55 |
('package/swagger-ui-standalone-preset.js*', destination), |
| 56 |
('package/swagger-ui-bundle.js*', destination), |
| 57 |
] |
| 58 |
|
| 59 |
for pattern, dest_dir in file_mappings: |
| 60 |
for member in tar.getmembers(): |
| 61 |
|
| 62 |
from fnmatch import fnmatch |
| 63 |
if fnmatch(member.name, pattern): |
| 64 |
member.name = member.name.lstrip('package/') |
| 65 |
tar.extract(member, dest_dir) |
| 66 |
|
| 67 |
# # Cleanup: remove the downloaded tar file |
| 68 |
# os.remove(f'swagger-ui-dist-{version}.tgz') |
| 69 |
|
| 70 |
|
| 71 |
from setuptools.command.install import install |
| 72 |
from setuptools.command.develop import develop |
| 73 |
|
| 74 |
|
| 75 |
class MixinSwaggerUiInstall: |
| 76 |
|
| 77 |
def _get_swagger_ui_version_string(self): |
| 78 |
# return '3.52.5' |
| 79 |
|
| 80 |
try: |
| 81 |
|
| 82 |
extras_require = self.distribution.extras_require |
| 83 |
version = None |
| 84 |
|
| 85 |
if 'swagger-ui' in extras_require: |
| 86 |
dependency = extras_require['swagger-ui'][0] |
| 87 |
match = re.match(r'swagger-ui_(\d+\.\d+)', dependency) |
| 88 |
if match: |
| 89 |
version = match.group(1) |
| 90 |
|
| 91 |
return version |
| 92 |
except AttributeError: |
| 93 |
logging.exception('Failing to read swagger-ui version from extras_require') |
| 94 |
return None |
| 95 |
|
| 96 |
def run(self): |
| 97 |
super().run() |
| 98 |
# Your custom script logic goes here |
| 99 |
version = self._get_swagger_ui_version_string() |
| 100 |
# print("Running custom script during installation...") |
| 101 |
if version: |
| 102 |
logging.info(f"Running custom script during installation... swagger-ui=={version}") |
| 103 |
download_swagger_ui(version=version) |
| 104 |
|
| 105 |
|
| 106 |
class SwaggerUiInstallCommand(MixinSwaggerUiInstall, install): |
| 107 |
pass |
| 108 |
|
| 109 |
|
| 110 |
class SwaggerUiDevelopCommand(MixinSwaggerUiInstall, develop): |
| 111 |
pass |
| 112 |
|
| 113 |
|
| 114 |
setup( |
| 115 |
name='flasgger', |
| 116 |
version=grep('__version__'), |
| 117 |
url='https://github.com/flasgger/flasgger/', |
| 118 |
license='MIT', |
| 119 |
author=grep('__author__'), |
| 120 |
author_email=grep('__email__'), |
| 121 |
description='Extract swagger specs from your flask project', |
| 122 |
long_description=desc(), |
| 123 |
long_description_content_type="text/markdown", |
| 124 |
packages=find_packages( |
| 125 |
exclude=[ |
| 126 |
'tests', 'tests.*', |
| 127 |
'examples', 'examples.*', |
| 128 |
'demo_app', 'demo_app.*', |
| 129 |
'etc', 'etc.*' |
| 130 |
] |
| 131 |
), |
| 132 |
include_package_data=True, |
| 133 |
zip_safe=False, |
| 134 |
platforms='any', |
| 135 |
install_requires=[ |
| 136 |
'Flask>=0.10', |
| 137 |
'PyYAML>=3.0', |
| 138 |
'jsonschema>=3.0.1', |
| 139 |
'mistune', |
| 140 |
'six>=1.10.0', |
| 141 |
'packaging', |
| 142 |
'certifi', # For downloading external dependencies such as swagger-ui |
| 143 |
], |
| 144 |
classifiers=[ |
| 145 |
'Intended Audience :: Developers', |
| 146 |
'Programming Language :: Python :: 3', |
| 147 |
'Programming Language :: Python :: 3.6', |
| 148 |
], |
| 149 |
entry_points={ |
| 150 |
'flask.commands': [ |
| 151 |
'generate-api-schema=flasgger.commands:generate_api_schema', |
| 152 |
], |
| 153 |
}, |
| 154 |
extras_require={ |
| 155 |
# 'swagger-ui': ['swagger-ui==3.28'], |
| 156 |
}, |
| 157 |
cmdclass={ |
| 158 |
'install': SwaggerUiInstallCommand, |
| 159 |
'develop': SwaggerUiDevelopCommand, |
| 160 |
}, |
| 161 |
) |