File: set_path

package info (click to toggle)
mrtrix3 3.0~rc3+git135-g2b8e7d0c2-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 34,248 kB
  • sloc: cpp: 117,101; python: 6,472; sh: 638; makefile: 226; xml: 39; ansic: 20
file content (113 lines) | stat: -rwxr-xr-x 3,627 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python

# 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' ]).splitlines()[0].strip()
  sys.exit (subprocess.call ([ python_cmd ] + sys.argv))

# check whether we are in the right location:
basedir = os.getcwd()
if not os.path.isfile (os.path.join(basedir, 'bin', 'mrinfo')):
  basedir = os.path.dirname (os.path.abspath(__file__))
  if not os.path.isfile (os.path.join(basedir, 'bin', 'mrinfo')):
    print ('''
ERROR: MRtrix3 executables not found in expected location.

This script needs to be run from within the MRtrix3 toplevel directory, AND
after the build script has successfully completed. In addition, the set_path
script should NOT be moved from its original location in the MRtrix3 toplevel
directory.
''')
    sys.exit (1)


# set default destination file based on OS:
filename = os.path.expanduser('~')
sysname = platform.system().lower()
if sysname == 'darwin':
  filename = os.path.join(filename, '.bash_profile')
else:
  filename = os.path.join(filename, '.bashrc')


# check whether destination file has been specified explicitly:
if len(sys.argv) == 2:
  filename = sys.argv[1]


# what will be written to file:
comment='# MRtrix3 PATH automatically generated by set_path script - do NOT modify:\n'
set_path = 'export PATH="' + os.path.join(basedir, 'bin') + 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 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
  
      output += line


# 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)')