| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Utilities to work with importable python zip packages.""" | 5 """Utilities to work with importable python zip packages.""" |
| 6 | 6 |
| 7 import atexit | 7 import atexit |
| 8 import collections | 8 import collections |
| 9 import cStringIO as StringIO | 9 import cStringIO as StringIO |
| 10 import os | 10 import os |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 def get_main_script_path(): | 230 def get_main_script_path(): |
| 231 """If running from zip returns path to a zip file, else path to __main__. | 231 """If running from zip returns path to a zip file, else path to __main__. |
| 232 | 232 |
| 233 Basically returns path to a file passed to python for execution | 233 Basically returns path to a file passed to python for execution |
| 234 as in 'python <main_script>' considering a case of executable zip package. | 234 as in 'python <main_script>' considering a case of executable zip package. |
| 235 | 235 |
| 236 Returns path relative to a current directory of when process was started. | 236 Returns path relative to a current directory of when process was started. |
| 237 """ | 237 """ |
| 238 # If running from interactive console __file__ is not defined. | 238 # If running from interactive console __file__ is not defined. |
| 239 main = sys.modules['__main__'] | 239 main = sys.modules['__main__'] |
| 240 return get_module_zip_archive(main) or getattr(main, '__file__') | 240 return get_module_zip_archive(main) or getattr(main, '__file__', None) |
| 241 | 241 |
| 242 | 242 |
| 243 def extract_resource(package, resource): | 243 def extract_resource(package, resource): |
| 244 """Returns real file system path to a |resource| file from a |package|. | 244 """Returns real file system path to a |resource| file from a |package|. |
| 245 | 245 |
| 246 If it's inside a zip package, will extract it first into temp file created | 246 If it's inside a zip package, will extract it first into temp file created |
| 247 with tempfile.mkstemp. Such file is readable and writable only by the creating | 247 with tempfile.mkstemp. Such file is readable and writable only by the creating |
| 248 user ID. | 248 user ID. |
| 249 | 249 |
| 250 |package| is a python module object that represents a package. | 250 |package| is a python module object that represents a package. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 """Removes all temporary files created by extract_resource. | 283 """Removes all temporary files created by extract_resource. |
| 284 | 284 |
| 285 Executed as atexit hook. | 285 Executed as atexit hook. |
| 286 """ | 286 """ |
| 287 with _extracted_files_lock: | 287 with _extracted_files_lock: |
| 288 while _extracted_files: | 288 while _extracted_files: |
| 289 try: | 289 try: |
| 290 os.remove(_extracted_files.pop()) | 290 os.remove(_extracted_files.pop()) |
| 291 except OSError: | 291 except OSError: |
| 292 pass | 292 pass |
| OLD | NEW |