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@18
|
59 |
home_drive = os.path.expandvars("%HOMEDRIVE%") + os.sep
|
om@18
|
60 |
path_hint = [
|
om@18
|
61 |
os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin')),
|
om@18
|
62 |
os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin64')),
|
om@18
|
63 |
os.path.abspath(os.path.join(home_drive, 'cygwin')),
|
om@18
|
64 |
os.path.abspath(os.path.join(home_drive, 'cygwin64'))
|
om@18
|
65 |
]
|
om@18
|
66 |
path_valid = [ p for p in path_hint if os.path.exists(p) ]
|
om@18
|
67 |
return path_valid[0]
|
om@13
|
68 |
|
om@13
|
69 |
|
dyle@53
|
70 |
def execute(self, command, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, window = False):
|
om@13
|
71 |
"""execute a cygwin shell command
|
om@13
|
72 |
|
om@13
|
73 |
command is list of arguments like ['/bin/ls', '-al', '-h']
|
om@13
|
74 |
|
om@13
|
75 |
a Popen object is returned"""
|
dyle@52
|
76 |
command_path = os.sep.join([Cygwin.root()] + command[0].split('/')[1:])
|
om@13
|
77 |
command = [command_path] + command[1:]
|
dyle@52
|
78 |
|
dyle@53
|
79 |
if not window:
|
dyle@53
|
80 |
# hide any window
|
dyle@53
|
81 |
shadowrun_path = os.path.join(Cygwin.root(), 'bin', 'shadowrun.exe')
|
dyle@53
|
82 |
if os.access(shadowrun_path, os.X_OK):
|
dyle@53
|
83 |
command = [shadowrun_path] + command
|
dyle@53
|
84 |
else:
|
dyle@53
|
85 |
print("EPIC FAIL!")
|
dyle@52
|
86 |
|
dyle@50
|
87 |
try:
|
dyle@52
|
88 |
print('cygwin: ' + ' '.join(command))
|
dyle@50
|
89 |
return subprocess.Popen(command, shell = False, stdin = stdin, stdout = stdout, stderr = stderr)
|
dyle@50
|
90 |
except:
|
dyle@52
|
91 |
sys.stderr.write('Failed to execute cygwin command.\n\tcommand="' + ' '.join(command) + '"\n')
|
om@13
|
92 |
|
om@13
|
93 |
|
om@13
|
94 |
@staticmethod
|
om@13
|
95 |
def is_X11_running():
|
om@13
|
96 |
"""check if we can connect to a X11 running instance"""
|
om@13
|
97 |
p = Cygwin()(['/bin/bash', '-c', 'DISPLAY=:0 /usr/bin/xset -q'])
|
om@13
|
98 |
stdout, stderr = p.communicate()
|
om@13
|
99 |
return p.returncode == 0
|
om@13
|
100 |
|
om@13
|
101 |
|
om@13
|
102 |
@staticmethod
|
om@13
|
103 |
def start_X11():
|
om@13
|
104 |
"""start X11 in the background (if not already running) on DISPLAY=:0"""
|
om@13
|
105 |
|
om@13
|
106 |
# do not start if already running
|
om@13
|
107 |
if Cygwin.is_X11_running():
|
om@13
|
108 |
return
|
om@13
|
109 |
|
om@13
|
110 |
# launch X11 (forget output and return immediately)
|
om@13
|
111 |
p = Cygwin()(['/bin/bash', '--login', '-i', '-c', ' X :0 -multiwindow'], stdin = None, stdout = None, stderr = None)
|
om@13
|
112 |
|
om@13
|
113 |
|
om@13
|
114 |
# start
|
om@13
|
115 |
if __name__ == "__main__":
|
om@13
|
116 |
|
om@18
|
117 |
print(Cygwin.root())
|
om@18
|
118 |
sys.exit(1)
|
om@18
|
119 |
|
om@13
|
120 |
# execute what is given on the command line
|
om@13
|
121 |
c = Cygwin()
|
om@13
|
122 |
p = c(sys.argv[1:])
|
om@13
|
123 |
|
om@13
|
124 |
# wait until the process finished and grab the output
|
om@13
|
125 |
stdout, stderr = p.communicate()
|
om@13
|
126 |
print('=== call result on stdout: ===\n' + stdout)
|
om@13
|
127 |
print('=== call result on stderr: ===\n' + stderr)
|
om@13
|
128 |
|