]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
cleanup for init function
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 27 Apr 2010 21:45:01 +0000 (17:45 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 27 Apr 2010 21:45:01 +0000 (17:45 -0400)
alembic/command.py
alembic/options.py
alembic/script.py
alembic/util.py

index 9e79c0709307c2dd11cd25bd8cb34f195059cfc4..c9a80ab7bee0abe7d2be11bafa7c417ae12f4e36 100644 (file)
@@ -2,7 +2,6 @@ from alembic.script import ScriptDirectory
 from alembic import options, util
 import os
 import sys
-import shutil
 
 def list_templates(opts):
     """List available templates"""
@@ -28,6 +27,9 @@ def init(opts):
 
     util.status("Creating directory %s" % os.path.abspath(dir_),
                 os.makedirs, dir_)
+
+    script = ScriptDirectory(dir_)
+
     template_dir = os.path.join(opts.get_template_directory(),
                                     opts.cmd_line_options.template)
     if not os.access(template_dir, os.F_OK):
@@ -35,20 +37,26 @@ def init(opts):
     for file_ in os.listdir(template_dir):
         if file_ == 'alembic.ini.mako':
             config_file = os.path.abspath(opts.cmd_line_options.config)
-            util.status("Generating %s" % config_file,
-                util.template_to_file,
-                os.path.join(template_dir, file_),
-                config_file,
-                script_location=dir_
-            )
+            if os.access(config_file, os.F_OK):
+                util.msg("File %s already exists, skipping" % config_file)
+            else:
+                script.generate_template(
+                    os.path.join(template_dir, file_),
+                    config_file,
+                    script_location=dir_
+                )
         else:
             output_file = os.path.join(dir_, file_)
-            util.status("Generating %s" % os.path.abspath(output_file), 
-                        shutil.copy, 
-                        os.path.join(template_dir, file_), output_file)
+            script.copy_file(
+                os.path.join(template_dir, file_), 
+                output_file
+            )
 
-    util.msg("\nPlease edit configuration/connection/logging "\
+    util.msg("Please edit configuration/connection/logging "\
             "settings in %r before proceeding." % config_file)
+
+def revision(opts):
+    """Create a new revision file."""
     
 def upgrade(opts):
     """Upgrade to the latest version."""
@@ -72,8 +80,6 @@ def history(opts):
 def splice(opts):
     """'splice' two branches, creating a new revision file."""
     
-def revision(opts):
-    """Create a new revision file."""
     
 def branches(opts):
     """Show current un-spliced branch points"""
\ No newline at end of file
index 2efe4d8b50777cf098d298507cbeee043c07a25a..3b020cb7ae01a9d1c0aac637b5cb6ccaba61bf98 100644 (file)
@@ -72,7 +72,7 @@ class Options(object):
         return dict(self.file_config.items(name))
      
     def err(self, msg):
-        sys.stderr.write(msg + "\n")
+        util.msg(msg)
         sys.exit(-1)
         
     def get_main_option(self, name, default=None):
index 3085d450c922161ce70b09b14bc40f4b19018b11..385041ec05ab1934dd644b6fac1b5e82791e8f7f 100644 (file)
@@ -1,4 +1,6 @@
 import os
+from alembic import util
+import shutil
 
 class ScriptDirectory(object):
     def __init__(self, dir):
@@ -8,3 +10,16 @@ class ScriptDirectory(object):
     def from_options(cls, options):
         return ScriptDirectory(options.get_main_option('script_location'))
 
+    def generate_template(self, src, dest, **kw):
+        util.status("Generating %s" % os.path.abspath(src),
+            util.template_to_file,
+            src, 
+            dest,
+            **kw
+        )
+        
+    def copy_file(self, src, dest):
+        util.status("Generating %s" % os.path.abspath(dest), 
+                    shutil.copy, 
+                    src, dest)
+        
\ No newline at end of file
index 54004bdbe7f84d98483785b7f162cd9bfe7bd5e6..88085eed219afd796c7d107dc9d7444d84a6f80a 100644 (file)
@@ -6,6 +6,10 @@ from sqlalchemy import util
 
 NO_VALUE = util.symbol("NO_VALUE")
 
+try:
+    width = int(os.environ['COLUMNS'])
+except (KeyError, ValueError):
+    width = 80
 
 def template_to_file(template_file, dest, **kw):
     f = open(dest, 'w')
@@ -19,8 +23,8 @@ def format_opt(opt, hlp, padding=22):
     return "  " + opt + \
         ((padding - len(opt)) * " ") + hlp
 
-def status(msg, fn, *arg, **kw):
-    sys.stdout.write("  " + msg + "...")
+def status(message, fn, *arg, **kw):
+    msg(message + "...", False)
     try:
         ret = fn(*arg, **kw)
         sys.stdout.write("done\n")
@@ -30,5 +34,9 @@ def status(msg, fn, *arg, **kw):
         raise
 
 
-def msg(msg):
-    sys.stdout.write(textwrap.wrap(msg))
\ No newline at end of file
+def msg(msg, newline=True):
+    lines = textwrap.wrap(msg, width)
+    if len(lines) > 1:
+        for line in lines[0:-1]:
+            sys.stdout.write("  " +line + "\n")
+    sys.stdout.write("  " + lines[-1] + ("\n" if newline else ""))