1
+ ## JavascriptSubtitlesOctopus
2
+ ## From https://github.com/emscripten-core/emscripten/blob/c834ef7d69ccb4100239eeba0b0f6573fed063bc/tools/tempfiles.py
3
+
1
4
# Copyright 2013 The Emscripten Authors. All rights reserved.
2
5
# Emscripten is available under two separate licenses, the MIT license and the
3
6
# University of Illinois/NCSA Open Source License. Both these licenses can be
4
7
# found in the LICENSE file.
5
8
6
- from __future__ import print_function
7
9
import os
8
10
import shutil
9
11
import tempfile
@@ -28,13 +30,14 @@ def try_delete(pathname):
28
30
if not os .path .exists (pathname ):
29
31
return
30
32
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
32
35
33
36
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
35
38
36
39
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 )
38
41
39
42
# Some tests make files and subdirectories read-only, so rmtree/unlink will not delete
40
43
# them. Force-make everything writable in the subdirectory to make it
@@ -54,9 +57,9 @@ def make_writable(path):
54
57
pass
55
58
56
59
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
60
63
self .save_debug_files = save_debug_files
61
64
self .to_clean = []
62
65
@@ -67,17 +70,19 @@ def note(self, filename):
67
70
68
71
def get (self , suffix ):
69
72
"""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 )
71
74
self .note (named_file .name )
72
75
return named_file
73
76
74
77
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 :
79
84
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 )
81
86
self_ .file .close () # NamedTemporaryFile passes out open file handles, but callers prefer filenames (and open their own handles manually if needed)
82
87
return self_ .file .name
83
88
@@ -88,20 +93,14 @@ def __exit__(self_, type, value, traceback):
88
93
89
94
def get_dir (self ):
90
95
"""Returns a named temp directory with the given prefix."""
91
- directory = tempfile .mkdtemp (dir = self .tmp )
96
+ directory = tempfile .mkdtemp (dir = self .tmpdir )
92
97
self .note (directory )
93
98
return directory
94
99
95
100
def clean (self ):
96
101
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 )
98
103
return
99
104
for filename in self .to_clean :
100
105
try_delete (filename )
101
106
self .to_clean = []
102
-
103
- def run_and_clean (self , func ):
104
- try :
105
- return func ()
106
- finally :
107
- self .clean ()
0 commit comments