diff options
author | Chris Matthews <cmatthews5@apple.com> | 2018-06-05 20:43:04 +0000 |
---|---|---|
committer | Chris Matthews <cmatthews5@apple.com> | 2018-06-05 20:43:04 +0000 |
commit | 803ed02dd676acf1b8663d2b17bf414ab94ef061 (patch) | |
tree | a4f32fdc48383312569800f3433af07a09247e42 | |
parent | b98a6adfd7adfbb14136fd0818c5a0c3f47ee7ce (diff) |
Some simple support for detecting which devices are attached to a mac
git-svn-id: https://llvm.org/svn/llvm-project/zorg/trunk@334047 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | dep/dep.py | 54 | ||||
-rw-r--r-- | dep/tests/test_dep.py | 12 |
2 files changed, 66 insertions, 0 deletions
@@ -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, } diff --git a/dep/tests/test_dep.py b/dep/tests/test_dep.py index ab8f8ffc..835ddd17 100644 --- a/dep/tests/test_dep.py +++ b/dep/tests/test_dep.py @@ -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() |