def __init__(self, version, download_path): # Making sure that the version contains # valid minor and patch elements counter = Counter(version) if counter['.'] == 0: version += '.0.0' elif counter['.'] == 1: version += '.0' self.expected_version = VersionInfo.parse(version=version) LOG.debug('Expected %s version: %s', self.binary, self.expected_version) self.download_path = Path(download_path) downloaded = False # First try to get the path from the system self._command = self._get_command_path(self.binary) if self._command is None: # First try to download the binary downloaded_file = self._download() self._command = self.process_download(path=downloaded_file) downloaded = True # No luck. Binary is not on the system and it also can't # be downloaded. if self._command is None: raise CommandNotFoundError(f'Not able to find or download ' f'{self.binary} version ' f'{self.expected_version}') # Checking if we have the right version result = run(cmd=self.get_version_command()) self._command_version = self.parse_version(version=result) if not self._compare(expected=self.expected_version, actual=self._command_version): if downloaded: # If even after download, the version doesn't match, # we are done trying. raise CommandNotFoundError(f'Downloaded version of ' f'{self.binary} did not match ' f'the expected version ' f'{self.expected_version}') # Version doesn't match and we didn't try to download # so far. Downloading. downloaded_file = self._download() self._command = self.process_download(path=downloaded_file) if self._command is None: raise CommandNotFoundError(f'Not able to download ' f'{self.binary}') # Checking if we have the right version after download result = run(cmd=self.get_version_command()) self._command_version = self.parse_version(version=result) if not self._compare(expected=self.expected_version, actual=self._command_version): raise CommandNotFoundError(f'Downloaded version of ' f'{self.binary} did not match ' f'the expected version ' f'{self.expected_version}')
def run(self, *args): """ Runs binary with arbitrary options. """ cmd = [self.command, *args] return run(cmd)