Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# -*- coding: utf-8 -*- 

 

import runmacs.processor.product as product 

from runmacs.processor.metaStorage import filterQuery 

from runmacs.processor.productBuilder import QueryResolutionIterator 

from runmacs.processor.querybuilder import NeedOtherComponent 

import sys 

import cStringIO as StringIO 

import traceback 

 

def flushSIO(sio, log, spaces=''): 

sio.seek(0) 

for line in sio: 

log.write(spaces+'(stdout)>> '+line.replace('<', '&lt;').replace('>', '&gt;')) 

sio.seek(0) 

sio.truncate(0) 

 

class MissingDataAnalyzer(object): 

def __init__(self, registry): 

self.registry = registry 

def findPotentialDerivates(self, p): 

""" 

Finds product classes which might be used to build a derived product 

of the product instance ``p``. 

 

:param p: Instance of a product. 

:returns: Generator over (<component name>, <derived product class>) 

""" 

for dp in self.registry.find_derived_products(): 

prototype = dp.build() 

try: 

componentNames = prototype.componentNames 

except AttributeError: 

print "Cannot work on %s"%prototype.canonicalName 

continue 

for component in componentNames: 

query = prototype.queryForSingleComponent(component) 

if filterQuery(query, p._productSpec): 

yield component, dp 

def _traceBuild(self, accessor, p, prototype, log, logLevel=1): 

spaces = u' '*logLevel 

_stdout = sys.stdout 

try: 

sio = StringIO.StringIO() 

sys.stdout = sio 

missingComponents = [cn for cn in sorted(prototype.componentNames) if cn not in prototype.components] 

if prototype.building: 

log.write(spaces+u'missing components are now: %s\r\n'%missingComponents) 

if len(missingComponents) == 0 or prototype.building == False: 

log.write(spaces+u'<b>FOUND RESULT:</b> %s\r\n'%prototype.hash) 

yield prototype 

else: 

for componentName in missingComponents: 

log.write(spaces+'searching for matching %s\r\n'%componentName) 

try: 

needed = prototype.whatIsNeeded(componentName) 

except ValueError: 

log.write(spaces+'whatIsNeeded failed!\r\n') 

continue 

except NeedOtherComponent as e: 

log.write(spaces+'whatIsNeeded failed (%s)!\r\n'%e) 

continue 

query = needed['query'] 

sort = None 

limit = None 

if isinstance(query, tuple): 

query, queryModifiers = query 

if 'sortBy' in queryModifiers: 

sort = queryModifiers['sortBy'] 

if 'limit' in queryModifiers: 

limit = queryModifiers['limit'] 

log.write(spaces+'using query: %s\r\n'%str(query)) 

log.write(spaces+'limit: %s, sort: %s\r\n'%(str(limit), str(sort))) 

cn = needed['componentName'] 

if cn != componentName: 

log.write(spaces+'missmatched need %s != %s\r\n'%(cn, componentName)) 

continue 

if needed['representation'] != 'single': 

log.write(spaces+'need single representation!\r\n') 

continue 

foundSometing = False 

for prod in accessor.query(query, limit=limit, sort=sort): 

foundSometing = True 

flushSIO(sio, log, spaces) 

for sub in QueryResolutionIterator(prototype, componentName, prod, accessor): 

flushSIO(sio, log, spaces) 

log.write(spaces+'found %s\r\n'%sub.canonicalName) 

for res in self._traceBuild(accessor, p, sub, log, logLevel+1): 

yield res 

flushSIO(sio, log, spaces) 

 

if not foundSometing: 

log.write(spaces+u'no result found for query: %s\r\n'%unicode(query)) 

except: 

traceback.print_exc(file=sio) 

flushSIO(sio,log,spaces) 

finally: 

sys.stdout = _stdout 

 

def traceBuild(self, accessor, p, der, log): 

""" 

Try to build a new product based on der from p, writing actions to log. 

""" 

_stdout = sys.stdout 

try: 

sio = StringIO.StringIO() 

sys.stdout = sio 

componentName, dp = der 

prototype = dp.build() 

log.write(u'trying to add base component\r\n') 

for sub in QueryResolutionIterator(prototype, componentName, p, accessor): 

flushSIO(sio, log) 

log.write(u'found %s\r\n'%sub.canonicalName) 

for res in self._traceBuild(accessor, p, sub, log): 

yield res 

flushSIO(sio, log) 

 

except: 

traceback.print_exc(file=sio) 

flushSIO(sio, log) 

finally: 

sys.stdout = _stdout