OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # coding=utf-8 | 2 # coding=utf-8 |
3 # Copyright 2012 The Swarming Authors. All rights reserved. | 3 # Copyright 2012 The Swarming Authors. All rights reserved. |
4 # Use of this source code is governed under the Apache License, Version 2.0 that | 4 # Use of this source code is governed under the Apache License, Version 2.0 that |
5 # can be found in the LICENSE file. | 5 # can be found in the LICENSE file. |
6 | 6 |
7 """Traces an executable and its child processes and extract the files accessed | 7 """Traces an executable and its child processes and extract the files accessed |
8 by them. | 8 by them. |
9 | 9 |
10 The implementation uses OS-specific API. The native Kernel logger and the ETL | 10 The implementation uses OS-specific API. The native Kernel logger and the ETL |
(...skipping 21 matching lines...) Expand all Loading... |
32 import tempfile | 32 import tempfile |
33 import threading | 33 import threading |
34 import time | 34 import time |
35 import weakref | 35 import weakref |
36 | 36 |
37 from third_party import colorama | 37 from third_party import colorama |
38 from third_party.depot_tools import fix_encoding | 38 from third_party.depot_tools import fix_encoding |
39 from third_party.depot_tools import subcommand | 39 from third_party.depot_tools import subcommand |
40 | 40 |
41 from utils import file_path | 41 from utils import file_path |
| 42 from utils import logging_utils |
42 from utils import tools | 43 from utils import tools |
43 | 44 |
44 ## OS-specific imports | 45 ## OS-specific imports |
45 | 46 |
46 if sys.platform == 'win32': | 47 if sys.platform == 'win32': |
47 from ctypes.wintypes import byref, c_int, c_wchar_p | 48 from ctypes.wintypes import byref, c_int, c_wchar_p |
48 from ctypes.wintypes import windll # pylint: disable=E0611 | 49 from ctypes.wintypes import windll # pylint: disable=E0611 |
49 | 50 |
50 | 51 |
51 __version__ = '0.1' | 52 __version__ = '0.1' |
(...skipping 3294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3346 return 1 | 3347 return 1 |
3347 except IOError as e: | 3348 except IOError as e: |
3348 if e.errno == errno.EPIPE: | 3349 if e.errno == errno.EPIPE: |
3349 # Do not print a stack trace when the output is piped to less and the user | 3350 # Do not print a stack trace when the output is piped to less and the user |
3350 # quits before the whole output was written. | 3351 # quits before the whole output was written. |
3351 return 1 | 3352 return 1 |
3352 raise | 3353 raise |
3353 return 0 | 3354 return 0 |
3354 | 3355 |
3355 | 3356 |
3356 class OptionParserTraceInputs(tools.OptionParserWithLogging): | 3357 class OptionParserTraceInputs(logging_utils.OptionParserWithLogging): |
3357 """Adds automatic --log handling.""" | 3358 """Adds automatic --log handling.""" |
3358 | 3359 |
3359 # Disable --log-file options since both --log and --log-file options are | 3360 # Disable --log-file options since both --log and --log-file options are |
3360 # confusing. | 3361 # confusing. |
3361 # TODO(vadimsh): Rename --log-file or --log to something else. | 3362 # TODO(vadimsh): Rename --log-file or --log to something else. |
3362 enable_log_file = False | 3363 enable_log_file = False |
3363 | 3364 |
3364 def __init__(self, **kwargs): | 3365 def __init__(self, **kwargs): |
3365 tools.OptionParserWithLogging.__init__(self, **kwargs) | 3366 logging_utils.OptionParserWithLogging.__init__(self, **kwargs) |
3366 self.add_option( | 3367 self.add_option( |
3367 '-l', '--log', help='Log file to generate or read, required') | 3368 '-l', '--log', help='Log file to generate or read, required') |
3368 | 3369 |
3369 def parse_args(self, *args, **kwargs): | 3370 def parse_args(self, *args, **kwargs): |
3370 """Makes sure the paths make sense. | 3371 """Makes sure the paths make sense. |
3371 | 3372 |
3372 On Windows, / and \ are often mixed together in a path. | 3373 On Windows, / and \ are often mixed together in a path. |
3373 """ | 3374 """ |
3374 options, args = tools.OptionParserWithLogging.parse_args( | 3375 options, args = logging_utils.OptionParserWithLogging.parse_args( |
3375 self, *args, **kwargs) | 3376 self, *args, **kwargs) |
3376 if not options.log: | 3377 if not options.log: |
3377 self.error('Must supply a log file with -l') | 3378 self.error('Must supply a log file with -l') |
3378 options.log = os.path.abspath(options.log) | 3379 options.log = os.path.abspath(options.log) |
3379 return options, args | 3380 return options, args |
3380 | 3381 |
3381 | 3382 |
3382 def main(argv): | 3383 def main(argv): |
3383 dispatcher = subcommand.CommandDispatcher(__name__) | 3384 dispatcher = subcommand.CommandDispatcher(__name__) |
3384 try: | 3385 try: |
3385 return dispatcher.execute( | 3386 return dispatcher.execute( |
3386 OptionParserTraceInputs(version=__version__), argv) | 3387 OptionParserTraceInputs(version=__version__), argv) |
3387 except TracingFailure, e: | 3388 except TracingFailure, e: |
3388 sys.stderr.write('\nError: ') | 3389 sys.stderr.write('\nError: ') |
3389 sys.stderr.write(str(e)) | 3390 sys.stderr.write(str(e)) |
3390 sys.stderr.write('\n') | 3391 sys.stderr.write('\n') |
3391 return 1 | 3392 return 1 |
3392 | 3393 |
3393 | 3394 |
3394 if __name__ == '__main__': | 3395 if __name__ == '__main__': |
3395 fix_encoding.fix_encoding() | 3396 fix_encoding.fix_encoding() |
3396 tools.disable_buffering() | 3397 tools.disable_buffering() |
3397 colorama.init() | 3398 colorama.init() |
3398 sys.exit(main(sys.argv[1:])) | 3399 sys.exit(main(sys.argv[1:])) |
OLD | NEW |