Package bodhi :: Package tools :: Module pickledb
[hide private]
[frames] | no frames]

Source Code for Module bodhi.tools.pickledb

  1  #!/usr/bin/python -tt 
  2  # $Id: $ 
  3  # 
  4  # This program is free software; you can redistribute it and/or modify 
  5  # it under the terms of the GNU General Public License as published by 
  6  # the Free Software Foundation; version 2 of the License. 
  7  # 
  8  # This program is distributed in the hope that it will be useful, 
  9  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 10  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 11  # GNU Library General Public License for more details. 
 12  # 
 13  # You should have received a copy of the GNU General Public License 
 14  # along with this program; if not, write to the Free Software 
 15  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 16   
 17  """ 
 18  This script pickles all updates/bugs/cves/comments and writes it out to disk 
 19  in the format of bodhi-pickledb-YYYYMMDD.HHMM 
 20  """ 
 21   
 22  import sys 
 23  import time 
 24  import cPickle as pickle 
 25   
 26  from os.path import isfile 
 27  from sqlobject import SQLObjectNotFound 
 28  from turbogears.database import PackageHub 
 29  from turbogears import update_config 
 30  from bodhi.exceptions import (DuplicateEntryError, SQLiteIntegrityError,  
 31                                PostgresIntegrityError) 
 32  from bodhi.model import (PackageUpdate, Release, Comment, Bugzilla, CVE, 
 33                           Package, PackageBuild) 
 34   
 35  hub = __connection__ = PackageHub("bodhi") 
 36   
37 -def load_config():
38 configfile = 'prod.cfg' 39 if not isfile(configfile): 40 configfile = 'dev.cfg' 41 update_config(configfile=configfile, modulename='bodhi.config')
42
43 -def save_db():
44 load_config() 45 updates = [] 46 47 for update in PackageUpdate.select(): 48 print update.title 49 data = {} 50 data['title'] = update.title 51 data['builds'] = [(build.package.name, build.nvr) for build in update.builds] 52 data['date_submitted'] = update.date_submitted 53 data['date_pushed'] = update.date_pushed 54 data['release'] = [update.release.name, update.release.long_name, 55 update.release.id_prefix, update.release.dist_tag] 56 data['submitter'] = update.submitter 57 data['update_id'] = update.update_id 58 data['type'] = update.type 59 data['karma'] = update.karma 60 data['cves'] = [cve.cve_id for cve in update.cves] 61 data['bugs'] = [(bug.bz_id, bug.title) for bug in update.bugs] 62 data['status'] = update.status 63 data['pushed'] = update.pushed 64 data['notes'] = update.notes 65 data['request'] = update.request 66 data['comments'] = [(c.timestamp, c.author, c.text, c.karma) for c in update.comments] 67 updates.append(data) 68 69 dump = file('bodhi-pickledb-%s' % time.strftime("%y%m%d.%H%M"), 'w') 70 pickle.dump(updates, dump) 71 dump.close()
72
73 -def load_db():
74 print "Loading pickled database %s" % sys.argv[2] 75 load_config() 76 db = file(sys.argv[2], 'r') 77 data = pickle.load(db) 78 for u in data: 79 try: 80 release = Release.byName(u['release'][0]) 81 except SQLObjectNotFound: 82 release = Release(name=u['release'][0], long_name=u['release'][1], 83 id_prefix=u['id_prefix'], dist_tag=u['dist_tag']) 84 request = None 85 if u['request'] == 'move': 86 request = 'stable' 87 elif u['request'] == 'push': 88 request = 'testing' 89 90 update = PackageUpdate(title=u['title'], 91 date_submitted=u['date_submitted'], 92 date_pushed=u['date_pushed'], 93 release=release, 94 submitter=u['submitter'], 95 update_id=u['update_id'], 96 type=u['type'], 97 status=u['status'], 98 pushed=u['pushed'], 99 notes=u['notes'], 100 karma=u['karma'], 101 request=request) 102 103 for pkg, nvr in u['builds']: 104 try: 105 package = Package.byName(pkg) 106 except SQLObjectNotFound: 107 package = Package(name=pkg) 108 build = PackageBuild(nvr=nvr, package=package) 109 update.addPackageBuild(build) 110 111 for bug_num, bug_title in u['bugs']: 112 try: 113 bug = Bugzilla(bz_id=bug_num) 114 bug.title = bug_title 115 except (DuplicateEntryError, SQLiteIntegrityError, 116 PostgresIntegrityError): 117 bug = Bugzilla.byBz_id(bug_num) 118 update.addBugzilla(bug) 119 120 for cve_id in u['cves']: 121 try: 122 cve = CVE(cve_id=cve_id) 123 except (DuplicateEntryError, SQLiteIntegrityError, 124 PostgresIntegrityError): 125 cve = CVE.byCve_id(cve_id) 126 update.addCVE(cve) 127 for timestamp, author, text, karma in u['comments']: 128 comment = Comment(timestamp=timestamp, author=author, text=text, 129 karma=karma, update=update) 130 131 print update 132 print
133
134 -def usage():
135 print "Usage: ./pickledb.py [ save | load <file> ]" 136 sys.exit(-1)
137 138 if __name__ == '__main__': 139 if len(sys.argv) < 2: 140 usage() 141 elif sys.argv[1] == 'save': 142 print "Pickling database..." 143 save_db() 144 elif sys.argv[1] == 'load' and len(sys.argv) == 3: 145 print "Loading db" 146 load_db() 147 else: 148 usage() 149