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
def test_validate_url(self, gh_ext): assert gh_ext.validate_url() is None with pytest.raises(GithubExtensionError): GithubExtension('http://github.com/Ulauncher/ulauncher-timer').validate_url() with pytest.raises(GithubExtensionError): GithubExtension('https://github.com/Ulauncher/ulauncher-timer/').validate_url()
def test_read_json__HTTPError__raises(self, gh_ext: GithubExtension, mocker): urlopen = mocker.patch('ulauncher.api.server.GithubExtension.urlopen') urlopen.side_effect = HTTPError('https://url', 404, 'urlopen error', {}, None) with pytest.raises(GithubExtensionError) as e: gh_ext._read_json('master', 'manifest.json') assert e.type == GithubExtensionError assert e.value.error_name == 'VersionsJsonNotFound'
def get_new_version(self, ext_id: str) -> LastCommit: """ Returns dict with commit info about a new version or raises ExtensionIsUpToDateError """ ext = self._find_extension(ext_id) url = ext['url'] gh_ext = GithubExtension(url) commit = gh_ext.find_compatible_version() need_update = ext['last_commit'] != commit['sha'] if not need_update: raise ExtensionIsUpToDateError('Extension is up to date') return { 'last_commit': commit['sha'], 'last_commit_time': commit['time'].isoformat() }
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
def gh_ext(self): return GithubExtension('https://github.com/Ulauncher/ulauncher-timer')
def test_read_json(self, gh_ext: GithubExtension, mocker): urlopen = mocker.patch('ulauncher.api.server.GithubExtension.urlopen') urlopen.return_value.read.return_value = dumps( manifest_example).encode('utf-8') actual = gh_ext._read_json('master', 'manifest.json') assert actual == manifest_example