Debian Codegraph @

build/msysutils.py

raw symbols SCIP
1 from os import environ
2 from os.path import isfile
3 from subprocess import PIPE, Popen
4 import sys
5
6 def _determineMounts():
7 # The MSYS shell provides a Unix-like file system by translating paths on
8 # the command line to Windows paths. Usually this is transparent, but not
9 # for us since we call GCC without going through the shell.
10
11 # Figure out the root directory of MSYS.
12 proc = Popen(
13 [ msysShell(), '-c', '"%s" -c \'import sys ; print(sys.argv[1])\' /'
14 % sys.executable.replace('\\', '\\\\') ],
15 stdin = None,
16 stdout = PIPE,
17 stderr = PIPE,
18 )
19 stdoutdata, stderrdata = proc.communicate()
20 if stderrdata or proc.returncode:
21 if stderrdata:
22 print('Error determining MSYS root:', stderrdata, file = sys.stderr)
23 if proc.returncode:
24 print('Exit code %d' % proc.returncode, file = sys.stderr)
25 raise IOError('Error determining MSYS root')
26 msysRoot = stdoutdata.strip()
27
28 # Figure out all mount points of MSYS.
29 mounts = { '/': msysRoot + '/' }
30 fstab = msysRoot + '/etc/fstab'
31 if isfile(fstab):
32 try:
33 inp = open(fstab, 'r')
34 try:
35 for line in inp:
36 line = line.strip()
37 if line and not line.startswith('#'):
38 nativePath, mountPoint = (
39 path.rstrip('/') + '/' for path in line.split()
40 )
41 mounts[mountPoint] = nativePath
42 finally:
43 inp.close()
44 except IOError as ex:
45 print('Failed to read MSYS fstab:', ex, file = sys.stderr)
46 except ValueError as ex:
47 print('Failed to parse MSYS fstab:', ex, file = sys.stderr)
48 return mounts
49
50 def msysPathToNative(path):
51 if path.startswith('/'):
52 if len(path) == 2 or (len(path) > 2 and path[2] == '/'):
53 # Support drive letters as top-level dirs.
54 return '%s:/%s' % (path[1], path[3 : ])
55 longestMatch = ''
56 for mountPoint in msysMounts.iterkeys():
57 if path.startswith(mountPoint):
58 if len(mountPoint) > len(longestMatch):
59 longestMatch = mountPoint
60 return msysMounts[longestMatch] + path[len(longestMatch) : ]
61 else:
62 return path
63
64 def msysActive():
65 return environ.get('OSTYPE') == 'msys' or 'MSYSCON' in environ
66
67 def msysShell():
68 return environ.get('MSYSCON') or environ.get('SHELL') or 'sh.exe'
69
70 if msysActive():
71 msysMounts = _determineMounts()
72 else:
73 msysMounts = None
74
75 if __name__ == '__main__':
76 print('MSYS mounts:', msysMounts)