File: msysutils.py

package info (click to toggle)
openmsx-debugger 0.1~git20160326-2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 2,036 kB
  • ctags: 1,708
  • sloc: cpp: 11,862; python: 518; sh: 45; makefile: 13
file content (76 lines) | stat: -rw-r--r-- 2,243 bytes parent folder | download | duplicates (8)
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
from os import environ
from os.path import isfile
from subprocess import PIPE, Popen
import sys

def _determineMounts():
	# The MSYS shell provides a Unix-like file system by translating paths on
	# the command line to Windows paths. Usually this is transparent, but not
	# for us since we call GCC without going through the shell.

	# Figure out the root directory of MSYS.
	proc = Popen(
		[ msysShell(), '-c', '"%s" -c \'import sys ; print sys.argv[1]\' /'
			% sys.executable.replace('\\', '\\\\') ],
		stdin = None,
		stdout = PIPE,
		stderr = PIPE,
		)
	stdoutdata, stderrdata = proc.communicate()
	if stderrdata or proc.returncode:
		if stderrdata:
			print >> sys.stderr, 'Error determining MSYS root:', stderrdata
		if proc.returncode:
			print >> sys.stderr, 'Exit code %d' % proc.returncode
		raise IOError('Error determining MSYS root')
	msysRoot = stdoutdata.strip()

	# Figure out all mount points of MSYS.
	mounts = { '/': msysRoot + '/' }
	fstab = msysRoot + '/etc/fstab'
	if isfile(fstab):
		try:
			inp = open(fstab, 'r')
			try:
				for line in inp:
					line = line.strip()
					if line and not line.startswith('#'):
						nativePath, mountPoint = (
							path.rstrip('/') + '/' for path in line.split()
							)
						mounts[mountPoint] = nativePath
			finally:
				inp.close()
		except IOError, ex:
			print >> sys.stderr, 'Failed to read MSYS fstab:', ex
		except ValueError, ex:
			print >> sys.stderr, 'Failed to parse MSYS fstab:', ex
	return mounts

def msysPathToNative(path):
	if path.startswith('/'):
		if len(path) == 2 or (len(path) > 2 and path[2] == '/'):
			# Support drive letters as top-level dirs.
			return '%s:/%s' % (path[1], path[3 : ])
		longestMatch = ''
		for mountPoint in msysMounts.iterkeys():
			if path.startswith(mountPoint):
				if len(mountPoint) > len(longestMatch):
					longestMatch = mountPoint
		return msysMounts[longestMatch] + path[len(longestMatch) : ]
	else:
		return path

def msysActive():
	return environ.get('OSTYPE') == 'msys' or 'MSYSCON' in environ

def msysShell():
	return environ.get('MSYSCON') or environ.get('SHELL') or 'sh.exe'

if msysActive():
	msysMounts = _determineMounts()
else:
	msysMounts = None

if __name__ == '__main__':
	print 'MSYS mounts:', msysMounts