apply PEP8 to version.py, fixed notification of missing test_db.cfg
This commit is contained in:
parent
1b927fa427
commit
7cd2c3233b
@ -1,37 +1,54 @@
|
|||||||
from migrate.versioning import exceptions,pathed,script
|
#!/usr/bin/env python
|
||||||
import os,re,shutil
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
from migrate.versioning import exceptions, pathed, script
|
||||||
|
|
||||||
|
|
||||||
class VerNum(object):
|
class VerNum(object):
|
||||||
"""A version number"""
|
"""A version number"""
|
||||||
_instances=dict()
|
|
||||||
def __new__(cls,value):
|
_instances = dict()
|
||||||
val=str(value)
|
|
||||||
|
def __new__(cls, value):
|
||||||
|
val = str(value)
|
||||||
if val not in cls._instances:
|
if val not in cls._instances:
|
||||||
cls._instances[val] = super(VerNum,cls).__new__(cls)
|
cls._instances[val] = super(VerNum, cls).__new__(cls)
|
||||||
ret = cls._instances[val]
|
ret = cls._instances[val]
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def __init__(self,value):
|
def __init__(self,value):
|
||||||
self.value=str(int(value))
|
self.value = str(int(value))
|
||||||
if self < 0:
|
if self < 0:
|
||||||
raise ValueError("Version number cannot be negative")
|
raise ValueError("Version number cannot be negative")
|
||||||
|
|
||||||
|
def __add__(self, value):
|
||||||
|
ret = int(self) + int(value)
|
||||||
|
return VerNum(ret)
|
||||||
|
|
||||||
|
def __sub__(self, value):
|
||||||
|
return self + (int(value) * -1)
|
||||||
|
|
||||||
|
def __cmp__(self, value):
|
||||||
|
return int(self) - int(value)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return str(self.value)
|
return str(self.value)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.value)
|
return str(self.value)
|
||||||
|
|
||||||
def __int__(self):
|
def __int__(self):
|
||||||
return int(self.value)
|
return int(self.value)
|
||||||
def __add__(self,value):
|
|
||||||
ret=int(self)+int(value)
|
|
||||||
return VerNum(ret)
|
|
||||||
def __sub__(self,value):
|
|
||||||
return self+(int(value)*-1)
|
|
||||||
def __cmp__(self,value):
|
|
||||||
return int(self)-int(value)
|
|
||||||
|
|
||||||
|
def str_to_filename(s):
|
||||||
|
"""Replaces spaces, (double and single) quotes
|
||||||
|
and double underscores to underscores
|
||||||
|
"""
|
||||||
|
|
||||||
def strToFilename(s):
|
|
||||||
s = s.replace(' ', '_').replace('"', '_').replace("'", '_')
|
s = s.replace(' ', '_').replace('"', '_').replace("'", '_')
|
||||||
while '__' in s:
|
while '__' in s:
|
||||||
s = s.replace('__', '_')
|
s = s.replace('__', '_')
|
||||||
@ -40,15 +57,19 @@ def strToFilename(s):
|
|||||||
|
|
||||||
class Collection(pathed.Pathed):
|
class Collection(pathed.Pathed):
|
||||||
"""A collection of versioning scripts in a repository"""
|
"""A collection of versioning scripts in a repository"""
|
||||||
|
|
||||||
FILENAME_WITH_VERSION = re.compile(r'^(\d+).*')
|
FILENAME_WITH_VERSION = re.compile(r'^(\d+).*')
|
||||||
def __init__(self,path):
|
|
||||||
super(Collection,self).__init__(path)
|
def __init__(self, path):
|
||||||
|
super(Collection, self).__init__(path)
|
||||||
|
|
||||||
# Create temporary list of files, allowing skipped version numbers.
|
# Create temporary list of files, allowing skipped version numbers.
|
||||||
files = os.listdir(path)
|
files = os.listdir(path)
|
||||||
if '1' in files:
|
|
||||||
raise Exception('It looks like you have a repository in the old format (with directories for each version). Please convert repository before proceeding.')
|
|
||||||
tempVersions = dict()
|
tempVersions = dict()
|
||||||
|
if '1' in files:
|
||||||
|
raise Exception('It looks like you have a repository in the old '
|
||||||
|
'format (with directories for each version). '
|
||||||
|
'Please convert repository before proceeding.')
|
||||||
for filename in files:
|
for filename in files:
|
||||||
match = self.FILENAME_WITH_VERSION.match(filename)
|
match = self.FILENAME_WITH_VERSION.match(filename)
|
||||||
if match:
|
if match:
|
||||||
@ -57,22 +78,24 @@ class Collection(pathed.Pathed):
|
|||||||
else:
|
else:
|
||||||
pass # Must be a helper file or something, let's ignore it.
|
pass # Must be a helper file or something, let's ignore it.
|
||||||
|
|
||||||
# Create the versions member where the keys are VerNum's and the values are Version's.
|
# Create the versions member where the keys
|
||||||
self.versions=dict()
|
# are VerNum's and the values are Version's.
|
||||||
|
self.versions = dict()
|
||||||
for num, files in tempVersions.items():
|
for num, files in tempVersions.items():
|
||||||
self.versions[VerNum(num)] = Version(num, path, files)
|
self.versions[VerNum(num)] = Version(num, path, files)
|
||||||
self.latest = max([VerNum(0)] + self.versions.keys()) # calculate latest version
|
# calculate latest version
|
||||||
|
self.latest = max([VerNum(0)] + self.versions.keys())
|
||||||
|
|
||||||
def version_path(self,ver):
|
def version_path(self, ver):
|
||||||
return os.path.join(self.path,str(ver))
|
return os.path.join(self.path, str(ver))
|
||||||
|
|
||||||
def version(self,vernum=None):
|
def version(self, vernum=None):
|
||||||
if vernum is None:
|
if vernum is None:
|
||||||
vernum = self.latest
|
vernum = self.latest
|
||||||
return self.versions[VerNum(vernum)]
|
return self.versions[VerNum(vernum)]
|
||||||
|
|
||||||
def getNewVersion(self):
|
def getNewVersion(self):
|
||||||
ver = self.latest+1
|
ver = self.latest + 1
|
||||||
# No change scripts exist for 0 (even though it's a valid version)
|
# No change scripts exist for 0 (even though it's a valid version)
|
||||||
if ver <= 0:
|
if ver <= 0:
|
||||||
raise exceptions.InvalidVersionError()
|
raise exceptions.InvalidVersionError()
|
||||||
@ -81,25 +104,29 @@ class Collection(pathed.Pathed):
|
|||||||
|
|
||||||
def createNewVersion(self, description, **k):
|
def createNewVersion(self, description, **k):
|
||||||
ver = self.getNewVersion()
|
ver = self.getNewVersion()
|
||||||
extra = strToFilename(description)
|
extra = str_to_filename(description)
|
||||||
|
|
||||||
if extra:
|
if extra:
|
||||||
if extra == '_':
|
if extra == '_':
|
||||||
extra = ''
|
extra = ''
|
||||||
elif not extra.startswith('_'):
|
elif not extra.startswith('_'):
|
||||||
extra = '_%s' % extra
|
extra = '_%s' % extra
|
||||||
|
|
||||||
filename = '%03d%s.py' % (ver, extra)
|
filename = '%03d%s.py' % (ver, extra)
|
||||||
filepath = self.version_path(filename)
|
filepath = self.version_path(filename)
|
||||||
|
|
||||||
if os.path.exists(filepath):
|
if os.path.exists(filepath):
|
||||||
raise Exception('Script already exists: %s' % filepath)
|
raise Exception('Script already exists: %s' % filepath)
|
||||||
else:
|
else:
|
||||||
script.PythonScript.create(filepath)
|
script.PythonScript.create(filepath)
|
||||||
|
|
||||||
self.versions[ver] = Version(ver, self.path, [filename])
|
self.versions[ver] = Version(ver, self.path, [filename])
|
||||||
|
|
||||||
def createNewSQLVersion(self, database, **k):
|
def createNewSQLVersion(self, database, **k):
|
||||||
# Determine version number to use.
|
# Determine version number to use.
|
||||||
# fix from Issue 29
|
# fix from Issue 29
|
||||||
ver = self.getNewVersion()
|
ver = self.getNewVersion()
|
||||||
self.versions[ver] = Version(ver, self.path, [])
|
self.versions[ver] = Version(ver, self.path, [])
|
||||||
|
|
||||||
# Create new files.
|
# Create new files.
|
||||||
for op in ('upgrade', 'downgrade'):
|
for op in ('upgrade', 'downgrade'):
|
||||||
@ -113,24 +140,25 @@ class Collection(pathed.Pathed):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def clear(cls):
|
def clear(cls):
|
||||||
super(Collection,cls).clear()
|
super(Collection, cls).clear()
|
||||||
|
|
||||||
|
|
||||||
class extensions:
|
class Extensions:
|
||||||
"""A namespace for file extensions"""
|
"""A namespace for file extensions"""
|
||||||
py='py'
|
py = 'py'
|
||||||
sql='sql'
|
sql = 'sql'
|
||||||
|
|
||||||
|
|
||||||
class Version(object): # formerly inherit from: (pathed.Pathed):
|
class Version(object): # formerly inherit from: (pathed.Pathed):
|
||||||
"""A single version in a repository
|
"""A single version in a repository """
|
||||||
"""
|
|
||||||
def __init__(self,vernum,path,filelist):
|
def __init__(self, vernum, path, filelist):
|
||||||
# Version must be numeric
|
# Version must be numeric
|
||||||
try:
|
try:
|
||||||
self.version=VerNum(vernum)
|
self.version = VerNum(vernum)
|
||||||
except:
|
except:
|
||||||
raise exceptions.InvalidVersionError(vernum)
|
raise exceptions.InvalidVersionError(vernum)
|
||||||
|
|
||||||
# Collect scripts in this folder
|
# Collect scripts in this folder
|
||||||
self.sql = dict()
|
self.sql = dict()
|
||||||
self.python = None
|
self.python = None
|
||||||
@ -140,22 +168,18 @@ class Version(object): # formerly inherit from: (pathed.Pathed):
|
|||||||
# just there to mark the package
|
# just there to mark the package
|
||||||
if script == '__init__.py':
|
if script == '__init__.py':
|
||||||
continue
|
continue
|
||||||
self._add_script(os.path.join(path,script))
|
self._add_script(os.path.join(path, script))
|
||||||
|
|
||||||
def script(self,database=None,operation=None):
|
def script(self, database=None, operation=None):
|
||||||
#if database is None and operation is None:
|
# Try to return a .sql script first
|
||||||
# return self._script_py()
|
|
||||||
#print database,operation,self.sql
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Try to return a .sql script first
|
return self._script_sql(database, operation)
|
||||||
return self._script_sql(database,operation)
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass # No .sql script exists
|
pass # No .sql script exists
|
||||||
|
|
||||||
|
# Try to return the default .sql script
|
||||||
try:
|
try:
|
||||||
# Try to return the default .sql script
|
return self._script_sql('default', operation)
|
||||||
return self._script_sql('default',operation)
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass # No .sql script exists
|
pass # No .sql script exists
|
||||||
|
|
||||||
@ -163,13 +187,15 @@ class Version(object): # formerly inherit from: (pathed.Pathed):
|
|||||||
|
|
||||||
assert ret is not None
|
assert ret is not None
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def _script_py(self):
|
def _script_py(self):
|
||||||
return self.python
|
return self.python
|
||||||
def _script_sql(self,database,operation):
|
|
||||||
|
def _script_sql(self, database, operation):
|
||||||
return self.sql[database][operation]
|
return self.sql[database][operation]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(cls,path):
|
def create(cls, path):
|
||||||
os.mkdir(path)
|
os.mkdir(path)
|
||||||
# craete the version as a proper Python package
|
# craete the version as a proper Python package
|
||||||
initfile = os.path.join(path, "__init__.py")
|
initfile = os.path.join(path, "__init__.py")
|
||||||
@ -177,25 +203,27 @@ class Version(object): # formerly inherit from: (pathed.Pathed):
|
|||||||
# just touch the file
|
# just touch the file
|
||||||
open(initfile, "w").close()
|
open(initfile, "w").close()
|
||||||
try:
|
try:
|
||||||
ret=cls(path)
|
ret = cls(path)
|
||||||
except:
|
except:
|
||||||
os.rmdir(path)
|
os.rmdir(path)
|
||||||
raise
|
raise
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def _add_script(self,path):
|
def _add_script(self, path):
|
||||||
if path.endswith(extensions.py):
|
if path.endswith(Extensions.py):
|
||||||
self._add_script_py(path)
|
self._add_script_py(path)
|
||||||
elif path.endswith(extensions.sql):
|
elif path.endswith(Extensions.sql):
|
||||||
self._add_script_sql(path)
|
self._add_script_sql(path)
|
||||||
|
|
||||||
SQL_FILENAME = re.compile(r'^(\d+)_([^_]+)_([^_]+).sql')
|
SQL_FILENAME = re.compile(r'^(\d+)_([^_]+)_([^_]+).sql')
|
||||||
def _add_script_sql(self,path):
|
|
||||||
|
def _add_script_sql(self, path):
|
||||||
match = self.SQL_FILENAME.match(os.path.basename(path))
|
match = self.SQL_FILENAME.match(os.path.basename(path))
|
||||||
|
|
||||||
if match:
|
if match:
|
||||||
version, dbms, op = match.group(1), match.group(2), match.group(3)
|
version, dbms, op = match.group(1), match.group(2), match.group(3)
|
||||||
else:
|
else:
|
||||||
raise exceptions.ScriptError("Invalid sql script name %s"%path)
|
raise exceptions.ScriptError("Invalid sql script name %s" % path)
|
||||||
|
|
||||||
# File the script into a dictionary
|
# File the script into a dictionary
|
||||||
dbmses = self.sql
|
dbmses = self.sql
|
||||||
@ -203,15 +231,16 @@ class Version(object): # formerly inherit from: (pathed.Pathed):
|
|||||||
dbmses[dbms] = dict()
|
dbmses[dbms] = dict()
|
||||||
ops = dbmses[dbms]
|
ops = dbmses[dbms]
|
||||||
ops[op] = script.SqlScript(path)
|
ops[op] = script.SqlScript(path)
|
||||||
def _add_script_py(self,path):
|
|
||||||
|
def _add_script_py(self, path):
|
||||||
if self.python is not None:
|
if self.python is not None:
|
||||||
raise Exception('You can only have one Python script per version, but you have: %s and %s' % (self.python, path))
|
raise Exception('You can only have one Python script per version,'
|
||||||
|
' but you have: %s and %s' % (self.python, path))
|
||||||
self.python = script.PythonScript(path)
|
self.python = script.PythonScript(path)
|
||||||
|
|
||||||
def _rm_ignore(self,path):
|
def _rm_ignore(self, path):
|
||||||
"""Try to remove a path; ignore failure"""
|
"""Try to remove a path; ignore failure"""
|
||||||
try:
|
try:
|
||||||
os.remove(path)
|
os.remove(path)
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -13,10 +13,8 @@ def readurls():
|
|||||||
try:
|
try:
|
||||||
fd=open(fullpath)
|
fd=open(fullpath)
|
||||||
except IOError:
|
except IOError:
|
||||||
print "You must specify the databases to use for testing!"
|
raise IOError("""You must specify the databases to use for testing!
|
||||||
tmplfile = "%s.tmpl"%filename
|
Copy %(filename)s.tmpl to %(filename)s and edit your database URLs.""" % locals())
|
||||||
print "Copy %s.tmpl to %s and edit your database URLs."%(tmplfile,filename)
|
|
||||||
raise
|
|
||||||
#fd = resource_stream('__main__',filename)
|
#fd = resource_stream('__main__',filename)
|
||||||
for line in fd:
|
for line in fd:
|
||||||
if line.startswith('#'):
|
if line.startswith('#'):
|
||||||
|
@ -45,10 +45,10 @@ class TestVerNum(fixture.Base):
|
|||||||
|
|
||||||
class TestDescriptionNaming(fixture.Base):
|
class TestDescriptionNaming(fixture.Base):
|
||||||
def test_names(self):
|
def test_names(self):
|
||||||
self.assertEquals(strToFilename(''), '')
|
self.assertEquals(str_to_filename(''), '')
|
||||||
self.assertEquals(strToFilename('a'), 'a')
|
self.assertEquals(str_to_filename('a'), 'a')
|
||||||
self.assertEquals(strToFilename('Abc Def'), 'Abc_Def')
|
self.assertEquals(str_to_filename('Abc Def'), 'Abc_Def')
|
||||||
self.assertEquals(strToFilename('Abc "D" Ef'), 'Abc_D_Ef')
|
self.assertEquals(str_to_filename('Abc "D" Ef'), 'Abc_D_Ef')
|
||||||
self.assertEquals(strToFilename("Abc's Stuff"), 'Abc_s_Stuff')
|
self.assertEquals(str_to_filename("Abc's Stuff"), 'Abc_s_Stuff')
|
||||||
self.assertEquals(strToFilename("a b"), 'a_b')
|
self.assertEquals(str_to_filename("a b"), 'a_b')
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user