def get_file_from_fuse_path(repository, fuse_path): """Get file in the repository for path. Path is a fuse-path pointing to the file. """ basename = path.basename(fuse_path) name_hash = hashutils.hash_string(basename)[:2] name_location = path.join(repository, '.names', name_hash) names = os.listdir(name_location) real_name = None for name in names: if name[40:] == basename: real_name = name break if real_name is not None: # TODO: We need to abstract query parsing here # 0th is empty because we start with / tag = fuse_path.split("/")[1] return path.join(repository, tag, real_name[:2], real_name) else: raise IOError("File not found")
def tag_file(repository, file_path, tag): """Tag a file. Args: repository: Path to the repository. Should be the full path pointing to the repository. file_path: File path. Should be the full path pointing to the file we want to tag. tag: Tag. """ file_hash = hashutils.hash_file_sha1(file_path) # first, create the tag/hash_prefix/ directory level1_prefix = file_hash[:2] location = path.join(repository, tag, level1_prefix) if not path.exists(location): os.makedirs(location) # link the file there, but first prefix its name with the hash of # the file it points to. basename = path.basename(file_path) hashed_name = file_hash + basename # TODO: add check to see if file with this hash is already present # and ask user if they want to replace it (or rename). os.link(file_path, path.join(location, hashed_name)) # add the file to the names database name_hash = hashutils.hash_string(basename)[:2] name_location = path.join(repository, '.names', name_hash) if not path.exists(name_location): os.makedirs(name_location) try: os.link(file_path, path.join(name_location, hashed_name)) except os.FileExistsError: pass