1
1
#!/usr/bin/env python3
2
2
# ----------------------------------------------------------------------------------------------------
3
3
#
4
- # Copyright (c) 2018, 2018 , Oracle and/or its affiliates. All rights reserved.
4
+ # Copyright (c) 2018, 2023 , Oracle and/or its affiliates. All rights reserved.
5
5
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6
6
#
7
7
# This code is free software; you can redistribute it and/or modify it
29
29
from os .path import exists , expanduser , join , isdir , isfile , realpath , dirname , abspath , basename , getmtime
30
30
from io import StringIO
31
31
32
+ default_jdk_cache_path = join (expanduser ('~' ), '.mx' , 'jdk_cache' )
33
+
32
34
def is_valid_jdk (jdk ):
33
35
"""
34
36
Determines if `jdk` looks like a valid JDK directory.
@@ -243,6 +245,64 @@ def sort_key(self):
243
245
def __lt__ (self , other ):
244
246
return self .sort_key () < other .sort_key ()
245
247
248
+ def choose_jdks (jdk_cache_path = default_jdk_cache_path , only_list = False ):
249
+ jdks = {}
250
+ if exists (jdk_cache_path ):
251
+ with open (jdk_cache_path ) as fp :
252
+ line_num = 1
253
+ for line in fp .readlines ():
254
+ jdk = JDKInfo .load_from_jdk_cache (line .strip (), jdk_cache_path , line_num )
255
+ line_num += 1
256
+ if jdk :
257
+ jdks [jdk .java_home ] = jdk
258
+ base_dir = dirname (jdk .java_home )
259
+ if base_dir .endswith ('/Contents/Home' ):
260
+ base_dir = base_dir [0 :- len ('/Contents/Home' )]
261
+ for java_home in find_jdks_in (base_dir ):
262
+ if java_home not in jdks :
263
+ jdks [java_home ] = JDKInfo .for_java_home (java_home )
264
+ for java_home in find_system_jdks ():
265
+ if java_home not in jdks :
266
+ jdks [java_home ] = JDKInfo .for_java_home (java_home )
267
+
268
+ sorted_jdks = sorted (jdks .values ())
269
+ choices = list (enumerate (sorted_jdks ))
270
+ col2_width = max (((len (jdk .name + '-' + jdk .java_specification_version )) for jdk in sorted_jdks )) + 1
271
+ col3_width = max (((len (jdk .java_vm_version )) for jdk in sorted_jdks )) + 1
272
+ if choices :
273
+ tmp_cache_path_fd , tmp_cache_path = tempfile .mkstemp (dir = dirname (jdk_cache_path ))
274
+ # Windows will complain about tmp_cache_path being in use by another process
275
+ # when calling os.rename if we don't close the file descriptor.
276
+ os .close (tmp_cache_path_fd )
277
+
278
+ java_home = os .environ .get ('JAVA_HOME' , '' )
279
+ extra_java_homes = os .environ .get ('EXTRA_JAVA_HOMES' , '' ).split (os .pathsep )
280
+ with open (tmp_cache_path , 'w' ) as fp :
281
+ for index , jdk in choices :
282
+ col1 = f'[{ index } ]'
283
+ col2 = f'{ jdk .name } -{ jdk .java_specification_version } '
284
+ col3 = jdk .java_vm_version
285
+ col4 = jdk .java_home
286
+ if only_list :
287
+ print (f'{ col2 :{col2_width }} { col3 :{col3_width }} { col4 } ' )
288
+ else :
289
+ line = f'{ col1 :>5} { col2 :{col2_width }} { col3 :{col3_width }} { col4 } '
290
+ if jdk .java_home == java_home :
291
+ line = colorize (f'{ line } {{JAVA_HOME}}' , 'green' )
292
+ elif jdk .java_home in extra_java_homes :
293
+ line = colorize (f'{ line } {{EXTRA_JAVA_HOMES[{ extra_java_homes .index (jdk .java_home )} ]}}' , 'cyan' )
294
+ print (line )
295
+ print (f'{ jdk .as_jdk_cache_line ()} ' , file = fp )
296
+ if only_list :
297
+ os .unlink (tmp_cache_path )
298
+ else :
299
+ os .unlink (jdk_cache_path )
300
+ os .rename (tmp_cache_path , jdk_cache_path )
301
+ choices = {str (index ):jdk for index , jdk in choices }
302
+ jdks = [choices [n ] for n in input ('Select JDK(s) (separate multiple choices by whitespace)> ' ).split () if n in choices ]
303
+ if jdks :
304
+ return jdks
305
+
246
306
if __name__ == '__main__' :
247
307
parser = ArgumentParser (prog = 'select_jdk' , usage = '%(prog)s [options] [<primary jdk> [<secondary jdk>...]]' + """
248
308
Selects values for the JAVA_HOME, EXTRA_JAVA_HOMES and PATH environment variables based on
@@ -297,7 +357,7 @@ def __lt__(self, other):
297
357
else :
298
358
args .shell = 'sh'
299
359
300
- jdk_cache_path = join ( expanduser ( '~' ), '.mx' , 'jdk_cache' )
360
+ jdk_cache_path = default_jdk_cache_path
301
361
if len (args .jdks ) != 0 :
302
362
if args .list :
303
363
print ('warning: ignore --list option since JDKs were specified on the command line' )
@@ -313,59 +373,6 @@ def __lt__(self, other):
313
373
print (f'{ jdk .as_jdk_cache_line ()} ' , file = fp )
314
374
apply_selection (args , abspath (args .jdks [0 ]), [abspath (a ) for a in args .jdks [1 :]])
315
375
else :
316
- jdks = {}
317
- if exists (jdk_cache_path ):
318
- with open (jdk_cache_path ) as fp :
319
- line_num = 1
320
- for line in fp .readlines ():
321
- jdk = JDKInfo .load_from_jdk_cache (line .strip (), jdk_cache_path , line_num )
322
- line_num += 1
323
- if jdk :
324
- jdks [jdk .java_home ] = jdk
325
- base_dir = dirname (jdk .java_home )
326
- if base_dir .endswith ('/Contents/Home' ):
327
- base_dir = base_dir [0 :- len ('/Contents/Home' )]
328
- for java_home in find_jdks_in (base_dir ):
329
- if java_home not in jdks :
330
- jdks [java_home ] = JDKInfo .for_java_home (java_home )
331
- for java_home in find_system_jdks ():
332
- if java_home not in jdks :
333
- jdks [java_home ] = JDKInfo .for_java_home (java_home )
334
-
335
- sorted_jdks = sorted (jdks .values ())
336
- choices = list (enumerate (sorted_jdks ))
337
- col2_width = max (((len (jdk .name + '-' + jdk .java_specification_version )) for jdk in sorted_jdks )) + 1
338
- col3_width = max (((len (jdk .java_vm_version )) for jdk in sorted_jdks )) + 1
339
- if choices :
340
- tmp_cache_path_fd , tmp_cache_path = tempfile .mkstemp (dir = dirname (jdk_cache_path ))
341
- # Windows will complain about tmp_cache_path being in use by another process
342
- # when calling os.rename if we don't close the file descriptor.
343
- os .close (tmp_cache_path_fd )
344
-
345
- java_home = os .environ .get ('JAVA_HOME' , '' )
346
- extra_java_homes = os .environ .get ('EXTRA_JAVA_HOMES' , '' ).split (os .pathsep )
347
- with open (tmp_cache_path , 'w' ) as fp :
348
- for index , jdk in choices :
349
- col1 = f'[{ index } ]'
350
- col2 = f'{ jdk .name } -{ jdk .java_specification_version } '
351
- col3 = jdk .java_vm_version
352
- col4 = jdk .java_home
353
- if args .list :
354
- print (f'{ col2 :{col2_width }} { col3 :{col3_width }} { col4 } ' )
355
- else :
356
- line = f'{ col1 :>5} { col2 :{col2_width }} { col3 :{col3_width }} { col4 } '
357
- if jdk .java_home == java_home :
358
- line = colorize (f'{ line } {{JAVA_HOME}}' , 'green' )
359
- elif jdk .java_home in extra_java_homes :
360
- line = colorize (f'{ line } {{EXTRA_JAVA_HOMES[{ extra_java_homes .index (jdk .java_home )} ]}}' , 'cyan' )
361
- print (line )
362
- print (f'{ jdk .as_jdk_cache_line ()} ' , file = fp )
363
- if args .list :
364
- os .unlink (tmp_cache_path )
365
- else :
366
- os .unlink (jdk_cache_path )
367
- os .rename (tmp_cache_path , jdk_cache_path )
368
- choices = {str (index ):jdk for index , jdk in choices }
369
- jdks = [choices [n ] for n in input ('Select JDK(s) (separate multiple choices by whitespace)> ' ).split () if n in choices ]
370
- if jdks :
371
- apply_selection (args , jdks [0 ].java_home , [jdk .java_home for jdk in jdks [1 :]])
376
+ jdks = choose_jdks (jdk_cache_path , args .list )
377
+ if jdks :
378
+ apply_selection (args , jdks [0 ].java_home , [jdk .java_home for jdk in jdks [1 :]])
0 commit comments