1#!/usr/bin/env python3
2
3# Copyright (c) 2009 Giampaolo Rodola'. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Nicely print wheels print in dist/ directory."""
8
9import collections
10import glob
11import os
12
13from psutil._common import print_color
14from psutil._common import bytes2human
15
16
17class Wheel:
18
19    def __init__(self, path):
20        self._path = path
21        self._name = os.path.basename(path)
22
23    def __repr__(self):
24        return "<Wheel(name=%s, plat=%s, arch=%s, pyver=%s)>" % (
25            self.name, self.platform(), self.arch(), self.pyver())
26
27    __str__ = __repr__
28
29    @property
30    def name(self):
31        return self._name
32
33    def platform(self):
34        plat = self.name.split('-')[-1]
35        pyimpl = self.name.split('-')[3]
36        ispypy = 'pypy' in pyimpl
37        if 'linux' in plat:
38            if ispypy:
39                return 'pypy_on_linux'
40            else:
41                return 'linux'
42        elif 'win' in plat:
43            if ispypy:
44                return 'pypy_on_windows'
45            else:
46                return 'windows'
47        elif 'macosx' in plat:
48            if ispypy:
49                return 'pypy_on_macos'
50            else:
51                return 'macos'
52        else:
53            raise ValueError("unknown platform %r" % self.name)
54
55    def arch(self):
56        if self.name.endswith(('x86_64.whl', 'amd64.whl')):
57            return '64'
58        return '32'
59
60    def pyver(self):
61        pyver = 'pypy' if self.name.split('-')[3].startswith('pypy') else 'py'
62        pyver += self.name.split('-')[2][2:]
63        return pyver
64
65    def size(self):
66        return os.path.getsize(self._path)
67
68
69def main():
70    groups = collections.defaultdict(list)
71    for path in glob.glob('dist/*.whl'):
72        wheel = Wheel(path)
73        groups[wheel.platform()].append(wheel)
74
75    tot_files = 0
76    tot_size = 0
77    templ = "%-54s %7s %7s %7s"
78    for platf, wheels in groups.items():
79        ppn = "%s (total = %s)" % (platf, len(wheels))
80        s = templ % (ppn, "size", "arch", "pyver")
81        print_color('\n' + s, color=None, bold=True)
82        for wheel in sorted(wheels, key=lambda x: x.name):
83            tot_files += 1
84            tot_size += wheel.size()
85            s = templ % (wheel.name, bytes2human(wheel.size()), wheel.arch(),
86                         wheel.pyver())
87            if 'pypy' in wheel.pyver():
88                print_color(s, color='violet')
89            else:
90                print_color(s, color='brown')
91
92    print_color("\ntotals: files=%s, size=%s" % (
93        tot_files, bytes2human(tot_size)), bold=True)
94
95
96if __name__ == '__main__':
97    main()
98