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