10000 Check if input_file is directory and then add all files within that d… · AutomatedTester/rules_python@0cd570e · GitHub
[go: up one dir, main page]

Skip to content

Commit 0cd570e

Browse files
AutomatedTesterJonathon Belotti
andauthored
Check if input_file is directory and then add all files within that directory (bazel-contrib#336)
If in a dependency tree a directory is used as out this is passed as input_file to wheelmaker which then generates an unhandled error as it tries to write the directory as a file. Co-authored-by: Jonathon Belotti <jonathon@canva.com>
1 parent 5eb0de8 commit 0cd570e

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

experimental/examples/wheel/BUILD

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ py_library(
3131
],
3232
)
3333

34+
py_library(
35+
name = "main_with_gen_data",
36+
srcs = ["main.py"],
37+
data = [
38+
":gen_dir",
39+
],
40+
)
41+
42+
genrule(
43+
name = "gen_dir",
44+
outs = ["someDir"],
45+
cmd = "mkdir -p $@ && touch $@/foo.py",
46+
)
47+
3448
# Package just a specific py_libraries, without their dependencies
3549
py_wheel(
3650
name = "minimal_with_py_library",
@@ -53,6 +67,12 @@ py_package(
5367
deps = [":main"],
5468
)
5569

70+
py_package(
71+
name = "example_pkg_with_data",
72+
packages = ["experimental.examples.wheel"],
73+
deps = [":main_with_gen_data"]
74+
)
75+
5676
py_wheel(
5777
name = "minimal_with_py_package",
5878
# Package data. We're building "example_minimal_package-0.0.1-py3-none-any.whl"
@@ -146,6 +166,16 @@ py_wheel(
146166
],
147167
)
148168

169+
py_wheel(
170+
name = "use_genrule_with_dir_in_outs",
171+
distribution = "use_genrule_with_dir_in_outs",
172+
python_tag = "py3",
173+
version = "0.0.1",
174+
deps = [
175+
":example_pkg_with_data"
176+
]
177+
)
178+
149179
py_wheel(
150180
name = "python_abi3_binary_wheel",
151181
abi = "abi3",
@@ -168,5 +198,6 @@ py_test(
168198
":minimal_with_py_package",
169199
":python_abi3_binary_wheel",
170200
":python_requires_in_a_package",
201+
":use_genrule_with_dir_in_outs",
171202
],
172203
)

experimental/examples/wheel/wheel_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,21 @@ def test_python_abi3_binary_wheel(self):
219219
""",
220220
)
221221

222+
def test_genrule_creates_directory_and_is_included_in_wheel(self):
223+
filename = os.path.join(os.environ['TEST_SRCDIR'],
224+
'rules_python', 'experimental',
225+
'examples', 'wheel',
226+
'use_genrule_with_dir_in_outs-0.0.1-py3-none-any.whl')
227+
228+
with zipfile.ZipFile(filename) as zf:
229+
self.assertEquals(
230+
zf.namelist(),
231+
['experimental/examples/wheel/main.py',
232+
'experimental/examples/wheel/someDir/foo.py',
233+
'use_genrule_with_dir_in_outs-0.0.1.dist-info/WHEEL',
234+
'use_genrule_with_dir_in_outs-0.0.1.dist-info/METADATA',
235+
'use_genrule_with_dir_in_outs-0.0.1.dist-info/RECORD'])
236+
222237

223238
if __name__ == '__main__':
224239
unittest.main()

experimental/tools/wheelmaker.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ def arcname_from(name):
102102

103103
return normalized_arcname
104104

105+
if os.path.isdir(real_filename):
106+
directory_contents = os.listdir(real_filename)
107+
for file_ in directory_contents:
108+
self.add_file("{}/{}".format(package_filename, file_),
109+
"{}/{}".format(real_filename, file_))
110+
return
111+
105112
arcname = arcname_from(package_filename)
106113

107114
self._zipfile.write(real_filename, arcname=arcname)

0 commit comments

Comments
 (0)
0