OpenSecurity/bin/cygwin.py
author om
Fri, 06 Dec 2013 12:47:53 +0100
changeset 18 d7d7b8dee78e
parent 16 e16d64b5e008
child 50 1f4ba6a6ecf5
permissions -rwxr-xr-x
vmmanger searches for cygwin paths now
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # cygwin command
     6 # 
     7 # executes a cygwin command inside the opensecurity project
     8 #
     9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    10 #
    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
    15 #
    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.
    19 # 
    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.
    24 # 
    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 # ------------------------------------------------------------
    30 
    31 
    32 # ------------------------------------------------------------
    33 # imports
    34 
    35 import os
    36 import subprocess
    37 import sys
    38 
    39 # local
    40 from environment import Environment
    41 
    42 
    43 # ------------------------------------------------------------
    44 # code
    45 
    46 
    47 class Cygwin(object):
    48 
    49     """Some nifty methods working with Cygwin"""
    50     
    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)
    54 
    55         
    56     @staticmethod
    57     def root():
    58         """get the path to our local cygwin installment"""
    59         home_drive = os.path.expandvars("%HOMEDRIVE%") + os.sep
    60         path_hint = [ 
    61             os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin')), 
    62             os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin64')), 
    63             os.path.abspath(os.path.join(home_drive, 'cygwin')),
    64             os.path.abspath(os.path.join(home_drive, 'cygwin64'))
    65         ]
    66         path_valid = [ p for p in path_hint if os.path.exists(p) ]
    67         return path_valid[0]
    68 
    69 
    70     def execute(self, command, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE):
    71         """execute a cygwin shell command
    72         
    73         command is list of arguments like ['/bin/ls', '-al', '-h']
    74         
    75         a Popen object is returned"""
    76         command_path = Cygwin.root() + os.sep.join(command[0].split('/'))
    77         command = [command_path] + command[1:]
    78         
    79         return subprocess.Popen(command, shell = False, stdin = stdin, stdout = stdout, stderr = stderr)
    80         
    81         
    82     @staticmethod
    83     def is_X11_running():
    84         """check if we can connect to a X11 running instance"""
    85         p = Cygwin()(['/bin/bash', '-c', 'DISPLAY=:0 /usr/bin/xset -q'])
    86         stdout, stderr = p.communicate()
    87         return p.returncode == 0
    88         
    89         
    90     @staticmethod
    91     def start_X11():
    92         """start X11 in the background (if not already running) on DISPLAY=:0"""
    93         
    94         # do not start if already running
    95         if Cygwin.is_X11_running():
    96             return
    97             
    98         # launch X11 (forget output and return immediately)
    99         p = Cygwin()(['/bin/bash', '--login', '-i', '-c', ' X :0 -multiwindow'], stdin = None, stdout = None, stderr = None)
   100         
   101     
   102 # start
   103 if __name__ == "__main__":
   104 
   105     print(Cygwin.root())
   106     sys.exit(1)
   107 
   108     # execute what is given on the command line
   109     c = Cygwin()
   110     p = c(sys.argv[1:])
   111     
   112     # wait until the process finished and grab the output
   113     stdout, stderr = p.communicate()
   114     print('=== call result on stdout: ===\n' + stdout)
   115     print('=== call result on stderr: ===\n' + stderr)
   116