diff --git a/tools/ade b/tools/ade index 629f952060cef98a2ad464995a74f60090c2d309..38c63a78971fbfe2ac26f2b935707a5e9b6eebac 100755 --- a/tools/ade +++ b/tools/ade @@ -93,20 +93,16 @@ class TargetTriplet: } def __init__(self, string): - self.triplet = string - for arch, triplet in self.SUPPORTED.items(): - if string == triplet: - self.arch = arch + for items in self.SUPPORTED.items(): + if string in items: + self.arch, self.triplet = items return - raise NotSupportedError() - - def get_architecture(self): - return self.arch + raise NotSupportedError class SysrootVersion: - def __init__(self, string=None, url=None, path=None): + def __init__(self, string, url=None): self.distro = '' self.release = '' self.arch = None @@ -114,10 +110,7 @@ class SysrootVersion: self.build = 0 self.author = '' self.url = url - if string: - self.parse_string(string) - if path: - self.parse_path(path) + self.parse_string(string) if url and not is_valid_url(url): raise ValueError("'url'") @@ -132,19 +125,6 @@ class SysrootVersion: self.build = int(m.groups()[3], 10) self.author = m.groups()[4] - def parse_path(self, path): - try: - with open(os.path.join(path, 'etc', 'image_version')) as f: - self.parse_string(f.read()) - with open(os.path.join(path, 'usr', 'lib', 'pkg-config.multiarch')) as f: - self.arch = TargetTriplet(f.read().strip()).get_architecture() - except FileNotFoundError as e: - if not os.path.exists(path) or not os.listdir(path): - raise NotInstalledError - raise e - if not path.endswith("/{0}/{1}/{2}".format(self.distro, self.release, self.arch)): - raise ValueError("'version'") - def get_tag(self): return "{0}-{1}-{2}_{3}.{4}".format(self.distro, self.release, @@ -202,6 +182,27 @@ class SysrootVersion: return False +class Sysroot: + + def __init__(self, path): + self.path = path + self.parse_path(path) + + def parse_path(self, path): + try: + with open(os.path.join(path, 'etc', 'image_version')) as f: + self.version = SysrootVersion(f.read()) + with open(os.path.join(path, 'usr', 'lib', 'pkg-config.multiarch')) as f: + self.version.arch = TargetTriplet(f.read().strip()).arch + except FileNotFoundError as e: + if not os.path.exists(path) or not os.listdir(path): + raise NotInstalledError + raise e + if not path.endswith("/{0}/{1}/{2}".format(self.version.distro, self.version.release, self.version.arch)): + raise ValueError("'version'") + + + class SysrootArchive: def __init__(self, filename): @@ -242,7 +243,7 @@ class SysrootArchive: try: with f.extractfile(arch_file) as reader: - self.version.arch = TargetTriplet(reader.read().decode().strip()).get_architecture() + self.version.arch = TargetTriplet(reader.read().decode().strip()).arch except NotSupportedError: raise InvalidSysrootArchiveError('Architecture is not supported') except: @@ -399,11 +400,11 @@ class Ade: except UnicodeDecodeError: self.die("Invalid sysroot version file") - def get_installed_version(self): + def get_installed_sysroot(self): self.info("* Checking currently installed version") try: p = os.path.join(self.path, self.distro, self.release, self.arch) - return SysrootVersion(path=p) + return Sysroot(p) except NotInstalledError: return None except (FileNotFoundError, ValueError): @@ -485,30 +486,29 @@ class Ade: os.rename(os.path.join(bindir, filename), os.path.join(path, filename)) os.rmdir(bindir) - installed_version = self.get_installed_version() - if not installed_version.is_compatible(archive.version): + installed = self.get_installed_sysroot() + if not installed.version.is_compatible(archive.version): self.die("Mismatch between installed sysroot ({0}) and expected one".format(archive.version)) # Don't fail on this check as it's broken for current images - if installed_version != archive.version: + if installed.version != archive.version: logging.warning("Mismatch between installed version and expected one") - return installed_version + return installed - def uninstall_sysroot(self, version): - path = os.path.join(self.path, version.distro, version.release, version.arch) - self.info("* Removing directory '{0}'".format(path)) - shutil.rmtree(path) + def uninstall_sysroot(self, sysroot): + self.info("* Removing directory '{0}'".format(sysroot.path)) + shutil.rmtree(sysroot.path) def do_sysroot_list(self): self.validate_install_path() sysroots = glob.glob(os.path.join(self.path, '*', '[0-9][0-9].[0-9][0-9]', '*')) versions = [] - for sysroot in sysroots: + for path in sysroots: try: - version = SysrootVersion(path=sysroot) - versions.append(version) + sysroot = Sysroot(path) + versions.append(sysroot.version) except Exception as e: pass if not versions: @@ -524,11 +524,11 @@ class Ade: def do_sysroot_installed(self): self.validate_install_path() self.validate_sysroot_id() - version = self.get_installed_version() - if version: - self.info("* Retrieved current version: {0}{1}{2}".format(Colors.WARNING, version, Colors.ENDC)) + sysroot = self.get_installed_sysroot() + if sysroot: + self.info("* Retrieved current version: {0}{1}{2}".format(Colors.WARNING, sysroot.version, Colors.ENDC)) if self.format == 'parseable': - print('InstalledVersion:' + version.get_tag()) + print('InstalledVersion:' + sysroot.version.get_tag()) else: self.info("* Sysroot {0}{1} {2} ({3}){4} is not currently installed" .format(Colors.OKBLUE, self.distro, self.release, self.arch, Colors.ENDC)) @@ -592,34 +592,34 @@ class Ade: self.arch = new_version.arch self.validate_install_path() - installed_version = self.get_installed_version() + installed = self.get_installed_sysroot() - if not installed_version: + if not installed: self.info("* Installing version {0}{1}{2}".format(Colors.OKGREEN, new_version, Colors.ENDC)) elif self.force: prefix = '' - if new_version == installed_version: + if new_version == installed.version: prefix = 're' self.info("* Forcing {0}installation of version {1}{2}{3}".format(prefix, Colors.OKGREEN, new_version, Colors.ENDC)) else: color = Colors.WARNING - if new_version == installed_version: + if new_version == installed.version: color = Colors.OKGREEN - self.die("Sysroot {0}{1}{2} is already installed".format(color, installed_version, Colors.ENDC)) + self.die("Sysroot {0}{1}{2} is already installed".format(color, installed.version, Colors.ENDC)) try: if not self.file: archive = self.download_sysroot(new_version) - if installed_version: - self.uninstall_sysroot(installed_version) - installed_version = self.install_sysroot(archive) + if installed: + self.uninstall_sysroot(installed) + installed = self.install_sysroot(archive) finally: if archive and not self.file: os.remove(archive.filename) self.info("* Installation has been completed") if self.format == 'parseable': - print('InstalledVersion:' + installed_version.get_tag()) + print('InstalledVersion:' + installed.version.get_tag()) def do_sysroot_update(self): self.validate_sysroot_id() @@ -629,55 +629,55 @@ class Ade: new_version = self.get_latest_version() self.validate_install_path() - installed_version = self.get_installed_version() + installed = self.get_installed_sysroot() - if not installed_version: + if not installed: self.die("No sysroot currently installed for {0}{1} {2} ({3}){4}" \ .format(Colors.OKBLUE, self.distro, self.release, self.arch, Colors.ENDC)) - elif installed_version < new_version: + elif installed.version < new_version: self.info("* Upgrading from {0}{1}{2} to version {3}{4}{5}" \ - .format(Colors.WARNING, installed_version, Colors.ENDC, + .format(Colors.WARNING, installed.version, Colors.ENDC, Colors.OKGREEN, new_version, Colors.ENDC)) elif self.force: self.info("* Forcing installation of version {0}{1}{2}" \ .format(Colors.OKBLUE, new_version, Colors.ENDC)) - elif installed_version == new_version: + elif installed.version == new_version: self.info("* Installed version {0}{1}{2} is already up-to-date" \ .format(Colors.OKGREEN, new_version, Colors.ENDC)) if self.format == 'parseable': - print('InstalledVersion:' + installed_version.get_tag()) + print('InstalledVersion:' + installed.version.get_tag()) return - elif installed_version > new_version: + elif installed.version > new_version: self.die("Installed version {0}{1}{2} is more recent than {3}{4}{5}" \ - .format(Colors.WARNING, installed_version, Colors.ENDC, + .format(Colors.WARNING, installed.version, Colors.ENDC, Colors.OKGREEN, new_version, Colors.ENDC)) try: archive = self.download_sysroot(new_version) - self.uninstall_sysroot(installed_version) - installed_version = self.install_sysroot(archive) + self.uninstall_sysroot(installed) + installed = self.install_sysroot(archive) finally: if archive: os.remove(archive.filename) self.info("* Update has been completed") if self.format == 'parseable': - print('InstalledVersion:' + installed_version.get_tag()) + print('InstalledVersion:' + installed.version.get_tag()) def do_sysroot_uninstall(self): self.validate_sysroot_id() self.validate_install_path() - installed_version = self.get_installed_version() + installed = self.get_installed_sysroot() - if not installed_version: + if not installed: self.die("No sysroot currently installed for {0}{1} {2} ({3}){4}" \ .format(Colors.OKBLUE, self.distro, self.release, self.arch, Colors.ENDC)) return else: self.info("* Uninstalling sysroot {0}{1}{2}" \ - .format(Colors.WARNING, installed_version, Colors.ENDC)) + .format(Colors.WARNING, installed.version, Colors.ENDC)) - self.uninstall_sysroot(installed_version) + self.uninstall_sysroot(installed) self.info("* Sysroot for {0}{1} {2} ({3}){4} has been uninstalled" \ .format(Colors.OKBLUE, self.distro, self.release, self.arch, Colors.ENDC))