diff --git a/doc/man/ade.1 b/doc/man/ade.1 index f1523fce27d4175df02841ca592ad4766535658e..be0bcc3aecee3b2117a584cfd14228e053e62752 100644 --- a/doc/man/ade.1 +++ b/doc/man/ade.1 @@ -21,6 +21,20 @@ Tools to help developpers of Apertis application bundles\&. .sp See the individual command man page for more information about its usage\&. .sp +.SH OPTIONS +.sp +.PP +\fI\-\-format\fR +.RS 4 +Sets the output format of the tool\&. +.sp +.RS 4 +\fIfriendly\fR Human readable\&. +.sp +\fIparseable\fR Easier to parse for automated tests and tool integration\&. +.RE +.sp +.RE .SH COMMANDS .sp .PP diff --git a/tools/ade b/tools/ade index a5b467cf746b0a87bf7a92f10a8f36c30390671c..f69880e91aadfc42c63e08d26fddada20145f21d 100755 --- a/tools/ade +++ b/tools/ade @@ -168,6 +168,13 @@ class SysrootVersion: 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, + self.arch, + self.date, + self.build) + def __str__(self): return "{0} {1} - {2}.{3} ({4})".format(self.distro, self.release, @@ -379,10 +386,10 @@ class Ade: self.die("Malformed {0} property in sysroot version file".format(e)) def get_latest_version(self): - print("* Checking latest version available for {0}{1} {2} ({3}){4}" \ + self.info("* Checking latest version available for {0}{1} {2} ({3}){4}" \ .format(Colors.OKBLUE, self.distro, self.release, self.arch, Colors.ENDC)) try: - print("* Downloading version file from: {0}".format(self.url)) + self.info("* Downloading version file from: {0}".format(self.url)) resp = urlopen(self.url) version = self.parse_version_file(resp.read().decode('utf-8')) @@ -392,7 +399,7 @@ class Ade: if not version.arch: version.arch = self.arch - print("* Retrieved latest version: {0}{1}{2}" \ + self.info("* Retrieved latest version: {0}{1}{2}" \ .format(Colors.OKGREEN, version, Colors.ENDC)) return version @@ -402,7 +409,7 @@ class Ade: self.die("Invalid sysroot version file") def get_installed_version(self): - print("* Checking currently installed version") + self.info("* Checking currently installed version") try: p = os.path.join(self.path, self.distro, self.release, self.arch) return SysrootVersion(path=p) @@ -419,8 +426,11 @@ class Ade: self.die("Couldn't create download directory: {0}".format(e)) try: - print("* Downloading sysroot from: {0}".format(version.url)) - _, headers = urlretrieve(version.url, filename=filename, reporthook=print_progress) + self.info("* Downloading sysroot from: {0}".format(version.url)) + hook = None + if self.format == 'friendly': + hook = print_progress + _, headers = urlretrieve(version.url, filename=filename, reporthook=hook) except URLError as e: try: os.remove(filename) @@ -429,7 +439,7 @@ class Ade: self.die("Error while downloading sysroot: {0}".format(e.reason)) try: - print("* Verifying downloaded sysroot version") + self.info("* Verifying downloaded sysroot version") f = SysrootArchive(filename) except InvalidSysrootArchiveError as e: os.remove(filename) @@ -446,12 +456,12 @@ class Ade: return f def verify_sysroot(self, path): - print("* Verifying sysroot archive '{0}'".format(self.file)) + self.info("* Verifying sysroot archive '{0}'".format(self.file)) try: archive = SysrootArchive(self.file) new_version = archive.version - print("* Sysroot archive has version: {0}{1}{2}".format(Colors.OKGREEN, new_version, Colors.ENDC)) + self.info("* Sysroot archive has version: {0}{1}{2}".format(Colors.OKGREEN, new_version, Colors.ENDC)) except Exception as e: self.die("Invalid sysroot archive: {0}".format(e)) @@ -459,7 +469,7 @@ class Ade: def install_sysroot(self, archive): try: - print("* Creating install directory") + self.info("* Creating install directory") path = os.path.join(self.path, self.distro, self.release, self.arch) if not os.path.isdir(path): # XXX Do earlier in case permission is denied or allow restart @@ -468,7 +478,7 @@ class Ade: self.die("Couldn't create install directory: {0}".format(e)) try: - print("* Extracting sysroot to '{0}'".format(path)) + self.info("* Extracting sysroot to '{0}'".format(path)) archive.extract(path) except Exception as e: shutil.rmtree(path) @@ -477,7 +487,7 @@ class Ade: bindir = os.path.join(path, 'binary') if os.path.isdir(bindir): - print("* Moving sysroot files out of the 'binary' directory") + self.info("* Moving sysroot files out of the 'binary' directory") for filename in os.listdir(bindir): os.rename(os.path.join(bindir, filename), os.path.join(path, filename)) os.rmdir(bindir) @@ -494,7 +504,7 @@ class Ade: def uninstall_sysroot(self, version): path = os.path.join(self.path, version.distro, version.release, version.arch) - print("* Removing directory '{0}'".format(path)) + self.info("* Removing directory '{0}'".format(path)) shutil.rmtree(path) def do_sysroot_list(self): @@ -509,16 +519,21 @@ class Ade: except Exception as e: pass if not versions: - print("{0}No sysroot installed in directory {1}.{2}".format(Colors.WARNING, self.path, Colors.ENDC)) + self.info("{0}No sysroot installed in directory {1}.{2}".format(Colors.WARNING, self.path, Colors.ENDC)) else: versions.sort() for version in versions: - print("* {0}".format(version)) + self.info("* {0}".format(version)) + if self.format == 'parseable': + l = [version.get_tag() for version in versions] + print('InstalledSysroots:' + ';'.join(l)) def do_sysroot_latest(self): self.validate_sysroot_id() self.validate_url() - self.get_latest_version() + version = self.get_latest_version() + if self.format == 'parseable': + print('LatestVersion:' + version.get_tag()) def do_sysroot_download(self): self.validate_sysroot_id() @@ -539,11 +554,16 @@ class Ade: if not os.path.exists(filename): break - print("* Moving downloaded sysroot to '{0}'".format(filename)) + self.info("* Moving downloaded sysroot to '{0}'".format(filename)) shutil.move(f.filename, filename) + if self.format == 'parseable': + print('DownloadedArchive:' + filename) + def do_sysroot_verify(self): - self.verify_sysroot(self.file) + archive = self.verify_sysroot(self.file) + if self.format == 'parseable': + print('VerifiedVersion:' + archive.version.get_tag()) def do_sysroot_install(self): archive = None @@ -565,31 +585,31 @@ class Ade: installed_version = self.get_installed_version() if not installed_version: - print("* Installing version {0}{1}{2}".format(Colors.OKGREEN, new_version, Colors.ENDC)) + self.info("* Installing version {0}{1}{2}".format(Colors.OKGREEN, new_version, Colors.ENDC)) elif self.force: prefix = '' if new_version == installed_version: prefix = 're' - print("* Forcing {0}installation of version {1}{2}{3}".format(prefix, Colors.OKGREEN, new_version, Colors.ENDC)) + 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: color = Colors.OKGREEN - print("* Sysroot {0}{1}{2} is already installed" \ - .format(color, installed_version, Colors.ENDC)) - return + 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) - self.install_sysroot(archive) + installed_version = self.install_sysroot(archive) finally: if archive and not self.file: os.remove(archive.filename) - print("* Installation has been completed") + self.info("* Installation has been completed") + if self.format == 'parseable': + print('InstalledVersion:' + installed_version.get_tag()) def do_sysroot_update(self): self.validate_sysroot_id() @@ -601,35 +621,37 @@ class Ade: installed_version = self.get_installed_version() if not installed_version: - print("* No sysroot currently installed for {0}{1} {2} ({3}){4}" \ + self.die("No sysroot currently installed for {0}{1} {2} ({3}){4}" \ .format(Colors.OKBLUE, self.distro, self.release, self.arch, Colors.ENDC)) - return elif installed_version < new_version: - print("* Upgrading from {0}{1}{2} to version {3}{4}{5}" \ + self.info("* Upgrading from {0}{1}{2} to version {3}{4}{5}" \ .format(Colors.WARNING, installed_version, Colors.ENDC, Colors.OKGREEN, new_version, Colors.ENDC)) elif self.force: - print("* Forcing {0}installation of version {1}{2}{3}" \ - .format(Colors.OKBLUE, new_version, Colors.ENDC, prefix)) + self.info("* Forcing installation of version {0}{1}{2}" \ + .format(Colors.OKBLUE, new_version, Colors.ENDC)) elif installed_version == new_version: - print("* Installed version {0}{1}{2} is already up-to-date" \ + 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()) return elif installed_version > new_version: - print("* Installed version {0}{1}{2} is more recent than {3}{4}{5}" \ + self.die("Installed version {0}{1}{2} is more recent than {3}{4}{5}" \ .format(Colors.WARNING, installed_version, Colors.ENDC, Colors.OKGREEN, new_version, Colors.ENDC)) - return try: archive = self.download_sysroot(new_version) self.uninstall_sysroot(installed_version) - self.install_sysroot(archive) + installed_version = self.install_sysroot(archive) finally: if archive: os.remove(archive.filename) - print("* Update has been completed") + self.info("* Update has been completed") + if self.format == 'parseable': + print('InstalledVersion:' + installed_version.get_tag()) def do_sysroot_uninstall(self): self.validate_sysroot_id() @@ -637,22 +659,28 @@ class Ade: installed_version = self.get_installed_version() if not installed_version: - print("* No sysroot currently installed for {0}{1} {2} ({3}){4}" \ - .format(Colors.OKBLUE, self.distro, self.release, self.arch, Colors.ENDC)) + 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: - print("* Uninstalling sysroot {0}{1}{2}" \ + self.info("* Uninstalling sysroot {0}{1}{2}" \ .format(Colors.WARNING, installed_version, Colors.ENDC)) self.uninstall_sysroot(installed_version) - print("* Sysroot for {0}{1} {2} ({3}){4} has been uninstalled" \ - .format(Colors.OKBLUE, self.distro, self.release, self.arch, Colors.ENDC)) + self.info("* Sysroot for {0}{1} {2} ({3}){4} has been uninstalled" \ + .format(Colors.OKBLUE, self.distro, self.release, self.arch, Colors.ENDC)) + + def info(self, message): + if self.format == 'friendly': + print(message) def die(self, message): logging.error(message) sys.exit(1) def run(self): + if self.format != 'friendly': + Colors.disable() self.find_configuration() method = 'do_' + self.command.replace('-', '_') + '_' + self.subcommand.replace('-', '_') getattr(self, method)() @@ -660,6 +688,8 @@ class Ade: if __name__ == '__main__': root_parser = argparse.ArgumentParser(description='ADE - Apertis Development Environment') + root_parser.add_argument('--format', choices=['friendly', 'parseable'], + default='friendly', help="Output format") argcomplete.autocomplete(root_parser) subparsers = root_parser.add_subparsers(dest='command') subparsers.required = True