-
Notifications
You must be signed in to change notification settings - Fork 366
/
02_temp_file_cleanup.py
50 lines (42 loc) · 1.45 KB
/
02_temp_file_cleanup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import json
import os
import sublime
import shutil
import tempfile
from .latextools_utils.logger import logger
# unfortunately, there is no reliable way to do clean-up on exit in ST
# see https://github.com/SublimeTextIssues/Core/issues/10
# here we cleanup any directories listed in the temporary_output_dirs
# file as having been previously created by the plugin
def plugin_loaded():
temporary_output_dirs = os.path.join(
sublime.cache_path(),
'LaTeXTools',
'temporary_output_dirs'
)
if os.path.exists(temporary_output_dirs):
with open(temporary_output_dirs, 'r') as f:
data = json.load(f)
tempdir = tempfile.gettempdir()
try:
for directory in data['directories']:
# shutil.rmtree is a rather blunt tool, so here we try to
# ensure we are only deleting legitimate temporary files
if (
directory is None or
not isinstance(directory, str) or
not directory.startswith(tempdir)
):
continue
try:
shutil.rmtree(directory)
except OSError:
pass
else:
logger.info('Deleted old temp directory %s', directory)
except KeyError:
pass
try:
os.remove(temporary_output_dirs)
except OSError:
pass