1+ ## JavascriptSubtitlesOctopus
2+ ## From https://github.com/emscripten-core/emscripten/blob/c834ef7d69ccb4100239eeba0b0f6573fed063bc/tools/tempfiles.py
3+
14# Copyright 2013 The Emscripten Authors. All rights reserved.
25# Emscripten is available under two separate licenses, the MIT license and the
36# University of Illinois/NCSA Open Source License. Both these licenses can be
47# found in the LICENSE file.
58
6- from __future__ import print_function
79import os
810import shutil
911import tempfile
@@ -28,13 +30,14 @@ def try_delete(pathname):
2830 if not os .path .exists (pathname ):
2931 return
3032
31- write_bits = stat .S_IWRITE | stat .S_IWGRP | stat .S_IWOTH
33+ # Ensure all files are readable and writable by the current user.
34+ permission_bits = stat .S_IWRITE | stat .S_IREAD
3235
3336 def is_writable (path ):
34- return (os .stat (path ).st_mode & write_bits ) == write_bits
37+ return (os .stat (path ).st_mode & permission_bits ) != permission_bits
3538
3639 def make_writable (path ):
37- os .chmod (path , os .stat (path ).st_mode | write_bits )
40+ os .chmod (path , os .stat (path ).st_mode | permission_bits )
3841
3942 # Some tests make files and subdirectories read-only, so rmtree/unlink will not delete
4043 # them. Force-make everything writable in the subdirectory to make it
@@ -54,9 +57,9 @@ def make_writable(path):
5457 pass
5558
5659
57- class TempFiles ( object ) :
58- def __init__ (self , tmp , save_debug_files = False ):
59- self .tmp = tmp
60+ class TempFiles :
61+ def __init__ (self , tmpdir , save_debug_files ):
62+ self .tmpdir = tmpdir
6063 self .save_debug_files = save_debug_files
6164 self .to_clean = []
6265
@@ -67,17 +70,19 @@ def note(self, filename):
6770
6871 def get (self , suffix ):
6972 """Returns a named temp file with the given prefix."""
70- named_file = tempfile .NamedTemporaryFile (dir = self .tmp , suffix = suffix , delete = False )
73+ named_file = tempfile .NamedTemporaryFile (dir = self .tmpdir , suffix = suffix , delete = False )
7174 self .note (named_file .name )
7275 return named_file
7376
7477 def get_file (self , suffix ):
75- """Returns an object representing a RAII-like access to a temp file, that has convenient pythonesque
76- semantics for being used via a construct 'with TempFiles.get_file(..) as filename:'. The file will be
77- deleted immediately once the 'with' block is exited."""
78- class TempFileObject (object ):
78+ """Returns an object representing a RAII-like access to a temp file
79+ that has convenient pythonesque semantics for being used via a construct
80+ 'with TempFiles.get_file(..) as filename:'.
81+ The file will be deleted immediately once the 'with' block is exited.
82+ """
83+ class TempFileObject :
7984 def __enter__ (self_ ):
80- self_ .file = tempfile .NamedTemporaryFile (dir = self .tmp , suffix = suffix , delete = False )
85+ self_ .file = tempfile .NamedTemporaryFile (dir = self .tmpdir , suffix = suffix , delete = False )
8186 self_ .file .close () # NamedTemporaryFile passes out open file handles, but callers prefer filenames (and open their own handles manually if needed)
8287 return self_ .file .name
8388
@@ -88,20 +93,14 @@ def __exit__(self_, type, value, traceback):
8893
8994 def get_dir (self ):
9095 """Returns a named temp directory with the given prefix."""
91- directory = tempfile .mkdtemp (dir = self .tmp )
96+ directory = tempfile .mkdtemp (dir = self .tmpdir )
9297 self .note (directory )
9398 return directory
9499
95100 def clean (self ):
96101 if self .save_debug_files :
97- print ('not cleaning up temp files since in debug-save mode, see them in %s' % ( self .tmp ,) , file = sys .stderr )
102+ print (f 'not cleaning up temp files since in debug-save mode, see them in { self .tmpdir } ' , file = sys .stderr )
98103 return
99104 for filename in self .to_clean :
100105 try_delete (filename )
101106 self .to_clean = []
102-
103- def run_and_clean (self , func ):
104- try :
105- return func ()
106- finally :
107- self .clean ()
0 commit comments