diff options
author | Mike Crute <mcrute@gmail.com> | 2015-12-13 17:40:25 -0800 |
---|---|---|
committer | Mike Crute <mcrute@gmail.com> | 2015-12-13 17:40:25 -0800 |
commit | ded63104b0c0e3977874142c37c6768381f468d8 (patch) | |
tree | f9a1647da81f144b9edea04f554416169a5f0664 /shim.py | |
download | cloudfront_logs_packaging-master.tar.bz2 cloudfront_logs_packaging-master.tar.xz cloudfront_logs_packaging-master.zip |
Diffstat (limited to 'shim.py')
-rw-r--r-- | shim.py | 105 |
1 files changed, 105 insertions, 0 deletions
@@ -0,0 +1,105 @@ | |||
1 | import re | ||
2 | import os | ||
3 | import atexit | ||
4 | import shutil | ||
5 | import subprocess | ||
6 | import __builtin__ | ||
7 | from cStringIO import StringIO | ||
8 | |||
9 | FILES = [] | ||
10 | |||
11 | _fdopen = os.fdopen | ||
12 | _oopen = os.open | ||
13 | _open = __builtin__.open | ||
14 | def write_files(): | ||
15 | os.open = _oopen | ||
16 | os.fdopen = _fdopen | ||
17 | __builtin__.open = _open | ||
18 | |||
19 | for file in FILES: | ||
20 | if not file._safe_name(): | ||
21 | continue | ||
22 | _open(file._safe_name(), 'w').write(file.buffer.getvalue()) | ||
23 | |||
24 | |||
25 | atexit.register(write_files) | ||
26 | |||
27 | |||
28 | class FakeOpenFile: | ||
29 | |||
30 | def __init__(self, name=None): | ||
31 | self.name = name | ||
32 | self.buffer = StringIO() | ||
33 | FILES.append(self) | ||
34 | |||
35 | def _safe_name(self): | ||
36 | name = self.name | ||
37 | if hasattr(self.name, 'name'): | ||
38 | name = self.name.name | ||
39 | if isinstance(name, int): | ||
40 | return None | ||
41 | return str(name).replace("/", "__") | ||
42 | |||
43 | def readline(self): | ||
44 | return "Ubuntu" | ||
45 | |||
46 | def writelines(self, lines): | ||
47 | self.buffer.write(lines) | ||
48 | |||
49 | def write(self, lines): | ||
50 | self.buffer.write(lines) | ||
51 | |||
52 | def read(self): | ||
53 | return "" | ||
54 | |||
55 | def close(self): | ||
56 | pass | ||
57 | |||
58 | def __enter__(self): | ||
59 | return self | ||
60 | |||
61 | def __exit__(self, exc_type, exc_val, exc_tb): | ||
62 | pass | ||
63 | |||
64 | |||
65 | shutil.copyfile = lambda x, y: False | ||
66 | |||
67 | def patched_call(*args, **kwargs): | ||
68 | print "CALL: {!r} {!r}".format(args, kwargs) | ||
69 | return 0 | ||
70 | |||
71 | subprocess.call = patched_call | ||
72 | |||
73 | os.geteuid = lambda: 0 | ||
74 | |||
75 | def patched_system(*args, **kwargs): | ||
76 | print "SYSTEM: {!r} {!r}".format(args, kwargs) | ||
77 | |||
78 | os.system = patched_system | ||
79 | |||
80 | _old_open = __builtin__.open | ||
81 | def patched_open(name): | ||
82 | def opener(*args, **kwargs): | ||
83 | if args[0] == os.devnull: | ||
84 | return _old_open(os.devnull) | ||
85 | |||
86 | print "{}: {!r} {!r}".format(name, args, kwargs) | ||
87 | |||
88 | return FakeOpenFile(args[0]) | ||
89 | return opener | ||
90 | |||
91 | os.open = patched_open("OS.OPEN") | ||
92 | os.fdopen = patched_open("OS.FDOPEN") | ||
93 | __builtin__.open = patched_open("OPEN") | ||
94 | |||
95 | def patched_mkdir(*args, **kwargs): | ||
96 | print "MKDIR: {!r} {!r}".format(args, kwargs) | ||
97 | |||
98 | os.mkdir = patched_mkdir | ||
99 | |||
100 | def patched_raw_input(prompt): | ||
101 | print prompt | ||
102 | value = re.findall('\[([^\]]+)\]', prompt)[0] | ||
103 | return value if value != 'Y' else 'n' | ||
104 | |||
105 | __builtin__.raw_input = patched_raw_input | ||