From: Mike Bayer Date: Tue, 27 Apr 2010 21:45:01 +0000 (-0400) Subject: cleanup for init function X-Git-Tag: rel_0_1_0~108 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d4fea1d2e9c19a9b8da4a415c6d303b58e7360c9;p=thirdparty%2Fsqlalchemy%2Falembic.git cleanup for init function --- diff --git a/alembic/command.py b/alembic/command.py index 9e79c070..c9a80ab7 100644 --- a/alembic/command.py +++ b/alembic/command.py @@ -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 diff --git a/alembic/options.py b/alembic/options.py index 2efe4d8b..3b020cb7 100644 --- a/alembic/options.py +++ b/alembic/options.py @@ -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): diff --git a/alembic/script.py b/alembic/script.py index 3085d450..385041ec 100644 --- a/alembic/script.py +++ b/alembic/script.py @@ -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 diff --git a/alembic/util.py b/alembic/util.py index 54004bdb..88085eed 100644 --- a/alembic/util.py +++ b/alembic/util.py @@ -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 ""))