forked from inspirehep/inspire-schemas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
240 lines (195 loc) · 6.86 KB
/
setup.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# -*- coding: utf-8 -*-
#
# This file is part of INSPIRE-SCHEMAS.
# Copyright (C) 2016, 2017 CERN.
#
# INSPIRE-SCHEMAS is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# INSPIRE-SCHEMAS is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with INSPIRE-SCHEMAS; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.
"""INSPIRE schemas and related tools bundle."""
from __future__ import absolute_import, division, print_function
import json
import os
import shutil
from setuptools import find_packages, setup
from setuptools.command import develop, install, sdist
URL = 'https://github.com/inspirehep/inspire-schemas'
class CustomSdist(sdist.sdist):
def run(self, *args, **kwargs):
_generate_json_schemas()
# sdist is not a new class object, we can't use super
return sdist.sdist.run(self, *args, **kwargs)
class CustomInstall(install.install):
def run(self, *args, **kwargs):
_generate_json_schemas()
# install is not a new class object, we can't use super
return install.install.run(self, *args, **kwargs)
class CustomDevelop(develop.develop):
def run(self, *args, **kwargs):
_generate_country_js_file()
_generate_json_schemas()
# develop is not a new class object, we can't use super
return develop.develop.run(self, *args, **kwargs)
def _resolve_json_schema(json_schema, path):
import yaml
if isinstance(json_schema, list):
json_schema = [
_resolve_json_schema(item, path) for item in json_schema
]
elif isinstance(json_schema, dict):
for key in json_schema:
if key == '$ref' and not isinstance(json_schema[key], dict):
subschema_path = os.path.join(path, json_schema[key])
subschema_path = os.path.splitext(subschema_path)[0]
with open(subschema_path+'.yml', 'rb') as yaml_fd:
raw_data = yaml_fd.read()
data = yaml.load(raw_data)
data = _resolve_json_schema(data, os.path.join(path, os.path.dirname(json_schema[key])))
return data
else:
json_schema[key] = _resolve_json_schema(json_schema[key], path)
return json_schema
def _yaml2json(yaml_file, json_file):
import yaml
with open(yaml_file, 'rb') as yaml_fd:
raw_data = yaml_fd.read()
data = yaml.load(raw_data)
with open(json_file, 'w') as json_fd:
json.dump(
data, json_fd, indent=4, separators=(',', ': '), sort_keys=True
)
json_fd.write('\n')
path = os.path.dirname(yaml_file)
resolved_json_object = _resolve_json_schema(data, path)
with open(json_file, 'w') as json_fd:
json.dump(
resolved_json_object,
json_fd,
indent=4,
separators=(',', ': '),
sort_keys=True,
)
json_fd.write('\n')
def _find(basepath, extension='.yml'):
basepath, dirs, files = next(os.walk(basepath))
for filename in files:
if filename.endswith(extension):
yield os.path.join(basepath, filename)
for dirname in dirs:
for filename in _find(
basepath=os.path.join(basepath, dirname),
extension=extension,
):
yield filename
def _generate_country_js_file():
import json
from inspire_schemas.utils import COUNTRY_CODE_TO_NAME
schemas_js_dir = os.path.join(
os.path.dirname(__file__),
'js'
)
with open(os.path.join(schemas_js_dir, 'countryCodeToName.json'), 'w+') as json_fd:
json.dump(COUNTRY_CODE_TO_NAME, json_fd)
def _generate_country_code(base_path):
import yaml
from inspire_schemas.utils import COUNTRY_CODE_TO_NAME
countries_code_path = os.path.join(base_path, 'elements', 'country_code.yml')
data = {
'maxLength': 2,
'minLength': 2,
'title': 'ISO 3166-1 or 3166-3 alpha 2 country code',
'type': 'string',
'enum': list(COUNTRY_CODE_TO_NAME.keys())
}
with open(countries_code_path, 'w') as yaml_fd:
yaml.dump(data, yaml_fd)
def _generate_json_schemas():
schemas_dir = os.path.join(
os.path.dirname(__file__),
'inspire_schemas/records'
)
_generate_country_code(schemas_dir)
for yaml_file in _find(basepath=schemas_dir, extension='.yml'):
json_file = yaml_file.rsplit('.', 1)[0] + '.json'
_yaml2json(yaml_file=yaml_file, json_file=json_file)
build_requires = [
'autosemver==0.5.5',
'pyyaml',
]
tests_require = [
'check-manifest',
'coverage',
'isort~=4.0,>=4.3.0',
'pytest-cache',
'pytest-cov==2.6.1',
'pytest~=3.0,>=3.6.0',
'pytest-pep8',
'mock',
'idutils',
]
docs_require = [
'jsonschema2rst>=0.1',
'Sphinx',
]
extras_require = {
'docs': docs_require,
'tests': tests_require,
'tests:python_version=="2.7"': [
'unicode-string-literal~=1.0,>=1.1',
],
}
def do_setup():
setup(
author='CERN',
author_email='[email protected]',
cmdclass={
'sdist': CustomSdist,
'install': CustomInstall,
'develop': CustomDevelop,
},
description='Inspire JSON schemas and utilities to use them.',
install_requires=[
'bleach~=3.0,>=3.2.1',
'Unidecode~=1.0,>=1.0.22',
'autosemver==0.5.5',
'jsonschema~=2.0,>=2.6.0',
'idutils',
'inspire-utils~=3.0,>=3.0.0',
'isodate',
'pyyaml',
'pytz',
'rfc3987',
'six',
# requests requires a urllib3 version <1.26 but not 1.25.0 and 1.25.1 we pin it down here to solve dependency problems
'urllib3>=1.21.1,<1.26,!=1.25.0,!=1.25.1',
],
tests_require=tests_require,
extras_require=extras_require,
build_requires=build_requires,
license='GPLv2',
name='inspire-schemas',
package_data={'': ['*.json', 'CHANGELOG', 'AUTHORS']},
packages=find_packages(),
setup_requires=['autosemver==0.5.5', 'pyyaml'],
url=URL,
bugtracker_url=URL + '/issues/',
zip_safe=False,
autosemver=True,
)
if __name__ == '__main__':
do_setup()