-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
87 lines (74 loc) · 3.01 KB
/
setup.py
File metadata and controls
87 lines (74 loc) · 3.01 KB
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
import os
import re
import subprocess
import glob
import setuptools
from distutils.command.build_py import build_py as build_py_orig
class BuildProto(setuptools.Command):
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
subprocess.run(["mkdir", "-p", "cartaicdproto"])
proto_files = glob.glob('carta-protobuf/*/*.proto')
proto_dirs = set(os.path.dirname(f) for f in proto_files)
includes = [f"-I{d}" for d in proto_dirs]
subprocess.run([
'protoc',
*includes,
'--python_out=cartaicdproto/',
*proto_files,
])
with open('cartaicdproto/__init__.py', 'w') as initfile:
for pb2_file in glob.glob('cartaicdproto/*_pb2.py'):
# There seriously isn't a better way to fix this relative import as of time of writing
with open(pb2_file) as f:
data = f.read()
data = re.sub("^(import .*_pb2)", r"from . \1", data, flags=re.MULTILINE)
with open(pb2_file, 'w') as f:
f.write(data)
# We also automatically import all the submodules to allow discovery
submodule = os.path.splitext(os.path.basename(pb2_file))[0]
initfile.write(f"from . import {submodule} as {submodule[:-4]}\n")
# Automatically parse the version from the docs
with open('carta-protobuf/docs/src/changelog.rst') as f:
data = f.read()
icd_version = re.findall(r' \* - ``(\d+)\.\d+\.\d+``', data)[0]
initfile.write(f"MAJOR_VERSION = {icd_version}\n")
# This is a horrible hack; it may be fixed in the latest protoc
with open('cartaicdproto/enums_pb2.py') as f:
data = f.read()
data = re.sub("^None = 0$", "globals()['None'] = 0", data, flags=re.MULTILINE)
with open('cartaicdproto/enums_pb2.py', 'w') as f:
f.write(data)
class BuildPy (build_py_orig):
def run(self):
self.run_command('build_proto')
super(BuildPy, self).run()
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="cartaicd",
version="0.0.1",
author="Adrianna Pińska",
author_email="adrianna.pinska@gmail.com",
description="Python interface to the CARTA backend",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/idia-astro/carta-python-icd",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
"Operating System :: OS Independent",
],
python_requires='>=3.10',
install_requires=[
"websockets>=9.1",
],
cmdclass={
"build_py": BuildPy,
"build_proto": BuildProto,
},
)