[asterisk-commits] alembic creator: Expose SQL script generation in separate fu... (repotools[master])

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu May 7 15:38:07 CDT 2015


Matt Jordan has submitted this change and it was merged.

Change subject: alembic_creator: Expose SQL script generation in separate function
......................................................................


alembic_creator: Expose SQL script generation in separate function

As the mkrelease script is being rewritten in Python, it is useful to be
able to generate the SQL scripts outside of invoking this script from
the shell. This patch moves the actual act of generating the scripts for
some given database/Alembic script tuple into a separate function. This
function can be imported into another Python module, and invoked from
that location.

REP-15

Change-Id: Ieead9af724854c97afb1a6d89b16591f8d791b0d
---
M alembic_creator.py
1 file changed, 27 insertions(+), 15 deletions(-)

Approvals:
  Samuel Galarneau: Looks good to me, but someone else must approve
  Matt Jordan: Looks good to me, approved; Verified



diff --git a/alembic_creator.py b/alembic_creator.py
index 26a6685..9056e03 100755
--- a/alembic_creator.py
+++ b/alembic_creator.py
@@ -1,15 +1,35 @@
 #!/usr/bin/env python
-''' Utilities to create SQL scripts from Asterisk's Alembic scripts
+"""Utilities to create SQL scripts from Asterisk's Alembic scripts
 
-Copyright (C) 2013, Digium, Inc.
+Copyright (C) 2013-2015, Digium, Inc.
 Matt Jordan <mjordan at digium.com>
+"""
 
-'''
-
+import os
 import sys
 from alembic.config import Config
 from alembic import command
 from optparse import OptionParser
+
+
+def create_db_script(database, script, output_location=None):
+    """Create a database script
+
+    Keyword Arguments:
+    database A database to create a set of scripts for
+    script A script to generate
+    output_location Prefix of the directory to create the scripts in
+    """
+    prefix = output_location or os.curdir
+    script_file = os.path.join(prefix, "{0}_{1}.sql".format(database, script))
+    with open(script_file, "w") as out_file:
+        url = '{0}://root:password at localhost/asterisk'.format(database)
+        config = Config()
+        config.output_buffer = out_file
+        config.set_main_option('script_location', script)
+        config.set_main_option('sqlalchemy.url', url)
+        command.upgrade(config, 'head', sql=True)
+
 
 def main(argv=None):
     if argv is None:
@@ -18,9 +38,9 @@
     parser = OptionParser()
 
     parser.add_option('-d', '--db', action='append',
-        dest='databases', help='Databases to generate')
+                      dest='databases', help='Databases to generate')
     parser.add_option('-s', '--script', action='append',
-        dest='scripts', help='Script locations to generate')
+                      dest='scripts', help='Script locations to generate')
 
     (options, args) = parser.parse_args(argv)
 
@@ -34,18 +54,10 @@
 
     for db in options.databases:
         for script in options.scripts:
-            url = '%s://root:password@localhost/asterisk' % db
-            config = Config()
-            out_file = open('%s_%s.sql' % (db, script), 'w')
-            config.output_buffer = out_file
-            config.set_main_option('script_location', script)
-            config.set_main_option('sqlalchemy.url', url)
-            command.upgrade(config, 'head', sql=True)
-            out_file.close()
+            create_db_script(db, script)
 
     return 0
 
 
 if __name__ == "__main__":
     main(sys.argv)
-

-- 
To view, visit https://gerrit.asterisk.org/378
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ieead9af724854c97afb1a6d89b16591f8d791b0d
Gerrit-PatchSet: 1
Gerrit-Project: repotools
Gerrit-Branch: master
Gerrit-Owner: Matt Jordan <mjordan at digium.com>
Gerrit-Reviewer: Matt Jordan <mjordan at digium.com>
Gerrit-Reviewer: Samuel Galarneau <sgalarneau at digium.com>



More information about the asterisk-commits mailing list