Put a file in the cache.

        Args:
          filepath: Path to file on disk.
          buildroot: Path to buildroot
          buildrule: The rule that generated this file.
          metahash: hash object
519244
def putfile(self, filepath, buildroot, metahash):
        
        def gen_obj_path(filename):
            filehash = util.hash_file(filepath).hexdigest()
            return filehash, os.path.join(self.obj_cachedir, filehash[0:2],
                                          filehash[2:4], filehash)

        filepath_relative = filepath.split(buildroot)[1][1:]  # Strip leading /
        # Path for the metahashed reference:
        incachepath = self._genpath(filepath_relative, metahash)

        filehash, obj_path = gen_obj_path(filepath)
        if not os.path.exists(obj_path):
            obj_dir = os.path.dirname(obj_path)
            if not os.path.exists(obj_dir):
                os.makedirs(obj_dir)
            log.debug('Adding to obj cache: %s -> %s', filepath, obj_path)
            os.link(filepath, obj_path)

        if os.path.exists(incachepath):
            existingfile_hash = util.hash_file(incachepath).hexdigest()
            if filehash != existingfile_hash:
                log.warn('File found in mh cache, but checksum differs. '
                         'Replacing with this new version. (File: %s)',
                         filepath)
                log.warn('Possible reasons for this:')
                log.warn(' 1. This build is not hermetic, and something '
                         'differs about the build environment compared to the '
                         'previous build.')
                log.warn(' 2. This file has a timestamp or other build-time '
                         'related data encoded into it, which will always '
                         'cause the checksum to differ when built.')
                log.warn(' 3. Everything is terrible and nothing works.')
                os.unlink(incachepath)

        if not os.path.exists(incachepath):
            log.debug('Adding to mh cache: %s -> %s', filepath, incachepath)
            if not os.path.exists(os.path.dirname(incachepath)):
                os.makedirs(os.path.dirname(incachepath))
            os.link(obj_path, incachepath)
1369804
def insert(self, path, simfile):
        
        if self.state is not None:
            simfile.set_state(self.state)
        mountpoint, chunks = self.get_mountpoint(path)

        if mountpoint is None:
            self._files[self._join_chunks(chunks)] = simfile
            return True
        else:
            return mountpoint.insert(chunks, simfile)
654747
def CacheFileObject(self, path_spec, file_object):
    
    self._file_object_cache.CacheObject(path_spec.comparable, file_object)
1229604
def copy_file(self, filepath):
        
        copy_file = False
        try:
            copy_file = self.data[filepath]['copy']
        except KeyError:
            return False
        return copy_file
60715
def _genpath(self, filename, mhash):
        
        mhash = mhash.hexdigest()
        return os.path.join(self.mh_cachedir, mhash[0:2], mhash[2:4],
                            mhash, filename)
762772
public function setFilePath($filepath)
    {
        if (!isset($filepath) || empty($filepath)) {
            throw new PageCacheException(__CLASS__ . ' file path not set or empty');
        }

        $this->filepath = $filepath;
    }
766738
protected function cacheFile($path, $cachefsPath)
    {
        if (array_key_exists($path, $this->cachedFiles)) {
            return;
        }

        if (file_exists($path)) {
            $contents = file_get_contents($path);
            file_put_contents($cachefsPath, $contents);

            $this->cachedFiles[$path] = $cachefsPath;
        }
    }
1130950
def generate_build_file(startpath, outfilename=DEFAULT_BUILDFILE):
    
    buildfilepath = os.path.join(startpath, outfilename)
    if os.path.exists(buildfilepath):
        raise BuildException("Build file %s already exists." % buildfilepath)

    contents = generate_contents(startpath, outfilename)

    with open(buildfilepath, 'w') as outfile:
        yaml.dump(contents, outfile, default_flow_style=False)
    return buildfilepath
252134
def _filehash(filepath, blocksize=4096):
    
    sha = hashlib.sha256()
    with open(filepath, 'rb') as fp:
        while 1:
            data = fp.read(blocksize)
            if data:
                sha.update(data)
            else:
                break
    return sha
720019
def fs_put(self, rpath, data):
        
        try:
            self.begin()

            # Add the file to the fs
            self.file_put_contents(rpath, data)

            # Add to the manifest
            manifest = self.read_local_manifest()
            manifest['files'][rpath] = self.get_single_file_info(rpath)
            self.write_local_manifest(manifest)

            self.commit()
        except:
            self.rollback(); raise
253365
def AddFile(self, filepath):
    
    if filepath not in self._files:
      self._files.add(filepath)
      return True
    return False
510966
function getFileHash(filePath, callback) {
  var stream = fs.ReadStream(filePath);
  var md5sum = crypto.createHash('md5');

  stream.on('data', function(data) {
    md5sum.update(data);
  });

  stream.on('end', function() {
    callback(md5sum.digest('hex'));
  });
}
843438
def reparse(self, filepath):
        """"""
        #The easiest way to do this is to touch the file and then call
        #the regular parse method so that the cache becomes invalidated.
        self.tramp.touch(filepath)
        self.parse(filepath)
898844
public void registerCachedFile(String filePath, String name) throws IOException {
		if (!this.cacheFile.containsKey(name)) {
			try {
				URI u = new URI(filePath);
				if (!u.getPath().startsWith("/")) {
					u = new URI(new File(filePath).getAbsolutePath());
				}
				FileSystem fs = FileSystem.get(u);
				if (fs.exists(new Path(u.getPath()))) {
					this.cacheFile.put(name, u.toString());
				} else {
					throw new RuntimeException("File " + u.toString() + " doesn't exist.");
				}
			} catch (URISyntaxException ex) {
				throw new RuntimeException("Invalid path: " + filePath, ex);
			}
		} else {
			throw new RuntimeException("cache file " + name + "already exists!");
		}
	}
53634
def cache_file(self, local_path, ip, path):
        
        destination = self.__destination(ip, path)
        atomicish_move(local_path, destination)
926465
def add_fast(self, filepath, hashfn=None, force=False):
        
        if hashfn is None:
            hashfn = fast_hashes
        self.add(filepath, hashfn, force, shortcircuit=True)
287659
def save_file(self, srcfile):
        
        filehash = digest_file(srcfile)
        if not os.path.exists(self.object_path(filehash)):
            # Copy the file to a temporary location first, then move, to make sure we don't end up with
            # truncated contents if the build gets interrupted.
            tmppath = self.temporary_object_path(filehash)
            copyfile(srcfile, tmppath)
            self._move_to_store(tmppath, filehash)

        return filehash