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