Michael Merickel
2018-10-15 0c29cf2df41600d3906d521c72991c7686018b71
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
import sys
import platform
import pkg_resources
import argparse
from operator import itemgetter
 
 
def out(*args):  # pragma: no cover
    for arg in args:
        sys.stdout.write(arg)
        sys.stdout.write(' ')
    sys.stdout.write('\n')
 
 
def get_parser():
    parser = argparse.ArgumentParser(
        description="Show Python distribution versions and locations in use"
    )
    return parser
 
 
def main(
    argv=sys.argv,
    pkg_resources=pkg_resources,
    platform=platform.platform,
    out=out,
):
    # all args except argv are for unit testing purposes only
    parser = get_parser()
    parser.parse_args(argv[1:])
    packages = []
    for distribution in pkg_resources.working_set:
        name = distribution.project_name
        packages.append(
            {
                'version': distribution.version,
                'lowername': name.lower(),
                'name': name,
                'location': distribution.location,
            }
        )
    packages = sorted(packages, key=itemgetter('lowername'))
    pyramid_version = pkg_resources.get_distribution('pyramid').version
    plat = platform()
    out('Pyramid version:', pyramid_version)
    out('Platform:', plat)
    out('Packages:')
    for package in packages:
        out(' ', package['name'], package['version'])
        out('   ', package['location'])
 
 
if __name__ == '__main__':  # pragma: no cover
    sys.exit(main() or 0)