1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/OpenSecurity/bin/cygwin.py Fri Dec 06 10:51:15 2013 +0100
1.3 @@ -0,0 +1,105 @@
1.4 +#!/bin/env python
1.5 +# -*- coding: utf-8 -*-
1.6 +
1.7 +# ------------------------------------------------------------
1.8 +# cygwin command
1.9 +#
1.10 +# executes a cygwin command inside the opensecurity project
1.11 +#
1.12 +# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
1.13 +#
1.14 +# Copyright (C) 2013 AIT Austrian Institute of Technology
1.15 +# AIT Austrian Institute of Technology GmbH
1.16 +# Donau-City-Strasse 1 | 1220 Vienna | Austria
1.17 +# http://www.ait.ac.at
1.18 +#
1.19 +# This program is free software; you can redistribute it and/or
1.20 +# modify it under the terms of the GNU General Public License
1.21 +# as published by the Free Software Foundation version 2.
1.22 +#
1.23 +# This program is distributed in the hope that it will be useful,
1.24 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1.25 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.26 +# GNU General Public License for more details.
1.27 +#
1.28 +# You should have received a copy of the GNU General Public License
1.29 +# along with this program; if not, write to the Free Software
1.30 +# Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.31 +# Boston, MA 02110-1301, USA.
1.32 +# ------------------------------------------------------------
1.33 +
1.34 +
1.35 +# ------------------------------------------------------------
1.36 +# imports
1.37 +
1.38 +import os
1.39 +import subprocess
1.40 +import sys
1.41 +
1.42 +# local
1.43 +from environment import Environment
1.44 +
1.45 +
1.46 +# ------------------------------------------------------------
1.47 +# code
1.48 +
1.49 +
1.50 +class Cygwin(object):
1.51 +
1.52 + """Some nifty methods working with Cygwin"""
1.53 +
1.54 + def __call__(self, command, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE):
1.55 + """make an instance of this object act as a function"""
1.56 + return self.execute(command, stdin, stdout, stderr)
1.57 +
1.58 +
1.59 + @staticmethod
1.60 + def root():
1.61 + """get the path to our local cygwin installment"""
1.62 + return os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin')
1.63 +
1.64 +
1.65 + def execute(self, command, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE):
1.66 + """execute a cygwin shell command
1.67 +
1.68 + command is list of arguments like ['/bin/ls', '-al', '-h']
1.69 +
1.70 + a Popen object is returned"""
1.71 + command_path = Cygwin.root() + os.sep.join(command[0].split('/'))
1.72 + command = [command_path] + command[1:]
1.73 +
1.74 + return subprocess.Popen(command, shell = False, stdin = stdin, stdout = stdout, stderr = stderr)
1.75 +
1.76 +
1.77 + @staticmethod
1.78 + def is_X11_running():
1.79 + """check if we can connect to a X11 running instance"""
1.80 + p = Cygwin()(['/bin/bash', '-c', 'DISPLAY=:0 /usr/bin/xset -q'])
1.81 + stdout, stderr = p.communicate()
1.82 + return p.returncode == 0
1.83 +
1.84 +
1.85 + @staticmethod
1.86 + def start_X11():
1.87 + """start X11 in the background (if not already running) on DISPLAY=:0"""
1.88 +
1.89 + # do not start if already running
1.90 + if Cygwin.is_X11_running():
1.91 + return
1.92 +
1.93 + # launch X11 (forget output and return immediately)
1.94 + p = Cygwin()(['/bin/bash', '--login', '-i', '-c', ' X :0 -multiwindow'], stdin = None, stdout = None, stderr = None)
1.95 +
1.96 +
1.97 +# start
1.98 +if __name__ == "__main__":
1.99 +
1.100 + # execute what is given on the command line
1.101 + c = Cygwin()
1.102 + p = c(sys.argv[1:])
1.103 +
1.104 + # wait until the process finished and grab the output
1.105 + stdout, stderr = p.communicate()
1.106 + print('=== call result on stdout: ===\n' + stdout)
1.107 + print('=== call result on stderr: ===\n' + stderr)
1.108 +