Module manage
[hide private]
[frames] | no frames]

Source Code for Module manage

  1  #!/usr/bin/env python 
  2   
  3  import argparse 
  4  import os 
  5  import subprocess 
  6   
  7  import flask 
  8  from flask.ext.script import Manager, Command, Option, Group 
  9   
 10  from coprs import app 
 11  from coprs import db 
 12  from coprs import exceptions 
 13  from coprs import models 
 14  from coprs.logic import coprs_logic 
 15   
16 -class TestCommand(Command):
17 - def run(self, test_args):
18 os.environ['COPRS_ENVIRON_UNITTEST'] = '1' 19 os.environ['COPR_CONFIG'] = '/etc/copr/copr_unit_test.conf' 20 os.environ['PYTHONPATH'] = '.' 21 subprocess.call(['py.test'] + (test_args or []))
22 23 option_list = ( 24 Option('-a', 25 dest='test_args', 26 nargs=argparse.REMAINDER), 27 )
28
29 -class CreateSqliteFileCommand(Command):
30 'Create the sqlite DB file (not the tables). Used for alembic, "create_db" does this automatically.'
31 - def run(self):
32 if flask.current_app.config['SQLALCHEMY_DATABASE_URI'].startswith('sqlite'): 33 # strip sqlite:/// 34 datadir_name = os.path.dirname(flask.current_app.config['SQLALCHEMY_DATABASE_URI'][10:]) 35 if not os.path.exists(datadir_name): 36 os.makedirs(datadir_name)
37
38 -class CreateDBCommand(Command):
39 'Create the DB scheme'
40 - def run(self, alembic_ini=None):
41 CreateSqliteFileCommand().run() 42 db.create_all() 43 44 # load the Alembic configuration and generate the 45 # version table, "stamping" it with the most recent rev: 46 from alembic.config import Config 47 from alembic import command 48 alembic_cfg = Config(alembic_ini) 49 command.stamp(alembic_cfg, "head")
50 51 option_list = ( 52 Option('--alembic', 53 '-f', 54 dest='alembic_ini', 55 help='Path to the alembic configuration file (alembic.ini)', 56 required=True), 57 )
58
59 -class DropDBCommand(Command):
60 'Delete DB'
61 - def run(self):
62 db.drop_all()
63
64 -class ChrootCommand(Command):
65 - def print_invalid_format(self, chroot_name):
66 print '{0} - invalid chroot format, must be "{release}-{version}-{arch}".'.format(chroot_name)
67
68 - def print_already_exists(self, chroot_name):
69 print '{0} - already exists.'.format(chroot_name)
70
71 - def print_doesnt_exist(self, chroot_name):
72 print '{0} - chroot doesn\'t exist.'.format(chroot_name)
73 74 75 option_list = ( 76 Option('chroot_names', 77 help='Chroot name, e.g. fedora-18-x86_64.', 78 nargs='+'), 79 )
80
81 -class CreateChrootCommand(ChrootCommand):
82 'Creates a mock chroot in DB'
83 - def run(self, chroot_names):
92
93 -class AlterChrootCommand(ChrootCommand):
94 'Activates or deactivates a chroot'
95 - def run(self, chroot_names, action):
96 activate = (action == 'activate') 97 for chroot_name in chroot_names: 98 try: 99 coprs_logic.MockChrootsLogic.edit_by_name(None, chroot_name, activate) 100 db.session.commit() 101 except exceptions.MalformedArgumentException: 102 self.print_invalid_format(chroot_name) 103 except exceptions.NotFoundException: 104 self.print_doesnt_exist(chroot_name)
105 106 option_list = ChrootCommand.option_list + ( 107 Option('--action', 108 '-a', 109 dest='action', 110 help='Action to take - currently activate or deactivate', 111 choices=['activate', 'deactivate'], 112 required=True), 113 )
114
115 -class DropChrootCommand(ChrootCommand):
116 'Activates or deactivates a chroot'
117 - def run(self, chroot_names):
126
127 -class DisplayChrootsCommand(Command):
128 'Displays current mock chroots'
129 - def run(self, active_only):
130 for ch in coprs_logic.MockChrootsLogic.get_multiple(None, active_only=active_only).all(): 131 print ch.chroot_name
132 133 option_list = ( 134 Option('--active-only', 135 '-a', 136 dest='active_only', 137 help='Display only active chroots', 138 required=False, 139 action='store_true', 140 default=False), 141 )
142
143 -class AlterUserCommand(Command):
144 - def run(self, name, **kwargs):
145 user = models.User.query.filter(models.User.openid_name==models.User.openidize_name(name)).first() 146 if not user: 147 print 'No user named {0}.'.format(name) 148 return 149 150 if kwargs['admin']: 151 user.admin = True 152 if kwargs['no_admin']: 153 user.admin = False 154 if kwargs['proven']: 155 user.proven = True 156 if kwargs['no_proven']: 157 user.proven = False 158 159 db.session.commit()
160 161 option_list = ( 162 Option('name'), 163 Group( 164 Option('--admin', 165 action='store_true'), 166 Option('--no-admin', 167 action='store_true'), 168 exclusive=True 169 ), 170 Group( 171 Option('--proven', 172 action='store_true'), 173 Option('--no-proven', 174 action='store_true'), 175 exclusive=True 176 ) 177 )
178 179 manager = Manager(app) 180 manager.add_command('test', TestCommand()) 181 manager.add_command('create_sqlite_file', CreateSqliteFileCommand()) 182 manager.add_command('create_db', CreateDBCommand()) 183 manager.add_command('drop_db', DropDBCommand()) 184 manager.add_command('create_chroot', CreateChrootCommand()) 185 manager.add_command('alter_chroot', AlterChrootCommand()) 186 manager.add_command('display_chroots', DisplayChrootsCommand()) 187 manager.add_command('drop_chroot', DropChrootCommand()) 188 manager.add_command('alter_user', AlterUserCommand()) 189 190 if __name__ == '__main__': 191 manager.run() 192