forked from OasisLMF/ODS_Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
128 lines (102 loc) · 4.02 KB
/
setup.py
File metadata and controls
128 lines (102 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import io
import os
import re
import setuptools
import setuptools.command.install as orig
import urllib.request
import json
SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
OED_VERSION = '4.0.0'
# ORD_VERSION =
def get_readme():
with io.open(os.path.join(SCRIPT_DIR, 'README.md'), encoding='utf-8') as readme:
return readme.read()
def get_version():
"""
Return package version as listed in `__version__` in `init.py`.
"""
with io.open(os.path.join(SCRIPT_DIR, 'ods_tools', '__init__.py'), encoding='utf-8') as init_py:
return re.search('__version__ = [\'"]([^\'"]+)[\'"]', init_py.read()).group(1)
def get_install_requirements():
with io.open(os.path.join(SCRIPT_DIR, 'requirements.in'), encoding='utf-8') as reqs:
return reqs.readlines()
def get_extra_requirements():
with io.open(os.path.join(SCRIPT_DIR, 'requirements-extra.in'), encoding='utf-8') as extrareqs:
return extrareqs.readlines()
class DownloadSpecODS(orig.install):
"""A custom command to download a JSON ODS spec during installation.
Example Install:
pip install -v . --install-option="--local-oed-spec=<path>" .
"""
description = 'Download a ODS JSON spec file from a release URL.'
user_options = orig.install.user_options + [
('local-oed-spec=', None, 'Override to build package with extracted spec (filepath)'),
]
def __init__(self, *args, **kwargs):
self.filename = 'OpenExposureData_Spec.json'
self.ods_repo = 'OasisLMF/ODS_OpenExposureData'
self.oed_version = OED_VERSION
self.url = f'https://github.com/{self.ods_repo}/releases/download/{self.oed_version}/{self.filename}'
orig.install.__init__(self, *args, **kwargs)
def initialize_options(self):
orig.install.initialize_options(self)
self.local_oed_spec = None
def finalize_options(self):
print("Local OED Spec:", str(self.local_oed_spec))
if self.local_oed_spec is not None:
if not os.path.isfile(self.local_oed_spec):
raise ValueError(f"Local OED Spec '{self.local_oed_spec}' not found")
orig.install.finalize_options(self)
def run(self):
if self.local_oed_spec:
# Install with local json spec
print('OED Version: Local File')
print(f'Install from path: {self.local_oed_spec}')
with open(self.local_oed_spec, 'r') as f:
data = json.load(f)
data['version'] = f'Local-file-install: {self.local_oed_spec}'
else:
# Install from relalse URL
print(f'OED Version: {OED_VERSION}')
print(f'Install from url: {self.url}')
response = urllib.request.urlopen(self.url)
data = json.loads(response.read())
data['version'] = OED_VERSION
download_path = os.path.join(self.build_lib, 'ods_tools', 'data', self.filename)
with open(download_path, 'w+') as f:
json.dump(data, f)
orig.install.run(self)
version = get_version()
readme = get_readme()
reqs = get_install_requirements()
extra_reqs = get_extra_requirements()
setuptools.setup(
name="ods_tools",
version=version,
include_package_data=True,
package_data={
"": ["*.md"], # Copy in readme
"ods_tools": ["data/*", "odtf/data/**/*"] # Copy spec JSON/CSV and YAML mappings
},
entry_points={
'console_scripts': [
'ods_tools=ods_tools.main:main',
]
},
author='Oasis LMF',
author_email="support@oasislmf.org",
packages=setuptools.find_packages(exclude=('tests', 'tests.*', 'tests.*.*')),
package_dir={'ods_tools': 'ods_tools'},
python_requires='>=3.8',
install_requires=reqs,
extras_require={
'extra': extra_reqs,
},
description='Tools to manage ODS files',
long_description=readme,
long_description_content_type='text/markdown',
url='https://github.com/OasisLMF/OpenDataStandards',
cmdclass={
'install': DownloadSpecODS,
},
)