4
4
5
5
import argparse
6
6
import os
7
+
7
8
import yaml
8
9
10
+
9
11
# parse compilers.yaml file.
10
12
# return a list with the compiler descriptions from the yaml file.
11
13
def load_compilers_yaml (path ):
12
- with open (path , 'r' ) as file :
14
+ with open (path , "r" ) as file :
13
15
data = yaml .safe_load (file )
14
16
compilers = [c ["compiler" ] for c in data ["compilers" ]]
15
17
return compilers
16
18
19
+
17
20
def parse_export (line ):
18
- s = line .replace ('=' , ' ' ).split ()
21
+ s = line .replace ("=" , " " ).split ()
19
22
var = s [1 ]
20
23
paths = None
21
- if len (s )> 2 :
22
- paths = s [2 ].rstrip (';' ).split (':' )
24
+ if len (s ) > 2 :
25
+ paths = s [2 ].rstrip (";" ).split (":" )
23
26
return {"variable" : var , "paths" : paths }
24
27
28
+
25
29
def split_line (line ):
26
- return line .strip ().rstrip (';' ).replace ('=' , ' ' ).split ()
30
+ return line .strip ().rstrip (";" ).replace ("=" , " " ).split ()
31
+
27
32
28
33
def is_export (parts ):
29
- return len (parts )> 1 and parts [0 ]== "export"
34
+ return len (parts ) > 1 and parts [0 ] == "export"
35
+
30
36
31
37
def is_alias (parts ):
32
- return len (parts )> 0 and parts [0 ]== "alias"
38
+ return len (parts ) > 0 and parts [0 ] == "alias"
39
+
33
40
34
41
# Returns True if the given path is a descendant of prefix, False otherwise.
35
42
def has_prefix (path , prefix ):
36
43
prefix = os .path .realpath (prefix )
37
44
path = os .path .realpath (path )
38
45
return os .path .commonprefix ([path , prefix ]) == prefix
39
46
47
+
40
48
parser = argparse .ArgumentParser ()
41
49
parser .add_argument ("compiler_path" , help = "Path to the compilers.yaml file" )
42
50
parser .add_argument ("activate_path" , help = "Path to the activate script to configure" )
@@ -59,12 +67,12 @@ def has_prefix(path, prefix):
59
67
60
68
paths = []
61
69
for c in compilers :
62
- local_paths = set ([os .path .dirname (v ) for k ,v in c ["paths" ].items ()])
70
+ local_paths = set ([os .path .dirname (v ) for k , v in c ["paths" ].items ()])
63
71
paths += local_paths
64
72
print (f'adding compiler { c ["spec" ]} -> { [p for p in local_paths ]} ' )
65
73
66
74
# find unique paths and concatenate them
67
- pathstring = ':' .join (set (paths ))
75
+ pathstring = ":" .join (set (paths ))
68
76
69
77
# Parse the spack env activation script line by line.
70
78
# Remove spack-specific environment variables and references the build path.
@@ -73,7 +81,7 @@ def has_prefix(path, prefix):
73
81
# etc. This may or may not be surprising for users, and we may have to append
74
82
# :$PATH, :$CPATH, etc.
75
83
76
- lines = []
84
+ lines = []
77
85
with open (args .activate_path ) as fid :
78
86
for line in fid :
79
87
parts = split_line (line )
@@ -86,22 +94,24 @@ def has_prefix(path, prefix):
86
94
87
95
# parse PATH to remove references to the build directory
88
96
if export ["variable" ] == "PATH" :
89
- paths = [p for p in export ["paths" ] if not has_prefix (p , args .build_path )]
97
+ paths = [
98
+ p for p in export ["paths" ] if not has_prefix (p , args .build_path )
99
+ ]
90
100
lines .append (f"export PATH={ ':' .join (paths )} ;\n " )
91
101
92
102
# drop the SPACK_ENV variable
93
103
elif export ["variable" ] == "SPACK_ENV" :
94
104
pass
95
105
96
106
else :
97
- lines .append (line .strip ()+ "\n " )
107
+ lines .append (line .strip () + "\n " )
98
108
else :
99
- lines .append (line .strip ()+ "\n " )
109
+ lines .append (line .strip () + "\n " )
100
110
101
111
# Prepend the compiler paths to PATH
102
112
lines .append ("# compiler paths added by stackinator\n " )
103
113
lines .append (f"export PATH={ pathstring } :$PATH;\n " )
104
114
105
115
# Write a modified version of the activation script.
106
- with open (args .activate_path , 'w' ) as fid :
116
+ with open (args .activate_path , "w" ) as fid :
107
117
fid .writelines (lines )
0 commit comments