Module EDUtilsPath
[hide private]
[frames] | no frames]

Source Code for Module EDUtilsPath

  1  # coding: utf8 
  2  # 
  3  #    Project: The EDNA Kernel 
  4  #             http://www.edna-site.org 
  5  # 
  6  #    Copyright (C) 2008-2012 European Synchrotron Radiation Facility 
  7  #                            Grenoble, France 
  8  # 
  9  #    Principal authors: Marie-Francoise Incardona (incardon@esrf.fr) 
 10  #                       Olof Svensson (svensson@esrf.fr)  
 11  #                       Jérôme Kieffer (jerome.kieffer@esrf.eu) 
 12  # 
 13  #    This program is free software: you can redistribute it and/or modify 
 14  #    it under the terms of the GNU Lesser General Public License as published 
 15  #    by the Free Software Foundation, either version 3 of the License, or 
 16  #    (at your option) any later version. 
 17  # 
 18  #    This program is distributed in the hope that it will be useful, 
 19  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 20  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 21  #    GNU Lesser General Public License for more details. 
 22  # 
 23  #    You should have received a copy of the GNU General Public License 
 24  #    and the GNU Lesser General Public License  along with this program.   
 25  #    If not, see <http://www.gnu.org/licenses/>. 
 26  # 
 27  from __future__ import with_statement 
 28   
 29  __authors__ = [ "Marie-Francoise Incardona", "Olof Svensson", "Jérôme Kieffer" ] 
 30  __contact__ = "svensson@esrf.fr" 
 31  __license__ = "LGPLv3+" 
 32  __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France" 
 33  __date__ = "20120216" 
 34   
 35   
 36  import os, glob, tempfile, getpass, hashlib 
 37  from EDThreading import Semaphore 
 38  from EDVerbose import EDVerbose 
 39  from os.path import dirname, abspath, exists 
40 41 -class classproperty(property):
42 - def __get__(self, obj, type_):
43 return self.fget.__get__(None, type_)()
44 - def __set__(self, obj, value):
45 cls = type(obj) 46 return self.fset.__get__(None, cls)(value)
47
48 49 -class EDUtilsPath:
50 """ 51 This is a static utility class for handling of paths. 52 """ 53 54 EDNA_HOME = dirname(dirname(dirname(abspath(__file__)))) 55 os.environ["EDNA_HOME"] = EDNA_HOME 56 _EDNA_SITE = None 57 _EDNA_USERTEMPFOLDER = None 58 _EDNA_TESTIMAGES = None 59 _EDNA_TESTS = None 60 _EDNA_TESTDATA = None 61 _USER = os.getenv("USER", "UndefindedUser") 62 _TMPDIR = os.getenv("TMPDIR", tempfile.gettempdir()) 63 64 _EDNA_PROJECTS = {} # key: project name. Value: path to the project root 65 _EDNA_PLUGINCACHE = None 66 67 __semaphore = Semaphore() 68 69 70 @classmethod
71 - def appendListOfPaths(cls, _strPath, _listOfPaths):
72 """ 73 Appends a list of paths to a path, for example [ "..", "..", "..", "..", "src" ], 74 and returns the absolute path. 75 """ 76 77 return abspath(os.path.join(_strPath, * _listOfPaths))
78 79 80 @classmethod
81 - def mergePath(cls, _strPath1, _strPath2):
82 """ 83 Merges two paths and returns the absolute path. 84 Deprecated: please use 'os.path.join' instead 85 """ 86 EDVerbose.log("EDUtilsPath.mergePath is deprecated, please use 'os.path.join' instead") 87 strNewPath = os.path.join(_strPath1, _strPath2) 88 return abspath(strNewPath)
89 90 91 @classmethod
92 - def getAbsolutePath(cls, _strPath):
93 """ 94 Returns the absolute path. 95 Deprecated: please use 'os.path.abspath' instead 96 """ 97 EDVerbose.log("EDUtilsPath.getAbsolutePath is deprecated, please use 'os.path.abspath' instead") 98 return abspath(_strPath)
99 100 101 @classmethod
102 - def existPath(cls, _strPath):
103 """ 104 Checks if a folder exists. 105 Deprecated: please use 'os.path.exists' instead 106 """ 107 EDVerbose.log("EDUtilsPath.existPath is deprecated, please use 'os.path.exists' instead") 108 return exists(_strPath)
109 110 111 @classmethod
112 - def getEdnaHome(cls):
113 """ 114 Returns the EDNA_HOME variable from environment if not already defined 115 """ 116 return cls.EDNA_HOME
117 118 119 @classmethod
120 - def setEdnaHome(cls, _strEdnaHome):
121 """ 122 Sets EDNA_HOME home 123 """ 124 cls.EDNA_HOME = _strEdnaHome
125 126 127 @classmethod
128 - def getEdnaSite(cls):
129 """ 130 Returns the EDNA_SITE variable from environment 131 """ 132 # EDNA Site 133 if(cls._EDNA_SITE == None): 134 cls._EDNA_SITE = os.environ.get("EDNA_SITE") 135 if(cls._EDNA_SITE is None): 136 EDVerbose.warning("EDUtilsPath.getEdnaSite: EDNA_SITE not set, using EDNA_SITE='default'") 137 cls._EDNA_SITE = "default" 138 else: 139 EDVerbose.DEBUG("EDUtilsPath.getEdnaSite: EDNA_SITE is set: %s" % cls._EDNA_SITE) 140 return cls._EDNA_SITE
141 142 143 @classmethod
144 - def setEdnaSite(cls, _strEdnaSite):
145 """ 146 Sets EDNA_SITE 147 """ 148 cls._EDNA_SITE = _strEdnaSite
149 EDNA_SITE = classproperty(getEdnaSite, setEdnaSite) 150 151 @classmethod
152 - def getCwd(cls):
153 """ 154 Returns the current directory. 155 Deprecated: please use 'os.getcwd' instead 156 """ 157 EDVerbose.log("EDUtilsPath.getCwd is deprecated, please use 'os.getcwd' instead") 158 return os.getcwd()
159 160 161 @classmethod
162 - def createFolder(cls, _strFolderName):
163 """ 164 Creates a folder (directory) if it doesn't already exists. 165 This used to be deprecated but IS neverthless thread safe (see bug#681) 166 """ 167 EDVerbose.log("EDUtilsPath.createFolder: %s" % _strFolderName) 168 with cls.__semaphore: 169 if (not exists(_strFolderName)): 170 os.makedirs(_strFolderName)
171 172 173 @classmethod
174 - def getFileList(cls, _strPath="./", _strMask="*"):
175 """ 176 Returns a list of file and directory names given a path to a directory. 177 """ 178 if _strMask == "*": 179 listFile = os.listdir(_strPath) 180 else: 181 strFind = os.path.join(_strPath, _strMask) 182 listPath = glob.glob(strFind) 183 listFile = [] 184 for strPath in listPath: 185 strPath = os.path.basename(strPath) 186 listFile.append(strPath) 187 return listFile
188 189 190 @classmethod
191 - def getFolderName(cls, _str):
192 """ 193 Returns the name of a folder (directory) for a given path. 194 Deprecated: please use 'os.path.dirname' instead 195 """ 196 return dirname(_str)
197 198 199 @classmethod
200 - def getEdnaUserTempFolder(cls):
201 """ 202 Returns the name of a temporary folder that is unique for a given user. 203 """ 204 if cls._EDNA_USERTEMPFOLDER is None: 205 if os.environ.has_key("EDNA_USERTEMPFOLDER"): 206 cls._EDNA_USERTEMPFOLDER = os.environ["EDNA_USERTEMPFOLDER"] 207 if not os.path.exists(cls._EDNA_USERTEMPFOLDER): 208 EDVerbose.warning("EDNA_USERTEMPFOLDER environment variable is set to %s put the directory does not exist!" % cls._EDNA_USERTEMPFOLDER) 209 cls._EDNA_USERTEMPFOLDER = None 210 elif (not os.access(cls._EDNA_USERTEMPFOLDER, os.W_OK)): 211 EDVerbose.warning("EDNA_USERTEMPFOLDER environment variable is set to %s put the directory cannot be accessed!" % cls._EDNA_USERTEMPFOLDER) 212 cls._EDNA_USERTEMPFOLDER = None 213 if cls._EDNA_USERTEMPFOLDER is None: 214 strEdnaTempFileDir = tempfile.gettempdir() 215 try: 216 # Working on Windows and Linux: 217 strUserName = getpass.getuser() 218 except Exception: 219 # Working on MacOS: 220 strUserName = os.getlogin() 221 bIsOk = False 222 # Check that we have write access to this directory: 223 if os.access(strEdnaTempFileDir, os.W_OK) and os.access(strEdnaTempFileDir, os.X_OK): 224 cls._EDNA_USERTEMPFOLDER = os.path.join(strEdnaTempFileDir, "edna-%s" % strUserName) 225 # Check that we have write access to this directory: 226 if not os.path.exists(cls._EDNA_USERTEMPFOLDER): 227 try: 228 os.mkdir(cls._EDNA_USERTEMPFOLDER) 229 except Exception: 230 EDVerbose.WARNING("Error when trying to create the directory %s" % cls._EDNA_USERTEMPFOLDER) 231 if os.access(cls._EDNA_USERTEMPFOLDER, os.W_OK) and os.access(cls._EDNA_USERTEMPFOLDER, os.X_OK): 232 bIsOk = True 233 if not bIsOk: 234 # We cannot use the edna-<user name> folder... 235 EDVerbose.WARNING("EDUtilsFile.getEdnaUserTempFolder: cannot access user temporary directory %s" % cls._EDNA_USERTEMPFOLDER) 236 # Create temporary directory 237 cls._EDNA_USERTEMPFOLDER = tempfile.mkdtemp(prefix="edna-") 238 EDVerbose.WARNING("Created temporary directory for this session: %s" % cls._EDNA_USERTEMPFOLDER) 239 EDVerbose.WARNING("If you would like to continue to use this directory for future sessions") 240 EDVerbose.WARNING("please set then environment variable EDNA_USERTEMPFOLDER to %s" % cls._EDNA_USERTEMPFOLDER) 241 return cls._EDNA_USERTEMPFOLDER
242 243 244 @classmethod
245 - def getEdnaPluginCachePath(cls):
246 """This private method initializes the path to the plugin cache file""" 247 if cls._EDNA_PLUGINCACHE is None: 248 if "EDNA_PLUGINCACHE" in os.environ.keys(): 249 cls._EDNA_PLUGINCACHE = os.environ["EDNA_PLUGINCACHE"] 250 strDirName = os.path.dirname(cls._EDNA_PLUGINCACHE) 251 if os.path.exists(cls._EDNA_PLUGINCACHE) and (not os.access(cls._EDNA_PLUGINCACHE, os.W_OK)): 252 EDVerbose.warning("EDNA_PLUGINCACHE environment variable is set to %s but the file is not writeable!" % cls._EDNA_PLUGINCACHE) 253 cls._EDNA_PLUGINCACHE = None 254 elif not os.path.exists(strDirName): 255 EDVerbose.warning("EDNA_PLUGINCACHE environment variable is set to %s put the parent directory does not exist!" % cls._EDNA_PLUGINCACHE) 256 cls._EDNA_PLUGINCACHE = None 257 elif not os.access(strDirName, os.W_OK): 258 EDVerbose.warning("EDNA_PLUGINCACHE environment variable is set to %s put the parent directory cannot be accessed!" % cls._EDNA_PLUGINCACHE) 259 cls._EDNA_PLUGINCACHE = None 260 if cls._EDNA_PLUGINCACHE is None: 261 strTempFileDir = EDUtilsPath.getEdnaUserTempFolder() 262 # We create a hash of the path in order to be able to reference several different EDNA_HOME 263 # caches in the same directory 264 strCacheFileName = hashlib.sha1(os.getenv('EDNA_HOME')).hexdigest() + ".xml" 265 cls._EDNA_PLUGINCACHE = os.path.abspath(os.path.join(strTempFileDir, strCacheFileName)) 266 EDVerbose.DEBUG("EDFactoryPlugin: Path to plugin cache: %s" % cls._EDNA_PLUGINCACHE) 267 return cls._EDNA_PLUGINCACHE
268 269 @classmethod
270 - def getEdnaTestPath(cls):
271 """ 272 This class method initializes the path to the test path (probably unused) 273 """ 274 if cls._EDNA_TESTS is None: 275 if "EDNA_TESTS" in os.environ.keys(): 276 cls.setEdnaTestDataPath(os.environ["EDNA_TESTS"]) 277 if cls._EDNA_TESTS is None: 278 cls._EDNA_TESTS = os.path.join(cls.EDNA_HOME, "tests",) 279 EDVerbose.DEBUG("EDFactoryPlugin: Path to Test Data directory : %s" % cls._EDNA_TESTS) 280 return cls._EDNA_TESTS
281 282 @classmethod
283 - def setEdnaTestPath(cls, value):
284 """ 285 This class method initializes the path to the test data (probably unused) 286 """ 287 cls._EDNA_TESTS = os.path.abspath(value) 288 if not os.path.isdir(cls._EDNA_TESTS): 289 EDVerbose.warning("EDNA_TESTS environment variable is set to %s put the directory does not exist!" % cls._EDNA_TESTS) 290 cls._EDNA_TESTS = None
291 EDNA_TESTS = classproperty(getEdnaTestPath, setEdnaTestPath) 292 293 294 @classmethod
295 - def getEdnaTestDataPath(cls):
296 """ 297 This class method initializes the path to the test data (probably unused) 298 """ 299 if cls._EDNA_TESTDATA is None: 300 if "EDNA_TESTDATA" in os.environ.keys(): 301 cls.setEdnaTestDataPath(os.environ["EDNA_TESTDATA"]) 302 if cls._EDNA_TESTDATA is None: 303 cls._EDNA_TESTDATA = os.path.join(cls.EDNA_HOME, "tests", "data") 304 EDVerbose.DEBUG("EDFactoryPlugin: Path to Test Data directory : %s" % cls._EDNA_TESTDATA) 305 return cls._EDNA_TESTDATA
306 307 @classmethod
308 - def setEdnaTestDataPath(cls, value):
309 """ 310 This class method initializes the path to the test data (probably unused) 311 """ 312 cls._EDNA_TESTDATA = os.path.abspath(value) 313 if not os.path.isdir(cls._EDNA_TESTDATA): 314 EDVerbose.warning("EDNA_TESTDATA environment variable is set to %s put the directory does not exist!" % cls._EDNA_TESTDATA) 315 cls._EDNA_TESTDATA = None
316 EDNA_TESTDATA = classproperty(getEdnaTestDataPath, setEdnaTestDataPath) 317 318 319 @classmethod
321 """ 322 This class method initializes the path to the test images downloaded from internet 323 """ 324 if cls._EDNA_TESTIMAGES is None: 325 if "EDNA_TESTIMAGES" in os.environ.keys(): 326 cls.setEdnaTestDataImagesPath(os.environ["EDNA_TESTIMAGES"]) 327 if cls._EDNA_TESTIMAGES is None: 328 cls._EDNA_TESTIMAGES = os.path.join(cls.EDNA_HOME, "tests", "data", "images") 329 EDVerbose.DEBUG("EDFactoryPlugin: Path to Temporary Images directory : %s" % cls._EDNA_TESTIMAGES) 330 return cls._EDNA_TESTIMAGES
331 332 @classmethod
333 - def setEdnaTestDataImagesPath(cls, value):
334 """ 335 This class method initializes the path to the test images downloaded from internet 336 """ 337 cls._EDNA_TESTIMAGES = os.path.abspath(value) 338 if os.path.exists(cls._EDNA_TESTIMAGES) and (not os.access(cls._EDNA_TESTIMAGES, os.W_OK)): 339 EDVerbose.warning("EDNA_TESTIMAGES environment variable is set to %s but the file is not writeable!" % cls._EDNA_TESTIMAGES) 340 cls._EDNA_TESTIMAGES = None 341 elif not os.path.exists(cls._EDNA_TESTIMAGES): 342 EDVerbose.warning("EDNA_TESTIMAGES environment variable is set to %s put the directory does not exist!" % cls._EDNA_TESTIMAGES) 343 cls._EDNA_TESTIMAGES = None 344 elif not os.access(cls._EDNA_TESTIMAGES, os.W_OK): 345 EDVerbose.warning("EDNA_TESTIMAGES environment variable is set to %s put the directory cannot be accessed!" % cls._EDNA_TESTIMAGES) 346 cls._EDNA_TESTIMAGES = None
347 EDNA_TESTIMAGES = classproperty(getEdnaTestDataImagesPath, setEdnaTestDataImagesPath) 348 349 @classmethod
350 - def getDictOfPaths(cls):
351 """ 352 Return an os.environ like dict enriched with internal path 353 """ 354 res = {"${EDNA_HOME}": cls.EDNA_HOME, 355 "${EDNA_SITE}": cls.EDNA_SITE, 356 "${EDNA_TESTS}": cls.EDNA_TESTS, 357 "${EDNA_TESTDATA}": cls.EDNA_TESTDATA, 358 "${EDNA_TESTIMAGES}": cls.EDNA_TESTIMAGES, 359 "${USER}": cls._USER, 360 "${TMPDIR}": cls._TMPDIR 361 } 362 return res
363