Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run_command at setuptools with options?

I have two custom commands at my setup.py file: create_tables and drop_tables:

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()

Command $ setup.py create_tables -r -dmain should run command drop_tables and create new tables at main database, but run_command method doesn't allow to provide options to the command. How to specify option database for drop_tables inside create_tables command?

like image 566
quasiyoke Avatar asked Jun 22 '14 17:06

quasiyoke


1 Answers

Right now I've used this hack:

cmd_obj = self.distribution.get_command_obj('drop_tables')
cmd_obj.database = self.database
self.run_command('drop_tables')
like image 138
quasiyoke Avatar answered Sep 18 '22 06:09

quasiyoke