def download_to_dir(self, workspace_id, name, directory, progress_cb=None):
     model_tar = os.path.join(directory, rand_str(10) + '.tar')
     self.download_to_tar(workspace_id, name, model_tar, progress_cb)
     model_dir = os.path.join(directory, name)
     with tarfile.open(model_tar) as archive:
         archive.extractall(model_dir)
     silent_remove(model_tar)
     return model_dir
Ejemplo n.º 2
0
    def download_or_get_repo(self):
        git_url = self.app_info["githubUrl"]
        version = self.app_info.get("version", "master")

        already_downloaded = False
        path_cache = None
        if version != "master":
            path_cache = os.path.join(
                constants.APPS_STORAGE_DIR(),
                *Path(git_url.replace(".git", "")).parts[1:], version)
            already_downloaded = sly.fs.dir_exists(path_cache)

        if already_downloaded is False:
            self.logger.info("Git repo will be downloaded")

            api = Api(self.info['server_address'], self.info['api_token'])
            tar_path = os.path.join(self.dir_task_src, 'repo.tar.gz')
            api.app.download_git_archive(self.app_info["moduleId"],
                                         self.app_info["id"],
                                         version,
                                         tar_path,
                                         log_progress=True,
                                         ext_logger=self.logger)
            with tarfile.open(tar_path) as archive:
                archive.extractall(self.dir_task_src)

            subdirs = get_subdirs(self.dir_task_src)
            if len(subdirs) != 1:
                raise RuntimeError(
                    "Repo is downloaded and extracted, but resulting directory not found"
                )
            extracted_path = os.path.join(self.dir_task_src, subdirs[0])

            for filename in os.listdir(extracted_path):
                shutil.move(os.path.join(extracted_path, filename),
                            os.path.join(self.dir_task_src, filename))
            remove_dir(extracted_path)
            silent_remove(tar_path)

            #git.download(git_url, self.dir_task_src, github_token, version)
            if path_cache is not None:
                shutil.copytree(self.dir_task_src, path_cache)
        else:
            self.logger.info("Git repo already exists")
            shutil.copytree(path_cache, self.dir_task_src)
Ejemplo n.º 3
0
def download(github_url,
             dest_dir,
             github_token=None,
             version="master",
             log_progress=True):
    tar_path = os.path.join(dest_dir, 'repo.tar.gz')
    download_tar(github_url, tar_path, github_token, version, log_progress)

    with tarfile.open(tar_path) as archive:
        archive.extractall(dest_dir)

    subdirs = get_subdirs(dest_dir)
    if len(subdirs) != 1:
        raise RuntimeError(
            "Repo is downloaded and extracted, but resulting directory not found"
        )
    extracted_path = os.path.join(dest_dir, subdirs[0])

    for filename in os.listdir(extracted_path):
        shutil.move(os.path.join(extracted_path, filename),
                    os.path.join(dest_dir, filename))
    remove_dir(extracted_path)
    silent_remove(tar_path)
Ejemplo n.º 4
0
def image_ext_to_png(im_path):
    if get_file_ext(im_path) != '.png':
        im = Image.open(im_path).convert('RGB')
        im.save(im_path[:-1 * len(get_file_ext(im_path))] + '.png')
        silent_remove(im_path)