Coverage for /media/ldata/code/tendril/tendril/utils/fsutils.py: 33%

# Coverage for /media/ldata/code/tendril/tendril/utils/fsutils.py : 33%

## 147 statements 49 run 98 missing 0 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | # Copyright (C) 2015 Chintalagiri Shashank # # This file is part of Tendril. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. """ The Filesystem Utils Module (:mod:`tendril.utils.fsutils`) ========================================================== This module provides utilities to deal with filesystems. For the most part, this module basically proxies specific requests to various other third-party or python libraries. .. rubric:: Module Contents .. autosummary:: TEMPDIR get_tempname fsutils_cleanup zipdir Crumb get_path_breadcrumbs get_folder_mtime get_file_mtime get_file_hash VersionedOutputFile import_ """ import imp import tempfile import zipfile import atexit import os import glob import string import hashlib import base64 from fs.opener import fsopendir from fs.errors import ResourceNotFoundError from datetime import datetime from collections import namedtuple from tendril.utils import log logger = log.get_logger(__name__, log.INFO) if tempfile.tempdir is None: tempfile.tempdir = tempfile.mkdtemp() #: The path to the temporary directory which all application code can import, #: and create whatever temporary files it needs within it. #: #: This directory will be removed by Tendril at clean application exit or #: by the Operating System as per it's policies. #: #: Every execution of tendril in a separate process owns it's own temporary #: directory. #: #: .. seealso:: :func:`fsutils_cleanup` #: TEMPDIR = tempfile.gettempdir() temp_fs = fsopendir(TEMPDIR) def get_tempname(): """ Gets a random string for use as a temporary filename. :return: A filename that can be used. """ return next(tempfile._get_candidate_names()) def zipdir(path, zfpath): """ Creates a zip file at ``zfpath`` containing all the files in ``path``. This function is simple wrapper around python's :mod:`zipfile` module. :param path: Path of the source folder, which is to be added to the zip file. :param zfpath: Path of the zip file to create. :return: The path of the created zip file. """ zfile = zipfile.ZipFile(zfpath, 'w') for root, dirs, files in os.walk(path): for f in files: zfile.write( os.path.join(root, f), os.path.relpath( os.path.join(root, f), os.path.join(path, '..') ) ) zfile.close() return zfpath #: A named tuple definition for a Crumb of a Breadcrumb. #: This can be used to construct breadcrumb navigation by application #: code. While it resides in the :mod:`tendril.utils.fs` module, the same #: type should be used for other forms of breadcrumbs as well. It is #: placed here due to its proximity to :mod:`os.path`. Crumb = namedtuple('Crumb', 'name path') def get_path_breadcrumbs(path, base=None, rootst='Root'): """ Given a certain filesystem ``path`` and an optional ``base``, this function returns a list of :class:`Crumb` objects, forming the breadcrumbs to that path from the base. The value of ``rootst`` is prepended to the list, providing a means to insert a title indicating the base of the breadcrumb list. :param path: The path to the target, compatible with :mod:`os.path` :type path: str :param base: The path of the base, compatible with :mod:`os.path`. Optional. :type base: str :param rootst: The string for the root breadcrumb. :type rootst: str :return: The breadcrumbs. :rtype: :class:`list` of :class:`Crumb` """ if base is not None: path = os.path.relpath(path, base) crumbs = [] while True: head, tail = os.path.split(path) if not tail: break crumbs = [Crumb(name=tail, path=path)] + crumbs path = head crumbs = [Crumb(name=rootst, path='')] + crumbs return crumbs def get_folder_mtime(folder, fs=None): """ Given the path to a certain filesystem ``folder``, this function returns a :class:`datetime.datetime` instance representing the time of the latest change of any file contained within the folder. :param folder: The path to the ``folder``, compatible with :mod:`os.path` :type folder: str :param fs: The pyfilesystem to use. (Default) None for local fs. :type fs: :class:`fs.base.FS` :return: The time of the latest change within the ``folder`` :rtype: :class:`datetime.datetime` .. seealso:: :func:`get_file_mtime` """ last_change = None if fs is None: filelist = [os.path.join(folder, f) for f in os.listdir(folder)] for f in filelist: if os.path.isfile(f): fct = get_file_mtime(f) elif os.path.isdir(f): fct = get_folder_mtime(f) else: raise OSError("Not file, not directory : " + f) if fct is not None and (last_change is None or fct > last_change): last_change = fct else: filelist = fs.listdir(path=folder, files_only=True, full=True) dirlist = fs.listdir(path=folder, dirs_only=True, full=True) for f in filelist: fct = get_file_mtime(f, fs) if last_change is None or fct > last_change: last_change = fct for d in dirlist: fct = get_folder_mtime(d, fs) if last_change is None or fct > last_change: last_change = fct return last_change def get_file_mtime(f, fs=None): """ Given the path to a certain filesystem ``file``, this function returns a :class:`datetime.datetime` instance representing the time of the latest change of that file. :param f: The path to the ``file``, compatible with :mod:`os.path` :type f: str :param fs: The pyfilesystem to use. (Default) None for local fs. :type fs: :class:`fs.base.FS` :return: The time of the latest change within the ``folder`` :rtype: :class:`datetime.datetime` .. seealso:: :func:`get_folder_mtime` """ if fs is None: try: return datetime.fromtimestamp(os.path.getmtime(f)) except OSError: return None else: try: return fs.getinfo(f)['modified_time'] except ResourceNotFoundError: return None def get_file_hash(filepath, hasher=None, blocksize=65536): """ Return the hash of the file located at the given filepath, using the hasher specified. The hash is encoded in base64 to make it shorter while preserving collision resistance. Note that the resulting hash is case sensitive. .. seealso:: :mod:`hashlib` :param filepath: Path of the file which needs to be hashed. :param hasher: Hash function to use. Default :mod:`hashlib.sha256` :param blocksize: Size of each block to hash. :return: The hex digest for the file. """ if hasher is None: hasher = hashlib.sha256() with open(filepath, 'rb') as afile: buf = afile.read(blocksize) while len(buf) > 0: hasher.update(buf) buf = afile.read(blocksize) return base64.b64encode(hasher.digest()) class VersionedOutputFile: """ This is like a file object opened for output, but it makes versioned backups of anything it might otherwise overwrite. `http://code.activestate.com/recipes/\ 52277-saving-backups-when-writing-files/`_ """ def __init__(self, pathname, numSavedVersions=3): """ Create a new output file. :param pathname: The name of the file to [over]write. :param numSavedVersions: How many of the most recent versions of `pathname` to save. """ self._pathname = pathname self._tmpPathname = "%s.~new~" % self._pathname self._numSavedVersions = numSavedVersions self._outf = open(self._tmpPathname, "wb") def __del__(self): self.close() def close(self): if self._outf: self._outf.close() self._replace_current_file() self._outf = None def as_file(self): """ Return self's shadowed file object, since marshal is pretty insistent on working w. pure file objects. """ return self._outf def __getattr__(self, attr): """ Delegate most operations to self's open file object. """ return getattr(self.__dict__['_outf'], attr) def _replace_current_file(self): """ Replace the current contents of self's named file. """ self._backup_current_file() os.rename(self._tmpPathname, self._pathname) def _backup_current_file(self): """ Save a numbered backup of self's named file. """ # If the file doesn't already exist, there's nothing to do. if os.path.isfile(self._pathname): new_name = self._versioned_name(self._current_revision() + 1) os.rename(self._pathname, new_name) # Maybe get rid of old versions. if (self._numSavedVersions is not None) and \ (self._numSavedVersions > 0): self._delete_old_revisions() def _versioned_name(self, revision): """ Get self's pathname with a revision number appended. """ return "%s.~%s~" % (self._pathname, revision) def _current_revision(self): """ Get the revision number of self's largest existing backup. """ revisions = [0] + self._revisions() return max(revisions) def _revisions(self): """ Get the revision numbers of all of self's backups. """ revisions = [] backup_names = glob.glob("%s.~[0-9]*~" % self._pathname) for name in backup_names: try: revision = int(string.split(name, "~")[-2]) revisions.append(revision) except ValueError: # Some ~[0-9]*~ extensions may not be wholly numeric. pass revisions.sort() return revisions def _delete_old_revisions(self): """ Delete old versions of self's file, so that at most :attr:`_numSavedVersions` versions are retained. """ revisions = self._revisions() revisions_to_delete = revisions[:-self._numSavedVersions] for revision in revisions_to_delete: pathname = self._versioned_name(revision) if os.path.isfile(pathname): os.remove(pathname) def fsutils_cleanup(): """ Called when the python interpreter is shutting down. Cleans up all `tendril.utils.fs` related objects and other artifacts created by the module. Each user of the TEMPDIR should clean up it's own files and folders before now. If TEMPDIR is non-empty at this point, this function won't delete the folder. Performs the following tasks: - Removes the :data:`TEMPDIR` """ try: os.rmdir(tempfile.gettempdir()) except OSError: pass def import_(fpath): """ Imports the file specified by the ``fpath`` parameter using the :mod:`imp` python module and returns the loaded module. :param fpath: Path of the python module to import. :return: Module object of the imported python module. """ (path, name) = os.path.split(fpath) (name, ext) = os.path.splitext(name) (f, filename, data) = imp.find_module(name, [path]) return imp.load_module(name, f, filename, data) def get_parent(obj, n=1): """ This function is intended for use by modules imported from outside the package via the filesystem to get around the behavior of python's super() which breaks when something is effectively reloaded. .. todo:: A cleaner solution to handle this condition is needed. """ import inspect return inspect.getmro(obj.__class__)[n] atexit.register(fsutils_cleanup)