]>
git.proxmox.com Git - rustc.git/blob - src/llvm/tools/clang/tools/scan-view/startfile.py
1 """Utility for opening a file using the default application in a cross-platform
2 manner. Modified from http://code.activestate.com/recipes/511443/.
17 class BaseController(object):
18 '''Base class for open program controllers.'''
20 def __init__(self
, name
):
23 def open(self
, filename
):
24 raise NotImplementedError
27 class Controller(BaseController
):
28 '''Controller for a generic open program.'''
30 def __init__(self
, *args
):
31 super(Controller
, self
).__init
__(os
.path
.basename(args
[0]))
32 self
.args
= list(args
)
34 def _invoke(self
, cmdline
):
35 if sys
.platform
[:3] == 'win':
37 startupinfo
= subprocess
.STARTUPINFO()
38 startupinfo
.dwFlags |
= subprocess
.STARTF_USESHOWWINDOW
43 if (os
.environ
.get('DISPLAY') or sys
.platform
[:3] == 'win' or
44 sys
.platform
== 'darwin'):
45 inout
= file(os
.devnull
, 'r+')
47 # for TTY programs, we need stdin/out
50 # if possible, put the child precess in separate process group,
51 # so keyboard interrupts don't affect child precess as well as
53 setsid
= getattr(os
, 'setsid', None)
55 setsid
= getattr(os
, 'setpgrp', None)
57 pipe
= subprocess
.Popen(cmdline
, stdin
=inout
, stdout
=inout
,
58 stderr
=inout
, close_fds
=closefds
,
59 preexec_fn
=setsid
, startupinfo
=startupinfo
)
61 # It is assumed that this kind of tools (gnome-open, kfmclient,
62 # exo-open, xdg-open and open for OSX) immediately exit after lauching
63 # the specific application
64 returncode
= pipe
.wait()
65 if hasattr(self
, 'fixreturncode'):
66 returncode
= self
.fixreturncode(returncode
)
69 def open(self
, filename
):
70 if isinstance(filename
, basestring
):
71 cmdline
= self
.args
+ [filename
]
73 # assume it is a sequence
74 cmdline
= self
.args
+ filename
76 return self
._invoke
(cmdline
)
81 # Platform support for Windows
82 if sys
.platform
[:3] == 'win':
84 class Start(BaseController
):
85 '''Controller for the win32 start progam through os.startfile.'''
87 def open(self
, filename
):
89 os
.startfile(filename
)
91 # [Error 22] No application is associated with the specified
92 # file for this operation: '<URL>'
97 _controllers
['windows-default'] = Start('start')
98 _open
= _controllers
['windows-default'].open
101 # Platform support for MacOS
102 elif sys
.platform
== 'darwin':
103 _controllers
['open']= Controller('open')
104 _open
= _controllers
['open'].open
107 # Platform support for Unix
112 # @WARNING: use the private API of the webbrowser module
113 from webbrowser
import _iscommand
115 class KfmClient(Controller
):
116 '''Controller for the KDE kfmclient program.'''
118 def __init__(self
, kfmclient
='kfmclient'):
119 super(KfmClient
, self
).__init
__(kfmclient
, 'exec')
120 self
.kde_version
= self
.detect_kde_version()
122 def detect_kde_version(self
):
125 info
= commands
.getoutput('kde-config --version')
127 for line
in info
.splitlines():
128 if line
.startswith('KDE'):
129 kde_version
= line
.split(':')[-1].strip()
131 except (OSError, RuntimeError):
136 def fixreturncode(self
, returncode
):
137 if returncode
is not None and self
.kde_version
> '3.5.4':
142 def detect_desktop_environment():
143 '''Checks for known desktop environments
145 Return the desktop environments name, lowercase (kde, gnome, xfce)
150 desktop_environment
= 'generic'
152 if os
.environ
.get('KDE_FULL_SESSION') == 'true':
153 desktop_environment
= 'kde'
154 elif os
.environ
.get('GNOME_DESKTOP_SESSION_ID'):
155 desktop_environment
= 'gnome'
158 info
= commands
.getoutput('xprop -root _DT_SAVE_MODE')
159 if ' = "xfce4"' in info
:
160 desktop_environment
= 'xfce'
161 except (OSError, RuntimeError):
164 return desktop_environment
167 def register_X_controllers():
168 if _iscommand('kfmclient'):
169 _controllers
['kde-open'] = KfmClient()
171 for command
in ('gnome-open', 'exo-open', 'xdg-open'):
172 if _iscommand(command
):
173 _controllers
[command
] = Controller(command
)
177 'gnome': 'gnome-open',
182 desktop_environment
= detect_desktop_environment()
185 controller_name
= controllers_map
[desktop_environment
]
186 return _controllers
[controller_name
].open
189 if _controllers
.has_key('xdg-open'):
190 return _controllers
['xdg-open'].open
192 return webbrowser
.open
195 if os
.environ
.get("DISPLAY"):
196 register_X_controllers()
201 '''Open a file or an URL in the registered default application.'''
203 return _open(filename
)