Changeset 13449 for main/waeup.uniben/trunk
- Timestamp:
- 12 Nov 2015, 17:03:44 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.uniben/trunk/src/waeup/uniben/scripts.py
r13210 r13449 2 2 3 3 To make this work, you have to pip install psutil in your local virtualenv. 4 5 Other functions can be called from the commandline. These comprise: 6 7 - `bedless_students` 8 - `remove_orphan_beds` 9 10 Once, you have installed uniben, you can use `bin/python-console`:: 11 12 13 $ ,/bin/python-console 14 >>> from waeup.uniben.scripts import bedless_students 15 >>> studs = [x for x in bedless_students()] 16 ... lots of output ... 17 ... the cmd takes some time to finish ... 18 ... please be patient ... 19 >>> len(studs) 20 196 21 22 etc. Use Ctrl-D to quit. 23 4 24 """ 5 25 import argparse … … 9 29 import tempfile 10 30 import time 31 import transaction 11 32 from ZODB import DB, DemoStorage, FileStorage 12 33 from ZODB.blob import BlobStorage … … 58 79 59 80 60 def init_dbs( ):81 def init_dbs(read_only=True): 61 82 """Setup databases. 62 83 … … 74 95 db_path = os.path.join(db_dir, 'Data.fs') 75 96 async_db_path = os.path.join(db_dir, 'Data.async.fs') 76 async_storage = FileStorage.FileStorage(async_db_path, read_only= True)97 async_storage = FileStorage.FileStorage(async_db_path, read_only=read_only) 77 98 db1 = DB(async_storage, database_name="async", databases=databases) 78 base_storage = FileStorage.FileStorage(db_path, read_only= True)99 base_storage = FileStorage.FileStorage(db_path, read_only=read_only) 79 100 blob_storage = BlobStorage(blob_dir, base_storage) 80 101 db2 = DB(blob_storage, databases=databases) … … 200 221 t2 = time.time() 201 222 print("Elapsed: %s secs" % (t2 - t1)) 223 224 225 def bedless_students(university_inst=None): 226 """Get students with beds gone. 227 """ 228 conn, closeables = None, None 229 if university_inst is None: 230 closeables = init_dbs(read_only=False) 231 conn = closeables[0].open() 232 university_inst = get_university(conn) 233 for stud_id in university_inst['students']: 234 stud = university_inst['students'][stud_id] 235 if not 'accommodation' in stud.keys(): 236 continue 237 accomm = stud['accommodation'] 238 for bed_ticket_id in accomm.keys(): 239 ticket = accomm[bed_ticket_id] 240 bed = ticket.bed 241 if bed is None: 242 continue 243 if getattr( 244 getattr(bed, '__parent__', None), 245 '__parent__', None) is None: 246 yield stud_id, ticket, bed 247 if conn is not None: 248 conn.close() 249 close_dbs(closeables) 250 251 252 def remove_orphan_beds(): 253 """Delete orphaned beds. 254 """ 255 closeables = init_dbs(read_only=False) 256 conn = closeables[0].open() 257 uni = get_university(conn) 258 for stud_id, bed_ticket, bed in bedless_students(uni): 259 print("DELETE bed of student %s" % stud_id) 260 bed_ticket.bed = None 261 del bed 262 transaction.commit() 263 conn.close() 264 close_dbs(closeables)
Note: See TracChangeset for help on using the changeset viewer.