Module dbmanager
[hide private]
[frames] | no frames]

Source Code for Module dbmanager

  1  # -*- coding: utf-8 -*- 
  2   
  3  ############################################################################ 
  4  # dbmanager.py 
  5  # 
  6  # Class that manages database related tasks. Connection management, 
  7  # pooling, schema management and migration are tasks of this class. 
  8  # 
  9  # (C) 2008 Likya Software Ltd. 
 10  ############################################################################ 
 11   
 12  import os, sys, md5 
 13  import glob, tarfile, zipfile 
 14  import cStringIO, shutil 
 15   
 16  from twisted.python import log 
 17  import migrate.versioning.api as api 
 18  from migrate.versioning.exceptions import DatabaseAlreadyControlledError 
 19   
 20  from constants import Constants 
 21   
22 -class DBManager(object):
23
24 - def __init__(self):
25 ''' 26 Creates a new instance of ServerCodeManager, which is responsible for 27 extracting MOC packages to temporary directories on server. 28 ''' 29 pass
30
31 - def controlSchema(self, domain):
32 ''' 33 Controls (ensures that it is up-to-date) the db schema of the given domain if it exists. 34 35 @type domainList: list 36 @param domainList: List of Domain objects 37 ''' 38 log.msg('[%s] Checking database migration...' % domain.name()) 39 # If the domain has a db connection address 40 if domain.db(): 41 42 for moduleFile in glob.glob(os.path.join(Constants.DIST_DIRECTORY, domain.dist(), '*' + Constants.MODULE_EXT)): 43 44 if os.path.isfile(moduleFile): 45 46 moduleName = os.path.basename(moduleFile)[:-len(Constants.MODULE_EXT)] 47 dbRepository = os.path.join(Constants.WORK_DIRECTORY, domain.dist(), moduleName, 'db') 48 49 # First try to create version control table 50 try: 51 api.version_control(repository=dbRepository, url=domain.db()) 52 except DatabaseAlreadyControlledError: 53 # This is very probably normal, we may have already created version control database 54 pass 55 56 # Upgrade pending database schema changes 57 api.upgrade(repository=dbRepository, url=domain.db()) 58 log.msg('[%s] DB Upgrade done for module (%s)' % (domain.name(), moduleName)) 59 60 # Domain has no db info 61 else: 62 log.msg('[%s] NO DB Info' % domain.name())
63
64 - def controlAllSchemas(self, domainList):
65 ''' 66 Controls all (ensures that it is up-to-date) schemas of given domains. 67 68 @type domainList: list 69 @param domainList: List of Domain objects 70 ''' 71 for domain in domainList: 72 self.controlSchema(domain)
73
74 - def extractAll(self):
75 ''' 76 Extracts all available distributions into the temporary working directory. 77 ''' 78 # First create temporary working directory (remove any existing one) 79 shutil.rmtree(Constants.WORK_DIRECTORY, ignore_errors=True) 80 os.mkdir(Constants.WORK_DIRECTORY) 81 # Traverse distributions 82 for distDir in glob.glob(os.path.join(Constants.DIST_DIRECTORY, '*')): 83 if os.path.isdir(distDir): 84 self._extractDistribution(distDir)
85
86 - def _extractDistribution(self, distPath):
87 ''' 88 Extracts distribution contents to the temporary working directory. 89 90 @type distPath: string 91 @param distPath: String points to the distribution base directory 92 (relative to the application server working directory) 93 ''' 94 log.msg('Extracting Distribution: %s' % distPath) 95 96 for moduleFile in glob.glob(os.path.join(distPath, '*' + Constants.MODULE_EXT)): 97 if os.path.isfile(moduleFile): 98 modPackage = tarfile.open(moduleFile, 'r:bz2') 99 # Create base directory for distribution 100 newDistDirectory = os.path.join(Constants.WORK_DIRECTORY, os.path.basename(distPath)) 101 os.mkdir(newDistDirectory) 102 modPackage.extractall(newDistDirectory)
103