]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- more fixes for #163, make sure env.py load figures out to load env.pyc or
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 31 Dec 2013 20:19:40 +0000 (15:19 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 31 Dec 2013 20:19:40 +0000 (15:19 -0500)
env.pyo as well

alembic/util.py
tests/__init__.py
tests/test_versioning.py

index 93b6b764d11db16c27fb0b94b946114e2cb91248..4bcd8b57c854d8acdb04b2e7e63f6284e355e770 100644 (file)
@@ -198,13 +198,28 @@ def load_python_file(dir_, filename):
     path = os.path.join(dir_, filename)
     _, ext = os.path.splitext(filename)
     if ext == ".py":
-        module = load_module_py(module_id, path)
+        if os.path.exists(path):
+            module = load_module_py(module_id, path)
+        elif os.path.exists(simple_pyc_file_from_path(path)):
+            # look for sourceless load
+            module = load_module_pyc(module_id, simple_pyc_file_from_path(path))
+        else:
+            raise ImportError("Can't find Python file %s" % path)
     elif ext in (".pyc", ".pyo"):
         module = load_module_pyc(module_id, path)
     del sys.modules[module_id]
     return module
 
 def simple_pyc_file_from_path(path):
+    """Given a python source path, return the so-called
+    "sourceless" .pyc or .pyo path.
+
+    This just a .pyc or .pyo file where the .py file would be.
+
+    Even with PEP-3147, which normally puts .pyc/.pyo files in __pycache__,
+    this use case remains supported as a so-called "sourceless module import".
+
+    """
     if sys.flags.optimize:
         return path + "o"  # e.g. .pyo
     else:
index ad5b03335be8c1e45ad496b0cd0080f51ded3052..9bb40f1b6371ca229cb0e4059cfa104b738e9338 100644 (file)
@@ -303,7 +303,7 @@ def _testing_config():
     return Config(os.path.join(staging_directory, 'test_alembic.ini'))
 
 
-def staging_env(create=True, template="generic"):
+def staging_env(create=True, template="generic", sourceless=False):
     from alembic import command, script
     cfg = _testing_config()
     if create:
@@ -311,6 +311,19 @@ def staging_env(create=True, template="generic"):
         if os.path.exists(path):
             shutil.rmtree(path)
         command.init(cfg, path)
+        if sourceless:
+            try:
+                # do an import so that a .pyc/.pyo is generated.
+                util.load_python_file(path, 'env.py')
+            except AttributeError:
+                # we don't have the migration context set up yet
+                # so running the .env py throws this exception.
+                # theoretically we could be using py_compiler here to
+                # generate .pyc/.pyo without importing but not really
+                # worth it.
+                pass
+            make_sourceless(os.path.join(path, "env.py"))
+
     sc = script.ScriptDirectory.from_config(cfg)
     return sc
 
@@ -339,16 +352,21 @@ def write_script(scriptdir, rev_id, content, encoding='ascii', sourceless=False)
     script.nextrev = old.nextrev
 
     if sourceless:
-        # note that if -O is set, you'd see pyo files here,
-        # the pyc util function looks at sys.flags.optimize to handle this
-        assert os.access(pyc_path, os.F_OK)
-        # look for a non-pep3147 path here.
-        # if not present, need to copy from __pycache__
-        simple_pyc_path = util.simple_pyc_file_from_path(path)
-        if not os.access(simple_pyc_path, os.F_OK):
-            shutil.copyfile(pyc_path, simple_pyc_path)
-        os.unlink(path)
+        make_sourceless(path)
+
+def make_sourceless(path):
+    # note that if -O is set, you'd see pyo files here,
+    # the pyc util function looks at sys.flags.optimize to handle this
+    pyc_path = util.pyc_file_from_path(path)
+    assert os.access(pyc_path, os.F_OK)
+
+    # look for a non-pep3147 path here.
+    # if not present, need to copy from __pycache__
+    simple_pyc_path = util.simple_pyc_file_from_path(path)
 
+    if not os.access(simple_pyc_path, os.F_OK):
+        shutil.copyfile(pyc_path, simple_pyc_path)
+    os.unlink(path)
 
 def three_rev_fixture(cfg):
     a = util.rev_id()
index a4be95f25c0de50ad9f74545b9cc2e446b8c1974..41e4be11efdabd6393647ec731cc0b70a00ae80b 100644 (file)
@@ -99,7 +99,7 @@ class VersioningTest(unittest.TestCase):
 
     @classmethod
     def setup_class(cls):
-        cls.env = staging_env()
+        cls.env = staging_env(sourceless=cls.sourceless)
         cls.cfg = _sqlite_testing_config()
 
     @classmethod