aboutsummaryrefslogtreecommitdiff
path: root/dep/dep.py
blob: 8850fbec8e6f30223c91c7d60f160823f716ced1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
#!/usr/bin/python2.7
"""
Dependency manager for llvm CI builds.

We have complex dependencies for some of our CI builds. This will serve
as a system to help document and enforce them.

Developer notes:

- We are trying to keep package dependencies to a minimum in this project. So it
does not require an installer. It should be able to be run as a stand alone script
when checked out of VCS. So, don't import anything not in the Python 2.7
standard library.

"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import argparse
import collections
import json
import platform
import re
import subprocess


try:
    # noinspection PyUnresolvedReferences
    from typing import List, Text, Union, Dict, Type, Optional  # noqa
except ImportError as e:
    pass  # Not really needed at runtime, so okay to not have installed.


VERSION = '0.1'
"""We have a built in version check, so we can require specific features and fixes."""


class Version(object):
    """Model a version number, which can be compared to another version number.

    Keeps a nice looking text version around as well for printing.

    This abstraction exists to make some of the more complex comparisons easier,
    as well as collecting and printing versions.

    In the future, we might want to have some different comparison,
    for instance, 4.0 == 4.0.0 -> True.

    """

    def __init__(self, text):
        """Create a version from a . separated version string."""
        self.text = text
        self.numeric = [int(d) for d in text.split(".")]

    def __gt__(self, other):
        """Compare the numeric representation of the version."""
        return self.numeric.__gt__(other.numeric)

    def __lt__(self, other):
        """Compare the numeric representation of the version."""
        return self.numeric.__lt__(other.numeric)

    def __eq__(self, other):
        """Compare the numeric representation of the version."""
        return self.numeric.__eq__(other.numeric)

    def __le__(self, other):
        """Compare the numeric representation of the version."""
        return self.numeric.__le__(other.numeric)

    def __ge__(self, other):
        """Compare the numeric representation of the version."""
        return self.numeric.__ge__(other.numeric)

    def __repr__(self):
        """Print the original text representation of the Version."""
        return "v{}".format(self.text)


class Dependency(object):
    """Dependency Abstract base class."""

    def __init__(self, line, str_kind):
        """Save line information.

        :param line: A parsed Line object that contains the raw dependency deceleration.
        :param str_kind: The determined kind of the Dependency.
        """
        # type: (Line, Text) -> object
        self.line = line
        self.str_kind = str_kind
        self.installed_version = None

    def parse(self):
        """Read the input line and prepare to verify this dependency.

        Raise a MalformedDependencyError if three is something wrong.

        Should return nothing, but get the dependency ready for verification.
        """
        raise NotImplementedError()

    def verify(self):
        # type: () -> bool
        """Determine if this dependency met.

        :returns: True when the dependency is met, otherwise False.
        """
        raise NotImplementedError()

    def inject(self):
        """If possible, modify the system to meet the dependency."""
        raise NotImplementedError()

    def verify_and_act(self):
        """Parse, then verify and trigger pass or fail.

        Extract that out here, so we don't duplicate the logic in the subclasses.
        """
        met = self.verify()
        if met:
            self.verify_pass()
        else:
            self.verify_fail()

    def verify_fail(self):
        """When dependency is not met, raise an exception.

        This is the default behavior; but I want the subclasses to be able
        to change it.
        """
        raise MissingDependencyError(self, self.installed_version)

    def verify_pass(self):
        """Print a nice message that the dependency is met.

        I'm not sure we even want to print this, but we will for now. It might
        be to verbose.  Subclasses should override this if wanted.
        """
        print("Dependency met", str(self))


class MalformedDependency(Exception):
    """Raised when parsing a dependency directive fails.

    This is situations like the regexes not matching, or part of the dependency directive missing.

    Should probably record more useful stuff, but Exception.message is set. So we can print it later.
    """

    pass


def brew_cmd(command):
    # type: (List[Text]) -> List[Dict[Text, object]]
    """Help run a brew command, and parse the output.

    Brew has a json output option which we use.  Run the command and parse the stdout
    as json and return the result.
    :param command: The brew command to execute, and parse the output of.
    :return:
    """
    assert "--json=v1" in command, "Must pass JSON arg so we can parse the output."
    out = subprocess.check_output(command)
    brew_info = json.loads(out)
    return brew_info


class MissingDependencyError(Exception):
    """Fail verification with one of these when we determine a dependency is not met.

    For each dependency, we will print a useful message with the dependency line as well as the
    reason it was not matched.
    """

    def __init__(self, dependency, installed=None):
        # type: (Dependency, Optional[Text]) -> None
        """Raise when a dependency is not met.

        This exception can be printed as the error message.

        :param dependency: The dependency that is not being met.
        :param installed: what was found to be installed instead.
        """
        # type: (Dependency, Union[Text, Version]) -> None
        super(MissingDependencyError, self).__init__()
        self.dependency = dependency
        self.installed = installed

    def __str__(self):
        """For now, we will just print these as our error message."""
        return "missing dependency: {}, found {} installed, requested from {}".format(self.dependency,
                                                                                      self.installed,
                                                                                      self.dependency.line)


def check_version(installed, operator, requested):
    """Check that the installed version does the operator of the requested.

    :param installed: The installed Version of the requirement.
    :param operator: the text operator (==, <=, >=)
    :param requested: The requested Version of the requirement.
    :return: True if the requirement is satisfied.
    """
    # type: (Version, Text, Version) -> bool

    dependency_met = False
    if operator == "==" and installed == requested:
        dependency_met = True
    if operator == "<=" and installed <= requested:
        dependency_met = True
    if operator == ">=" and installed >= requested:
        dependency_met = True
    return dependency_met


class ConMan(Dependency):
    """Version self-check of this tool.

    In case we introduce something in the future, the dep files can
    be made to depend on a specific version of this tool.  We will
    increment the versions manually.

    """

    conman_re = re.compile(r'(?P<command>\w+)\s+(?P<operator>>=|<=|==)\s*(?P<version_text>[\d.-_]+)')
    """For example: config_manager <= 0.1"""

    def __init__(self, line, kind):
        """Check that this tool is up to date."""
        super(ConMan, self).__init__(line, kind)
        self.command = None
        self.operator = None
        self.version = None
        self.version_text = None
        self.installed_version = None

    def parse(self):
        """Parse dependency."""
        text = self.line.text
        match = self.conman_re.match(text)
        if not match:
            raise MalformedDependency("Expression does not compile in {}: {}".format(self.__class__.__name__,
                                                                                     self.line))
        self.__dict__.update(match.groupdict())

        self.version = Version(self.version_text)

    def verify(self):
        """Verify the version of this tool."""
        self.installed_version = Version(VERSION)

        return check_version(self.installed_version, self.operator, self.version)

    def inject(self):
        """Can't really do much here."""
        pass

    def __str__(self):
        """Show as dependency and version."""
        return "{} {}".format(self.str_kind, self.version)


class HostOSVersion(Dependency):
    """Use Python's platform module to get host OS information and verify.

    Wew can only verify, but not inject for host OS version.
    """

    conman_re = re.compile(r'(?P<command>\w+)\s+(?P<operator>>=|<=|==)\s*(?P<version_text>[\d.-_]+)')

    def __init__(self, line, kind):
        """Parse and Verify host OS version using Python's platform module.

        :param line: Line with teh Dependencies deceleration.
        :param kind: the dependency kind that was detected by the parser.
        """
        # type: (Line, Text) -> None
        super(HostOSVersion, self).__init__(line, kind)
        self.command = None
        self.operator = None
        self.version = None
        self.version_text = None
        self.installed_version = None

    def parse(self):
        """Parse dependency."""
        text = self.line.text
        match = self.conman_re.match(text)
        if not match:
            raise MalformedDependency("Expression does not compile in {}: {}".format(self.__class__.__name__,
                                                                                     self.line))
        self.__dict__.update(match.groupdict())

        self.version = Version(self.version_text)

    def verify(self):
        """Verify the request host OS version holds."""
        self.installed_version = Version(platform.mac_ver()[0])

        return check_version(self.installed_version, self.operator, self.version)

    def inject(self):
        """Can't change the host OS version, so not much to do here."""
        pass

    def __str__(self):
        """For printing in error messages."""
        return "{} {}".format(self.str_kind, self.version)


class Brew(Dependency):
    """Verify and Inject brew package dependencies."""

    # brew <package> <operator> <version>.  Operator may not have spaces around it.
    brew_re = re.compile(r'(?P<command>\w+)\s+(?P<package>\w+)\s*(?P<operator>>=|<=|==)\s*(?P<version_text>[\d.-_]+)')

    def __init__(self, line, kind):
        # type: (Line, Text) -> None
        """Parse and verify brew package is installed.

        :param line: the Line with the deceleration of the dependency.
        :param kind: the detected dependency kind.
        """
        super(Brew, self).__init__(line, kind)
        self.command = None
        self.operator = None
        self.package = None
        self.version = None
        self.version_text = None
        self.installed_version = None

    def parse(self):
        """Parse this dependency."""
        text = self.line.text
        match = self.brew_re.match(text)
        if not match:
            raise MalformedDependency("Expression does not compile in {}: {}".format(self.__class__.__name__,
                                                                                     self.line))
        self.__dict__.update(match.groupdict())

        self.version = Version(self.version_text)

    def verify(self):
        """Verify the packages in brew match this dependency."""
        brew_package_config = brew_cmd(['/usr/local/bin/brew', 'info', self.package, "--json=v1"])
        version = None
        for brew_package in brew_package_config:
            name = brew_package['name']
            install_info = brew_package.get('installed')
            for versions in install_info:
                version = versions['version'] if versions else None
            if name == self.package:
                break
        if not version:
            # The package is not installed at all.
            raise MissingDependencyError(self, "nothing")
        self.installed_version = Version(version)
        return check_version(self.installed_version, self.operator, self.version)

    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.package, self.version)


class Xcode(Dependency):
    """Verify and Inject Xcode version dependencies."""

    # xcode <operator> <version>.  Operator may not have spaces around it.
    xcode_dep_re = re.compile(r'(?P<command>\w+)\s+(?P<operator>>=|<=|==)\s*(?P<version_text>[\d.-_]+)')

    def __init__(self, line, kind):
        # type: (Line, Text) -> None
        """Parse and xcode version installed.

        :param line: the Line with the deceleration of the dependency.
        :param kind: the detected dependency kind.
        """
        super(Xcode, self).__init__(line, kind)
        self.command = None
        self.operator = None
        self.version = None
        self.version_text = None
        self.installed_version = None

    def parse(self):
        """Parse this dependency."""
        text = self.line.text
        match = self.xcode_dep_re.match(text)
        if not match:
            raise MalformedDependency("Expression does not compile in {}: {}".format(self.__class__.__name__,
                                                                                     self.line))
        self.__dict__.update(match.groupdict())

        self.version = Version(self.version_text)

    def verify(self):
        """Verify the installed Xcode matches this dependency."""
        installed_version_output = subprocess.check_output(['/usr/bin/xcrun', 'xcodebuild', "-version"])
        installed_version_re = re.compile(r"^Xcode\s(?P<version_text>[\d/.]+)")
        match = installed_version_re.match(installed_version_output)
        if not match:
            raise MissingDependencyError(self, "Did not find Xcode version in output:" + installed_version_output)
        version = match.groupdict().get('version_text')
        if not version:
            # The package is not installed at all.
            raise AssertionError("No version text found.")
        self.installed_version = Version(version)
        return check_version(self.installed_version, self.operator, self.version)

    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.version)


class Sdk(Dependency):
    """Verify and Inject Sdk version dependencies."""

    # sdk <operator> <version>.  Operator may not have spaces around it.
    sdk_dep_re = re.compile(r'(?P<command>\w+)\s+(?P<sdk>[\w/.]+)\s*'
                            r'(?P<operator>>=|<=|==)\s*(?P<version_text>[\d.-_]+)')

    def __init__(self, line, kind):
        # type: (Line, Text) -> None
        """Parse and sdk version installed.

        :param line: the Line with the deceleration of the dependency.
        :param kind: the detected dependency kind.
        """
        super(Sdk, self).__init__(line, kind)
        self.command = None
        self.sdk = None
        self.operator = None
        self.version = None
        self.version_text = None
        self.installed_version = None

    def parse(self):
        """Parse this dependency."""
        text = self.line.text
        match = self.sdk_dep_re.match(text)
        if not match:
            raise MalformedDependency("Expression does not compile in {}: {}".format(self.__class__.__name__,
                                                                                     self.line))
        self.__dict__.update(match.groupdict())

        self.version = Version(self.version_text)

    def verify(self):
        """Verify the installed Sdk matches this dependency."""
        installed_version_output = subprocess.check_output(['/usr/bin/xcrun', 'xcodebuild', "-showsdks"])
        installed_version_re = re.compile(r".*-sdk\s+(?P<sdk_text>\S+)")

        matches = [installed_version_re.match(l).groupdict()['sdk_text']
                   for l in installed_version_output.split('\n') if installed_version_re.match(l)]

        if not matches:
            raise MissingDependencyError(self, "Did not find Sdk version in output:" + installed_version_output)

        extract_version_names = re.compile(r'(?P<pre>\D*)(?P<version_text>[\d+/.]*)(?P<post>.*)')

        sdks = [extract_version_names.match(sdk_text).groupdict()
                for sdk_text in matches if extract_version_names.match(sdk_text)]

        installed_sdks = collections.defaultdict(list)
        for sdk in sdks:
            name = sdk['pre']
            if sdk.get('post'):
                name += "." + sdk.get('post')
            if sdk.get('version_text'):
                version = Version(sdk['version_text'].rstrip('.'))
            else:
                continue
            installed_sdks[name].append(version)

        if self.sdk not in installed_sdks.keys():
            raise MissingDependencyError(self, "{} not found in installed SDKs.".format(self.sdk))

        self.installed_version = installed_sdks[self.sdk]

        satisfied = [check_version(s, self.operator, self.version) for s in self.installed_version]
        return any(satisfied)

    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.version)


class Pip(Dependency):
    """Verify and Inject pip package dependencies."""

    # pip <package> <operator> <version>.  Operator may not have spaces around it.
    pip_re = re.compile(r'(?P<command>\w+)\s+(?P<package>\w+)\s*(?P<operator>>=|<=|==)\s*(?P<version_text>[\d.-_]+)')

    def __init__(self, line, kind):
        # type: (Line, Text) -> None
        """Parse and verify pip package is installed.

        :param line: the Line with the deceleration of the dependency.
        :param kind: the detected dependency kind.
        """
        super(Pip, self).__init__(line, kind)
        self.command = None
        self.operator = None
        self.package = None
        self.version = None
        self.version_text = None
        self.installed_version = None

    def parse(self):
        """Parse this dependency."""
        text = self.line.text
        match = self.pip_re.match(text)
        if not match:
            raise MalformedDependency("Expression does not compile in {}: {}".format(self.__class__.__name__,
                                                                                     self.line))
        self.__dict__.update(match.groupdict())

        self.version = Version(self.version_text)

    def verify(self):
        """Verify the packages in pip match this dependency."""
        try:
            pip_package_config = json.loads(subprocess.check_output(["/usr/bin/env",
                                                                     "python", "-m", "pip", "list", "--format=json"]))
        except (subprocess.CalledProcessError, OSError):
            raise MissingDependencyError(self, "Cannot find pip")

        installed = {p['name']: p['version'] for p in pip_package_config}  # type: Dict[Text, Text]

        package = installed.get(self.package)

        if not package:
            # The package is not installed at all.
            raise MissingDependencyError(self, "not in package list")
        self.installed_version = Version(package)
        return check_version(self.installed_version, self.operator, self.version)

    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.package, self.version)


dependencies_implementations = {'brew': Brew,
                                'os_version': HostOSVersion,
                                'config_manager': ConMan,
                                'xcode': Xcode,
                                'sdk': Sdk,
                                'pip': Pip,
                                }


def dependency_factory(line):
    """Given a line, create a concrete dependency for it.

    :param line: The line with the dependency info
    :return: Some subclass of Dependency, based on what was in the line.
    """
    # type: Text -> Dependency
    kind = line.text.split()[0]
    try:
        return dependencies_implementations[kind](line, kind)
    except KeyError:
        raise MalformedDependency("Don't know about {} kind of dependency.".format(kind))


class Line(object):
    """A preprocessed line. Understands file and line number as well as comments."""

    def __init__(self, filename, line_number, text, comment):
        # type: (Text, int, Text, Text) -> None
        """Raw Line information, split into the dependency deceleration and comment.

        :param filename: the input filename.
        :param line_number: the line number in the input file.
        :param text: Non-comment part of the line.
        :param comment: Text from the comment part of the line if any.
        """
        self.filename = filename
        self.line_number = line_number
        self.text = text
        self.comment = comment

    def __repr__(self):
        """Reconstruct the line for pretty printing."""
        return "{}:{}: {}{}".format(self.filename,
                                    self.line_number,
                                    self.text,
                                    " # " + self.comment if self.comment else "")


# For stripping comments out of lines.
comment_re = re.compile(r'(?P<main_text>.*)#(?P<comment>.*)')


# noinspection PyUnresolvedReferences
def _parse_dep_file(lines, filename):
    # type: (List[Text], Text) -> List[Line]
    process_lines = []
    for num, text in enumerate(lines):
        if "#" in text:
            bits = comment_re.match(text)
            main_text = bits.groupdict().get('main_text')
            comment = bits.groupdict().get('comment')
        else:
            main_text = text
            comment = None
        if main_text:
            main_text = main_text.strip()
        if comment:
            comment = comment.strip()
        process_lines.append(Line(filename, num, main_text, comment))

    return process_lines


def parse_dependencies(file_names):
    """Program logic: read files, verify dependencies.

    For each input file, read lines and create dependencies. Verify each dependency.

    :param file_names: files to read dependencies from.
    :return: The list of dependencies, each verified.
    """
    # type: (List[Text]) -> List[Type[Dependency]]
    preprocessed_lines = []
    for file_name in file_names:
        with open(file_name, 'r') as f:
            lines = f.readlines()
            preprocessed_lines.extend(_parse_dep_file(lines, file_name))

    dependencies = [dependency_factory(l) for l in preprocessed_lines if l.text]
    [d.parse() for d in dependencies]
    for d in dependencies:
        try:
            met = d.verify()
            if met:
                d.verify_pass()
            else:
                d.verify_fail()

        except MissingDependencyError as exec_info:
            print("Error:", exec_info)

    return dependencies


def main():
    """Parse arguments and trigger dependency verification."""
    parser = argparse.ArgumentParser(description='Verify and install dependencies.')
    parser.add_argument('command', help="What to do.")

    parser.add_argument('dependencies',  nargs='+', help="Path to dependency files.")

    args = parser.parse_args()

    parse_dependencies(args.dependencies)

    return True


if __name__ == '__main__':
    main()