Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
148 lines (131 loc) · 5.11 KB
/
Copy pathsetup.py
File metadata and controls
148 lines (131 loc) · 5.11 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
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
# Project RoboOrchard
#
# Copyright (c) 2024 Horizon Robotics. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
import os
import subprocess
import warnings
from foxglove_schemas_protobuf.PoseInFrame_pb2 import PoseInFrame
from foxglove_schemas_protobuf.Quaternion_pb2 import Quaternion
from foxglove_schemas_protobuf.Vector3_pb2 import Vector3
from robo_orchard_build.ext.build_ext import RoboOrchardBuildExt
from robo_orchard_build.ext.protobuf import ProtocolExtension
from robo_orchard_build.utils.file import scan_folder
from setuptools import setup
PROJECT_NAME = "robo_orchard_schemas"
PYTHON_BASE_DIR = os.path.abspath(os.path.dirname(__file__))
LICENSE_HEADER = """# Project RoboOrchard
#
# Copyright (c) 2024-2025 Horizon Robotics. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
"""
class VersionPostfixNotExist(Exception):
pass
def get_version() -> str:
with open(os.path.join(PYTHON_BASE_DIR, "VERSION"), "r") as fp:
base_version = fp.read().strip()
def get_full_version(
version_postfix_path, throw_if_not_exist: bool = True
) -> str:
if os.path.exists(version_postfix_path):
with open(version_postfix_path, "r") as fp:
post_fix = fp.read().strip()
return f"{base_version}{post_fix}"
elif throw_if_not_exist:
raise VersionPostfixNotExist(
"VERSION_POSTFIX file not found! please call `make version` first" # noqa: E501
)
else:
warnings.warn(
"VERSION_POSTFIX file not found! using base version only",
UserWarning,
)
return base_version
# generate git hash
try:
repo_git_hash = subprocess.check_output(
"git log -1 --pretty=format:%h".split()
).decode()
except subprocess.CalledProcessError:
# if git command fails, use a placeholder
warnings.warn(
"Failed to get git hash, using 'unknown' as placeholder",
UserWarning,
)
repo_git_hash = "unknown"
version_postfix_path = os.path.join(PYTHON_BASE_DIR, "VERSION_POSTFIX")
try:
full_version = get_full_version(version_postfix_path)
except VersionPostfixNotExist:
# if VERSION_POSTFIX file not found, check the parent directory
version_postfix_path = os.path.join(
os.path.dirname(PYTHON_BASE_DIR), "VERSION_POSTFIX"
)
try:
full_version = get_full_version(version_postfix_path)
except VersionPostfixNotExist:
# if still not found, use base version only
warnings.warn(
"VERSION_POSTFIX file not found in both current and parent directories! " # noqa: E501
"try to using base version.",
UserWarning,
)
full_version = f"{base_version}.dev+{repo_git_hash}"
with open(
os.path.join(PYTHON_BASE_DIR, PROJECT_NAME, "version.py"), "w"
) as fp: # noqa: E501
fp.write(LICENSE_HEADER + "\n")
fp.write(
'"""This file is auto-generated by setup.py, do not edit it!"""\n\n' # noqa: E501
)
fp.write(f'__version__ = "{base_version}"\n')
fp.write(f'__full_version__ = "{full_version}"\n')
fp.write(f'__git_hash__ = "{repo_git_hash}"\n')
return full_version
if __name__ == "__main__":
schema_dir = "./schemas/proto"
setup(
version=get_version(),
ext_modules=[
ProtocolExtension(
name="robo_orchard_schemas",
proto_files=list(
scan_folder(
folder=schema_dir,
ending="proto",
recursive=True,
list_folders=False,
)
),
output_dir=PYTHON_BASE_DIR,
import_dirs=[schema_dir],
dependencies=[Vector3, Quaternion, PoseInFrame],
package_name_mapping={"foxglove": "foxglove_schemas_protobuf"},
)
],
cmdclass={
"build_ext": RoboOrchardBuildExt,
},
)