home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / JunkMatcher 1.5.5.dmg / JunkMatcher.app / Contents / Resources / Engine / installFactoryPatterns.py < prev    next >
Encoding:
Python Source  |  2005-06-01  |  5.9 KB  |  167 lines

  1. #
  2. #  installFactoryPatterns.py
  3. #  JunkMatcher
  4. #
  5. #  Created by Benjamin Han on 5/28/05.
  6. #  Copyright (c) 2005 Benjamin Han. All rights reserved.
  7. #
  8.  
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13.  
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # GNU General Public License for more details.
  18.  
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  22.  
  23. #!/usr/bin/env python
  24.  
  25. import sys, os.path, difflib
  26.  
  27. from consts import *
  28. from Tests import *
  29. from MetaPatterns import *
  30. from comparePatterns import *
  31.  
  32.  
  33. def installFactoryPatterns (currentPatterns, currentMetaPatterns, currentTests):
  34.     """Installs the factory versions of patterns and meta patterns. Returns True if
  35.     any change occurs."""
  36.     
  37.     factoryPatterns = Patterns('%s%s' % (DEFAULTS_PATH, 'patterns'))
  38.     factoryMetaPatterns = MetaPatterns('%s%s' % (DEFAULTS_PATH, 'metaPatterns'))
  39.     factoryTests = Tests(Properties('%s%s' % (DEFAULTS_PATH, 'properties')),
  40.                          factoryPatterns,
  41.                          '%s%s' % (DEFAULTS_PATH, 'tests'))
  42.     
  43.     # ====== install patterns ======
  44.     changeDelta, removeDelta, addDelta = comparePatterns(currentPatterns, factoryPatterns,
  45.                                                          currentTests, factoryTests)
  46.  
  47.     changed = len(changeDelta) or len(removeDelta) or len(addDelta)
  48.  
  49.     # change the current patterns
  50.     for pat, pc in changeDelta.items():
  51.         # don't worry about p.viewsInUse and p.metaPatterns - they'll be recomputed in the GUI
  52.         p = currentPatterns[pat]
  53.         
  54.         action = pc.action
  55.         if action == 'p':
  56.             p.changePattern(pc.new)
  57.         elif action == 'n':
  58.             p.name = pc.new
  59.         else:
  60.             p.changePattern(pc.new[0])
  61.             p.name = pc.new[1]
  62.  
  63.     # remove patterns - make sure we start from the test of the largest index!
  64.     removeList = []
  65.     for pat, idxList in filter(lambda i: i[1], removeDelta.items()):
  66.         del currentPatterns[pat]
  67.         removeList.extend(map(lambda i: (i, pat), idxList))
  68.     removeList.sort()
  69.     removeList.reverse()
  70.     
  71.     for idx, pat in removeList:
  72.         del currentTests[idx]
  73.  
  74.     # add patterns - watch out for possibly duplicate user patterns!
  75.     for pat, tList in filter(lambda i: i[1], addDelta.items()):
  76.         if not pat in currentPatterns:
  77.             # no duplicate user pattern found - safe to add the pattern
  78.             currentPatterns[pat] = tList[0].propertyOrPattern
  79.             for t in tList:                
  80.                 currentTests.append(t)
  81.  
  82.             
  83.     # ====== install meta patterns ======
  84.     changeDelta, removeDelta, addDelta = compareMetaPatterns(currentMetaPatterns, factoryMetaPatterns)
  85.  
  86.     if not changed:
  87.         changed = len(changeDelta) or len(removeDelta) or len(addDelta)
  88.  
  89.     mpInUseSet = sets.Set()
  90.     for p in currentPatterns.values():
  91.         mpInUseSet |= currentMetaPatterns.findAll(p.origPattern)
  92.  
  93.     # change the current meta patterns
  94.     for oldName, pc in changeDelta.items():
  95.         action = pc.action
  96.         oldMP = currentMetaPatterns[oldName]
  97.         
  98.         if action == 'p':
  99.             currentMetaPatterns[oldName] = (pc.new, ) + oldMP[1:]
  100.         else:
  101.             # can we remove the old name?
  102.             if not oldName in mpInUseSet:
  103.                 del currentMetaPatterns[oldName]
  104.  
  105.             if action == 'n':
  106.                 newName = pc.new
  107.                 newPat = oldMP[0]
  108.             else:
  109.                 newName = pc.new[1]
  110.                 newPat = pc.new[0]
  111.  
  112.             # can we add the new name?
  113.             if not newName in currentMetaPatterns:
  114.                 currentMetaPatterns[newName] = (newPat, ) + oldMP[1:]
  115.             else:
  116.                 # no; merge the two meta patterns into one
  117.                 anotherOldMP = currentMetaPatterns[newName]
  118.                 currentMetaPatterns[newName] = ('(?:%s|%s)' % (anotherOldMP[0], newPat), ) + anotherOldMP[1:]
  119.  
  120.     # remove meta patterns
  121.     for oldName in removeDelta - mpInUseSet:
  122.         del currentMetaPatterns[oldName]
  123.  
  124.     # add meta patterns
  125.     for newName in addDelta:
  126.         newMP = factoryMetaPatterns[newName]
  127.         
  128.         if newName in currentMetaPatterns:
  129.             oldMP = currentMetaPatterns[newName]
  130.             currentMetaPatterns[newName] = ('(?:%s|%s)' % (oldMP[0], newMP[0]), ) + oldMP[1:]
  131.         else:
  132.             currentMetaPatterns[newName] = newMP
  133.  
  134.     return changed
  135.         
  136.  
  137. if __name__ == '__main__':
  138.     if len(sys.argv) != 2:
  139.         print 'Usage: ./installFactoryPatterns.py <output path>'
  140.         sys.exit(1)
  141.  
  142.     outputPath = sys.argv[1]
  143.  
  144.     currentProperties = Properties('%s%s' % (CONF_PATH, 'properties'))
  145.     currentPatterns = Patterns('%s%s' % (CONF_PATH, 'patterns'))
  146.     currentMetaPatterns = MetaPatterns('%s%s' % (CONF_PATH, 'metaPatterns'))
  147.     currentTests = Tests(currentProperties,
  148.                          currentPatterns,
  149.                          '%s%s' % (CONF_PATH, 'tests'))
  150.     
  151.     changed = installFactoryPatterns(currentPatterns, currentMetaPatterns, currentTests)
  152.  
  153.     currentProperties.fn = os.path.join(outputPath, 'properties')
  154.     currentPatterns.fn = os.path.join(outputPath, 'patterns')
  155.     currentMetaPatterns.fn = os.path.join(outputPath, 'metaPatterns')
  156.     currentTests.fn = os.path.join(outputPath, 'tests')
  157.  
  158.     if changed:
  159.         currentProperties.writeToFile()
  160.         currentPatterns.writeToFile()
  161.         currentMetaPatterns.writeToFile()
  162.         currentTests.writeToFile()
  163.         print 'Patterns/meta patterns are changed.'
  164.         
  165.     else:
  166.         print 'No change has occurred.'
  167.