-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpip-pip
executable file
·1491 lines (1324 loc) · 50.4 KB
/
pip-pip
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- mode:python -*-
# $PIP_license: <Simplified BSD License>
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
# $
# $RIKEN_copyright: Riken Center for Computational Sceience (R-CCS),
# System Software Development Team, 2020-2021
# $
# $PIP_PIP: Version 1.0.0$
#
# $Author: Atsushi Hori (R-CCS)
# Query: [email protected]
# User ML: [email protected]
# $
"PiP-pip: PiP (Process-in-Process) package installation program"
from __future__ import print_function
import datetime
import time
import argparse
import shutil
import os
import sys
import subprocess as sp
import re
arch_supported = [ 'x86_64', 'aarch64' ]
how_supported = [ 'docker', 'spack', 'github', 'yum' ]
centos_supported = [ 7, 8 ]
pipver_supported = [ -1, 2, 3 ]
# not ready list: { how : [ ( arch, centos, pip ), ... ], ... }
not_ready = { 'yum' : [ ( 'x86_64', 7, 1 ),
( 'x86_64', 7, 2 ),
( 'x86_64', 7, 3 ),
( 'x86_64', 8, 1 ),
( 'x86_64', 8, 2 ),
( 'x86_64', 8, 3 ),
( 'aarch64', 7, 1 ),
( 'aarch64', 7, 2 ),
( 'aarch64', 7, 3 ),
( 'aarch64', 8, 1 ),
( 'aarch64', 8, 2 ),
( 'aarch64', 8, 3 ) ],
'docker' : [ ( 'x86_64', 7, 1 ),
( 'x86_64', 8, 1 ),
( 'aarch64', 7, 1 ),
( 'aarch64', 8, 1 ) ],
'spack' : [ ( 'x86_64', 7, 1 ),
( 'x86_64', 8, 1 ),
( 'aarch64', 7, 1 ),
( 'aarch64', 8, 1 ) ],
'github': [ ( 'x86_64', 8, 1 ),
( 'aarch64', 8, 1 ) ]
}
git_url_tab = { 'github' : 'https://github.com/RIKEN-SysSoft' }
git_repo_tab = { 'github' : { 'glibc': 'PiP-glibc.git',
'pip' : 'PiP.git',
'gdb' : 'PiP-gdb.git',
'test' : 'PiP-Testsuite.git' } }
gnu_branch_tab = { 7 : ( 'centos/glibc-2.17-260.el7.pip.branch',
'centos/gdb-7.6.1-94.el7.pip.branch' ),
8 : ( 'centos/glibc-2.28-72.el8_1.1.pip.branch',
'centos/gdb-8.2-12.el8.pip.branch' ) }
docker_tagtab = { ( 'x86_64', 7, 2 ) : 'process-in-process/pip:2-centos7-amd64',
( 'aarch64', 7, 2 ) : 'process-in-process/pip:2-centos7-arm64v8',
( 'x86_64', 8, 2 ) : 'process-in-process/pip:2-centos8-amd64',
( 'aarch64', 8, 2 ) : 'process-in-process/pip:2-centos8-arm64v8',
( 'x86_64', 7, 3 ) : 'process-in-process/pip:3-centos7-amd64',
( 'aarch64', 7, 3 ) : 'process-in-process/pip:3-centos7-arm64v8',
( 'x86_64', 8, 3 ) : 'process-in-process/pip:3-centos8-amd64' ,
( 'aarch64', 8, 3 ) : 'process-in-process/pip:3-centos8-arm64v8' }
def is_ready( how, arch, centos, pip_ver ):
if ( arch, centos, pip_ver ) in not_ready[how]:
return False
return True
def resource_id( how, centos, pip_ver ):
global ARGS, arch
if how == 'yum':
return 'process-in-process-v' + str( pip_ver )
elif how == 'docker':
try:
return docker_tagtab[ arch, int(centos), pip_ver ]
except:
return None
elif how == 'spack':
return 'process-in-process@' + str( pip_ver )
else:
return None
def spack_url():
spack_url = 'https://github.com/spack/spack.git'
spack_url_dev = '[email protected]:software/PIP-Spack.git'
if how == 'spack':
url = spack_url
else:
url = spack_url_dev
bsnam = os.path.splitext( os.path.basename( url ) )[0]
return url, bsnam
MSG_ENTER = '>>>'
MSG_EXIT = '<<<'
MSG_ERROR = '<<E'
MSG_PREFIX ='---'
MSG_PREFIX1 ='###'
ERR_PREFIX ='PIP-PIP ERROR:'
WARN_PREFIX ='PIP-PIP WARNING:'
SEP = '\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n'
sudo_cmd = []
query_prefix = False
query_work = False
spack_path = None
DRYRUN = False
FLAG_SUBDIR = True
FLAG_NOUPDATE = False
FLAG_KEEP = False
FLAG_QUIET = False
FLAG_YES = False
SUDO_ERR = False
ERROR = False
def mesg( words, newline, fs ):
"general print message func"
if words != []:
for word in words[0:-1]:
print( word, end=' ', file=fs )
if newline:
print( words[-1], file=fs )
else:
print( words[-1], end='', file=fs )
elif newline:
print( '', file=fs )
fs.flush()
def message( words, newline=True, out=sys.stdout, log=None ):
"print message"
if out is not None and not FLAG_QUIET:
mesg( words, newline, out )
if log is not None and not DRYRUN:
mesg( words, newline, log )
def err_msg( words, log=None ):
"print error message"
global ERROR
ERROR = True
message( [ ERR_PREFIX ] + words, out=sys.stderr, log=log )
def warn_msg( words, newline=True, log=None ):
"print warning message"
message( [ WARN_PREFIX ] + words, newline, out=sys.stderr, log=log )
urlparser = None
def concat_urls( url0, url1 ):
"concatenate URLs"
global urlparser
if urlparser == None:
try:
import urllib.parse
urlparser = urllib.parse
except Exception as e0:
try:
import urlparse
urlparser = urlparse
except Exception as e1:
err_msg( [ 'No urllib module:', e0 ] )
err_msg( [ 'No urlparse module:', e1 ] )
NULL_FS.close()
sys.exit( 1 )
return urlparser.urljoin( url0+'/', url1 )
NULL_FS = open( os.devnull, 'w' )
def git_clone( dir, url, branch, log_fs ):
if branch != '':
if execute( dir, [ 'git', 'clone', '-b', branch, url ], log_fs ):
return True
else:
if execute( dir, [ 'git', 'clone', url ], log_fs ):
return True
return False
def get_centos_version():
"obtain RedHat/CentOS version"
try:
with open( '/etc/redhat-release', mode='r' ) as f:
rhel = f.readline().split()
while True:
if rhel == []:
raise Exception( 'Unknown /etc/readhat-release content' )
token = rhel.pop(0)
if token == 'release':
verstr = rhel.pop(0)
ver = int( verstr.split( '.' ).pop( 0 ) )
return ver
except Exception:
err_msg( [ "Not a CentOS distribution" ] )
sys.exit( 1 )
def cpu_arch():
"obtain CPU architecture"
return sp.check_output( [ 'uname', '-m' ] ).decode().split().pop(0)
def is_privileged( log_fs ):
"check if the current user can sudo"
global ARGS, sudo_cmd, SUDO_ERR
if DRYRUN:
return True
if SUDO_ERR:
return False
user = os.environ['USER']
if user == 'root':
# no need of sudo
sudo_cmd = []
return True
if sudo_cmd is not None:
if sudo_cmd != []:
return True
sudo_rc = sp.call( [ '/bin/sh', '-c', 'sudo -S true < /dev/null' ],
stdout=NULL_FS, stderr=NULL_FS )
if sudo_rc == 0:
able_to_sudo = True
else:
able_to_sudo = False
if ARGS.sudo:
if able_to_sudo:
sudo_cmd = [ 'sudo' ]
return True
sudo_cmd = []
err_msg( [ "'"+user+"'", 'is unable to sudo' ], log=log_fs )
SUDO_ERR = True
else:
SUDO_ERR = True
if able_to_sudo:
err_msg( [ '--sudo option is required' ], log=log_fs )
else:
err_msg( [ "'"+user+"'", 'is unable to sudo' ], log=log_fs )
return False
try:
with open( '.pip-pip.cmd', mode='w' ) as f:
nowstr = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S' )
print( '#', nowstr, file=f )
for tkn in sys.argv:
print( tkn, end='', file=f )
print( '', file=f )
except:
pass
cmd = sys.argv[0]
parser = argparse.ArgumentParser( description='pip-pip: Process-in-Process '+
'package installing program',
prog='pip-pip',
add_help = True )
parser.add_argument( '--how', '-H',
help = 'specifying how to install ' \
'[yum|docker|spack|GITHUB]',
type = str,
action = 'append' )
parser.add_argument( '--version', '-v',
help = "specifying PiP version (2 or 3. default '2')",
type = str,
action = 'append' )
parser.add_argument( '--centos', '-c',
help = "Docker only: specifying RedHat/CentOS version (7 or 8. default 8)",
type = str,
action = 'append' )
parser.add_argument( '--prefix', '-p',
help = "install (prefix) directory " \
"(github [, spack]. default './install')",
type = str,
default = 'install',
nargs = '?' )
parser.add_argument( '--work', '-w',
help = "working directory (default './work')",
type = str,
default = 'work',
nargs = '?' )
parser.add_argument( '--keep', '-k',
help = 'keep working directory. ' +
'Work directory(ies) is (are) deleted when succeeded by default',
action = 'store_true' )
parser.add_argument( '--sudo', '-s',
help = 'allow sudo (docker)',
action = 'store_true' )
parser.add_argument( '--yes', '-y',
help = "answer 'yes' to all questions",
action = 'store_true' )
parser.add_argument( '--quiet', '-q',
help = 'quiet mode',
action = 'store_true' )
parser.add_argument( '--noglibc',
help = 'do not build PiP-glibc and PiP_gdb (github)',
action = 'store_true' )
parser.add_argument( '--nogdb',
help = 'do not build PiP-gdb (github)',
action = 'store_true' )
parser.add_argument( '--notest',
help = 'do not test installed PiP (and PiP-gdb)',
action = 'store_true' )
parser.add_argument( '--noupdate',
help = 'do not update if specified PiP installation exists',
action = 'store_true' )
parser.add_argument( '--nosubdir',
help = 'do not create subdirectory(ies) in the prefix directory',
action = 'store_true' )
parser.add_argument( '--threshold', '-t',
help = 'threshold of the number of PiP Testsuite errors',
type = int,
nargs = '?',
default = 10 )
parser.add_argument( '--ready', '-r',
help = "list ready to install and exit",
type = str,
nargs = '?',
default = '' )
parser.add_argument( '--dryrun', '--dry', '-d',
help = 'dry run',
action = 'store_true' )
ARGS = parser.parse_args()
DRYRUN = ARGS.dryrun
FLAG_QUIET = ARGS.quiet
FLAG_NOUPDATE = ARGS.noupdate
FLAG_KEEP = ARGS.keep
FLAG_YES = ARGS.yes
if ARGS.threshold < 0:
threshold = [ 'PIP_TEST_THRESHOLD=10' ]
else:
threshold = [ 'PIP_TEST_THRESHOLD='+str(ARGS.threshold) ]
if ARGS.nosubdir:
FLAG_SUBDIR = False
def check_command( command ):
"check if the Linux command exists"
if DRYRUN:
return True
try:
if sp.check_call( ['sh','-c','type '+command+' >/dev/null 2>&1' ] ) == 0:
return True
except:
return False
return False
def expand( lst, lower=False ):
"flatten list"
if lst == [] or lst is None:
return []
if lower:
token = lst.pop( 0 ).lower()
else:
token = lst.pop( 0 )
if ',' in token:
return token.split( ',' ) + expand( lst )
if '+' in token:
return token.split( '+' ) + expand( lst )
return [ token ] + expand( lst )
## check how
how_list = expand( ARGS.how, lower=True )
how_all = how_supported
os_list = expand( ARGS.centos )
if os_list == []:
os_list = [ '8' ]
centos_list = []
if 'all' in how_list or 'All' in how_list:
how_list = how_all
elif how_list == []:
how_list = [ 'github' ]
else:
errlist = []
howlist = []
for h in how_list:
if not h in how_all:
errlist.append( h )
if errlist != []:
if len(errlist) == 1:
err_msg( [ errlist[0], 'is not supported value of --how' ] )
else:
err_msg( [ ','.join(errlist), 'are not supported values of --how' ] )
parser.print_help()
else:
for h in how_all:
if h in how_list:
howlist.append( h )
if howlist == []:
ERROR = True
how_list = howlist
if len( how_list ) > 1 and not FLAG_SUBDIR:
err_msg( [ 'Multiple versions cannot be installed without having sub-directoris' ] )
if 'yum' in how_list:
if not check_command( 'yum' ):
warn_msg( [ "'yum' command not found -- yum installation skipped" ] )
how_list.remove( 'yum' )
elif not is_privileged( None ):
how_list.remove( 'yum' )
if 'docker' in how_list:
if not check_command( 'docker' ):
warn_msg( [ "'docker' command not found -- docker installation skipped" ] )
how_list.remove( 'docker' )
elif not is_privileged( None ):
how_list.remove( 'docker' )
elif 'all' in os_list or 'ALL' in os_list:
centos_list = [ '7', '8' ]
else:
for centos in os_list:
if not centos in [ '7', '8' ]:
warn_msg( [ "Unsupported CentOS version (" + centos + ")" ] )
elif not centos in centos_list:
centos_list.append( centos )
if centos_list is []:
warn_msg( [ "Docker installation skipped" ] )
how_list.remove( 'docker' )
if 'spack' in how_list:
if check_command( 'spack' ):
spack_path = 'spack'
elif not check_command( 'git' ):
spack_path = ''
warn_msg( [ "'spack' and 'git' commands not found -- spack installation skipped" ] )
how_list.remove( 'spack' )
if 'github' in how_list:
if not check_command( 'git' ):
err_msg( [ "'git' command not found -- git installation skipped" ] )
if 'github' in how_list:
how_list.remove( 'github' )
if how_list == []:
ERROR = True
## check Linux distribution and version
centos_version = get_centos_version()
if not centos_version in centos_supported:
err_msg( [ 'RedHat/CentOS-'+str(centos_version), 'is not supported' ] )
sys.exit( 1 )
## Check PiP version
version = expand( ARGS.version )
pip_vers = []
if 'ALL' in version:
if centos_version == 7:
pip_vers = [ 1, 2, 3 ]
else:
pip_vers = [ 2, 3 ]
elif 'all' in version:
pip_vers = [ 2, 3 ]
elif version == []:
pip_vers = [ 2 ]
else:
version = sorted( set( version ) )
errlist = []
for ver_str in version:
try:
ver = int( ver_str )
if ver in pipver_supported:
pip_vers.append( abs( ver ) )
else:
errlist.append( ver_str )
except:
errlist.append( ver_str )
errlen = len( errlist )
if errlen > 0:
if errlen == 1:
err_msg( [ errlist[0], 'is not an supported value of --version' ] )
else:
err_msg( [ ','.join(errlist),
'are not supported values of --version' ] )
# check arch
arch = cpu_arch()
if not arch in arch_supported:
err_msg( [ arch, 'is not supported by PiP' ] )
if ARGS.ready != '':
list_ready = []
if ARGS.ready == 'all':
for how in how_supported:
if how in [ 'yum', 'docker' ] and SUDO_ERR:
continue
for cpu in arch_supported:
for linux in [ 7, 8 ]:
for pip in [ 2, 3 ]:
if is_ready( how, cpu, linux, pip ):
list_ready.append( ( how, cpu, linux, pip ) )
elif ARGS.ready == 'ALL':
for how in how_supported:
if how in [ 'yum', 'docker' ] and SUDO_ERR:
continue
for cpu in arch_supported:
for linux in [ 7, 8 ]:
for pip in [ 1, 2, 3 ]:
if is_ready( how, cpu, linux, pip ):
list_ready.append( ( how, cpu, linux, pip ) )
elif ARGS.ready in [ 'arch', 'cpu' ]:
for how in how_supported:
if how in [ 'yum', 'docker' ] and SUDO_ERR:
continue
for linux in [ 7, 8 ]:
for pip in [ 1, 2, 3 ]:
if is_ready( how, arch, linux, pip ):
list_ready.append( ( how, arch, linux, pip ) )
elif ARGS.ready in [ 'linux', 'redhat', 'centos' ]:
for how in how_supported:
if how in [ 'yum', 'docker' ] and SUDO_ERR:
continue
for arch in arch_supported:
for pip in [ 1, 2, 3 ]:
if is_ready( how, arch, centos_version, pip ):
list_ready.append( ( how, arch, centos_version, pip ) )
else:
for how in how_list:
if how in [ 'yum', 'docker' ] and SUDO_ERR:
continue
for pip in pip_vers:
if is_ready( how, arch, centos_version, pip ):
list_ready.append( ( how, arch, centos_version, pip ) )
if list_ready == []:
message( [ 'Nothing ready' ] )
else:
message( [ 'Ready list' ] )
for ( how, arch, linux, pip ) in list_ready:
msgstr = '{0:>10}\t{1:>10}\tRedHat/CentOS:{2:} \tPiP-{3:}'.format(how,arch,linux,pip)
message( [ msgstr ] )
sys.exit( 0 )
def make_directory( path ):
"make directory"
if DRYRUN:
return path
try:
if not os.path.isdir( path ):
os.mkdir( path )
return path
except Exception as e:
err_msg( [ 'make_directory:', e ] )
return None
def query( prompt, ans ):
try:
while True: # python2
choice = raw_input( prompt ).lower()
if choice != '' and choice[0] in ans:
return choice[0]
continue
except:
try:
while True: # python3
choice = input( prompt ).lower()
if choice != '' and choice[0] in ans:
return choice[0]
continue
except:
message( [] )
NULL_FS.close()
sys.exit( 1 )
# check and create prefix dir
def check_prefix( pref ):
"check the prefix directory"
global query_prefix, ERROR
if DRYRUN:
return True
if os.path.isfile( pref ):
err_msg( [ pref, 'is not a directory but a file' ] )
return False
if os.path.isdir( pref ):
if not FLAG_YES and not query_prefix:
warn_msg( [ 'Prefix directory already exists: ', pref ] )
ans = query( 'Are you sure to install into the prefix directory? ' \
"'yes', 'all' or 'no' [Y/A/N]: ",
'yan' )
if ans == 'n':
return False
if ans == 'a':
query_prefix = True # never ask again
return True
def prefix_path( prefix, how, ver ):
"make the prefix path"
global ERROR
if not FLAG_SUBDIR:
return prefix
return os.path.join( prefix,
arch + '_' +
'cetos-' + str( centos_version ) + '_' +
how + '_' +
'pip-' + str( ver ) )
def create_prefix( prefix, how, ver ):
"create the prefix directory"
pdir = prefix_path( prefix, how, ver )
if DRYRUN:
return pdir
try:
if not os.path.isdir( pdir ):
os.makedirs( pdir )
return pdir
except Exception as e:
err_msg( [ 'create_prefix:', e ] )
return None
def delete_work_dir( wdir ):
"delete work directory"
if DRYRUN:
message( [ '[DRY] Deleting work directory ('+wdir+') .. done' ] )
return True
message( [ 'Deleting work directory ('+wdir+') ..' ], newline=False )
try_shutil = True
retry_count = 3
retry = retry_count
retry_wait = 1
while True:
try:
if try_shutil:
try_shutil = False
shutil.rmtree( wdir )
else:
if sp.run( [ '/bin/sh', '-c', 'rm -f -r ' + wdir ],
check=False,
stdout=NULL_FS, stderr=NULL_FS ).returncode != 0:
raise Exception( 'rm -r -f ' + wdir + ' FAILD' )
# success
message( [ ' done' ] )
return True
except:
if retry == 0:
message( [ 'gave up' ] )
return False
message( [ 'retrying.. ' ], newline=False )
time.sleep( retry_wait )
retry_wait *= 2
retry -= 1
continue
# check and create work dir
def work_path( work, how, ver ):
"create work directory path"
path = os.path.join( work,
arch + '_' +
'centos-' + str( centos_version ) + '_' +
how + '_' +
'pip-' + str( ver ) )
return path
def check_work( work ):
"check working directory"
global list_del_work, query_work, ERROR
if DRYRUN:
return True
if os.path.isfile( work ):
err_msg( [ work, 'is not a directory but a file' ] )
return False
if os.path.isdir( work ):
if not FLAG_YES and not query_work:
warn_msg( [ 'Work directory already exists:', work ] )
ans = query( 'Delete work directory before install? ' \
"'yes', 'all' or 'no' [Y/A/N]: ",
'yan' )
if ans == 'n':
ERROR = True
return False
if ans == 'a':
query_work = True # never ask again
list_del_work.append( work )
return True
def create_work( work ):
"make work directory"
if DRYRUN:
return work, None
try:
log_dir = os.path.join( work, 'log' )
if not os.path.isdir( log_dir ):
os.makedirs( log_dir )
log_path = os.path.join( log_dir, 'pip-pip.log' )
except Exception as e:
err_msg( [ 'create_work:', e ] )
return None, None
return work, log_path
def check_file( file ):
"check if the file exists or not"
if DRYRUN:
return True
return os.path.isfile( file )
def do_poll( proc, t, dot, inc, tmax, log_fs, silent ):
"do poll and output dots"
t0 = t
if t < 100:
delta = 0.2
elif t < 1000:
delta = 1.0
else:
delta = 10.0
td = 0.0
while True:
ret = proc.poll()
if ret is not None or t >= tmax:
if t < 60:
tu = t
unit = 's'
elif t < 3600:
tu = t / 60
unit = 'm'
else:
tu = t / 3600
unit = 'h'
if ret is None:
if not silent:
message( [ str(int(tu))+unit ], newline=False )
return None, t
if not silent:
message( [ ' '+str(int(tu))+unit ], newline=False )
break
if td >= inc:
t += inc
td = 0.0
if not silent:
message( [ dot ], newline=False )
time.sleep( delta )
td += delta
if ret != 0:
if not silent:
message( [ ' - NG' ], log=log_fs )
return False, t
if not silent:
message( [ ' - OK' ], log=log_fs )
return True, t
def execute( wd, cmd, log_fs, silent=False, env=[] ):
"execute a Linux command"
global ERROR
if DRYRUN:
if not silent:
message( [ '[DRY]' ] + cmd, log=log_fs )
return True
if ERROR:
if not silent:
message( [ MSG_PREFIX ] +
cmd +
[ '-- execution skipped due to prev. error' ],
log=log_fs )
return False
try:
if not silent:
message( [ MSG_PREFIX ] + env + cmd, newline=False, log=log_fs )
message( [], newline=True, log=log_fs, out=None )
if wd is not None:
cmd_list = [ 'cd', wd, '&&' ] + cmd
else:
cmd_list = cmd
shell_cmd = [ '/bin/sh', '-c', ' '.join( env + cmd_list ) ]
proc = sp.Popen( shell_cmd, close_fds=True,
stdout=log_fs, stderr=log_fs )
if not silent:
message( ' ', newline=False )
inctab = [ ( '.', 1, 10 ),
( '.', 6, 60 ),
( '.', 60, 360 ),
( '.', 360, 3600 ),
( '.', 3600, 24*3600 ),
( '.', 24*3600, 9999*999 ) ]
t = 0
for ( dot, inc, tmax ) in inctab:
( ret, t ) = do_poll( proc, t, dot, inc, tmax, log_fs, silent )
if ret is not None:
time.sleep( 1 ) # wait for stdout/stderr flushing
return ret
except Exception as e:
err_msg( [ '\nException:', e ], log=log_fs )
return False
def is_already_installed( prefix, how, pip_ver, centos ):
global spack_path
if DRYRUN:
return False
if how == 'yum':
yum_pip = resource_id( how, centos, pip_ver )
if execute( None, [ 'yum', 'list', 'installed', yum_pip ], NULL_FS, True ):
return True
elif how == 'docker':
docker_pip = resource_id( how, centos, pip_ver )
if execute( None,
sudo_cmd + [ 'docker', 'inspect', docker_pip ],
NULL_FS, True ):
return True;
elif how == 'spack' and spack_path is not None:
spack_pip = resource_id( how, centos, pip_ver )
if execute( None, [ spack_path, 'find', spack_pip ], NULL_FS, True ):
return True;
else: # GIT
libpipso = os.path.join( prefix, 'lib', 'libpip.so' )
if os.path.isfile( libpipso ):
return True
return False
def install_yum( pip_ver, work_dir, log_fs ):
global threshold
"yum install"
yum_pip = resource_id( 'yum', centos_version, pip_ver )
message( [ MSG_ENTER, 'Yum install', yum_pip ], log=log_fs )
retuple = ( 'yum', yum_pip, '' )
if not execute( None,
sudo_cmd + [ 'yum', 'reinstall', '-y', yum_pip ],
log_fs ):
return False, retuple
if not ARGS.notest:
repo_test = concat_urls( git_url_tab['github'],
git_repo_tab['github']['test'] )
message( [ MSG_PREFIX1, 'Testing', repo_test ], log=log_fs )
if not git_clone( work_dir, repo_test, '', log_fs ):
return False, retuple
test_dir = os.path.join( work_dir, 'PiP-Testsuite' )
configure = os.path.join( test_dir, 'configure' )
if not execute( test_dir,
[ configure, '--with-pip=/usr' ],
log_fs ):
return False, retuple
if not execute( test_dir, [ 'make', 'test' ], log_fs, env=threshold ):
return False, retuple
return True, retuple
def install_docker( pip_ver, centos, log_fs ):
"docker install"
docker_pip = resource_id( 'docker', centos, pip_ver )
message( [ MSG_ENTER, 'Docker install', docker_pip ], log=log_fs )
retuple = ( 'docker', docker_pip, '' )
if execute( None,
sudo_cmd + [ 'docker', 'inspect', docker_pip ], log_fs ):
message( [ MSG_PREFIX1,
'Removing existing image', docker_pip ],
log=log_fs )
if not execute( None,
sudo_cmd + [ 'docker', 'rmi', docker_pip ],
log_fs ):
err_msg( [ 'Failed to remove existing image', docker_pip ],
log=log_fs )
return False, retuple
message( [ MSG_PREFIX1, 'Pulling existing image', docker_pip ], log=log_fs )
if not execute( None, sudo_cmd + [ 'docker', 'pull', docker_pip ], log_fs ):
return False, retuple
return True, retuple
def install_spack( prefix, how, pip_ver, log_fs ):
"Spack install"
global spack_path
spack_pip = resource_id( 'spack', centos_version, pip_ver )
message( [ MSG_ENTER, 'Spack install', spack_pip ], log=log_fs )
retuple = ( 'spack', spack_pip, '' )
if spack_path == '': # Spack not found
return False, retuple
if spack_path is None:
prefix_dir = create_prefix( prefix, how, pip_ver )
if prefix_dir is None:
return False, retuple
( url, bsnam ) = spack_url()
spack_dir = os.path.join( prefix_dir, bsnam )
if os.path.isdir( os.path.join( spack_dir, '.git' ) ):
message( [ MSG_PREFIX1, 'Pulling Spack into', prefix_dir ], log=log_fs )
if not execute( spack_dir, [ 'git', 'pull', url ], log_fs ):
return False, retuple
else:
message( [ MSG_PREFIX1, 'Cloning Spack at', prefix_dir ], log=log_fs )
if not git_clone( prefix_dir, url, '', log_fs ):
spack_path = '' # mark as not found
return False, retuple
spack_path = os.path.join( prefix_dir, bsnam, 'bin', 'spack' )
retuple = ( 'spack', spack_pip, spack_path )
execute( None, [ spack_path, 'compiler', 'find' ], log_fs )
if execute( None, [ spack_path, 'find', spack_pip ], log_fs ):
message( [ MSG_PREFIX1, 'Uninstalling', spack_pip ], log=log_fs )
if not execute( None, [ spack_path, 'uninstall', '-y', spack_pip ], log_fs ):
return False, retuple
message( [ MSG_PREFIX1, 'Installing', spack_pip ], log=log_fs )
if not execute( None, [ spack_path, 'install', spack_pip ], log_fs ):
return False, retuple
return True, retuple
def byte_to_char( byte_str ):
"convert byte string to char (for compatibility between Python2 and 3)"
try:
if byte_str == '' or byte_str[0] in 'abc':
ch_str = byte_str # python2
else:
ch_str = byte_str
except: # python3
ch_str = byte_str.decode()
return ch_str
def wget_install( work_dir, prefix_dir, pkgnam_b, url_b, log_fs ):
"wget install for required package"
pkgname = byte_to_char( pkgnam_b )
url = byte_to_char( url_b )
if not execute( work_dir, [ 'wget', url ], log_fs ):
err_msg( [ 'wget_install: wget', url, '-- FAILED' ], log=log_fs )
raise Exception()
[ mod_tar ] = re.findall( '.*/(.*)', url )
if not check_file( os.path.join( work_dir, mod_tar ) ):
err_msg( [ 'wget_install:', mod_tar, '-- unable to find' ], log=log_fs )
raise Exception()
if not execute( work_dir, [ 'tar', 'xzf', mod_tar ], log_fs ):
err_msg( [ 'wget_install: tar xzf', mod_tar, '-- FAILED' ], log=log_fs )
raise Exception()
os.remove( os.path.join( work_dir, mod_tar ) )
[ mod_name ] = re.findall( r'.*/(.*)\.tar.gz', url )
mod_work = os.path.join( work_dir, mod_name )
if not execute( mod_work,
[ './configure', '--prefix=' + prefix_dir ],
log_fs ):
err_msg( [ 'wget_install: '
'./configure', '--prefix='+prefix_dir,
'['+mod_name+'] -- FAILED' ],
log=log_fs )
raise Exception()
if not execute( mod_work, [ 'make' ], log_fs ):
err_msg( [ 'wget_install: make ['+mod_name+'] -- FAILED' ],
log=log_fs )
raise Exception()
if not execute( mod_work, [ 'make', 'install' ], log_fs ):
err_msg( [ 'wget_install: make install ['+mod_name+'] -- FAILED' ],
log=log_fs)
raise Exception()