home *** CD-ROM | disk | FTP | other *** search
- #
- # installFactoryPatterns.py
- # JunkMatcher
- #
- # Created by Benjamin Han on 5/28/05.
- # Copyright (c) 2005 Benjamin Han. All rights reserved.
- #
-
- # This program is free software; you can redistribute it and/or
- # modify it under the terms of the GNU General Public License
- # as published by the Free Software Foundation; either version 2
- # of the License, or (at your option) any later version.
-
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
-
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
- #!/usr/bin/env python
-
- import sys, os.path, difflib
-
- from consts import *
- from Tests import *
- from MetaPatterns import *
- from comparePatterns import *
-
-
- def installFactoryPatterns (currentPatterns, currentMetaPatterns, currentTests):
- """Installs the factory versions of patterns and meta patterns. Returns True if
- any change occurs."""
-
- factoryPatterns = Patterns('%s%s' % (DEFAULTS_PATH, 'patterns'))
- factoryMetaPatterns = MetaPatterns('%s%s' % (DEFAULTS_PATH, 'metaPatterns'))
- factoryTests = Tests(Properties('%s%s' % (DEFAULTS_PATH, 'properties')),
- factoryPatterns,
- '%s%s' % (DEFAULTS_PATH, 'tests'))
-
- # ====== install patterns ======
- changeDelta, removeDelta, addDelta = comparePatterns(currentPatterns, factoryPatterns,
- currentTests, factoryTests)
-
- changed = len(changeDelta) or len(removeDelta) or len(addDelta)
-
- # change the current patterns
- for pat, pc in changeDelta.items():
- # don't worry about p.viewsInUse and p.metaPatterns - they'll be recomputed in the GUI
- p = currentPatterns[pat]
-
- action = pc.action
- if action == 'p':
- p.changePattern(pc.new)
- elif action == 'n':
- p.name = pc.new
- else:
- p.changePattern(pc.new[0])
- p.name = pc.new[1]
-
- # remove patterns - make sure we start from the test of the largest index!
- removeList = []
- for pat, idxList in filter(lambda i: i[1], removeDelta.items()):
- del currentPatterns[pat]
- removeList.extend(map(lambda i: (i, pat), idxList))
- removeList.sort()
- removeList.reverse()
-
- for idx, pat in removeList:
- del currentTests[idx]
-
- # add patterns - watch out for possibly duplicate user patterns!
- for pat, tList in filter(lambda i: i[1], addDelta.items()):
- if not pat in currentPatterns:
- # no duplicate user pattern found - safe to add the pattern
- currentPatterns[pat] = tList[0].propertyOrPattern
- for t in tList:
- currentTests.append(t)
-
-
- # ====== install meta patterns ======
- changeDelta, removeDelta, addDelta = compareMetaPatterns(currentMetaPatterns, factoryMetaPatterns)
-
- if not changed:
- changed = len(changeDelta) or len(removeDelta) or len(addDelta)
-
- mpInUseSet = sets.Set()
- for p in currentPatterns.values():
- mpInUseSet |= currentMetaPatterns.findAll(p.origPattern)
-
- # change the current meta patterns
- for oldName, pc in changeDelta.items():
- action = pc.action
- oldMP = currentMetaPatterns[oldName]
-
- if action == 'p':
- currentMetaPatterns[oldName] = (pc.new, ) + oldMP[1:]
- else:
- # can we remove the old name?
- if not oldName in mpInUseSet:
- del currentMetaPatterns[oldName]
-
- if action == 'n':
- newName = pc.new
- newPat = oldMP[0]
- else:
- newName = pc.new[1]
- newPat = pc.new[0]
-
- # can we add the new name?
- if not newName in currentMetaPatterns:
- currentMetaPatterns[newName] = (newPat, ) + oldMP[1:]
- else:
- # no; merge the two meta patterns into one
- anotherOldMP = currentMetaPatterns[newName]
- currentMetaPatterns[newName] = ('(?:%s|%s)' % (anotherOldMP[0], newPat), ) + anotherOldMP[1:]
-
- # remove meta patterns
- for oldName in removeDelta - mpInUseSet:
- del currentMetaPatterns[oldName]
-
- # add meta patterns
- for newName in addDelta:
- newMP = factoryMetaPatterns[newName]
-
- if newName in currentMetaPatterns:
- oldMP = currentMetaPatterns[newName]
- currentMetaPatterns[newName] = ('(?:%s|%s)' % (oldMP[0], newMP[0]), ) + oldMP[1:]
- else:
- currentMetaPatterns[newName] = newMP
-
- return changed
-
-
- if __name__ == '__main__':
- if len(sys.argv) != 2:
- print 'Usage: ./installFactoryPatterns.py <output path>'
- sys.exit(1)
-
- outputPath = sys.argv[1]
-
- currentProperties = Properties('%s%s' % (CONF_PATH, 'properties'))
- currentPatterns = Patterns('%s%s' % (CONF_PATH, 'patterns'))
- currentMetaPatterns = MetaPatterns('%s%s' % (CONF_PATH, 'metaPatterns'))
- currentTests = Tests(currentProperties,
- currentPatterns,
- '%s%s' % (CONF_PATH, 'tests'))
-
- changed = installFactoryPatterns(currentPatterns, currentMetaPatterns, currentTests)
-
- currentProperties.fn = os.path.join(outputPath, 'properties')
- currentPatterns.fn = os.path.join(outputPath, 'patterns')
- currentMetaPatterns.fn = os.path.join(outputPath, 'metaPatterns')
- currentTests.fn = os.path.join(outputPath, 'tests')
-
- if changed:
- currentProperties.writeToFile()
- currentPatterns.writeToFile()
- currentMetaPatterns.writeToFile()
- currentTests.writeToFile()
- print 'Patterns/meta patterns are changed.'
-
- else:
- print 'No change has occurred.'
-