2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
7 # executes a cygwin command inside the opensecurity project
9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
11 # Copyright (C) 2013 AIT Austrian Institute of Technology
12 # AIT Austrian Institute of Technology GmbH
13 # Donau-City-Strasse 1 | 1220 Vienna | Austria
14 # http://www.ait.ac.at
16 # This program is free software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License
18 # as published by the Free Software Foundation version 2.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 # Boston, MA 02110-1301, USA.
29 # ------------------------------------------------------------
32 # ------------------------------------------------------------
40 from environment import Environment
43 # ------------------------------------------------------------
49 """Some nifty methods working with Cygwin"""
51 def __call__(self, command, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE):
52 """make an instance of this object act as a function"""
53 return self.execute(command, stdin, stdout, stderr)
58 """get the path to our local cygwin installment"""
59 return os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin')
62 def execute(self, command, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE):
63 """execute a cygwin shell command
65 command is list of arguments like ['/bin/ls', '-al', '-h']
67 a Popen object is returned"""
68 command_path = Cygwin.root() + os.sep.join(command[0].split('/'))
69 command = [command_path] + command[1:]
71 return subprocess.Popen(command, shell = False, stdin = stdin, stdout = stdout, stderr = stderr)
76 """check if we can connect to a X11 running instance"""
77 p = Cygwin()(['/bin/bash', '-c', 'DISPLAY=:0 /usr/bin/xset -q'])
78 stdout, stderr = p.communicate()
79 return p.returncode == 0
84 """start X11 in the background (if not already running) on DISPLAY=:0"""
86 # do not start if already running
87 if Cygwin.is_X11_running():
90 # launch X11 (forget output and return immediately)
91 p = Cygwin()(['/bin/bash', '--login', '-i', '-c', ' X :0 -multiwindow'], stdin = None, stdout = None, stderr = None)
95 if __name__ == "__main__":
97 # execute what is given on the command line
101 # wait until the process finished and grab the output
102 stdout, stderr = p.communicate()
103 print('=== call result on stdout: ===\n' + stdout)
104 print('=== call result on stderr: ===\n' + stderr)