-
Notifications
You must be signed in to change notification settings - Fork 0
/
executable.py
66 lines (49 loc) · 2.17 KB
/
executable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import stat
import subprocess
import shutil
class Executable:
"""Prepare and run 64 bit Linux executable with AWS Lambda
Written by Brandon Owen - github.com/umeat
"""
def __init__(self, source, no_move=False):
"""Prepare access and permissions for executable
Source executable file is moved to /tmp/ directory and given execution
permissions - effectively 'chmod +x'
Input:
source String containing relative location of executable file
no_move Whether or not to move executable to tmp space
Self variables:
command New location of executable, used to run executable
"""
if no_move:
self.command = source
else:
# Create copy of executable in /tmp/
self.command = '/tmp/{}'.format(os.path.basename(source))
shutil.copyfile(source, self.command)
# Grant execution permissions
st = os.stat(self.command)
os.chmod(self.command, st.st_mode | stat.S_IEXEC)
def run(self, args=''):
"""Run executable from shell with given arguments
Input:
args Optional, should be given a string to suffix a command
line call to the executable, eg `-sw1 -sw2`
Self variables:
stdout, stderr Process standard communication channels
returncode Process exit status
Process output variables are overwritten if run multiple times
Additionally returns stdout
"""
# Clear output variables from any previous execution
self.stdout, self.stderr, self.returncode = None, None, None
# Run executable from shell with arguments as string
process = subprocess.Popen(
'{} {}'.format(self.command, args), shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Store stdout and stderr
self.stdout, self.stderr = process.communicate()
self.returncode = process.returncode
# Return stdout
return self.stdout