Package Ganga :: Package GPIDev :: Package Streamers :: Module MigrationControl
[hide private]
[frames] | no frames]

Source Code for Module Ganga.GPIDev.Streamers.MigrationControl

  1  ################################################################################ 
  2  # Ganga Project. http://cern.ch/ganga 
  3  # 
  4  # $Id: MigrationControl.py,v 1.1 2008-07-17 16:40:56 moscicki Exp $ 
  5  ################################################################################ 
  6   
  7  import Ganga.Utility.logging 
  8  logger = Ganga.Utility.logging.getLogger() 
  9   
 10  import Ganga.Utility.Config 
 11  config = Ganga.Utility.Config.makeConfig('MigrationControl','migration of different job versions in the peristent repository') 
 12  ##config.addOption('migration','interactive','selecting plugins to be migrated interactively') 
 13  ##config.addOption('migration','allow','select all possible plugins for the migration') 
 14  config.addOption('migration','deny','no plugin migration') 
 15  config.addOption('display','compact','display limited number of choices for the interactive migration') 
 16  ## config.addOption('display','full','display complete number of choices for the interactive migration') 
 17   
 18  ################################################################################ 
 19  # class controlling migration process in case of incompatible schemas 
 20  # this module has to have an instance of this class called "migration" 
21 -class MigrationControl(object):
22 23 _choices_compact = { 24 '1' : 'Yes', 25 '2' : 'No', 26 '3' : 'Yes for All', 27 '4' : 'No for All'} 28 29 _choices_full = { 30 '1' : 'Yes', 31 '2' : 'No', 32 '3' : 'Yes for All', 33 '4' : 'No for All', 34 '5' : 'Yes for All with this version', 35 '6' : 'No for All with this version', 36 '7' : 'Yes for All with this name', 37 '8' : 'No for All with this name', 38 '9' : 'Yes for All within this category', 39 '10': 'No for All within this category'} 40
41 - class TreeNode(type({})):
42 ## helper class
43 - def __init__(self, subtree, flag = None):
44 self.flag = flag 45 super(MigrationControl.TreeNode, self).__init__(subtree)
46
47 - def is_allowed(self):
48 return self.flag
49
50 - def allow(self):
51 self.flag = True 52 # set allow flag in all children 53 for k in self: 54 self[k].allow()
55
56 - def deny(self):
57 self.flag = False 58 # set deny flag in all children 59 for k in self: 60 self[k].deny()
61
62 - def get(self, key, def_value = None):
63 if not def_value: 64 def_value = MigrationControl.TreeNode({}) 65 if not self.has_key(key): 66 self[key] = def_value 67 self[key].flag = self.flag ##child inherits flag 68 return self[key]
69
70 - def __init__(self, display = 'full'):
71 # dictionary where user answers are remembered 72 # by default silent migration for all possible plugins is not allowed 73 self._all_categs = self.TreeNode({}) 74 self.display = display
75
76 - def isAllowed(self, category, name, version, msg = ''):
77 """This method checks whether migration for the plugin called 'name' of version 'version' 78 from category 'category' is allowed or not. If migration is not explicitly allowed through 79 MigrationControl.migration object it will warn user with the message 'msg' and 80 ask for permission to allow migration. 81 """ 82 res = self._all_categs.get(category).get(name).get(version).is_allowed() 83 if res == None: 84 # ask for the user input 85 answer = self.getUserInput(msg) 86 if answer == 'Yes': 87 res = True 88 elif answer == 'No': 89 res = False 90 elif answer == 'Yes for All with this version': 91 self.allow(category = category, name = name, version = version) 92 res = True 93 elif answer == 'No for All with this version': 94 self.deny(category = category, name = name, version = version) 95 res = False 96 elif answer == 'Yes for All with this name': 97 self.allow(category = category, name = name) 98 res = True 99 elif answer == 'No for All with this name': 100 self.deny(category = category, name = name) 101 res = False 102 elif answer == 'Yes for All within this category': 103 self.allow(category = category) 104 res = True 105 elif answer == 'No for All within this category': 106 self.deny(category = category) 107 res = False 108 elif answer == 'Yes for All': 109 self.allow() 110 res = True 111 elif answer == 'No for All': 112 self.deny() 113 res = False 114 else: 115 # any other answer 116 res = True 117 return res
118
119 - def allow(self, category = None, name = None, version = None):
120 """This method allows migration. 121 allow() --> allow all 122 allow(category)--> allow all from 'category' 123 allow(category, name) --> allow for all versions of 'name' from 'category' 124 allow(category, name, version) --> allow for 'version' and 'name' from 'category' 125 """ 126 # allow flag takes precedence of deny flag 127 if category == None: 128 # allow all possible migration 129 self._all_categs.allow() 130 elif name == None: 131 # allow all in the category 132 self._all_categs.get(category).allow() 133 elif version == None: 134 # allow all versions 135 self._all_categs.get(category).get(name).allow() 136 else: 137 # allow particular version 138 self._all_categs.get(category).get(name).get(version).allow()
139
140 - def deny(self, category = None, name = None, version = None):
141 """This method denies migration. 142 deny() --> deny all 143 deny(category)--> deny all from 'category' 144 deny(category, name) --> deny for all versions of 'name' from 'category' 145 deny(category, name, version) --> deny for 'version' and 'name' from 'category' 146 """ 147 148 # deny is opposite to allow 149 if category == None: 150 # deny all possible migration 151 self._all_categs.deny() 152 elif name == None: 153 # deny all in the category 154 self._all_categs.get(category).deny() 155 elif version == None: 156 # deny all versions 157 self._all_categs.get(category).get(name).deny() 158 else: 159 # deny particular version 160 self._all_categs.get(category).get(name).get(version).deny()
161
162 - def getUserInput(self, msg):
163 """This method is for getting user permission for migration. 164 Args: msg - is the warning message. 165 """ 166 logger.warning(msg) 167 prompt = "Would you like to migrate the plugin(s)?\n" 168 prompt += "Once migrated they will be not backward compatible\n" 169 prompt += "Please make your choice:\n" 170 if self.display == 'compact': 171 choices = self._choices_compact 172 else: 173 choices = self._choices_full 174 chcs = map(int, choices.keys()) 175 chcs.sort() 176 chcs = map(str,chcs) 177 for k in chcs: 178 prompt += "%s : %s\n" % (k, choices[k]) 179 prompt += "Any other key == 'Yes'\n" 180 aid = raw_input(prompt) 181 return choices.get(aid,'')
182 183 184 migration = MigrationControl() 185 migration.display = config['display'] 186 if config['migration'] == 'allow': 187 migration.allow() 188 elif config['migration'] == 'deny': 189 migration.deny() 190 191 # list of migrated jobs 192 migrated_jobs = [] 193