migration.py
changeset 5430 ed8f71e244f8
parent 5426 0d4853a6e5ee
child 5436 2455ca3a2a3a
equal deleted inserted replaced
5427:f0ea91195ef6 5430:ed8f71e244f8
   279                 else:
   279                 else:
   280                     context[attr[4:]] = getattr(self, attr)
   280                     context[attr[4:]] = getattr(self, attr)
   281         return context
   281         return context
   282 
   282 
   283     def cmd_process_script(self, migrscript, funcname=None, *args, **kwargs):
   283     def cmd_process_script(self, migrscript, funcname=None, *args, **kwargs):
   284         """execute a migration script
   284         """execute a migration script in interactive mode
   285         in interactive mode,  display the migration script path, ask for
   285 
   286         confirmation and execute it if confirmed
   286         Display the migration script path, ask for confirmation and execute it
       
   287         if confirmed
       
   288 
       
   289         Context environment can have these variables defined:
       
   290         - __name__   : will be determine by funcname parameter
       
   291         - __file__   : is the name of the script if it exists
       
   292         - scriptargs : script arguments coming from command-line
       
   293 
       
   294         :param migrscript: name of the script
       
   295         :param funcname: defines __name__ internally (or use __main__)
       
   296         :params args: arguments of the script
       
   297         :keyword args: extra arguments of the script given by command-line
   287         """
   298         """
   288         migrscript = os.path.normpath(migrscript)
   299         migrscript = os.path.normpath(migrscript)
   289         if migrscript.endswith('.py'):
   300         if migrscript.endswith('.py'):
   290             script_mode = 'python'
   301             script_mode = 'python'
   291         elif migrscript.endswith('.txt') or migrscript.endswith('.rst'):
   302         elif migrscript.endswith(('.txt', '.rst')):
   292             script_mode = 'doctest'
   303             script_mode = 'doctest'
   293         else:
   304         else:
   294             raise Exception('This is not a valid cubicweb shell input')
   305             raise Exception('This is not a valid cubicweb shell input')
   295         if not self.execscript_confirm(migrscript):
   306         if not self.execscript_confirm(migrscript):
   296             return
   307             return
   298         if script_mode == 'python':
   309         if script_mode == 'python':
   299             if funcname is None:
   310             if funcname is None:
   300                 pyname = '__main__'
   311                 pyname = '__main__'
   301             else:
   312             else:
   302                 pyname = splitext(basename(migrscript))[0]
   313                 pyname = splitext(basename(migrscript))[0]
   303             scriptlocals.update({'__file__': migrscript, '__name__': pyname})
   314             scriptlocals.update({'__file__': migrscript, '__name__': pyname,
       
   315                                  'scriptargs': kwargs.pop("args", [])})
   304             execfile(migrscript, scriptlocals)
   316             execfile(migrscript, scriptlocals)
   305             if funcname is not None:
   317             if funcname is not None:
   306                 try:
   318                 try:
   307                     func = scriptlocals[funcname]
   319                     func = scriptlocals[funcname]
   308                     self.info('found %s in locals', funcname)
   320                     self.info('found %s in locals', funcname)