@@ -27,6 +27,11 @@ public class CondaPackageRegistry {
27
27
private static final String DEFAULT_PYPI_INDEX = "https://pypi.org/simple" ;
28
28
private static final String CONDA_YAML_FILE = "conda-desc.yaml" ;
29
29
30
+ private static final String PIP_INDEX_URL_FLAG = "--index-url" ;
31
+ private static final String PIP_EXTRA_INDEX_URL_FLAG = "--extra-index-url" ;
32
+ private static final String PIP_TRUSTED_HOST_FLAG = "--trusted-host" ;
33
+ private static final String PIP_NO_DEPS_FLAG = "--no-deps" ;
34
+
30
35
// TODO(artolord) remove this ugly hack after removing conda.yaml
31
36
private static final Map <String , String > NAME_TO_PYTHON_VERSION = Map .of (
32
37
"py37" , "3.7" ,
@@ -95,7 +100,8 @@ private record CondaEnv(
95
100
String pythonVersion ,
96
101
String pypiIndex ,
97
102
boolean noDeps ,
98
- List <String > extraIndexUrls
103
+ List <String > extraIndexUrls ,
104
+ List <String > trustedHosts
99
105
) {}
100
106
101
107
/**
@@ -111,12 +117,13 @@ public String buildCondaYaml(String condaYaml) {
111
117
throw new IllegalArgumentException ("Cannot build env from yaml" );
112
118
}
113
119
114
- return buildCondaYaml (env .packages , env .pythonVersion , env .pypiIndex , env .noDeps , env .extraIndexUrls );
120
+ return buildCondaYaml (env .packages , env .pythonVersion , env .pypiIndex , env .noDeps , env .extraIndexUrls ,
121
+ env .trustedHosts );
115
122
}
116
123
117
124
@ Nullable
118
125
private String buildCondaYaml (Map <String , Package > packages , String pythonVersion , String pypiIndex ,
119
- boolean noDeps , List <String > extraIndexUrls )
126
+ boolean noDeps , List <String > extraIndexUrls , List < String > trustedHosts )
120
127
{
121
128
try {
122
129
var installedEnv = envs .values ().stream ()
@@ -150,15 +157,15 @@ private String buildCondaYaml(Map<String, Package> packages, String pythonVersio
150
157
}
151
158
152
159
return buildYaml (new CondaEnv (installedEnv .name , packages , installedEnv .pythonVersion ,
153
- pypiIndex , noDeps , extraIndexUrls ));
160
+ pypiIndex , noDeps , extraIndexUrls , trustedHosts ));
154
161
}
155
162
156
163
} catch (Exception e ) {
157
164
LOG .error ("Error while building conda yaml for packages {}: " , packages , e );
158
165
}
159
166
160
167
return buildYaml (new CondaEnv ("py" + pythonVersion , packages , pythonVersion , pypiIndex , noDeps ,
161
- extraIndexUrls ));
168
+ extraIndexUrls , trustedHosts ));
162
169
}
163
170
164
171
public void notifyInstalled (String condaYaml ) {
@@ -217,6 +224,7 @@ CondaEnv build(String condaYaml) {
217
224
String pypiIndex = null ;
218
225
boolean noDeps = false ;
219
226
List <String > extraIndexUrls = new ArrayList <>();
227
+ List <String > trustedHosts = new ArrayList <>();
220
228
221
229
for (var dep : deps ) {
222
230
if (dep instanceof String ) {
@@ -239,27 +247,34 @@ CondaEnv build(String condaYaml) {
239
247
}
240
248
241
249
//noinspection unchecked
242
- for (var pipDep : (List <Object >) pipDeps ) {
243
- if (!(pipDep instanceof String )) {
250
+ for (var rawPipDep : (List <Object >) pipDeps ) {
251
+ if (!(rawPipDep instanceof String pipDep )) {
244
252
return null ;
245
253
}
246
254
247
- if (((String ) pipDep ).startsWith ("--index-url" )) {
248
- var parts = ((String ) pipDep ).split (" " );
249
- pypiIndex = parts .length > 1 ? parts [1 ] : null ;
255
+ if (pipDep .startsWith (PIP_INDEX_URL_FLAG )) {
256
+ pypiIndex = parsePipOptionValue (PIP_INDEX_URL_FLAG , pipDep );
257
+ continue ;
258
+ }
259
+ if (pipDep .startsWith (PIP_EXTRA_INDEX_URL_FLAG )) {
260
+ final var extraIndex = parsePipOptionValue (PIP_EXTRA_INDEX_URL_FLAG , pipDep );
261
+ if (extraIndex != null ) {
262
+ extraIndexUrls .add (extraIndex );
263
+ }
250
264
continue ;
251
265
}
252
- if (((String ) pipDep ).startsWith ("--extra-index-url" )) {
253
- var parts = ((String ) pipDep ).split (" " );
254
- var extraIndex = parts .length > 1 ? parts [1 ] : null ;
255
- extraIndexUrls .add (extraIndex );
266
+ if (pipDep .startsWith (PIP_TRUSTED_HOST_FLAG )) {
267
+ final var trustedHost = parsePipOptionValue (PIP_TRUSTED_HOST_FLAG , pipDep );
268
+ if (trustedHost != null ) {
269
+ trustedHosts .add (trustedHost );
270
+ }
256
271
continue ;
257
272
}
258
- if ((( String ) pipDep ) .startsWith ("--no-deps" )) {
273
+ if (pipDep .startsWith (PIP_NO_DEPS_FLAG )) {
259
274
noDeps = true ;
260
275
}
261
276
262
- var dat = (( String ) pipDep ) .split (VERSION_REGEX , SPLIT_LIMIT );
277
+ var dat = pipDep .split (VERSION_REGEX , SPLIT_LIMIT );
263
278
264
279
var pkgName = normalizePkgName (dat [0 ]);
265
280
if (dat .length == 1 ) {
@@ -291,19 +306,33 @@ CondaEnv build(String condaYaml) {
291
306
}
292
307
293
308
return new CondaEnv (name , pkgs , pythonVersion , pypiIndex == null ? DEFAULT_PYPI_INDEX : pypiIndex , noDeps ,
294
- extraIndexUrls );
309
+ extraIndexUrls , trustedHosts );
310
+ }
311
+
312
+ @ Nullable
313
+ private String parsePipOptionValue (String optionName , String optionString ) {
314
+ String [] optionParts = optionString .split ("\\ s+" , 2 );
315
+ if (optionParts .length == 1 || !optionParts [0 ].equals (optionName )) {
316
+ LOG .warn ("Unable to parse value for option '{}' from '{}'" , optionName , optionString );
317
+ return null ;
318
+ }
319
+ return optionParts [1 ];
295
320
}
296
321
297
322
private String buildYaml (CondaEnv env ) {
298
323
var pkgs = new ArrayList <>();
299
- pkgs .add ("--index-url " + env .pypiIndex );
324
+ pkgs .add (PIP_INDEX_URL_FLAG + " " + env .pypiIndex );
300
325
301
326
if (env .noDeps ) {
302
- pkgs .add ("--no-deps" );
327
+ pkgs .add (PIP_NO_DEPS_FLAG );
303
328
}
304
329
305
330
for (var extraUrl : env .extraIndexUrls ) {
306
- pkgs .add ("--extra-index-url " + extraUrl );
331
+ pkgs .add (PIP_EXTRA_INDEX_URL_FLAG + " " + extraUrl );
332
+ }
333
+
334
+ for (var trustedHost : env .trustedHosts ) {
335
+ pkgs .add (PIP_TRUSTED_HOST_FLAG + " " + trustedHost );
307
336
}
308
337
309
338
for (var p : env .packages .values ()) {
0 commit comments