From: Mike Bayer Date: Wed, 8 Feb 2012 05:30:14 +0000 (-0500) Subject: - [feature] script_location can be interpreted X-Git-Tag: rel_0_2_2~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=72bb16079a554f6b6c0dd6b26c496d20d636c07a;p=thirdparty%2Fsqlalchemy%2Falembic.git - [feature] script_location can be interpreted by pkg_resources.resource_filename(), if it is a non-absolute URI that contains colons. This scheme is the same one used by Pyramid. [#29] --- diff --git a/CHANGES b/CHANGES index 6bdca518..27945569 100644 --- a/CHANGES +++ b/CHANGES @@ -3,6 +3,12 @@ - [bug] setup.py won't install argparse if on Python 2.7/3.2 +- [feature] script_location can be interpreted + by pkg_resources.resource_filename(), if + it is a non-absolute URI that contains + colons. This scheme is the same + one used by Pyramid. [#29] + 0.2.1 ===== - [bug] Fixed the generation of CHECK constraint, diff --git a/alembic/script.py b/alembic/script.py index f9e02240..952b572f 100644 --- a/alembic/script.py +++ b/alembic/script.py @@ -30,7 +30,9 @@ class ScriptDirectory(object): @classmethod def from_config(cls, config): return ScriptDirectory( - config.get_main_option('script_location'), + util.coerce_resource_to_filename( + config.get_main_option('script_location') + ), file_template = config.get_main_option( 'file_template', _default_file_template) diff --git a/alembic/util.py b/alembic/util.py index 3ae15f91..3599531d 100644 --- a/alembic/util.py +++ b/alembic/util.py @@ -111,6 +111,18 @@ def create_module_class_proxy(cls, globals_, locals_): else: attr_names.add(methname) +def coerce_resource_to_filename(fname): + """Interpret a filename as either a filesystem location or as a package resource. + + Names that are non absolute paths and contain a colon + are interpreted as resources and coerced to a file location. + + """ + if not os.path.isabs(fname) and ":" in fname: + import pkg_resources + fname = pkg_resources.resource_filename(*fname.split(':')) + return fname + def status(_statmsg, fn, *arg, **kw): msg(_statmsg + "...", False) try: diff --git a/docs/build/conf.py b/docs/build/conf.py index 165fa60e..44dde4b6 100644 --- a/docs/build/conf.py +++ b/docs/build/conf.py @@ -206,4 +206,6 @@ latex_documents = [ #{'python': ('http://docs.python.org/3.2', None)} -intersphinx_mapping = {'sqla':('http://www.sqlalchemy.org/docs/', None)} +intersphinx_mapping = { + 'sqla':('http://www.sqlalchemy.org/docs/', None), +} diff --git a/docs/build/tutorial.rst b/docs/build/tutorial.rst index c5bee931..8e47f1be 100644 --- a/docs/build/tutorial.rst +++ b/docs/build/tutorial.rst @@ -114,7 +114,7 @@ The file generated with the "generic" configuration looks like:: [alembic] # path to migration scripts - script_location = %(here)s/alembic + script_location = alembic # template used to generate migration files # file_template = %%(rev)s_%%(slug)s @@ -165,11 +165,22 @@ This file contains the following features: * ``[alembic]`` - this is the section read by Alembic to determine configuration. Alembic itself does not directly read any other areas of the file. -* ``script_location`` - this is the location of the Alembic environment, relative to - the current directory, unless the path is an absolute file path. +* ``script_location`` - this is the location of the Alembic environment. It is normally + specified as a filesystem location, either relative or absolute. If the location is + a relative path, it's interpreted as relative to the current directory. + This is the only key required by Alembic in all cases. The generation of the .ini file by the command ``alembic init alembic`` automatically placed the - directory name ``alembic`` here. + directory name ``alembic`` here. The special variable ``%(here)s`` can also be used, + as in ``%(here)s/alembic``. + + For support of applications that package themselves into .egg files, the value can + also be specified + as a `package resource `_, in which + case ``resource_filename()`` is used to find the file (new in 0.2.2). Any non-absolute + URI which contains colons is interpreted here as a resource name, rather than + a straight filename. + * ``file_template`` - this is the naming scheme used to generate new migration files. The value present is the default, so is commented out. The two tokens available are ``%%(rev)s`` and ``%%(slug)s``, where ``%%(slug)s`` is a truncated string derived