コード例 #1
0
    def update(self, ext_id: str) -> bool:
        """
        :raises ExtensionDownloaderError:
        :rtype: boolean
        :returns: False if already up-to-date, True if was updated
        """
        commit = self.get_new_version(ext_id)
        ext = self._find_extension(ext_id)

        logger.info('Updating extension "%s" from commit %s to %s', ext_id,
                    ext['last_commit'][:8], commit['last_commit'][:8])

        if self.ext_runner.is_running(ext_id):
            self.ext_runner.stop(ext_id)

        ext_path = os.path.join(EXTENSIONS_DIR, ext_id)

        gh_ext = GithubExtension(ext['url'])
        filename = download_zip(gh_ext.get_download_url(commit['last_commit']))
        unzip(filename, ext_path)

        ext['updated_at'] = datetime.now().isoformat()
        ext['last_commit'] = commit['last_commit']
        ext['last_commit_time'] = commit['last_commit_time']
        self.ext_db.put(ext_id, ext)
        self.ext_db.commit()

        self.ext_runner.run(ext_id)

        return True
コード例 #2
0
    def download(self, url: str) -> str:
        """
        1. check if ext already exists
        2. get last commit info
        3. download & unzip
        4. add it to the db

        :rtype: str
        :returns: Extension ID
        :raises AlreadyDownloadedError:
        """
        gh_ext = GithubExtension(url)
        gh_ext.validate_url()

        # 1. check if ext already exists
        ext_id = gh_ext.get_ext_id()
        ext_path = os.path.join(EXTENSIONS_DIR, ext_id)
        # allow user to re-download an extension if it's not running
        # most likely it has some problems with manifest file if it's not running
        if os.path.exists(ext_path) and self.ext_runner.is_running(ext_id):
            raise ExtensionDownloaderError(
                'Extension with URL "%s" is already added' % url,
                ErrorName.ExtensionAlreadyAdded)

        # 2. get last commit info
        commit = gh_ext.find_compatible_version()

        # 3. download & unzip
        filename = download_zip(gh_ext.get_download_url(commit['sha']))
        unzip(filename, ext_path)

        # 4. add to the db
        self.ext_db.put(
            ext_id, {
                'id': ext_id,
                'url': url,
                'updated_at': datetime.now().isoformat(),
                'last_commit': commit['sha'],
                'last_commit_time': commit['time'].isoformat()
            })
        self.ext_db.commit()

        return ext_id