From bdbe221935d3272eb8cb0d7956da528a218cee70 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Louis-Francis=20Ratt=C3=A9-Boulianne?= <lfrb@collabora.com>
Date: Fri, 9 Dec 2016 22:12:30 -0500
Subject: [PATCH] Add SDK and Simulator classes to handle these targets
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

 * Support for SDK in `info` command has also been added

Signed-off-by: Louis-Francis Ratté-Boulianne <lfrb@collabora.com>
Reviewed-by: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
Differential Revision: https://phabricator.apertis.org/D5229
---
 doc/man/ade-info.1 |  7 +++++
 tools/ade          | 76 ++++++++++++++++++++++++++++++++++------------
 2 files changed, 63 insertions(+), 20 deletions(-)

diff --git a/doc/man/ade-info.1 b/doc/man/ade-info.1
index d460c65..0135744 100644
--- a/doc/man/ade-info.1
+++ b/doc/man/ade-info.1
@@ -13,6 +13,7 @@ ade-info \- Print information about an entity
 .sp
 .nf
 \fIade info\fR --device [<user>[:<pass>]@]<hostname>[:<port>]
+\fIade info\fR --sdk
 \fIade info\fR --sysroot <sysroot>
 .fi
 .sp
@@ -29,6 +30,12 @@ Return image version of given device (e.g. user@apertis)\&.
 .sp
 .RE
 .PP
+\fI\-\-sdk\fR
+.RS 4
+Return SDK version\&.
+.sp
+.RE
+.PP
 \fI\-\-sysroot\fR
 .RS 4
 Return installed version and path of given sysroot (e.g. apertis-16.12-armhf)\&.
diff --git a/tools/ade b/tools/ade
index 1233f0e..f3329ea 100755
--- a/tools/ade
+++ b/tools/ade
@@ -131,6 +131,12 @@ class TargetTriplet:
         raise NotSupportedError
 
 
+class Simulator:
+
+    def __init__(self):
+        pass
+
+
 class Device:
 
     def __init__(self, host, port=22, user=None, password=None):
@@ -303,6 +309,17 @@ class Sysroot:
         if not path.endswith("/{0}/{1}/{2}".format(self.version.distro, self.version.release, self.version.arch)):
             raise ValueError("'version'")
 
+class SDK:
+
+    def __init__(self):
+        try:
+            with open('/etc/image_version') as f:
+                self.version = SysrootVersion.from_string(f.read())
+            with open('/usr/lib/pkg-config.multiarch') as f:
+                self.version.arch = TargetTriplet(f.read().strip()).arch
+        except FileNotFoundError:
+            # Missing file means we're not running in the Apertis SDK
+            raise NotInstalledError
 
 
 class SysrootArchive:
@@ -556,6 +573,8 @@ class Ade:
         self.dest = None
 
         self.target = None
+        self.sdk = False
+        self.simulator = False
         self.sysroot = None
         self.device = None
 
@@ -569,17 +588,13 @@ class Ade:
 
         self.force = False
 
-    def get_host_distro(self):
+    def get_sdk(self):
         try:
-            f = open('/etc/image_version')
-            version = SysrootVersion.from_string(f.read())
-            if not version.distro:
-                self.die("No distribution name found in version string")
-            return (version.distro, version.release)
-        except FileNotFoundError:
-            self.die("Couldn't find file /etc/image_version")
-        except:
-            self.die("Version file isn't matching expected format")
+            return SDK()
+        except NotInstalledError:
+            return None
+        except Exception as e:
+            seld.die("Invalid SDK installation: {0}".format(e))
 
     def find_configuration(self):
         if not self.config:
@@ -589,14 +604,20 @@ class Ade:
         # XXX If only URL is specified, try to obtain distro and arch from it.
         # XXX Allow project-specific config file to specify these info
 
-        if not self.distro:
-            self.info("* No distribution specified, defaulting to host distribution")
-            self.distro, release = self.get_host_distro()
-        if not self.release:
-            self.info("* No release version specified, defaulting to host release version")
-            distro, self.release = self.get_host_distro()
-            if distro != self.distro:
-                self.die("Mismatch between host distro and specified distro")
+        if not self.distro or not self.release:
+            try:
+                sdk = SDK()
+                if not self.distro:
+                    self.info("* No distribution specified, defaulting to host distribution")
+                    self.distro = sdk.version.distro
+                if not self.release:
+                    self.info("* No release version specified, defaulting to host release version")
+                    self.release = sdk.version.release
+                    if sdk.version.distro != self.distro:
+                        self.die("Mismatch between host distro and specified distro")
+            except:
+                self.die("No distribution/release specified")
+
         if not self.arch:
             self.info("* No architecture specified, defaulting to 'armhf'")
             self.arch = 'armhf'
@@ -610,7 +631,16 @@ class Ade:
         if self.target:
             return self.target
 
-        if self.sysroot:
+        if self.sdk:
+            try:
+                self.target = SDK()
+            except NotInstalledError:
+                self.die("Not running in a SDK distribution")
+            except Exception as e:
+                self.die("Invalid SDK installation: {0}".format(e))
+        elif self.simulator:
+            self.target = Simulator()
+        elif self.sysroot:
             try:
                 version = SysrootVersion.from_id(self.sysroot)
                 self.target = self.get_sysroot_manager().get_installed(version)
@@ -629,7 +659,7 @@ class Ade:
             except InvalidDeviceError as e:
                 self.die("Invalid device: {0}".format(e))
         else:
-            self.die("No target (sysroot or device) specified")
+            self.die("No target (simulator, sysroot or device) specified")
 
         return self.target
 
@@ -821,6 +851,11 @@ class Ade:
     def do_info(self):
         target = self.get_target()
 
+        if isinstance(target, SDK):
+            self.info("* SDK version is {0}{1}{2}"
+                    .format(Colors.WARNING, self.target.version, Colors.ENDC))
+            if self.format == 'parseable':
+                print("SDKVersion:{0}".format(self.target.version.get_tag()))
         if isinstance(target, Sysroot):
             self.info("* Sysroot version {0}{1}{2} is installed at '{3}'"
                     .format(Colors.WARNING, self.target.version, Colors.ENDC, self.target.path))
@@ -862,6 +897,7 @@ if __name__ == '__main__':
     # Info parser
     info_parser = subparsers.add_parser('info', help="Retrieve information about an object")
     group = info_parser.add_mutually_exclusive_group()
+    group.add_argument('--sdk', help="Use SDK as target", action='store_true')
     group.add_argument('--sysroot', help="Use sysroot as target (e.g. apertis-16.12-armhf)")
     group.add_argument('--device', help="Use device as target (e.g. user:pass@apertis)")
 
-- 
GitLab