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