OpenSecurity/bin/cygwin.py
author om
Fri, 06 Dec 2013 12:10:30 +0100
changeset 14 c187aaceca32
parent 3 OpenSecurity/client/cygwin.py@65432e6c6042
child 16 e16d64b5e008
permissions -rwxr-xr-x
renamed "client" to "bin"
om@3
     1
#!/bin/env python
om@3
     2
# -*- coding: utf-8 -*-
om@3
     3
om@3
     4
# ------------------------------------------------------------
om@3
     5
# cygwin command
om@3
     6
# 
om@3
     7
# executes a cygwin command inside the opensecurity project
om@3
     8
#
om@3
     9
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
om@3
    10
#
om@3
    11
# Copyright (C) 2013 AIT Austrian Institute of Technology
om@3
    12
# AIT Austrian Institute of Technology GmbH
om@3
    13
# Donau-City-Strasse 1 | 1220 Vienna | Austria
om@3
    14
# http://www.ait.ac.at
om@3
    15
#
om@3
    16
# This program is free software; you can redistribute it and/or
om@3
    17
# modify it under the terms of the GNU General Public License
om@3
    18
# as published by the Free Software Foundation version 2.
om@3
    19
# 
om@3
    20
# This program is distributed in the hope that it will be useful,
om@3
    21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
om@3
    22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
om@3
    23
# GNU General Public License for more details.
om@3
    24
# 
om@3
    25
# You should have received a copy of the GNU General Public License
om@3
    26
# along with this program; if not, write to the Free Software
om@3
    27
# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
om@3
    28
# Boston, MA  02110-1301, USA.
om@3
    29
# ------------------------------------------------------------
om@3
    30
om@3
    31
om@3
    32
# ------------------------------------------------------------
om@3
    33
# imports
om@3
    34
om@3
    35
import os
om@3
    36
import subprocess
om@3
    37
import sys
om@3
    38
om@3
    39
# local
om@3
    40
from environment import Environment
om@3
    41
om@3
    42
om@3
    43
# ------------------------------------------------------------
om@3
    44
# code
om@3
    45
om@3
    46
om@3
    47
class Cygwin(object):
om@3
    48
om@3
    49
    """Some nifty methods working with Cygwin"""
om@3
    50
    
om@3
    51
    def __call__(self, command, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE):
om@3
    52
        """make an instance of this object act as a function"""
om@3
    53
        return self.execute(command, stdin, stdout, stderr)
om@3
    54
om@3
    55
        
om@3
    56
    @staticmethod
om@3
    57
    def root():
om@3
    58
        """get the path to our local cygwin installment"""
om@3
    59
        return os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin')
om@3
    60
om@3
    61
om@3
    62
    def execute(self, command, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE):
om@3
    63
        """execute a cygwin shell command
om@3
    64
        
om@3
    65
        command is list of arguments like ['/bin/ls', '-al', '-h']
om@3
    66
        
om@3
    67
        a Popen object is returned"""
om@3
    68
        command_path = Cygwin.root() + os.sep.join(command[0].split('/'))
om@3
    69
        command = [command_path] + command[1:]
om@3
    70
        
om@3
    71
        return subprocess.Popen(command, shell = False, stdin = stdin, stdout = stdout, stderr = stderr)
om@3
    72
        
om@3
    73
        
om@3
    74
    @staticmethod
om@3
    75
    def is_X11_running():
om@3
    76
        """check if we can connect to a X11 running instance"""
om@3
    77
        p = Cygwin()(['/bin/bash', '-c', 'DISPLAY=:0 /usr/bin/xset -q'])
om@3
    78
        stdout, stderr = p.communicate()
om@3
    79
        return p.returncode == 0
om@3
    80
        
om@3
    81
        
om@3
    82
    @staticmethod
om@3
    83
    def start_X11():
om@3
    84
        """start X11 in the background (if not already running) on DISPLAY=:0"""
om@3
    85
        
om@3
    86
        # do not start if already running
om@3
    87
        if Cygwin.is_X11_running():
om@3
    88
            return
om@3
    89
            
om@3
    90
        # launch X11 (forget output and return immediately)
om@3
    91
        p = Cygwin()(['/bin/bash', '--login', '-i', '-c', ' X :0 -multiwindow'], stdin = None, stdout = None, stderr = None)
om@3
    92
        
om@3
    93
    
om@3
    94
# start
om@3
    95
if __name__ == "__main__":
om@3
    96
om@3
    97
    # execute what is given on the command line
om@3
    98
    c = Cygwin()
om@3
    99
    p = c(sys.argv[1:])
om@3
   100
    
om@3
   101
    # wait until the process finished and grab the output
om@3
   102
    stdout, stderr = p.communicate()
om@3
   103
    print('=== call result on stdout: ===\n' + stdout)
om@3
   104
    print('=== call result on stderr: ===\n' + stderr)
om@3
   105