예제 #1
0
 def write(self):
     self.sort()
     # write to a temp file
     fd, tmpname = tempfile.mkstemp()
     with os.fdopen(fd, "w") as f:
         for (mode, sha, name) in self.cache:
             f.write("{}\t{}\t{}\n".format(mode, sha, name))
     # hash the file
     self.hash = compute_hash(tmpname)
     # that is the name of the file
     objfile = os.path.join(self.path, self.hash)
     os.chmod(tmpname, int("755", 8))
     safe_rename(tmpname, objfile)
     if self.add_to_git:
         call_subprocess([GIT_EXE_PATH, "add", self.hash], cwd=self.path, verbose=False)
     return self.hash
예제 #2
0
def setup_path_for_hashes(role: str, name: str, workspace: Workspace,
                          local_path: str):
    """When creating the resource, make sure we have a path for the hashes.
    Subclasses of LocalFilesResource will need to call this in their factory
    functions."""
    workspace_path = workspace.get_workspace_local_path_if_any()
    if isinstance(workspace, git_backend.Workspace):
        assert workspace_path is not None
        hash_path = join(workspace_path,
                         _relative_rsrc_dir_for_git_workspace(role, name))
        try:
            os.makedirs(hash_path)
            with open(os.path.join(hash_path, "dummy.txt"), "w") as f:
                f.write("Placeholder to ensure directory is added to git\n")
            call_subprocess(
                [
                    GIT_EXE_PATH,
                    "add",
                    join(_relative_rsrc_dir_for_git_workspace(role, name),
                         "dummy.txt"),
                ],
                cwd=workspace_path,
            )
            call_subprocess(
                [GIT_EXE_PATH, "commit", "-m",
                 "Adding resource %s" % name],
                cwd=workspace_path)
        except OSError as exc:
            if exc.errno == EEXIST and os.path.isdir(hash_path):
                pass
            else:
                raise
    else:
        non_git_hashes = join(local_path, ".hashes")
        if not exists(non_git_hashes):
            os.mkdir(non_git_hashes)