-
Notifications
You must be signed in to change notification settings - Fork 182
/
set_path
executable file
·160 lines (125 loc) · 4.75 KB
/
set_path
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
#!/usr/bin/env python
# Copyright (c) 2008-2024 the MRtrix3 contributors.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Covered Software is provided under this License on an "as is"
# basis, without warranty of any kind, either expressed, implied, or
# statutory, including, without limitation, warranties that the
# Covered Software is free of defects, merchantable, fit for a
# particular purpose or non-infringing.
# See the Mozilla Public License v. 2.0 for more details.
#
# For more details, see http://www.mrtrix.org/.
# automatically set the PATH environment variable to include the MRtrix3
# executables and scripts. This script must be run after a successful build,
# from the MRtrix3 toplevel folder:
#
# ./set_path
#
# By default, the script will add the relevant 'export PATH' directive to the
# relevant BASH rc file: ~/.bashrc (~/.bash_profile on MacOSX). If this in not
# appropriate, it is possible to specify the file to be modified as the
# argument, e.g.:
#
# ./set_path ~/.profile
#
# Note that this works only for Bourne shell and derivatives (BASH in
# particular). This will not work for the C shell and derivatives.
import sys, os, platform, subprocess
# on Windows, need to use MSYS2 version of python - not MinGW version:
if sys.executable[0].isalpha() and sys.executable[1] == ':':
python_cmd = subprocess.check_output ([ 'cygpath.exe', '-w', '/usr/bin/python' ]).decode(errors='ignore').splitlines()[0].strip()
sys.exit (subprocess.call ([ python_cmd ] + sys.argv))
# check whether we are in the right location:
label = ""
script_dir = os.path.dirname (os.path.abspath(__file__))
if not os.path.samefile (os.getcwd(), script_dir):
label = '"' + os.path.basename(os.getcwd()) + '" module '
bin_dir = os.path.join(os.getcwd(), 'bin')
if not os.path.isdir (bin_dir):
print ('''
ERROR: no bin/ folder found in expected location!
This script needs to be run from the MRtrix3 toplevel folder (or an MRtrix3
module's toplevel folder).
''')
sys.exit (1)
# set default destination file based on OS:
filename = os.path.expanduser('~')
sysname = platform.system().lower()
if sysname == 'darwin':
if os.getenv('SHELL') == '/bin/zsh':
filename = os.path.join(filename, '.zprofile')
else:
filename = os.path.join(filename, '.bash_profile')
else:
filename = os.path.join(filename, '.bashrc')
# check for the -remove flag:
remove = False
if '-remove' in sys.argv[1:]:
remove = True
sys.argv.remove ('-remove')
# check whether destination file has been specified explicitly:
if len(sys.argv) > 2:
print ('''
ERROR: usage is:
./set_path [-remove] [rc_path]
Where:
rc_path [optional] allows the user to specify the startup file to be
modified (e.g. './set_path ~/.bash_profile').
-remove [optional] indicates that the path should be removed from the
startup file
''')
sys.exit (1)
if len(sys.argv) == 2:
filename = sys.argv[1]
# what will be written to file:
comment='# MRtrix3 ' + label + 'PATH automatically generated by set_path script - do NOT modify:\n'
set_path = 'export PATH="' + bin_dir + os.pathsep + '$PATH"\n'
is_next_line = False
append_path = True
output = ''
if os.path.isfile (filename):
with open (filename, 'r') as f:
for line in f:
if is_next_line is True:
if not line.startswith ('export PATH='):
print ('''
ERROR: File appears to have been modified by this script previously, but the
contents do not match the expected format.
You will need to manually edit the relevant file "''' + filename + '''" in
one of two ways:
1. Amend the existing file contents, so that PATH is correctly updated to
include MRtrix3 binaries and scripts.
2. Remove the text that was previously added to the file by this script, and
then run the set_path script again. The offending text is the following line
AND the one immediately after it:
''' + comment + '''
''')
sys.exit (1)
if not remove:
if line == set_path:
print ('File "' + filename + '" is already up to date')
sys.exit (0)
output += set_path
is_next_line = False
append_path = False
continue
if line == comment:
is_next_line = True
if remove:
continue
output += line
if not remove:
# comment was the last line:
if is_next_line is True:
output += set_path
# if previous modification not detected:
elif append_path is True:
output += '\n' + comment + set_path
with open (filename, 'w') as f:
f.write (output)
print ('File "''' + filename + '" succesfully updated')
print ('(Close terminal and open a new one for change to take effect)')