forked from lk-geimfari/mimesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·307 lines (252 loc) · 7.76 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import os
from os.path import (
abspath,
dirname,
exists,
join,
splitext,
relpath,
getsize,
)
import json
import re
from shutil import rmtree
import sys
from distutils.core import setup
from setuptools import Command
from setuptools.command.test import test as TestCommand
VERSION_MINOR_MAX = 10
VERSION_MICRO_MAX = 10
here = abspath(dirname(__file__))
tests_requirements = [
'pytest',
'flake8-builtins',
'flake8-commas',
'flake8-quotes',
'pytest-flake8',
]
# Long description.
with open('PYPI_README.rst', 'r', encoding='utf-8') as f:
readme = f.read()
about = {}
# Get meta-data from __version__.py
with open(join(here, 'mimesis', '__version__.py')) as f:
exec(f.read(), about)
class BaseCommand(Command):
description = ''
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
pass
class Upload(BaseCommand):
"""Support setup.py upload."""
def run(self):
try:
rmtree(os.path.join(here, 'dist'))
except OSError:
pass
os.system('{0} setup.py sdist bdist_wheel --universal'.format(sys.executable))
os.system('twine upload dist/*')
sys.exit()
class PyTest(TestCommand):
"""Custom command for running test using setup.py test"""
user_options = [('pytest-args=', 'a', 'Arguments to pass to py.test')]
def initialize_options(self):
TestCommand.initialize_options(self)
self.pytest_args = []
def finalize_options(self):
TestCommand.finalize_options(self)
self.test_args = []
self.test_suite = True
def run_tests(self):
import pytest
errno = pytest.main(self.pytest_args)
exit(errno)
class Minimizer(BaseCommand):
"""Minify content of all json file for all locales.
"""
def initialize_options(self):
"""Find all files of all locales.
"""
self.paths = []
self.data_path = '/mimesis/data'
self.separators = (',', ':')
self.data_dir = here + self.data_path
self.before_total = 0
self.after_total = 0
for root, _, files in os.walk(self.data_dir):
for file in sorted(files):
if splitext(file)[1] == '.json':
self.paths.append(
join(
relpath(root, self.data_dir),
file,
)
)
@staticmethod
def size_of(num):
for unit in ['B', 'KB', 'MB']:
if abs(num) < 1024.0:
return "%3.1f%s" % (num, unit)
num = num / 1024.0
return "%.1f" % num
def minify(self, file_path):
size_before = getsize(file_path)
self.before_total += size_before
size_before = self.size_of(size_before)
with open(file_path, 'r', 1) as f:
json_text = json.loads(f.read())
minimized = json.dumps(
json_text, separators=self.separators, ensure_ascii=False)
if len(file_path) > 0:
output_path = abspath(file_path)
abs_path = abspath(dirname(output_path))
if not exists(abs_path):
os.makedirs(abs_path)
with open(output_path, 'w+', 1) as f:
f.write(minimized)
size_after = getsize(file_path)
self.after_total += size_after
size_after = self.size_of(size_after)
json_file = '/'.join(file_path.split('/')[-2:])
template = "\033[34m{}\033[0m : " \
"\033[92mminimized\033[0m : " \
"\033[33m{}\033[0m -> \033[92m{}\033[0m".format(
json_file,
size_before,
size_after,
)
print(template)
def run(self):
"""Start json minimizer and exit when all json
files was minimized.
"""
for rel_path in sorted(self.paths):
file_path = join(self.data_dir, rel_path)
self.minify(file_path)
after = self.size_of(self.after_total)
before = self.size_of(self.before_total)
saved = self.size_of(self.before_total - self.after_total)
template = '\nTotal: ' \
'\033[92m{}\033[0m -> \033[92m{}\033[0m. ' \
'Compressed: \033[92m{}\033[0m\n'.format(before, after, saved)
print(template)
class Versioner(BaseCommand):
"""Custom command for versioning"""
def initialize_options(self):
self.current = about['__version__']
print('Previous version: '
'\033[33m{}\033[0m.\n'.format(self.current))
@staticmethod
def automatically(version):
"""Automatically increment version string.
:param version: Current version.
:return: Next version.
"""
major, minor, micro = [
int(i) for i in version.split('.')
]
if VERSION_MICRO_MAX > micro:
micro += 1
elif VERSION_MICRO_MAX == micro:
micro = 0
minor += 1
elif VERSION_MINOR_MAX > minor:
minor += 1
elif VERSION_MINOR_MAX == minor:
micro, minor = 0, 0
major += 1
if VERSION_MINOR_MAX < minor:
minor, micro = 0, 0
major += 1
return '.'.join([str(i) for i
in (major, minor, micro)])
def rewrite(self, version=None):
if not version:
version = self.current
with open(join(here, 'mimesis', '__version__.py'), 'r+') as f:
version_str = '__version__ = \'{}\''.format(version)
regexp = r'__version__ = .*'
meta = re.sub(regexp, version_str, f.read())
f.seek(0)
f.write(meta)
f.truncate()
print('Updated! Current version is: '
'\033[34m{}\033[0m.\n'.format(version))
exit()
def run(self):
response = input('Are you sure? (yes/no): ')
if response.lower() in ('yes', 'y'):
self.rewrite(
self.automatically(
self.current,
),
)
setup(
name=about['__title__'],
version=about['__version__'],
description=about['__description__'],
long_description=readme,
author=about['__author__'],
author_email=about['__author_email__'],
url=about['__url__'],
license=about['__license__'],
packages=[
'mimesis',
'mimesis.data',
'mimesis.data.int',
'mimesis.builtins',
'mimesis.constants',
'mimesis.providers',
],
keywords=[
'fake',
'mock',
'data',
'populate',
'database',
'testing',
'generate',
'mimesis',
'dummy',
],
package_data={
'mimesis': [
'data/*/*',
],
},
exclude_package_data={
'mimesis': [
# It's for development.
'data/locale_template/*'
],
},
data_files=[
('', ['LICENSE',
'PYPI_README.rst',
],
),
],
classifiers=[
'License :: OSI Approved :: MIT License',
'Intended Audience :: Developers',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: Implementation :: CPython',
'Operating System :: OS Independent',
'Topic :: Software Development',
'Topic :: Software Development :: Testing',
],
tests_require=tests_requirements,
cmdclass={
'test': PyTest,
'versioner': Versioner,
'minify': Minimizer,
'upload': Upload,
},
)