12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import collections
15
+ from enum import Enum
16
16
import itertools
17
17
import json
18
18
import os
21
21
import sys
22
22
import textwrap
23
23
import venv
24
- from typing import Dict , List , Optional
24
+ from typing import Dict , List , NamedTuple
25
25
26
26
import importlib_metadata
27
27
28
28
29
- EnvFile = collections .namedtuple ("EnvFile" , ["path" , "site_packages_path" ])
29
+ class EnvPathType (Enum ):
30
+ SITE_PACKAGES = 1
31
+ DATA = 2
32
+
33
+
34
+ class EnvFile (NamedTuple ):
35
+ path : pathlib .Path
36
+ env_path : pathlib .Path
37
+ type_ : EnvPathType = EnvPathType .SITE_PACKAGES
30
38
31
39
32
40
def console_script (env_path : pathlib .Path , module : str , func : str ) -> str :
@@ -48,9 +56,9 @@ def path_starts_with(path: pathlib.Path, prefix: pathlib.Path) -> bool:
48
56
return path .parts [: len (prefix .parts )] == prefix .parts
49
57
50
58
51
- def get_site_packages_path (
59
+ def get_env_path (
52
60
workspace : str , path : pathlib .Path , imports : List [pathlib .Path ]
53
- ) -> pathlib . Path :
61
+ ) -> EnvFile :
54
62
# Import prefixes start with the workspace name, which might be the local workspace.
55
63
# We first normalize the given path so that it starts with its workspace name.
56
64
if path .parts [0 ] == ".." :
@@ -62,20 +70,24 @@ def get_site_packages_path(
62
70
63
71
for imp in imports :
64
72
if path_starts_with (wspath , imp ):
65
- return wspath .relative_to (imp )
73
+ return EnvFile (path , wspath .relative_to (imp ))
74
+
75
+ imp_data = imp .parent / "data"
76
+ if path_starts_with (wspath , imp_data ):
77
+ return EnvFile (path , wspath .relative_to (imp_data ), EnvPathType .DATA )
66
78
67
79
if not is_external :
68
80
# If the input wasn't an external path and it didn't match any import prefixes,
69
81
# just return it as given.
70
- return path
82
+ return EnvFile ( path , path )
71
83
72
84
# External file that didn't match imports. Include but warn.
73
85
# We include it as relative to its workspace directory, so strip the first component
74
86
# off wspath.
75
87
include_path = wspath .relative_to (wspath .parts [0 ])
76
88
print (f"Warning: [{ path } ] didn't match any imports. Including as [{ include_path } ]" )
77
89
78
- return include_path
90
+ return EnvFile ( path , include_path )
79
91
80
92
81
93
def is_external (file_ : pathlib .Path ) -> bool :
@@ -123,27 +135,32 @@ def get_files(build_env_input: Dict) -> List[EnvFile]:
123
135
paths = [input_path ]
124
136
125
137
for path in paths :
126
- site_packages_path = get_site_packages_path (workspace , path , imports )
127
- if site_packages_path != path or always_link :
128
- files .append (EnvFile ( path , site_packages_path ) )
138
+ env_file = get_env_path (workspace , path , imports )
139
+ if env_file . env_path != path or always_link :
140
+ files .append (env_file )
129
141
130
142
return files
131
143
132
144
133
145
def is_data_file (file : EnvFile ) -> bool :
134
- return file .site_packages_path .parts [0 ].endswith (".data" )
146
+ return (
147
+ file .type_ == EnvPathType .DATA
148
+ or file .env_path .parts [0 ].endswith (".data" )
149
+ )
135
150
136
151
137
152
def install_data_file (env_path : pathlib .Path , file : EnvFile ) -> None :
138
153
if (
139
- len (file .site_packages_path .parts ) > 2
140
- and file .site_packages_path .parts [1 ] == "scripts"
154
+ len (file .env_path .parts ) > 2
155
+ and file .env_path .parts [1 ] == "scripts"
141
156
):
142
157
install_included_script (env_path , file .path )
158
+ elif file .type_ == EnvPathType .DATA :
159
+ install_site_file (env_path , file )
143
160
144
161
145
162
def install_site_file (site_packages_path : pathlib .Path , file : EnvFile ) -> None :
146
- site_path = site_packages_path / file .site_packages_path
163
+ site_path = site_packages_path / file .env_path
147
164
if not site_path .exists ():
148
165
site_path .parent .mkdir (parents = True , exist_ok = True )
149
166
site_path .symlink_to (file .path .resolve ())
0 commit comments