2014-06-22 21 views
5

ho due comandi personalizzati a my setup.py file:create_tables e drop_tables:Come eseguire run_command con setuptools con opzioni?

class create_tables(command): 
    description = 'create DB tables' 

    user_options = [ 
     ('database=', 'd', 'which database configuration use'), 
     ('reset', 'r', 'reset all data previously'), 
    ] 

    def initialize_options(self): 
     command.initialize_options(self) 
     self.reset = False 

    def run(self): 
     if self.reset: 
      self.run_command('drop_tables') 
     else: 
      command.run(self) 
     from vk_relations import models 
     models.create_tables() 
     print 'Tables were created successfully' 


class drop_tables(command): 
    description = 'drop all created DB tables' 

    user_options = [ 
     ('database=', 'd', 'which database configuration use'), 
    ] 

    def run(self): 
     command.run(self) 
     answer = raw_input('Are you sure you want to clear all VK Relations data? (y/n): ') 
     if 'y' == answer: 
      from vk_relations import models 
      models.drop_tables() 
      print 'Tables were dropped successfully' 
     elif 'n' == answer: 
      quit() 
     else: 
      sys.exit() 

Comando $ setup.py create_tables -r -dmain dovrebbe eseguire il comando drop_tables e creare nuove tabelle a main database, ma run_command metodo non consente di fornire opzioni per il comando. Come specificare l'opzione database per drop_tables all'interno del comando create_tables?

risposta

1

In questo momento ho usato questo hack:

cmd_obj = self.distribution.get_command_obj('drop_tables') 
cmd_obj.database = self.database 
self.run_command('drop_tables')