comparison env/lib/python3.7/site-packages/virtualenv/run/app_data.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 import logging
2 import os
3 from argparse import Action, ArgumentError
4 from tempfile import mkdtemp
5
6 from appdirs import user_data_dir
7
8 from virtualenv.util.lock import ReentrantFileLock
9 from virtualenv.util.path import safe_delete
10
11
12 class AppData(object):
13 def __init__(self, folder):
14 self.folder = ReentrantFileLock(folder)
15 self.transient = False
16
17 def __repr__(self):
18 return "{}".format(self.folder.path)
19
20 def clean(self):
21 logging.debug("clean app data folder %s", self.folder.path)
22 safe_delete(self.folder.path)
23
24 def close(self):
25 """"""
26
27
28 class TempAppData(AppData):
29 def __init__(self):
30 super(TempAppData, self).__init__(folder=mkdtemp())
31 self.transient = True
32 logging.debug("created temporary app data folder %s", self.folder.path)
33
34 def close(self):
35 logging.debug("remove temporary app data folder %s", self.folder.path)
36 safe_delete(self.folder.path)
37
38
39 class AppDataAction(Action):
40 def __call__(self, parser, namespace, values, option_string=None):
41 folder = self._check_folder(values)
42 if folder is None:
43 raise ArgumentError("app data path {} is not valid".format(values))
44 setattr(namespace, self.dest, AppData(folder))
45
46 @staticmethod
47 def _check_folder(folder):
48 folder = os.path.abspath(folder)
49 if not os.path.exists(folder):
50 try:
51 os.makedirs(folder)
52 logging.debug("created app data folder %s", folder)
53 except OSError as exception:
54 logging.info("could not create app data folder %s due to %r", folder, exception)
55 return None
56 write_enabled = os.access(folder, os.W_OK)
57 if write_enabled:
58 return folder
59 logging.debug("app data folder %s has no write access", folder)
60 return None
61
62 @staticmethod
63 def default():
64 for folder in AppDataAction._app_data_candidates():
65 folder = AppDataAction._check_folder(folder)
66 if folder is not None:
67 return AppData(folder)
68 return None
69
70 @staticmethod
71 def _app_data_candidates():
72 key = str("VIRTUALENV_OVERRIDE_APP_DATA")
73 if key in os.environ:
74 yield os.environ[key]
75 else:
76 yield user_data_dir(appname="virtualenv", appauthor="pypa")
77
78
79 __all__ = (
80 "AppData",
81 "TempAppData",
82 "AppDataAction",
83 )