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