from alembic import options, util
import os
import sys
-import shutil
def list_templates(opts):
"""List available templates"""
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):
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."""
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
import os
+from alembic import util
+import shutil
class ScriptDirectory(object):
def __init__(self, dir):
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
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')
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")
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 ""))