def findMessages(mc,custid,count): print 'finding ' + str(count) + ' messages...' amsm = mc.getActiveMailboxStoreManager() msgs = HashMap() retries = 10 # 10 minutes while msgs.size() < count and retries > 0: sleep(60) retries = retries - 1 for p in mc.getPartitionManager().listPartitions(): if p.isReadOnly(): continue print 'searching for messages to be stored in',p for msg in amsm.findMessages([SearchConstraint(IActiveMailboxStoreManager.PROP_CUST_ID, SearchConstraintOperator.CONSTRAINT_EQUALS,int(custid))],p,True): msgs.put(msg.getMessageId(), msg) print 'found',msgs.size(),'messages',msgs.keySet() if msgs.isEmpty(): print 'Failed to find any messages in DB' raise Exception('Failed to find any messages in DB') if msgs.size() < count: print 'Warning, did not find all messages expected' return msgs.values()
def updateAttributes(self, context, configurationAttributes): print "Idp extension. Method: updateAttributes" attributeContext = context.getAttributeContext() customAttributes = HashMap() customAttributes.putAll(attributeContext.getIdPAttributes()) # Remove givenName attribute customAttributes.remove("givenName") # Update surname attribute if customAttributes.containsKey("sn"): customAttributes.get("sn").setValues( ArrayList(Arrays.asList(StringAttributeValue("Dummy")))) # Set updated attributes attributeContext.setIdPAttributes(customAttributes.values()) return True
def _addWebApp(self, appName, resourcePath, parentOsh, vHostDir, vHostJdbcOshMap, globalJdbcOshMap): webAppOsh = ObjectStateHolder('webapplication') webAppOsh.setAttribute('data_name', appName) webAppOsh.setAttribute('resource_path', resourcePath) webAppOsh.setContainer(parentOsh) self.OSHVResult.add(webAppOsh) appJdbcOshMap = HashMap(vHostJdbcOshMap) #report context and web config files logger.debug("report context and web config file for web application:", appName) for configFileName in ['context.xml', 'web.xml']: configFileToReport = self.createCF(webAppOsh, resourcePath + self.FileSeparator + "WEB-INF"+ self.FileSeparator + configFileName) if configFileToReport: logger.debug("found config file:", resourcePath + self.FileSeparator + "WEB-INF"+ self.FileSeparator + configFileName) self.OSHVResult.add(configFileToReport) appContextPath = vHostDir + appName + '.xml' if len(self.fileMonitor.getFilesInPath(vHostDir, appName + '.xml')) == 1: self.getContextJdbcResouces(appContextPath, appJdbcOshMap, globalJdbcOshMap) for appJdbcOsh in appJdbcOshMap.values(): link = modeling.createLinkOSH('usage', webAppOsh, appJdbcOsh) self.OSHVResult.add(link)
def create_0(cls, description, verbose): """ generated source for method create_0 """ print "Building propnet..." startTime = System.currentTimeMillis() description = GdlCleaner.run(description) description = DeORer.run(description) description = VariableConstrainer.replaceFunctionValuedVariables(description) description = Relationizer.run(description) description = CondensationIsolator.run(description) if verbose: for gdl in description: print gdl # We want to start with a rule graph and follow the rule graph. # Start by finding general information about the game model = SentenceDomainModelFactory.createWithCartesianDomains(description) # Restrict domains to values that could actually come up in rules. # See chinesecheckers4's "count" relation for an example of why this # could be useful. model = SentenceDomainModelOptimizer.restrictDomainsToUsefulValues(model) if verbose: print "Setting constants..." constantChecker = ConstantCheckerFactory.createWithForwardChaining(model) if verbose: print "Done setting constants" sentenceFormNames = SentenceForms.getNames(model.getSentenceForms()) usingBase = sentenceFormNames.contains("base") usingInput = sentenceFormNames.contains("input") # For now, we're going to build this to work on those with a # particular restriction on the dependency graph: # Recursive loops may only contain one sentence form. # This describes most games, but not all legal games. dependencyGraph = model.getDependencyGraph() if verbose: print "Computing topological ordering... ", System.out.flush() ConcurrencyUtils.checkForInterruption() topologicalOrdering = getTopologicalOrdering(model.getSentenceForms(), dependencyGraph, usingBase, usingInput) if verbose: print "done" roles = Role.computeRoles(description) components = HashMap() negations = HashMap() trueComponent = Constant(True) falseComponent = Constant(False) functionInfoMap = HashMap() completedSentenceFormValues = HashMap() for form in topologicalOrdering: ConcurrencyUtils.checkForInterruption() if verbose: print "Adding sentence form " + form, System.out.flush() if constantChecker.isConstantForm(form): if verbose: print " (constant)" # Only add it if it's important if form.__name__ == cls.LEGAL or form.__name__ == cls.GOAL or form.__name__ == cls.INIT: # Add it for trueSentence in constantChecker.getTrueSentences(form): trueProp.addInput(trueComponent) trueComponent.addOutput(trueProp) components.put(trueSentence, trueComponent) if verbose: print "Checking whether " + form + " is a functional constant..." addConstantsToFunctionInfo(form, constantChecker, functionInfoMap) addFormToCompletedValues(form, completedSentenceFormValues, constantChecker) continue if verbose: print # TODO: Adjust "recursive forms" appropriately # Add a temporary sentence form thingy? ... addSentenceForm(form, model, components, negations, trueComponent, falseComponent, usingBase, usingInput, Collections.singleton(form), temporaryComponents, temporaryNegations, functionInfoMap, constantChecker, completedSentenceFormValues) # TODO: Pass these over groups of multiple sentence forms if verbose and not temporaryComponents.isEmpty(): print "Processing temporary components..." processTemporaryComponents(temporaryComponents, temporaryNegations, components, negations, trueComponent, falseComponent) addFormToCompletedValues(form, completedSentenceFormValues, components) # if(verbose) # TODO: Add this, but with the correct total number of components (not just Propositions) # print " "+completedSentenceFormValues.get(form).size() + " components added"; # Connect "next" to "true" if verbose: print "Adding transitions..." addTransitions(components) # Set up "init" proposition if verbose: print "Setting up 'init' proposition..." setUpInit(components, trueComponent, falseComponent) # Now we can safely... removeUselessBasePropositions(components, negations, trueComponent, falseComponent) if verbose: print "Creating component set..." componentSet = HashSet(components.values()) # Try saving some memory here... components = None negations = None completeComponentSet(componentSet) ConcurrencyUtils.checkForInterruption() if verbose: print "Initializing propnet object..." # Make it look the same as the PropNetFactory results, until we decide # how we want it to look normalizePropositions(componentSet) propnet = PropNet(roles, componentSet) if verbose: print "Done setting up propnet; took " + (System.currentTimeMillis() - startTime) + "ms, has " + len(componentSet) + " components and " + propnet.getNumLinks() + " links" print "Propnet has " + propnet.getNumAnds() + " ands; " + propnet.getNumOrs() + " ors; " + propnet.getNumNots() + " nots" # print propnet; return propnet
def getActiveTransactions(self): activeTransactions = [] whereClauses = ArrayList() whereClauses.add("FUNCNAME IN ('SAPWL_TCODE_AGGREGATION','SAPWL_TCODE_AGGREGATION_COPY')"); result = self.executeQuery("TFDIR", whereClauses, "FUNCNAME")#@@CMD_PERMISION sap protocol execution functionName = None if result.next(): functionName = result.getString("FUNCNAME") if functionName == None: logger.warn('getActiveTransactions: active transaction function is not found') return activeTransactions day = self.__client.getProperty('from_date') if day == None: today = Date() sfDate = SimpleDateFormat("yyyyMMdd") day = sfDate.format(today) elif day.find('/') != -1: try: sfDate = SimpleDateFormat("MM/dd/yyyy") parsedDate = sfDate.parse(day) sfDate = SimpleDateFormat("yyyyMMdd") day = sfDate.format(parsedDate) except: logger.reportWarning('Failed to parse date ', day) logger.debug('Parsed start date:', day) logger.debug('Active transactions from data:', day) mapTransactionToUsers = None getUsers = Boolean.parseBoolean(self.__client.getProperty("get_users")) if getUsers: mapTransactionToUsers = HashMap() funcParams = HashMap() funcParams.put('READ_START_DATE', day) funcParams.put('READ_START_TIME', '000000') funcParams.put('READ_END_DATE', day) funcParams.put('READ_END_TIME', '235959') funcParams.put('READ_ONLY_MAINRECORDS', 'X') logger.debug('executing func:SAPWL_STATREC_FROM_REMOTE_SYS(', str(funcParams),')') fields = ArrayList() fields.add('TCODE') fields.add('ACCOUNT') usersResult = self.__client.executeFunction('SAPWL_STATREC_FROM_REMOTE_SYS', funcParams, 'NORMAL_RECORDS', fields) while usersResult.next(): transaction = usersResult.getString('TCODE') if len(transaction) > 0: user = usersResult.getString("ACCOUNT"); users = mapTransactionToUsers.get(transaction) if users == None: users = HashMap() mapTransactionToUsers.put(transaction,users) users.put(user,users); self.getSites() site = self.getSites().getCell(0,0) servers = self.getServers(site) numServers = servers.getRowCount() transactionToStats = HashMap() for j in range(numServers): try: instance = servers.getCell(j,0); logger.debug('getActiveTransactions:executing function[' + functionName + '] for instance [' + instance + ']') if functionName == 'SAPWL_TCODE_AGGREGATION_COPY': records = self.callSapwlTcodeAggregationCopy(instance,day) while records.next(): transaction = (str(records.getString(0))).strip() mapUsers = None if mapTransactionToUsers != None: mapUsers = mapTransactionToUsers.get(transaction) if (transaction != None) and (len(transaction) > 0): stats = transactionToStats.get(transaction) if stats == None: stats = TransactionStatistics(transaction) transactionToStats.put(transaction,stats) if mapUsers != None: stats.users = ArrayList(mapUsers.keySet()) if records.next(): stats.steps = stats.steps + int(float(records.getString(0))) if records.next(): stats.responseTime = stats.responseTime + int(float(records.getString(0))) if records.next(): stats.cpuTime = stats.cpuTime + int(float(records.getString(0))) if records.next(): stats.dbTime = stats.dbTime + int(float(records.getString(0))) if records.next(): stats.guiTime = stats.guiTime + int(float(records.getString(0))) if records.next(): stats.roundTrips = stats.roundTrips + int(float(records.getString(0))) if records.next(): stats.text = (str(records.getString(0))).strip() else: fields = ArrayList() fields.add('ENTRY_ID') fields.add('COUNT') fields.add('RESPTI') fields.add('CPUTI') fields.add('DBTIME') fields.add('GUITIME') fields.add('GUICNT') fields.add('TEXT') records = self.getApplicationStatistics(functionName, instance, day, fields) while records.next(): entryID = records.getString("ENTRY_ID"); transaction = self.getTransactionFromEntryID(entryID); mapUsers = None if mapTransactionToUsers != None: mapUsers = mapTransactionToUsers.get(transaction) if (transaction != None) and (len(transaction) > 0): stats = transactionToStats.get(transaction) if(stats == None): stats = TransactionStatistics(transaction) transactionToStats.put(transaction,stats) if(mapUsers != None): stats.users = ArrayList(mapUsers.keySet()) count = records.getString("COUNT") stats.steps = stats.steps + int(count) stats.responseTime = stats.responseTime + int(records.getString("RESPTI")) stats.cpuTime = stats.cpuTime + int(records.getString("CPUTI")) stats.dbTime = stats.dbTime + int(records.getString("DBTIME")) stats.guiTime = stats.guiTime + int(records.getString("GUITIME")) stats.roundTrips = stats.roundTrips + int(records.getString("GUICNT")) stats.text = records.getString("TEXT") except: msg = sys.exc_info()[1] strmsg = '%s' % msg if strmsg.find('NO_DATA_FOUND') != -1: logger.debug(strmsg) logger.reportWarning('No data found in the given time range') else: logger.debugException('Unexpected error getting transactions for function:' + str(functionName)) logger.reportWarning('Unexpected error getting transactions for function:' + str(functionName) + ':' + strmsg) transactions = ArrayList(transactionToStats.keySet()) logger.debug("getActiveTransactions: Found [" + str(transactions.size()) + "] active transactions") if logger.isDebugEnabled(): logger.debug("getActiveTransactions: transactions = " + str(transactions)) transactionsInfo = self.getTransactionsInfo(transactions) it = transactionToStats.values() for stats in it: prop = Properties() prop.setProperty('data_name', str(stats.transaction)) prop.setProperty('dialog_steps', str(stats.steps)) prop.setProperty('total_response_time', str(stats.responseTime)) prop.setProperty('average_response_time', str(stats.getAverageCPUTime())) prop.setProperty('total_cpu_time', str(stats.cpuTime)) prop.setProperty('average_cpu_time', str(stats.getAverageCPUTime())) prop.setProperty('round_trips', str(stats.roundTrips)) prop.setProperty('total_db_time', str(stats.dbTime)) prop.setProperty('average_db_time', str(stats.getAverageDBTime())) prop.setProperty('total_gui_time', str(stats.guiTime)) prop.setProperty('average_gui_time', str(stats.getAverageGUITime())) prop.setProperty('text', stats.text) prop.setProperty('saptransaction_averagedbtime', str(stats.users.size())) info = transactionsInfo.get(stats.transaction) if info != None: prop.setProperty('devclass', info.devclass) prop.setProperty('program', info.program) prop.setProperty('screen', info.screen) prop.setProperty('', info.screen) else: prop.setProperty('devclass', "") prop.setProperty('program', "") prop.setProperty('screen', "") prop.setProperty('version', "") activeTransactions.append(prop) return activeTransactions
def getJdbcResources(self, env, jdbcOshMap, globalJdbcResources=None): if env is None: return jdbcResources = HashMap() resources = env.getChildren('Resource') for resource in resources: name = resource.getAttributeValue('name') dsType = resource.getAttributeValue('type') driverClassName = resource.getAttributeValue('driverClassName') url = resource.getAttributeValue('url') maxActive = resource.getAttributeValue('maxActive') logger.debug('Found jdbc datasource ', name, ' driver ', str(driverClassName), ' url ', str(url)) jdbcResources.put(name, JdbcResource(name, dsType, driverClassName, url, maxActive)) for resource in resources: name = resource.getAttributeValue('name') if name is None: continue # do not read additional parameters for non-existing resource jdbcResource = jdbcResources.get(name) if jdbcResource is None: continue # update existing JDBC resource with absent parameters data for resourceParamsEl in env.getChildren('ResourceParams'): if resourceParamsEl.getAttributeValue('name') == name: resourceParams = self.getResourceParamsValues(resourceParamsEl) dsType = resourceParams.get('type') if (dsType is not None) and (jdbcResource.type is None): jdbcResource.type = dsType driverClassName = resourceParams.get('driverClassName') if (driverClassName is not None) and (jdbcResource.driverClass is None): jdbcResource.driverClass = driverClassName url = resourceParams.get('url') if (url is not None) and (jdbcResource.url is None): jdbcResource.url = url maxActive = resourceParams.get('maxActive') if (maxActive is not None) and (jdbcResource.maxActive is None): jdbcResource.maxActive = maxActive if jdbcResource.type != 'javax.sql.DataSource': jdbcResources.remove(name) resources = env.getChildren('ResourceLink') for resource in resources: name = resource.getAttributeValue('name') globalName = resource.getAttributeValue('global') dsType = resource.getAttributeValue('type') logger.debug('Found resource link ', name, ' for global name ', globalName, ' of type ', dsType) if dsType != 'javax.sql.DataSource': continue if globalJdbcResources is not None: jdbcResource = globalJdbcResources.get(globalName) if jdbcResource is None: continue logger.debug('Found jdbc datastore with global name ', globalName) jdbcOshMap.put(name, jdbcResource) dnsResolver = _DnsResolverDecorator(netutils.JavaDnsResolver(), self.destinationIp) reporter = jdbcModule.DnsEnabledJdbcTopologyReporter( jdbcModule.DataSourceBuilder(), dnsResolver) class Container: def __init__(self, osh): self.osh = osh def getOsh(self): return self.osh container = Container(self.tomcatOsh) for jdbc in jdbcResources.values(): datasource = jdbcModule.Datasource(jdbc.name, jdbc.url, driverClass=jdbc.driverClass) self.OSHVResult.addAll(reporter.reportDatasources(container, datasource)) jdbcOshMap.put(jdbc.name, datasource.getOsh())
def getJdbcResources(self, env, jdbcOshMap, globalJdbcResources=None): if env is None: return jdbcResources = HashMap() resources = env.getChildren('Resource') for resource in resources: name = resource.getAttributeValue('name') dsType = resource.getAttributeValue('type') driverClassName = resource.getAttributeValue('driverClassName') url = resource.getAttributeValue('url') maxActive = resource.getAttributeValue('maxActive') logger.debug('Found jdbc datasource ', name, ' driver ', str(driverClassName), ' url ', str(url)) jdbcResources.put( name, JdbcResource(name, dsType, driverClassName, url, maxActive)) for resource in resources: name = resource.getAttributeValue('name') if name is None: continue # do not read additional parameters for non-existing resource jdbcResource = jdbcResources.get(name) if jdbcResource is None: continue # update existing JDBC resource with absent parameters data for resourceParamsEl in env.getChildren('ResourceParams'): if resourceParamsEl.getAttributeValue('name') == name: resourceParams = self.getResourceParamsValues( resourceParamsEl) dsType = resourceParams.get('type') if (dsType is not None) and (jdbcResource.type is None): jdbcResource.type = dsType driverClassName = resourceParams.get('driverClassName') if (driverClassName is not None) and ( jdbcResource.driverClass is None): jdbcResource.driverClass = driverClassName url = resourceParams.get('url') if (url is not None) and (jdbcResource.url is None): jdbcResource.url = url maxActive = resourceParams.get('maxActive') if (maxActive is not None) and (jdbcResource.maxActive is None): jdbcResource.maxActive = maxActive if jdbcResource.type != 'javax.sql.DataSource': jdbcResources.remove(name) resources = env.getChildren('ResourceLink') for resource in resources: name = resource.getAttributeValue('name') globalName = resource.getAttributeValue('global') dsType = resource.getAttributeValue('type') logger.debug('Found resource link ', name, ' for global name ', globalName, ' of type ', dsType) if dsType != 'javax.sql.DataSource': continue if globalJdbcResources is not None: jdbcResource = globalJdbcResources.get(globalName) if jdbcResource is None: continue logger.debug('Found jdbc datastore with global name ', globalName) jdbcOshMap.put(name, jdbcResource) dnsResolver = _DnsResolverDecorator(netutils.JavaDnsResolver(), self.destinationIp) reporter = jdbcModule.DnsEnabledJdbcTopologyReporter( jdbcModule.DataSourceBuilder(), dnsResolver) class Container: def __init__(self, osh): self.osh = osh def getOsh(self): return self.osh container = Container(self.tomcatOsh) for jdbc in jdbcResources.values(): datasource = jdbcModule.Datasource(jdbc.name, jdbc.url, driverClass=jdbc.driverClass) self.OSHVResult.addAll( reporter.reportDatasources(container, datasource)) jdbcOshMap.put(jdbc.name, datasource.getOsh())
class FunType(Type): """ generated source for class FunType """ #@__init__.register(object, FunctionDef, Scope) def __init__(self, func=None, env=None): self.arrows = HashMap() self.cls = None self.defaultTypes = List() self.selfType = None self.func = None from pysonarsq.java.Analyzer import Analyzer super(FunType, self).__init__() self.env = None if isinstance(func, FunctionDef): self.func = func self.env = env elif isinstance(func, Type): from_, to = func, env self.addMapping(from_, to) self.getTable().addSuper(Analyzer.self.builtins.BaseFunction.getTable()) self.getTable().setPath(Analyzer.self.builtins.BaseFunction.getTable().getPath()) def addMapping(self, from_, to): if len(self.arrows) < 5: self.arrows[from_] = to oldArrows = self.arrows; self.arrows = self.compressArrows(self.arrows) if len(str(self)) > 900: self.arrows = oldArrows def getMapping(self, from_): return self.arrows.get(from_) def getReturnType(self): from pysonarsq.java.Analyzer import Analyzer if not _.isEmpty(self.arrows): #return self.arrows.values().iterator().next() return self.arrows.values()[0] else: return Analyzer.self.builtins.unknown def getFunc(self): return self.func def getEnv(self): return self.env def getCls(self): return self.cls def setCls(self, cls): self.cls = cls def getSelfType(self): return self.selfType def setSelfType(self, selfType): self.selfType = selfType def clearSelfType(self): self.selfType = None def getDefaultTypes(self): return self.defaultTypes def setDefaultTypes(self, defaultTypes): self.defaultTypes = defaultTypes def __eq__(self, other): if isinstance(other, (FunType, )): fo = other # cast in java code, should have been picked up by java2python return fo.getTable().getPath() == self.getTable().getPath() or self is other else: return False @classmethod def removeNoneReturn(cls, toType): from pysonarsq.java.Analyzer import Analyzer """ generated source for method removeNoneReturn """ if toType.isUnionType(): types.remove(Analyzer.self.builtins.Cont) return UnionType.newUnion(types) else: return toType def hashCode(self): return hash("FunType") def subsumed(self, type1, type2): return self.subsumedInner(type1, type2, TypeStack()) def subsumedInner(self, type1, type2, typeStack): from pysonarsq.java.Analyzer import Analyzer if typeStack.contains(type1, type2): return True if type1.isUnknownType() or type1 == Analyzer.self.builtins.None_ or type1 == type2: return True if isinstance(type1, (TupleType, )) and isinstance(type2, (TupleType, )): elems1 = type1.getElementTypes(); elems2 = type2.getElementTypes(); if len(elems1) == len(elems2): typeStack.push(type1, type2) for i in range(len(elems1)): if not self.subsumedInner(elems1[i], elems2[i], self.typeStack): self.typeStack.pop(type1, type2) return False return True return False def compressArrows(self, arrows): ret = HashMap() for e1 in arrows.items(): subsumed = False for e2 in arrows.items(): if e1 != e2 and self.subsumed(e1[0], e2[0]): subsumed = True break if not subsumed: ret[e1[0]] = e1[1] return ret def printType(self, ctr): from pysonarsq.java.Analyzer import Analyzer if _.isEmpty(self.arrows): return "? -> ?" sb = [] num = ctr.visit(self) if num is not None: sb.append("#") sb.append(num) else: newNum = ctr.push(self); i = 0; seen = set() for e in self.arrows.items(): as_ = e[0].printType(ctr) + " -> " + e[1].printType(ctr); if not as_ in seen: if i != 0: if Analyzer.self.multilineFunType: sb.append("\n| ") else: sb.append(" | ") sb.append(as_) seen.add(as_) i += 1 if ctr.isUsed(self): sb.append("=#") sb.append(newNum) sb.append(": ") ctr.pop(self) return ''.join(map(str,sb))
class Scope(object): #@overloaded def __init__(self, parent=None, type_=None): # stays null for most scopes (mem opt) # all are non-null except global table # link to the closest non-class scope, for lifting functions out # .... if not hasattr(self, 'scopeType'): self.scopeType = None self.supers = List() self.globalNames = Set() self.type_ = None self.path = "" self.parent = parent self.setScopeType(type_) self.forwarding = None self.table = Map() if isinstance(parent, Scope) and type_ is None: s = parent # case of creating a new scope from an existing one if s.table is not None: self.table = HashMap() self.table.update(s.table) self.parent = s.parent self.setScopeType(s.scopeType) self.forwarding = s.forwarding self.supers = s.supers self.globalNames = s.globalNames self.type_ = s.type_ self.path = s.path elif parent is not None: self.parent = parent self.setScopeType(type_) if type_ == self.ScopeType.CLASS: self.forwarding = (None if parent is None else parent.getForwarding()) else: self.forwarding = self # erase and overwrite this to s's contents def overwrite(self, s): self.table = s.table self.parent = s.parent self.setScopeType(s.scopeType) self.forwarding = s.forwarding self.supers = s.supers self.globalNames = s.globalNames self.type_ = s.type_ self.path = s.path def copy(self): return Scope(self) #@overloaded def _merge(self, other): for e1 in self.getInternalTable().items(): b1 = e1[1] b2 = other.getInternalTable().get(e1[0]) # both branch have the same name, need merge if b2 is not None and b1 != b2: b1 += b2 for e2 in other.getInternalTable().items(): b1 = self.getInternalTable().get(e2[0]); b2 = e2[1] # both branch have the same name, need merge if b1 is None and b1 != b2: self.update(e2[0], b2) return self @classmethod def merge(cls, scope1, scope2=None): if scope2 is not None: return scope1._merge(scope2) else: ret = scope1.copy() ret.merge(scope2) return ret def setParent(self, parent): if parent is not None: self.parent = parent def getParent(self): return self.parent def getForwarding(self): if self.forwarding is not None: return self.forwarding else: return self def addSuper(self, sup): if self.supers is None: self.supers = ArrayList() self.supers.append(sup) def setScopeType(self, type_): if type_ is not None: self.scopeType = type_ def getScopeType(self): return self.scopeType def addGlobalName(self, name): if self.globalNames is None: self.globalNames = HashSet() self.globalNames.add(name) def isGlobalName(self, name): if self.globalNames is not None: return name in self.globalNames elif self.parent is not None: return self.parent.isGlobalName(name) else: return False def remove(self, _id): if self.table is not None: if _id in self.table: del self.table[_id] # create new binding and insert def insert(self, id, node, type_, kind): b = Binding(id, node, type_, kind) if type_.isModuleType(): b.setQname(type_.asModuleType().getQname()) else: b.setQname(self.extendPath(id)) self.update(id, b) # directly insert a given binding #@overloaded def update(self, id, bs): if hasattr(bs, '__len__'): self.getInternalTable()[id] = bs else: bs = [bs] self.getInternalTable()[id] = bs return bs def setPath(self, path): self.path = path def getPath(self): return self.path def getType(self): return self.type_ def setType(self, type_): self.type_ = type_ # # * Look up a name in the current symbol table only. Don't recurse on the # * parent table. # def lookupLocal(self, name): if self.table is None: return None else: return self.table.get(name) # # * Look up a name (String) in the current symbol table. If not found, # * recurse on the parent table. # def lookup(self, name): b = self.getModuleBindingIfGlobal(name) if b is not None: return b else: ent = self.lookupLocal(name) if ent is not None: return ent elif self.getParent() is not None: return self.getParent().lookup(name) else: return None # # * Look up a name in the module if it is declared as global, otherwise look # * it up locally. # def lookupScope(self, name): b = self.getModuleBindingIfGlobal(name) if b is not None: return b else: return self.lookupLocal(name) # # * Look up an attribute in the type hierarchy. Don't look at parent link, # * because the enclosing scope may not be a super class. The search is # * "depth first, left to right" as in Python's (old) multiple inheritance # * rule. The new MRO can be implemented, but will probably not introduce # * much difference. # looked = HashSet() # circularity prevention def lookupAttr(self, attr): if self in self.looked: return None else: b = self.lookupLocal(attr); if b is not None: return b else: if self.supers is not None and len(self.supers): self.looked.add(self) for p in self.supers: b = p.lookupAttr(attr) if b is not None: self.looked.remove(self) return b self.looked.remove(self) return None else: return None # # * Look for a binding named {@code name} and if found, return its type. # def lookupType(self, name): bs = self.lookup(name) if bs is None: return None else: return self.makeUnion(bs) # # * Look for a attribute named {@code attr} and if found, return its type. # def lookupAttrType(self, attr): bs = self.lookupAttr(attr) if bs is None: return None else: return self.makeUnion(bs) @classmethod def makeUnion(cls, bs): from pysonarsq.java.Analyzer import Analyzer t = Analyzer.self.builtins.unknown for b in bs: t = UnionType.union(t, b.getType()) return t # # * Find a symbol table of a certain type in the enclosing scopes. # def getSymtabOfType(self, type_): if self.scopeType == type_: return self elif self.parent is None: return None else: return self.parent.getSymtabOfType(type_) # # * Returns the global scope (i.e. the module scope for the current module). # def getGlobalTable(self): result = self.getSymtabOfType(self.ScopeType.MODULE) if result is not None: return result else: _.die("Couldn't find global table. Shouldn't happen") return self # # * If {@code name} is declared as a global, return the module binding. # def getModuleBindingIfGlobal(self, name): if self.isGlobalName(name): module_ = self.getGlobalTable(); if module_ != self: return module_.lookupLocal(name) return None def putAll(self, other): self.getInternalTable().update(other.getInternalTable()) def keySet(self): if self.table is not None: return self.table.items() else: return set() def values(self): if self.table is not None: ret = list() for bs in self.table.values(): ret += bs return ret return Collections.emptySet() def entrySet(self): if self.table is not None: return self.table.items() return set() def isEmpty(self): return self.table is None or self.table.isEmpty() def extendPath(self, name): name = _.moduleName(name) if self.path == "": return name return self.path + "." + name def getInternalTable(self): if self.table is None: self.table = HashMap() return self.table def __str__(self): return "<Scope:" + str(self.getScopeType()) + ":" + str("{}" if self.table is None else self.table.items()) + ">"
class FunType(Type): """ generated source for class FunType """ #@__init__.register(object, FunctionDef, Scope) def __init__(self, func=None, env=None): self.arrows = HashMap() self.cls = None self.defaultTypes = List() self.selfType = None self.func = None from pysonarsq.java.Analyzer import Analyzer super(FunType, self).__init__() self.env = None if isinstance(func, FunctionDef): self.func = func self.env = env elif isinstance(func, Type): from_, to = func, env self.addMapping(from_, to) self.getTable().addSuper( Analyzer.self.builtins.BaseFunction.getTable()) self.getTable().setPath( Analyzer.self.builtins.BaseFunction.getTable().getPath()) def addMapping(self, from_, to): if len(self.arrows) < 5: self.arrows[from_] = to oldArrows = self.arrows self.arrows = self.compressArrows(self.arrows) if len(str(self)) > 900: self.arrows = oldArrows def getMapping(self, from_): return self.arrows.get(from_) def getReturnType(self): from pysonarsq.java.Analyzer import Analyzer if not _.isEmpty(self.arrows): #return self.arrows.values().iterator().next() return self.arrows.values()[0] else: return Analyzer.self.builtins.unknown def getFunc(self): return self.func def getEnv(self): return self.env def getCls(self): return self.cls def setCls(self, cls): self.cls = cls def getSelfType(self): return self.selfType def setSelfType(self, selfType): self.selfType = selfType def clearSelfType(self): self.selfType = None def getDefaultTypes(self): return self.defaultTypes def setDefaultTypes(self, defaultTypes): self.defaultTypes = defaultTypes def __eq__(self, other): if isinstance(other, (FunType, )): fo = other # cast in java code, should have been picked up by java2python return fo.getTable().getPath() == self.getTable().getPath( ) or self is other else: return False @classmethod def removeNoneReturn(cls, toType): from pysonarsq.java.Analyzer import Analyzer """ generated source for method removeNoneReturn """ if toType.isUnionType(): types.remove(Analyzer.self.builtins.Cont) return UnionType.newUnion(types) else: return toType def hashCode(self): return hash("FunType") def subsumed(self, type1, type2): return self.subsumedInner(type1, type2, TypeStack()) def subsumedInner(self, type1, type2, typeStack): from pysonarsq.java.Analyzer import Analyzer if typeStack.contains(type1, type2): return True if type1.isUnknownType( ) or type1 == Analyzer.self.builtins.None_ or type1 == type2: return True if isinstance(type1, (TupleType, )) and isinstance(type2, (TupleType, )): elems1 = type1.getElementTypes() elems2 = type2.getElementTypes() if len(elems1) == len(elems2): typeStack.push(type1, type2) for i in range(len(elems1)): if not self.subsumedInner(elems1[i], elems2[i], self.typeStack): self.typeStack.pop(type1, type2) return False return True return False def compressArrows(self, arrows): ret = HashMap() for e1 in arrows.items(): subsumed = False for e2 in arrows.items(): if e1 != e2 and self.subsumed(e1[0], e2[0]): subsumed = True break if not subsumed: ret[e1[0]] = e1[1] return ret def printType(self, ctr): from pysonarsq.java.Analyzer import Analyzer if _.isEmpty(self.arrows): return "? -> ?" sb = [] num = ctr.visit(self) if num is not None: sb.append("#") sb.append(num) else: newNum = ctr.push(self) i = 0 seen = set() for e in self.arrows.items(): as_ = e[0].printType(ctr) + " -> " + e[1].printType(ctr) if not as_ in seen: if i != 0: if Analyzer.self.multilineFunType: sb.append("\n| ") else: sb.append(" | ") sb.append(as_) seen.add(as_) i += 1 if ctr.isUsed(self): sb.append("=#") sb.append(newNum) sb.append(": ") ctr.pop(self) return ''.join(map(str, sb))
class Scope(object): #@overloaded def __init__(self, parent=None, type_=None): # stays null for most scopes (mem opt) # all are non-null except global table # link to the closest non-class scope, for lifting functions out # .... if not hasattr(self, 'scopeType'): self.scopeType = None self.supers = List() self.globalNames = Set() self.type_ = None self.path = "" self.parent = parent self.setScopeType(type_) self.forwarding = None self.table = Map() if isinstance(parent, Scope) and type_ is None: s = parent # case of creating a new scope from an existing one if s.table is not None: self.table = HashMap() self.table.update(s.table) self.parent = s.parent self.setScopeType(s.scopeType) self.forwarding = s.forwarding self.supers = s.supers self.globalNames = s.globalNames self.type_ = s.type_ self.path = s.path elif parent is not None: self.parent = parent self.setScopeType(type_) if type_ == self.ScopeType.CLASS: self.forwarding = (None if parent is None else parent.getForwarding()) else: self.forwarding = self # erase and overwrite this to s's contents def overwrite(self, s): self.table = s.table self.parent = s.parent self.setScopeType(s.scopeType) self.forwarding = s.forwarding self.supers = s.supers self.globalNames = s.globalNames self.type_ = s.type_ self.path = s.path def copy(self): return Scope(self) #@overloaded def _merge(self, other): for e1 in self.getInternalTable().items(): b1 = e1[1] b2 = other.getInternalTable().get(e1[0]) # both branch have the same name, need merge if b2 is not None and b1 != b2: b1 += b2 for e2 in other.getInternalTable().items(): b1 = self.getInternalTable().get(e2[0]) b2 = e2[1] # both branch have the same name, need merge if b1 is None and b1 != b2: self.update(e2[0], b2) return self @classmethod def merge(cls, scope1, scope2=None): if scope2 is not None: return scope1._merge(scope2) else: ret = scope1.copy() ret.merge(scope2) return ret def setParent(self, parent): if parent is not None: self.parent = parent def getParent(self): return self.parent def getForwarding(self): if self.forwarding is not None: return self.forwarding else: return self def addSuper(self, sup): if self.supers is None: self.supers = ArrayList() self.supers.append(sup) def setScopeType(self, type_): if type_ is not None: self.scopeType = type_ def getScopeType(self): return self.scopeType def addGlobalName(self, name): if self.globalNames is None: self.globalNames = HashSet() self.globalNames.add(name) def isGlobalName(self, name): if self.globalNames is not None: return name in self.globalNames elif self.parent is not None: return self.parent.isGlobalName(name) else: return False def remove(self, _id): if self.table is not None: if _id in self.table: del self.table[_id] # create new binding and insert def insert(self, id, node, type_, kind): b = Binding(id, node, type_, kind) if type_.isModuleType(): b.setQname(type_.asModuleType().getQname()) else: b.setQname(self.extendPath(id)) self.update(id, b) # directly insert a given binding #@overloaded def update(self, id, bs): if hasattr(bs, '__len__'): self.getInternalTable()[id] = bs else: bs = [bs] self.getInternalTable()[id] = bs return bs def setPath(self, path): self.path = path def getPath(self): return self.path def getType(self): return self.type_ def setType(self, type_): self.type_ = type_ # # * Look up a name in the current symbol table only. Don't recurse on the # * parent table. # def lookupLocal(self, name): if self.table is None: return None else: return self.table.get(name) # # * Look up a name (String) in the current symbol table. If not found, # * recurse on the parent table. # def lookup(self, name): b = self.getModuleBindingIfGlobal(name) if b is not None: return b else: ent = self.lookupLocal(name) if ent is not None: return ent elif self.getParent() is not None: return self.getParent().lookup(name) else: return None # # * Look up a name in the module if it is declared as global, otherwise look # * it up locally. # def lookupScope(self, name): b = self.getModuleBindingIfGlobal(name) if b is not None: return b else: return self.lookupLocal(name) # # * Look up an attribute in the type hierarchy. Don't look at parent link, # * because the enclosing scope may not be a super class. The search is # * "depth first, left to right" as in Python's (old) multiple inheritance # * rule. The new MRO can be implemented, but will probably not introduce # * much difference. # looked = HashSet() # circularity prevention def lookupAttr(self, attr): if self in self.looked: return None else: b = self.lookupLocal(attr) if b is not None: return b else: if self.supers is not None and len(self.supers): self.looked.add(self) for p in self.supers: b = p.lookupAttr(attr) if b is not None: self.looked.remove(self) return b self.looked.remove(self) return None else: return None # # * Look for a binding named {@code name} and if found, return its type. # def lookupType(self, name): bs = self.lookup(name) if bs is None: return None else: return self.makeUnion(bs) # # * Look for a attribute named {@code attr} and if found, return its type. # def lookupAttrType(self, attr): bs = self.lookupAttr(attr) if bs is None: return None else: return self.makeUnion(bs) @classmethod def makeUnion(cls, bs): from pysonarsq.java.Analyzer import Analyzer t = Analyzer.self.builtins.unknown for b in bs: t = UnionType.union(t, b.getType()) return t # # * Find a symbol table of a certain type in the enclosing scopes. # def getSymtabOfType(self, type_): if self.scopeType == type_: return self elif self.parent is None: return None else: return self.parent.getSymtabOfType(type_) # # * Returns the global scope (i.e. the module scope for the current module). # def getGlobalTable(self): result = self.getSymtabOfType(self.ScopeType.MODULE) if result is not None: return result else: _.die("Couldn't find global table. Shouldn't happen") return self # # * If {@code name} is declared as a global, return the module binding. # def getModuleBindingIfGlobal(self, name): if self.isGlobalName(name): module_ = self.getGlobalTable() if module_ != self: return module_.lookupLocal(name) return None def putAll(self, other): self.getInternalTable().update(other.getInternalTable()) def keySet(self): if self.table is not None: return self.table.items() else: return set() def values(self): if self.table is not None: ret = list() for bs in self.table.values(): ret += bs return ret return Collections.emptySet() def entrySet(self): if self.table is not None: return self.table.items() return set() def isEmpty(self): return self.table is None or self.table.isEmpty() def extendPath(self, name): name = _.moduleName(name) if self.path == "": return name return self.path + "." + name def getInternalTable(self): if self.table is None: self.table = HashMap() return self.table def __str__(self): return "<Scope:" + str(self.getScopeType()) + ":" + str( "{}" if self.table is None else self.table.items()) + ">"