Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / native_client / pnacl / driver / driver_temps.py
1 #!/usr/bin/python
2 # Copyright (c) 2014 The Native Client Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5 #
6 # IMPORTANT NOTE: If you make local mods to this file, you must run:
7 #   %  pnacl/build.sh driver
8 # in order for them to take effect in the scons build.  This command
9 # updates the copy in the toolchain/ tree.
10 #
11
12 """Manages files that'll get deleted when the driver exits unless SAVE_TEMPS.
13 """
14
15 import os
16 import shutil
17
18 import pathtools
19 from driver_env import env
20 from driver_log import Log, AtDriverExit
21
22 class TempFileHandler(object):
23   def __init__(self):
24     AtDriverExit(self.wipe)
25     self.files = []
26
27   def add(self, path):
28     if env.getbool('SAVE_TEMPS'):
29       return
30     path = pathtools.abspath(path)
31     self.files.append(path)
32
33   def wipe(self):
34     for path in self.files:
35       try:
36         sys_path = pathtools.tosys(path)
37         # If exiting early, the file may not have been created yet.
38         if os.path.exists(sys_path):
39           if os.path.isdir(sys_path):
40             shutil.rmtree(sys_path)
41           else:
42             os.remove(sys_path)
43       except OSError as err:
44         Log.Fatal("TempFileHandler: Unable to wipe file %s w/ error %s",
45                   pathtools.touser(path),
46                   err.strerror)
47     self.files = []
48
49 TempFiles = TempFileHandler()