This repository has been archived by the owner on Jan 3, 2022. It is now read-only.
forked from NOAA-ORR-ERD/OilLibrary
-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.py
executable file
·214 lines (173 loc) · 6.74 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
#!/usr/bin/env python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import sys
import fnmatch
import shutil
from datetime import datetime
from setuptools import setup, find_packages
from distutils.command.clean import clean
from setuptools import Command
from setuptools.command.build_py import build_py
from setuptools.command.test import test as TestCommand
here = os.path.abspath(os.path.dirname(__file__))
README = open(os.path.join(here, 'README.rst')).read()
pkg_name = 'oil_library'
def get_version():
"""
return the version number from the __init__
"""
for line in open(os.path.join(pkg_name, "__init__.py")):
if line.startswith("__version__"):
version = line.strip().split('=')[1].strip().strip("'").strip('"')
return version
raise ValueError("can't find version string in __init__")
pkg_version = get_version()
def get_repo_data():
try:
from git import Repo
from git.exc import InvalidGitRepositoryError
repo = Repo('.')
try:
branch_name = repo.active_branch.name
except TypeError:
branch_name = 'no-branch'
last_update = next(repo.iter_commits()).committed_datetime.isoformat()
except: # bare excepts are not good, but in this case,
# anything that goes wrong results in the same thing.
print("something wrong with accessing git repo -- using today's date as build date")
branch_name = 'no branch'
last_update = datetime.now().isoformat()
return branch_name, last_update
def clean_files(del_db=False):
src = os.path.join(here, r'oil_library')
to_rm = []
for root, _dirnames, filenames in os.walk(src):
for filename in fnmatch.filter(filenames, '*.pyc'):
to_rm.append(os.path.join(root, filename))
to_rm.extend([os.path.join(here, '{0}.egg-info'.format(pkg_name)),
os.path.join(here, 'build'),
os.path.join(here, 'dist')])
if del_db:
to_rm.extend([os.path.join(src, 'OilLib.db')])
for f in to_rm:
try:
if os.path.isdir(f):
shutil.rmtree(f)
else:
os.remove(f)
except Exception:
pass
print("Deleting {0} ..".format(f))
def init_db():
if os.path.exists(os.path.join(here, 'oil_library', 'OilLib.db')):
print('OilLibrary database exists - do not remake!')
else:
try:
import oil_library.initializedb
oil_library.initialize_console_log(level='info')
print("setting up logger to dump to: DB_build_Log.txt")
oil_library.add_file_log("DB_build_Log.txt", level='info')
print("calling initializedb.make_db()")
oil_library.initializedb.make_db()
print('OilLibrary database successfully generated from file!')
except Exception:
print('OilLibrary database generation failed')
raise
class cleanall(clean):
description = "cleans files generated by 'develop' and SQL lite DB file"
def run(self):
clean.run(self)
clean_files(del_db=True)
class remake_oil_db(Command):
'''
Custom command to reconstruct the oil_library database from flat file
'''
description = "remake oil_library SQL lite DB from flat file"
user_options = user_options = []
def initialize_options(self):
"""init options"""
pass
def finalize_options(self):
"""finalize options"""
pass
def run(self):
to_rm = os.path.join(here, r'oil_library', 'OilLib.db')
try:
os.remove(to_rm)
except OSError as e:
if e.errno == 2:
pass
else:
raise
print("Deleting {0} ..".format(to_rm))
# ret = call(db_init_script_path())
print("********\ncreating a new DB with direct call into package\n********")
init_db()
class PyTest(TestCommand):
"""So we can run tests with ``setup.py test``"""
def finalize_options(self):
TestCommand.finalize_options(self)
# runs the tests from inside the installed package
self.test_args = []
self.test_suite = True
def run_tests(self):
# no idea why it doesn't work to call pytest.main
# import pytest
# errno = pytest.main(self.test_args)
errno = os.system('py.test --pyargs oil_library')
sys.exit(errno)
class BuildPyCommand(build_py):
""" Custom build command. """
def run(self):
init_db()
# build_py is an old-style class, so we can't use super()
build_py.run(self)
DESCRIPTION = ('{}: The NOAA library of oils and their properties.\n'
'Branch: {}\n'
'LastUpdate: {}'
.format(pkg_name, *get_repo_data())
)
setup(name=pkg_name,
version=pkg_version,
description=DESCRIPTION,
long_description=README,
author='ADIOS/GNOME team at NOAA ORR',
author_email='[email protected]',
url='',
keywords='adios weathering oilspill modeling',
packages=find_packages(),
include_package_data=True,
package_data={'oil_library': ['OilLib.db',
'OilLib',
'OilLibTest',
'OilLibNorway',
'blacklist_whitelist.txt',
'tests/*.py',
'tests/sample_data/*']},
cmdclass={'remake_oil_db': remake_oil_db,
'cleanall': cleanall,
'test': PyTest,
'build_py': BuildPyCommand,
},
entry_points={'console_scripts': [('initialize_OilLibrary_db = '
'oil_library.initializedb'
':make_db'),
('diff_import_files = '
'oil_library.scripts.oil_import'
':diff_import_files_cmd'),
('add_header_to_import_file = '
'oil_library.scripts.oil_import'
':add_header_to_csv_cmd'),
('get_import_record_dates = '
'oil_library.scripts.oil_import'
':get_import_record_dates_cmd'),
],
},
zip_safe=False,
)
if 'develop' in sys.argv and '--uninstall' not in sys.argv:
init_db()