~pygame/pygame/trunk

« back to all changes in this revision

Viewing changes to trackmod/reporter.py

  • Committer: pygame
  • Date: 2017-01-10 00:31:42 UTC
  • Revision ID: git-v1:2eea4f299a2e791f884608d7ed601558634af73c
commit 1639c41a8cb3433046882ede92c80ce69d59016b
Author: Thomas Kluyver <takowl@gmail.com>
Date:   Sun Jan 8 18:46:46 2017 +0000

    Build newer versions of libogg and libvorbis into Linux base images

    Closes #317
    Closes #323

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# module trackmod.reporter
 
2
 
 
3
# Keep this first.
 
4
def listmods():
 
5
    return [n for n, m in sys.modules.iteritems() if m is not None]
 
6
 
 
7
import sys
 
8
previous_imports = listmods()  #  Keep this after sys but before other imports.
 
9
import threading
 
10
 
 
11
import module
 
12
 
 
13
 
 
14
# This module is does not need explicit thread protection since all calls
 
15
# to the data entry methods are made while the import lock is acquired.
 
16
collect_data = True
 
17
my_imports = None
 
18
accesses = None
 
19
failed_imports = None
 
20
 
 
21
try:
 
22
    next
 
23
except NameError:
 
24
    def next(iterator):
 
25
        return iterator.next()
 
26
 
 
27
class Largest(object):
 
28
    """This object is always greater than any other non Largest object"""
 
29
    def __lt__(self, other):
 
30
        return False
 
31
    def __le__(self, other):
 
32
        return self == other
 
33
    def __eq__(self, other):
 
34
        return isinstance(other, Largest)
 
35
    def __ne__(self, other):
 
36
        not self == other
 
37
    def __gt__(self, other):
 
38
        return True
 
39
    def __ge__(self, other):
 
40
        return True
 
41
 
 
42
def process_accessed():
 
43
    acc_names = dict(accessed)
 
44
    for name, attr in accessed:
 
45
        parts = name.split('.')
 
46
        for i in range(1, len(parts)):
 
47
            subname = '.'.join(parts[0:i])
 
48
            if subname not in acc_names:
 
49
                acc_names[subname] = parts[i]
 
50
    return set(acc_names.iteritems())
 
51
 
 
52
def begin():
 
53
    global previous_imports, my_imports, accesses, failed_imports
 
54
    my_imports = list(set(listmods()) - set(previous_imports))
 
55
    accesses = {}
 
56
    failed_imports = set()
 
57
 
 
58
def end():
 
59
    global collect_data
 
60
    collect_data = False
 
61
 
 
62
def add_import(name):
 
63
    """Add a module to the import list
 
64
 
 
65
    Expects to be called in the order in which modules are created:
 
66
    package, submodule, etc.
 
67
 
 
68
    """
 
69
    if collect_data:
 
70
        accesses[name] = set()
 
71
 
 
72
def remove_import(name):
 
73
    del accesses[name]
 
74
    failed_imports.add(name)
 
75
 
 
76
def add_access(name, attr):
 
77
    if collect_data:
 
78
        accesses[name].add(attr)
 
79
 
 
80
def get_previous_imports():
 
81
    """Return a new sorted name list of previously imported modules"""
 
82
    return sorted(previous_imports)
 
83
 
 
84
def get_my_imports():
 
85
    """Return a new sorted name list of module imported by this package"""
 
86
    return sorted(my_imports)
 
87
 
 
88
def get_imports():
 
89
    """Return a new sorted name list of imported modules"""
 
90
    tracked_types = (module.Module, module.TrackerModule)
 
91
    return sorted(n for n, m in list(sys.modules.iteritems())
 
92
                    if isinstance(m, tracked_types))
 
93
 
 
94
def get_unaccessed_modules():
 
95
    """Return a new sorted name list of unaccessed imported modules"""
 
96
    unaccessed = []
 
97
    iaccessed = iter(get_accessed_modules())
 
98
    accessed_name = ''
 
99
    for imports_name in get_imports():
 
100
        while accessed_name < imports_name:
 
101
            try:
 
102
                accessed_name = next(iaccessed)
 
103
            except StopIteration:
 
104
                accessed_name = Largest()
 
105
        if imports_name < accessed_name:
 
106
            unaccessed.append(imports_name)
 
107
    return unaccessed
 
108
 
 
109
def get_accessed_modules():
 
110
    """Return a new sorted name list of accessed modules"""
 
111
    accessed = []
 
112
    previous_name = ''
 
113
    for name, ignored in module.get_accesses():
 
114
        if name != previous_name:
 
115
            accessed.append(name)
 
116
            previous_name = name
 
117
    return accessed
 
118
 
 
119
def get_accesses():
 
120
    """Return a new dictionary of sorted lists of attributes by module name"""
 
121
    accesses = {}
 
122
    previous_name = ''
 
123
    for name, attribute in module.get_accesses():
 
124
        if name != previous_name:
 
125
            attributes = []
 
126
            accesses[name] = attributes
 
127
            previous_name = name
 
128
        attributes.append(attribute)
 
129
    return accesses
 
130
 
 
131
 
 
132
 
 
133
 
 
134