Skip to content

Commit

Permalink
compatibility with py3
Browse files Browse the repository at this point in the history
  • Loading branch information
piranha committed Aug 19, 2013
1 parent 5ec173a commit 049ec2c
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/dist/
/docs/_build/
MANIFEST
/_py3/
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: python
python:
- "2.6"
- "2.7"
- "3.3"
install: "pip install sqlalchemy cram"
script: "make test"
20 changes: 19 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
PWD = $(shell pwd)

PYVER = $(shell python -V 2>&1 | cut -c8)
ifeq ($(PYVER), 3)
PY3 = 1
endif

.PHONY: help docs test itest

help:
Expand All @@ -13,8 +20,19 @@ open:
cd docs && make open

test:
ifdef PY3
rm -rf _py3
mkdir -p _py3
cp -a nomad _py3/nomad
2to3 -x import -n -w _py3/nomad
2to3 -x import -n -w -d _py3/nomad
python _py3/nomad/utils.py
PYTHONPATH=$(PWD)/_py3 NOMAD="python $(PWD)/_py3/nomad/__init__.py" cram tests/*.t
else
python nomad/utils.py
cram tests/*.t
PYTHONPATH=$(PWD) NOMAD="python $(PWD)/nomad/__init__.py" cram --keep-tmpdir tests/*.t
# cram tests/*.t
endif

itest:
cram -i tests/*.t
8 changes: 4 additions & 4 deletions nomad/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def init(**opts):
print 'Versioning table initialized successfully'


@app.command(aliases=('ls',))
def list(all=('a', False, 'show all migrations (default: only non-applied)'),
@app.command(name='list', aliases=('ls',))
def list_(all=('a', False, 'show all migrations (default: only non-applied)'),
**opts):
'''List migrations
'''
Expand Down Expand Up @@ -91,10 +91,10 @@ def create(name,
abort('directory %s already exists' % path)
raise

with file(op.join(path, 'migration.ini'), 'w') as f:
with open(op.join(path, 'migration.ini'), 'w') as f:
f.write('[nomad]\n')
f.write('dependencies = %s\n' % ', '.join(d.name for d in deps))
with file(op.join(path, 'up.sql'), 'w') as f:
with open(op.join(path, 'up.sql'), 'w') as f:
f.write('-- SQL ALTER statements for database migration\n')


Expand Down
24 changes: 10 additions & 14 deletions nomad/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def __init__(self, confpath, overrides=None):
enginepath = 'nomad.engine.' + enginepath
try:
enginemod = __import__(enginepath, {}, {}, [''])
except ImportError:
raise NomadError('engine %s is unknown' % enginepath)
except ImportError, e:
raise NomadError('cannot use engine %s: %s' % (enginepath, e))
self.engine = getattr(enginemod, 'engine')(geturl(self))

def __repr__(self):
Expand Down Expand Up @@ -90,10 +90,10 @@ class Migration(object):
SINGLETONS = {}

def __new__(cls, repo, name, **kwargs):
if (repo, name) not in cls.SINGLETONS:
cls.SINGLETONS[(repo, name)] = object.__new__(
cls, repo, name, **kwargs)
return cls.SINGLETONS[(repo, name)]
key = (repo, name)
if key not in cls.SINGLETONS:
cls.SINGLETONS[key] = object.__new__(cls)
return cls.SINGLETONS[key]

def __init__(self, repo, name, force=False, applied=False):
self.repo = repo
Expand All @@ -113,14 +113,10 @@ def __repr__(self):
def __str__(self):
return self.name

def __cmp__(self, other):
def __lt__(self, other):
if isinstance(other, Migration) and self.repo == other.repo:
if self.name == other.name:
return 0
if self.name < other.name:
return -1
return 1
return id(self) - id(other)
return self.name < other.name
raise TypeError('Migrations can be compared only with other migrations')

@property
def path(self):
Expand All @@ -143,7 +139,7 @@ def apply(self):
continue
path = op.join(self.path, fn)
if fn.endswith('.sql'):
with file(path) as f:
with open(path) as f:
self.repo.engine.query(f.read())
print ' sql migration applied: %s' % fn
elif os.access(path, os.X_OK):
Expand Down
14 changes: 9 additions & 5 deletions nomad/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

from termcolor import cprint

if sys.version_info[0] == 3:
shell_split = shlex.split
else:
def shell_split(cmd):
return shlex.split(cmd.encode('utf-8'))

NUM_RE = re.compile('(\d+)')

Expand Down Expand Up @@ -77,7 +82,7 @@ def geturl(repo):
if 'url' in conf:
return conf['url']
if 'url-python' in conf:
pypath, attr = conf['url-python'].encode('utf-8').split(':')
pypath, attr = conf['url-python'].split(':')
if '/' in pypath or pypath.endswith('.py'):
# load from file
mod = loadpath(pypath)
Expand All @@ -87,16 +92,15 @@ def geturl(repo):
return reduce(lambda x, y: getattr(x, y), attr.split('.'), mod)
if 'url-file' in conf:
try:
return file(conf['url-file']).read().strip()
return open(conf['url-file']).read().strip()
except IOError, e:
abort(e)
if 'url-command' in conf:
try:
p1 = Popen(shlex.split(conf['url-command'].encode('utf-8')),
stdout=PIPE)
p1 = Popen(shell_split(conf['url-command']), stdout=PIPE)
except OSError, e:
abort(e)
return p1.communicate()[0].strip()
return p1.communicate()[0].strip().decode('utf-8')
abort('database url in %s is not found' % repo)


Expand Down
21 changes: 18 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
#!/usr/bin/env python

import os
import sys, os
from setuptools import setup, find_packages

PY3 = sys.version_info[0] >= 3

if PY3:
from distutils.command.build_py import build_py_2to3 as build_py
else:
from distutils.command.build_py import build_py


DEPS = ['opster>=4.0', 'termcolor']
if not PY3:
DEPS.append('configparser')


def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()


config = dict(
name = 'nomad',
description = 'simple sql migration tool to save you from becoming mad',
long_description = read('README.rst'),
license = 'BSD',
version = '0.2',
version = '0.3',
author = 'Alexander Solovyov',
author_email = 'alexander@solovyov.net',
url = 'http://github.com/piranha/nomad/',
Expand All @@ -26,10 +40,11 @@ def read(fname):
'Topic :: Database'
],

install_requires = ['opster>=4.0', 'configparser', 'termcolor'],
install_requires = DEPS,
packages = find_packages(),
entry_points = {'console_scripts': ['nomad=nomad:app.dispatch']},
platforms='any',
cmdclass={'build_py': build_py}
)

if __name__ == '__main__':
Expand Down
17 changes: 9 additions & 8 deletions tests/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Basic nomad tests

First, set up environment::

$ NOMAD=${NOMAD:-nomad}
$ cat > nomad.ini <<EOF
> [nomad]
> engine = sqla
Expand All @@ -15,7 +16,7 @@ First, set up environment::

First, initialize migrations repository::

$ nomad init
$ $NOMAD init
Versioning table initialized successfully
$ sqlite3 test.db '.schema'
CREATE TABLE nomad (
Expand All @@ -25,14 +26,14 @@ First, initialize migrations repository::

First migration::

$ nomad create 0-first
$ $NOMAD create 0-first
$ echo "create table test (value varchar(10));" > 0-first/up.sql
$ nomad ls
$ $NOMAD ls
\x1b[32m0-first\x1b[0m (esc)

Upgrading::

$ nomad apply -a
$ $NOMAD apply -a
applying migration 0-first:
sql migration applied: up.sql
$ sqlite3 test.db '.schema test'
Expand All @@ -42,12 +43,12 @@ Upgrading::

Dependencies::

$ nomad create 1-second
$ nomad create 2-third -d 1-second
$ nomad ls
$ $NOMAD create 1-second
$ $NOMAD create 2-third -d 1-second
$ $NOMAD ls
\x1b[32m1-second\x1b[0m (esc)
\x1b[32m2-third\x1b[0m (1-second) (esc)
$ nomad apply 2-third
$ $NOMAD apply 2-third
applying migration 1-second:
sql migration applied: up.sql
applying migration 2-third:
Expand Down
15 changes: 8 additions & 7 deletions tests/urls.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ Test different methods to acquire URLs.

Directly specified URL::

$ NOMAD=${NOMAD:-nomad}
$ cat > nomad.ini <<EOF
> [nomad]
> engine = sqla
> url = sqlite:///test.db
> EOF
$ nomad info
$ $NOMAD info
<Repository: .>:
<SAEngine: sqlite:///test.db>
Uninitialized repository
Expand All @@ -34,7 +35,7 @@ URL from Python object from ``sys.path``::
> engine = sqla
> url-python = somemod:dburl
> EOF
$ PYTHONPATH=. nomad info
$ PYTHONPATH=.:$PYTHONPATH $NOMAD info
<Repository: .>:
<SAEngine: sqlite:///test-py.db>
Uninitialized repository
Expand All @@ -47,7 +48,7 @@ URL from Python object using path::
> engine = sqla
> url-python = \${confdir}/somemod.py:dburl
> EOF
$ nomad info
$ $NOMAD info
<Repository: .>:
<SAEngine: sqlite:///test-py.db>
Uninitialized repository
Expand All @@ -62,7 +63,7 @@ URL from Python package::
> engine = sqla
> url-python = \${confdir}/package:dburl
> EOF
$ nomad info
$ $NOMAD info
<Repository: .>:
<SAEngine: sqlite:///test-py.db>
Uninitialized repository
Expand All @@ -76,7 +77,7 @@ URL from a file::
> engine = sqla
> url-file = url
> EOF
$ nomad info
$ $NOMAD info
<Repository: .>:
<SAEngine: sqlite:///test-file.db>
Uninitialized repository
Expand All @@ -90,7 +91,7 @@ URL from a command::
> engine = sqla
> url-command = cat url
> EOF
$ nomad info
$ $NOMAD info
<Repository: .>:
<SAEngine: sqlite:///test-file.db>
Uninitialized repository
Expand All @@ -99,6 +100,6 @@ URL from a command::
Nothing defined::

$ echo '[nomad]\nengine=sqla' > nomad.ini
$ nomad info
$ $NOMAD info
\x1b[31mError: database url in <Repository: .> is not found\x1b[0m (esc)
[1]

0 comments on commit 049ec2c

Please sign in to comment.