forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-chip-wheel.py
177 lines (146 loc) · 5.66 KB
/
build-chip-wheel.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
#
# Copyright (c) 2020 Project CHIP Authors
# Copyright (c) 2019 Google LLC.
# 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.
#
#
# Description:
# Builds a Python wheel package for CHIP.
#
from __future__ import absolute_import
import argparse
import json
import os
import shutil
from setuptools import Distribution, setup
parser = argparse.ArgumentParser(
description='build the pip package for chip using chip components generated during the build and python source code')
parser.add_argument('--package_name', default='chip',
help='configure the python package name')
parser.add_argument('--build_number', default='0.0',
help='configure the chip build number')
parser.add_argument('--build_dir', help='directory to build in')
parser.add_argument('--dist_dir', help='directory to place distribution in')
parser.add_argument('--manifest', help='list of files to package')
parser.add_argument(
'--plat-name', help='platform name to embed in generated filenames')
parser.add_argument(
'--lib-name', help='the native library to include (if any)', default=None)
args = parser.parse_args()
class InstalledScriptInfo:
"""Information holder about a script that is to be installed."""
def __init__(self, name):
self.name = name
self.installName = os.path.splitext(name)[0]
# Make sure wheel is not considered pure and avoid shared libraries in purelib
# folder.
class BinaryDistribution(Distribution):
def has_ext_modules(foo):
return True
packageName = args.package_name
libName = args.lib_name
chipPackageVer = args.build_number
# Record the current directory at the start of execution.
curDir = os.curdir
manifestFile = os.path.abspath(args.manifest)
buildDir = os.path.abspath(args.build_dir)
distDir = os.path.abspath(args.dist_dir)
# Use a temporary directory within the build directory to assemble the components
# for the installable package.
tmpDir = os.path.join(buildDir, "chip-wheel-components")
manifest = json.load(open(manifestFile, "r"))
try:
#
# Perform a series of setup steps prior to creating the chip package...
#
# Create the temporary components directory.
if os.path.isdir(tmpDir):
shutil.rmtree(tmpDir)
os.makedirs(tmpDir, exist_ok=True)
# Switch to the temporary directory. (Foolishly, setuptools relies on the current directory
# for many of its features.)
os.chdir(tmpDir)
manifestBase = os.path.dirname(manifestFile)
for entry in manifest['files']:
srcDir = os.path.join(manifestBase, entry['src_dir'])
for path in entry['sources']:
srcFile = os.path.join(srcDir, path)
dstFile = os.path.join(tmpDir, path)
os.makedirs(os.path.dirname(dstFile), exist_ok=True)
shutil.copyfile(srcFile, dstFile)
installScripts = [InstalledScriptInfo(script) for script in manifest['scripts']]
for script in installScripts:
os.rename(os.path.join(tmpDir, script.name),
os.path.join(tmpDir, script.installName))
requiredPackages = manifest['package_reqs']
#
# Build the chip package...
#
packages = manifest['packages']
print("packageName: {}".format(packageName))
print("libName: {}".format(libName))
# Invoke the setuptools 'bdist_wheel' command to generate a wheel containing
# the CHIP python packages, shared libraries and scripts.
setup(
name=packageName,
version=chipPackageVer,
description="Python-base APIs and tools for CHIP.",
url="https://github.com/project-chip/connectedhomeip",
license="Apache",
classifiers=[
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
],
python_requires=">=3.7",
packages=packages,
package_dir={
# By default, look in the tmp directory for packages/modules to be included.
'': tmpDir,
},
package_data={
packages[0]: [
libName
]
} if libName else {},
scripts=[name for name in map(
lambda script: os.path.join(tmpDir, script.installName),
installScripts
)],
install_requires=requiredPackages,
options={
'bdist_wheel': {
'universal': False,
# Place the generated .whl in the dist directory.
'dist_dir': distDir,
'py_limited_api': 'cp37',
'plat_name': args.plat_name,
},
'egg_info': {
# Place the .egg-info subdirectory in the tmp directory.
'egg_base': tmpDir
}
},
distclass=BinaryDistribution if libName else None,
script_args=['clean', '--all', 'bdist_wheel']
)
finally:
# Switch back to the initial current directory.
os.chdir(curDir)
# Remove the temporary directory.
if os.path.isdir(tmpDir):
shutil.rmtree(tmpDir)