def __init__(self, path): super(Version, self).__init__(path) # Version must be numeric try: self.version = VerNum(os.path.basename(path)) except: raise exceptions.InvalidVersionError(path) # Collect scripts in this folder self.logsql = dict() self.sql = dict() self.python = None try: for script in os.listdir(path): self._add_script(os.path.join(path, script)) except: raise exceptions.InvalidVersionError(path)
def getNewVersion(self): ver = self.latest + 1 # No change scripts exist for 0 (even though it's a valid version) if ver <= 0: raise exceptions.InvalidVersionError() self.latest = ver return ver
def runchange(self,ver,change,step): startver = ver endver = ver + step # Current database version must be correct! Don't run if corrupt! if self.version != startver: raise exceptions.InvalidVersionError("%s is not %s"%(self.version,startver)) # Run the change change.run(self.engine,step) # Update/refresh database version update = self.table.update(self.table.c.version == int(startver)) self.engine.execute(update, version=int(endver)) self._load()
def _validate_version(cls,repository,version): """Ensures this is a valid version number for this repository If invalid, raises cls.InvalidVersionError Returns a valid version number """ if version is None: version=0 try: version = VerNum(version) # raises valueerror if version < 0 or version > repository.latest: raise ValueError() except ValueError: raise exceptions.InvalidVersionError(version) return version
def _commit_py(self, path_py, required=None): if (not os.path.exists(path_py)) or (not os.path.isfile(path_py)): raise exceptions.InvalidVersionError(path_py) dest = os.path.join(self.path, '%s.%s' % (str(self.version), extensions.py)) # Create logsql files, if needed self._compile(path_py, required=required) # Move the committed py script to this version's folder shutil.move(path_py, dest) self._add_script(dest) # Also delete the .pyc file, if it exists path_pyc = path_py + 'c' if os.path.exists(path_pyc): self._rm_ignore(path_pyc)
def __init__(self, vernum, path, filelist): # Version must be numeric try: self.version = VerNum(vernum) except: raise exceptions.InvalidVersionError(vernum) # Collect scripts in this folder self.sql = dict() self.python = None for script in filelist: # skip __init__.py, because we assume that it's # just there to mark the package if script == '__init__.py': continue self._add_script(os.path.join(path, script))
def commit(self, path, ver=None, *p, **k): """Commit a script to this collection of scripts This compiles the script into a group of logsql files If there's an error compiling the script, the commit is cancelled """ maxver = self.latest + 1 if ver is None: ver = maxver # Ver must be valid: can't upgrade past the next version # No change scripts exist for 0 (even though it's a valid version) if ver > maxver or ver == 0: raise exceptions.InvalidVersionError() verpath = self.version_path(ver) tmpname = None try: # If replacing an old version, copy it in case it gets trashed if os.path.exists(verpath): tmpname = os.path.join( os.path.split(verpath)[0], "%s_tmp" % ver) shutil.copytree(verpath, tmpname) version = Version(verpath) else: # Create version folder version = Version.create(verpath) self.versions[ver] = version # Commit the individual script script = version.commit(path, *p, **k) except: # Can't leave the folder created above... shutil.rmtree(verpath) # Rollback if a version already existed above if tmpname is not None: shutil.move(tmpname, verpath) raise # Success: mark latest; delete old version if tmpname is not None: shutil.rmtree(tmpname) self.latest = ver