forked from NOAA-ORR-ERD/OilLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·196 lines (159 loc) · 6.25 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
#!/usr/bin/env python
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
from git import Repo
from git.exc import InvalidGitRepositoryError
here = os.path.abspath(os.path.dirname(__file__))
README = open(os.path.join(here, 'README.md')).read()
pkg_name = 'oil_library'
pkg_version = '1.1.1'
# try to get update date from repo
try:
repo = Repo('.')
try:
branch_name = repo.active_branch.name
except TypeError:
branch_name = 'no-branch'
try:
last_update = repo.iter_commits().next().committed_datetime.isoformat()
except:
last_update = next(repo.iter_commits()).committed_datetime.isoformat()
except InvalidGitRepositoryError:
# not builiding in a valid git repo
# use today's date.
print ("not in a valid git repo -- using today's date as build date")
branch_name = 'no-branch'
last_update = datetime.now().isoformat()
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
print ('got this version:', oil_library.__file__)
print ('calling initializedb.make_db() from the code')
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)
s = setup(name=pkg_name,
version=pkg_version,
description=('{}: The NOAA library of oils and their properties.\n'
'Branch: {}\n'
'LastUpdate: {}'
.format(pkg_name, branch_name, last_update)),
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 s.script_args and '--uninstall' not in s.script_args:
init_db()