-
Notifications
You must be signed in to change notification settings - Fork 13
/
setup.py
168 lines (138 loc) · 5.29 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
# Copyright (c) 2021 CNES/JPL
#
# All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
import datetime
import os
import pathlib
import re
import subprocess
import setuptools
def execute(cmd):
"""Executes a command and returns the lines displayed on the standard
output."""
process = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
return process.stdout.read().decode()
def update_meta(path, version):
"""Updating the version number description in conda/meta.yaml."""
with open(path, "r") as stream:
lines = stream.readlines()
pattern = re.compile(r'{% set version = ".*" %}')
for idx, line in enumerate(lines):
match = pattern.search(line)
if match is not None:
lines[idx] = '{%% set version = "%s" %%}\n' % version
with open(path, "w") as stream:
stream.write("".join(lines))
def update_sphinx_conf(conf, version, year):
"""Update the Sphinx configuration file."""
with open(conf, "r") as stream:
lines = stream.readlines()
pattern = re.compile(r'(\w+)\s+=\s+(.*)')
for idx, line in enumerate(lines):
match = pattern.search(line)
if match is not None:
if match.group(1) == 'version':
lines[idx] = "version = %r\n" % version
elif match.group(1) == 'release':
lines[idx] = "release = %r\n" % version
elif match.group(1) == 'copyright':
lines[idx] = "copyright = '(%s, CNES/JPL)'\n" % year
with open(conf, "w") as stream:
stream.write("".join(lines))
def read_version():
"""Returns the software version."""
module = pathlib.Path('swot_simulator', 'version.py')
# If the ".git" directory exists, this function is executed in the
# development environment, otherwise it's a release.
if not pathlib.Path('.git').exists():
pattern = re.compile(r'return "(\d+\.\d+\.\d+)"')
with open(module, "r") as stream:
for line in stream:
match = pattern.search(line)
if match:
return match.group(1)
raise AssertionError("The version module is invalid")
stdout = execute("git describe --tags --dirty --long --always").strip()
pattern = re.compile(r'([\w\d\.]+)-(\d+)-g([\w\d]+)(?:-(dirty))?')
match = pattern.search(stdout)
if match is None:
# No tag found, use the last commit
pattern = re.compile(r'([\w\d]+)(?:-(dirty))?')
match = pattern.search(stdout)
assert match is not None, f"Unable to parse git output {stdout!r}"
version = "0.0.0"
sha1 = match.group(1)
else:
version = match.group(1)
commits = int(match.group(2))
sha1 = match.group(3)
if commits != 0:
version += f".dev{commits}"
stdout = execute("git log %s -1 --format=\"%%H %%at\"" % sha1)
stdout = stdout.strip().split()
date = datetime.datetime.utcfromtimestamp(int(stdout[1]))
# Conda configuration files are not present in the distribution, but only
# in the GIT repository of the source code.
meta = pathlib.Path('conda', 'meta.yaml')
if meta.exists():
update_meta(meta, version)
# Updating the version number description for sphinx
update_sphinx_conf(pathlib.Path('docs', 'source', 'conf.py'), version,
date.year)
# Finally, write the file containing the version number.
with open(module, 'w') as handler:
handler.write('''
# Copyright (c) 2021 CNES/JPL
#
# All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
"""
Get software version information
================================
"""
def release() -> str:
"""Returns the software version number"""
return "{version}"
def date() -> str:
"""Returns the creation date of this release"""
return "{date}"
'''.format(version=version, date=date.strftime("%d %B %Y")))
return version
def main():
"""Main function."""
here = pathlib.Path(__file__).parent.absolute()
os.chdir(here)
with open("README.rst", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="swot_simulator",
include_package_data=True,
version=read_version(),
author="CNES/JPL",
author_email="[email protected]",
description="Simulate SWOT measurements on sea surface height with "
"simulated errors",
long_description=long_description,
long_description_content_type="text/x-rst",
url="https://github.com/CNES/swot_simulator",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
entry_points='''
[console_scripts]
swot_simulator=swot_simulator.cli.launcher:main
''',
python_requires='>=3.6',
install_requires=[
"python-dateutil", "distributed", "netCDF4", "numba", "numpy",
"pyinterp>=2022.9.1", "scipy", "xarray"
])
if __name__ == "__main__":
main()