Find the first file to match the path and ref, read the file out of hg
    and send the path to the newly cached file
503452
def find_file(path, tgt_env='base', **kwargs):  # pylint: disable=W0613
    '''
    
    '''
    fnd = {'path': '',
           'rel': ''}
    if os.path.isabs(path) or tgt_env not in envs():
        return fnd

    dest = os.path.join(__opts__['cachedir'], 'hgfs/refs', tgt_env, path)
    hashes_glob = os.path.join(__opts__['cachedir'],
                               'hgfs/hash',
                               tgt_env,
                               '{0}.hash.*'.format(path))
    blobshadest = os.path.join(__opts__['cachedir'],
                               'hgfs/hash',
                               tgt_env,
                               '{0}.hash.blob_sha1'.format(path))
    lk_fn = os.path.join(__opts__['cachedir'],
                         'hgfs/hash',
                         tgt_env,
                         '{0}.lk'.format(path))
    destdir = os.path.dirname(dest)
    hashdir = os.path.dirname(blobshadest)
    if not os.path.isdir(destdir):
        try:
            os.makedirs(destdir)
        except OSError:
            # Path exists and is a file, remove it and retry
            os.remove(destdir)
            os.makedirs(destdir)
    if not os.path.isdir(hashdir):
        try:
            os.makedirs(hashdir)
        except OSError:
            # Path exists and is a file, remove it and retry
            os.remove(hashdir)
            os.makedirs(hashdir)

    for repo in init():
        if repo['mountpoint'] \
                and not path.startswith(repo['mountpoint'] + os.path.sep):
            continue
        repo_path = path[len(repo['mountpoint']):].lstrip(os.path.sep)
        if repo['root']:
            repo_path = os.path.join(repo['root'], repo_path)

        repo['repo'].open()
        ref = _get_ref(repo, tgt_env)
        if not ref:
            # Branch or tag not found in repo, try the next
            repo['repo'].close()
            continue
        salt.fileserver.wait_lock(lk_fn, dest)
        if os.path.isfile(blobshadest) and os.path.isfile(dest):
            with salt.utils.files.fopen(blobshadest, 'r') as fp_:
                sha = fp_.read()
                if sha == ref[2]:
                    fnd['rel'] = path
                    fnd['path'] = dest
                    repo['repo'].close()
                    return fnd
        try:
            repo['repo'].cat(
                ['path:{0}'.format(repo_path)], rev=ref[2], output=dest
            )
        except hglib.error.CommandError:
            repo['repo'].close()
            continue
        with salt.utils.files.fopen(lk_fn, 'w'):
            pass
        for filename in glob.glob(hashes_glob):
            try:
                os.remove(filename)
            except Exception:
                pass
        with salt.utils.files.fopen(blobshadest, 'w+') as fp_:
            fp_.write(ref[2])
        try:
            os.remove(lk_fn)
        except (OSError, IOError):
            pass
        fnd['rel'] = path
        fnd['path'] = dest
        try:
            # Converting the stat result to a list, the elements of the
            # list correspond to the following stat_result params:
            # 0 => st_mode=33188
            # 1 => st_ino=10227377
            # 2 => st_dev=65026
            # 3 => st_nlink=1
            # 4 => st_uid=1000
            # 5 => st_gid=1000
            # 6 => st_size=1056233
            # 7 => st_atime=1468284229
            # 8 => st_mtime=1456338235
            # 9 => st_ctime=1456338235
            fnd['stat'] = list(os.stat(dest))
        except Exception:
            pass
        repo['repo'].close()
        return fnd
    return fnd
469165
def get_cached_files(url, server_name="", document_root=None):
    
    import glob
    url_info = process_url(url, server_name, document_root, check_security=False)

    # get path to cache directory with basename of file (no extension)
    filedir = os.path.dirname(url_info['requested_file'])
    fileglob = '{0}*{1}'.format(url_info['base_filename'], url_info['ext'])
    return glob.glob(os.path.join(filedir, fileglob))
167900
def lookupfile(filename):
    """"""
    logger.info('Looking up %s'%(filename))

    # First open cache file to see if we already looked up this stuff
    dirname=os.path.dirname(filename)
    basefilename=os.path.basename(filename)

    CACHE_FILE = os.path.join(dirname,'.'+basefilename+'.gpscache')
    cache=loadCacheFile(CACHE_FILE)

    # Get the input file
    positions=parsePositionFile(filename)

    # If load didn't work, read again and lookup
    if not cache:
        logger.info("%s - No cache file found, looking up location"\
                %(basefilename))
        cache=lookupGeoInfo(positions)
        # Save to DB
        json.dump(cache,open(CACHE_FILE,'w'))
    else:
        logger.info("%s - Found cache file for locations"\
                %(basefilename))

    return positions,cache
326286
def fromfilenames(cls, filenames, coltype=LIGOTimeGPS):
		
		cache = cls()
		for filename in filenames:
			cache.extend(cls.fromfile(open(filename), coltype=coltype))
		return cache
1143226
def get_cached_item(self, path):
        
        item_path = '%s/%s/%s' % (
                current_app.static_folder,
                self.cache_folder,
                path.strip('/')
            )

        if os.path.isfile(item_path):
            try:
                return Image.open(item_path)
            except IOError as err:  # pragma: no cover
                LOGGER.warning('Cached file not found on path "%s" with error: %s' % (item_path, str(err)))
                return False
        else:
            return False
1201513
def getrmlsthelper(referencefilepath, update, start):
    
    from subprocess import call
    analysistype = 'rMLST'
    # Folders are named based on the download date e.g 2016-04-26
    # Find all folders (with the trailing / in the glob search) and remove the trailing /
    lastfolder = sorted(glob('{}{}/*/'.format(referencefilepath, analysistype)))[-1].rstrip('/')
    # lastfolder = os.path.join(referencefilepath, analysistype)
    delta, foldersize, d1 = schemedate(lastfolder)
    # Extract the path of the current script from the full path + file name
    homepath = os.path.split(os.path.abspath(__file__))[0]
    # Set the path/name of the folder to contain the new alleles and profile
    newfolder = os.path.join(referencefilepath, analysistype, str(d1))
    # System call
    rmlstupdatecall = 'cd {} && perl {}/rest_auth.pl -a {}/secret.txt'.format(newfolder, homepath, homepath)
    if update:
        if delta.days > 7 or foldersize < 100:
            printtime("Last update of rMLST profile and alleles was {} days ago. Updating".format(str(delta.days)),
                      start)
            # Create the path
            make_path(newfolder)
            # Copy over the access token to be used in the authentication
            shutil.copyfile('{}/access_token'.format(homepath), '{}/access_token'.format(newfolder))
            # Run rest_auth.pl
            call(rmlstupdatecall, shell=True)
            # Get the new alleles into a list, and create the combinedAlleles file
            alleles = glob('{}/*.tfa'.format(newfolder))
            combinealleles(start, newfolder, alleles)
        # If the profile and alleles are up-to-date, set :newfolder to :lastfolder
        else:
            newfolder = lastfolder
        # Ensure that the profile/alleles updated successfully
        # Calculate the size of the folder by adding the sizes of all the files within the folder together
        newfoldersize = sum(os.path.getsize('{}/{}'.format(newfolder, f)) for f in os.listdir(newfolder)
                            if os.path.isfile('{}/{}'.format(newfolder, f)))
        # If the profile/allele failed, remove the folder, and use the most recent update
        if newfoldersize < 100:
            shutil.rmtree(newfolder)
            newfolder = sorted(glob('{}{}/*/'.format(referencefilepath, analysistype)))[-1].rstrip('/')
    # Don't update the profile/alleles if not requested
    else:
        newfolder = lastfolder
    # Return the system call and the folder containing the profile and alleles
    return rmlstupdatecall, newfolder
1056205
function readLastChunk(path, start, end, callback) {
  nodeFs.open(path, "r", function (err, fd) {
    if (err) {
      if (err.code === "EACCES") return callback();
      return callback(err);
    }
    var buffer = new Buffer(4096);
    var length = 0;
    read();
    // Only the first read needs to seek.
    // All subsequent reads will continue from the end of the previous.
    start = null;

    function read() {
      if (buffer.length - length === 0) {
        grow();
      }
      nodeFs.read(fd, buffer, length, buffer.length - length, start, onread);
    }

    function onread(err, bytesRead) {
      if (err) return callback(err);
      length += bytesRead;
      if (bytesRead === 0) {
        return callback(null, buffer.slice(0, buffer.length + end));
      }
      read();
    }

    function grow() {
      var newBuffer = new Buffer(buffer.length * 2);
      buffer.copy(newBuffer);
      buffer = newBuffer;
    }
  });
}
686048
def file_hash(load, fnd):
    '''
    
    '''
    if 'env' in load:
        # "env" is not supported; Use "saltenv".
        load.pop('env')

    if not all(x in load for x in ('path', 'saltenv')):
        return ''
    ret = {'hash_type': __opts__['hash_type']}
    relpath = fnd['rel']
    path = fnd['path']
    hashdest = os.path.join(__opts__['cachedir'],
                            'hgfs/hash',
                            load['saltenv'],
                            '{0}.hash.{1}'.format(relpath,
                                                  __opts__['hash_type']))
    if not os.path.isfile(hashdest):
        ret['hsum'] = salt.utils.hashutils.get_hash(path, __opts__['hash_type'])
        with salt.utils.files.fopen(hashdest, 'w+') as fp_:
            fp_.write(ret['hsum'])
        return ret
    else:
        with salt.utils.files.fopen(hashdest, 'rb') as fp_:
            ret['hsum'] = salt.utils.stringutils.to_unicode(fp_.read())
        return ret
348101
def file_hash(load, fnd):
    '''
    
    '''
    if 'env' in load:
        # "env" is not supported; Use "saltenv".
        load.pop('env')

    if not all(x in load for x in ('path', 'saltenv')):
        return ''
    saltenv = load['saltenv']
    if saltenv == 'base':
        saltenv = 'trunk'
    ret = {}
    relpath = fnd['rel']
    path = fnd['path']

    # If the file doesn't exist, we can't get a hash
    if not path or not os.path.isfile(path):
        return ret

    # Set the hash_type as it is determined by config
    ret['hash_type'] = __opts__['hash_type']

    # Check if the hash is cached
    # Cache file's contents should be "hash:mtime"
    cache_path = os.path.join(__opts__['cachedir'],
                              'svnfs',
                              'hash',
                              saltenv,
                              '{0}.hash.{1}'.format(relpath,
                                                    __opts__['hash_type']))
    # If we have a cache, serve that if the mtime hasn't changed
    if os.path.exists(cache_path):
        with salt.utils.files.fopen(cache_path, 'rb') as fp_:
            hsum, mtime = fp_.read().split(':')
            if os.path.getmtime(path) == mtime:
                # check if mtime changed
                ret['hsum'] = hsum
                return ret

    # if we don't have a cache entry-- lets make one
    ret['hsum'] = salt.utils.hashutils.get_hash(path, __opts__['hash_type'])
    cache_dir = os.path.dirname(cache_path)
    # make cache directory if it doesn't exist
    if not os.path.exists(cache_dir):
        os.makedirs(cache_dir)
    # save the cache object "hash:mtime"
    with salt.utils.files.fopen(cache_path, 'w') as fp_:
        fp_.write('{0}:{1}'.format(ret['hsum'], os.path.getmtime(path)))

    return ret
100304
def cache_local_file(self, path, **kwargs):
        '''
        
        '''
        dest = os.path.join(self.opts['cachedir'], 'localfiles',
                            path.lstrip('/'))
        destdir = os.path.dirname(dest)

        if not os.path.isdir(destdir):
            os.makedirs(destdir)

        shutil.copyfile(path, dest)
        return dest
473029
def _load(self, files_in, files_out, urlpath, meta=True):
        """"""
        import dask
        out = []
        outnames = []
        for file_in, file_out in zip(files_in, files_out):
            cache_path = file_out.path
            outnames.append(cache_path)

            # If `_munge_path` did not find a match we want to avoid
            # writing to the urlpath.
            if cache_path == urlpath:
                continue

            if not os.path.isfile(cache_path):
                logger.debug("Caching file: {}".format(file_in.path))
                logger.debug("Original path: {}".format(urlpath))
                logger.debug("Cached at: {}".format(cache_path))
                if meta:
                    self._log_metadata(urlpath, file_in.path, cache_path)
                ddown = dask.delayed(_download)
                out.append(ddown(file_in, file_out, self.blocksize,
                                 self.output))
        dask.compute(*out)
        return outnames
792455
function File(from, to) {
	var cached, dirname, external, local, source, resolved;

	resolved = _resolve(from, to);

	if (cached = cache[resolved]) {
		return cached;
	}

	if (!fs.existsSync(resolved)) {
		throw new Error('File "' + resolved + '" not found');
	}

	this.path = resolved;

	dirname = path.dirname(resolved);
	local = this.local = {};
	external = this.external = [];
	source = this.source = fs.readFileSync(resolved);

	each(detective(source), function(to) {
		if (~to.indexOf('/')) {
			local[to] = _resolve(dirname, to)
		}
		else {
			external.push(to);
		}
	});

	cache[resolved] = this;
}
1346297
def _get_cached_file_name(bucket_name, saltenv, path):
    '''
    
    '''

    file_path = os.path.join(_get_cache_dir(), saltenv, bucket_name, path)

    # make sure bucket and saltenv directories exist
    if not os.path.exists(os.path.dirname(file_path)):
        os.makedirs(os.path.dirname(file_path))

    return file_path
654747
def CacheFileObject(self, path_spec, file_object):
    
    self._file_object_cache.CacheObject(path_spec.comparable, file_object)
15704
def Readdir(self, path, fh=None):
    
    if self.DataRefreshRequired(path):
      self._RunAndWaitForVFSFileUpdate(path)

    return super(GRRFuse, self).Readdir(path, fh=None)
1313088
def load(self, f, precache=None):
        
        precache = precache or self.get_opt('cache') or False
        nodes = None
        if isinstance(f, file_):
            path = os.path.abspath(f.name)

        else:
            path = os.path.abspath(f)
            f = open(f)

        cache_path = os.path.splitext(path)[0] + '.ccss'

        if precache and os.path.exists(cache_path):
            ptime = os.path.getmtime(cache_path)
            ttime = os.path.getmtime(path)
            if ptime > ttime:
                dump = open(cache_path, 'rb').read()
                nodes = pickle.loads(dump)

        if not nodes:
            src = f.read()
            nodes = self.scan(src.strip())

        if precache:
            f = open(cache_path, 'wb')
            pickle.dump(nodes, f, protocol=1)

        self.parse(nodes)
        return ''.join(map(str, nodes))
1022320
def get(**file_finder_args)
      @cache[file_finder_args] || @mutex.synchronize do
        @cache[file_finder_args] ||= begin
          args = file_finder_args.dup
          args[:exclude] = @defaults[:exclude] + (args[:exclude] || [])
          args[:exclude].uniq!
          CachingFileFinder.new(**args)
        end
      end
    end