[zorg] r334047 - Some simple support for detecting which devices are attached to a mac

Chris Matthews via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 5 13:43:04 PDT 2018


Author: cmatthews
Date: Tue Jun  5 13:43:04 2018
New Revision: 334047

URL: http://llvm.org/viewvc/llvm-project?rev=334047&view=rev
Log:
Some simple support for detecting which devices are attached to a mac

Modified:
    zorg/trunk/dep/dep.py
    zorg/trunk/dep/tests/test_dep.py

Modified: zorg/trunk/dep/dep.py
URL: http://llvm.org/viewvc/llvm-project/zorg/trunk/dep/dep.py?rev=334047&r1=334046&r2=334047&view=diff
==============================================================================
--- zorg/trunk/dep/dep.py (original)
+++ zorg/trunk/dep/dep.py Tue Jun  5 13:43:04 2018
@@ -577,12 +577,66 @@ class Pip(Dependency):
         return "{} {} {}".format(self.str_kind, self.package, self.version)
 
 
+class Device(Dependency):
+    """Verify correct device is attached to this machine."""
+
+    # device somelongudidstring.
+    # We will filter dashes if they are added.
+    device_re = re.compile(r'(?P<command>\w+)\s+(?P<udid>.*)')
+
+    def __init__(self, line, kind):
+        # type: (Line, Text) -> None
+        """Parse and verify device is attached.
+
+        :param line: the Line with the deceleration of the dependency.
+        :param kind: the detected dependency kind.
+        """
+        super(Device, self).__init__(line, kind)
+        self.command = None
+        self.udid = None
+        self.installed_version = None
+
+    def parse(self):
+        """Parse this dependency."""
+        text = self.line.text
+        match = self.device_re.match(text)
+        if not match:
+            raise MalformedDependency("Expression does not compile in {}: {}".format(self.__class__.__name__,
+                                                                                     self.line))
+        self.__dict__.update(match.groupdict())
+        # Sometimes people put dashes in these, lets not compare with dashes.
+        self.udid = self.udid.replace("-", "")
+
+    def verify(self):
+        """Verify the device is attached."""
+
+        try:
+            instruments_output = subprocess.check_output(["xcrun", "instruments", "-s", "devices"]).decode("utf-8")
+        except (subprocess.CalledProcessError, OSError):
+            raise MissingDependencyError(self, "Cannot find instruments")
+        # Convert udids with dashes to without for comparison.
+        cleaned_instruments_output = instruments_output.replace(u"-", u"")
+        if self.udid not in cleaned_instruments_output:
+            # The device is not in instruments.
+            raise MissingDependencyError(self, "")
+        return True
+
+    def inject(self):
+        """Not implemented."""
+        raise NotImplementedError()
+
+    def __str__(self):
+        """Dependency kind, package and version, for printing in error messages."""
+        return "{} {} {}".format(self.str_kind, self.udid, "")
+
+
 dependencies_implementations = {'brew': Brew,
                                 'os_version': HostOSVersion,
                                 'config_manager': ConMan,
                                 'xcode': Xcode,
                                 'sdk': Sdk,
                                 'pip': Pip,
+                                'device': Device,
                                 }
 
 

Modified: zorg/trunk/dep/tests/test_dep.py
URL: http://llvm.org/viewvc/llvm-project/zorg/trunk/dep/tests/test_dep.py?rev=334047&r1=334046&r2=334047&view=diff
==============================================================================
--- zorg/trunk/dep/tests/test_dep.py (original)
+++ zorg/trunk/dep/tests/test_dep.py Tue Jun  5 13:43:04 2018
@@ -266,3 +266,15 @@ def test_pip_requirement(mocker):
     dep.subprocess.check_output.side_effect = subprocess.CalledProcessError(1, [], output=no_pip)
     with pytest.raises(MissingDependencyError):
         b.verify_and_act()
+
+
+def test_device_requirement(mocker):
+    """Detailed check of a device udid dependency."""
+    line = Line("foo.c", 10, "device aaabbbeeeec5fffff38bc8511112c2225f7333d44", "test")
+    b = dep.Device(line, "device")
+    b.parse()
+    with pytest.raises(MissingDependencyError):
+        b.verify_and_act()
+    mocker.patch('dep.subprocess.check_output')
+    dep.subprocess.check_output.side_effect = [open(here + '/assets/instruments_output.txt').read()]
+    b.verify_and_act()




More information about the llvm-commits mailing list