From 7f9439b16d08e7d2fa51da74460343913c285296 Mon Sep 17 00:00:00 2001 From: Guilherme Chapiewski Date: Thu, 12 Mar 2009 00:54:06 -0300 Subject: [PATCH] Refactoring to remove logging class and put thing on CLI. --- src/simple_db_migrate/cli.py | 12 ++++++++++++ src/simple_db_migrate/core.py | 9 +++++---- src/simple_db_migrate/logging.py | 10 ---------- src/simple_db_migrate/main.py | 31 ++++++++++++++++--------------- src/simple_db_migrate/mysql.py | 8 +++++--- tests/cli_test.py | 16 ++++++++++++++++ 6 files changed, 54 insertions(+), 32 deletions(-) delete mode 100644 src/simple_db_migrate/logging.py diff --git a/src/simple_db_migrate/cli.py b/src/simple_db_migrate/cli.py index f042fef..ccecee3 100644 --- a/src/simple_db_migrate/cli.py +++ b/src/simple_db_migrate/cli.py @@ -1,4 +1,5 @@ from optparse import OptionParser +import sys class CLI(object): @@ -39,3 +40,14 @@ def get_parser(self): def parse(self): return self.__parser.parse_args() + + def error_and_exit(self, msg): + print "[ERROR] %s\n" % (msg) + sys.exit(1) + + def info_and_exit(self, msg): + print "%s\n" % (msg) + sys.exit(0) + + def msg(self, msg): + print msg \ No newline at end of file diff --git a/src/simple_db_migrate/core.py b/src/simple_db_migrate/core.py index e2d1bc4..763e17c 100644 --- a/src/simple_db_migrate/core.py +++ b/src/simple_db_migrate/core.py @@ -1,4 +1,4 @@ -from logging import * +from cli import CLI from time import strftime import os import shutil @@ -11,6 +11,7 @@ class SimpleDBMigrate(object): def __init__(self, migrations_dir): self.__migrations_dir = migrations_dir + self.__cli = CLI() def get_all_migration_files(self): dir_list = os.listdir(self.__migrations_dir) @@ -21,7 +22,7 @@ def get_all_migration_files(self): files.append(dir_file) if len(files) == 0: - Log().error_and_exit("no migration files found") + self.__cli.error_and_exit("no migration files found") files.sort() @@ -76,7 +77,7 @@ def create_migration(self, migration_name): file_name = "%s_%s%s" % (timestamp, migration_name, self.__migration_files_extension) if not self.is_file_name_valid(file_name): - Log().error_and_exit("invalid migration name; it should contain only letters, numbers and/or underscores ('%s')" % migration_name) + self.__cli.error_and_exit("invalid migration name; it should contain only letters, numbers and/or underscores ('%s')" % migration_name) new_file = "%s/%s" % (self.__migrations_dir, file_name) @@ -85,7 +86,7 @@ def create_migration(self, migration_name): f.write(MigrationFile.template) f.close() except IOError: - Log().error_and_exit("could not create file ('%s')" % new_file) + self.__cli.error_and_exit("could not create file ('%s')" % new_file) return file_name diff --git a/src/simple_db_migrate/logging.py b/src/simple_db_migrate/logging.py deleted file mode 100644 index 49a7ff9..0000000 --- a/src/simple_db_migrate/logging.py +++ /dev/null @@ -1,10 +0,0 @@ -import sys - -class Log(object): - - def __print(self, level, msg): - print "[%s] %s\n" % (level, msg) - - def error_and_exit(self, msg): - self.__print("ERROR", msg) - sys.exit(1) \ No newline at end of file diff --git a/src/simple_db_migrate/main.py b/src/simple_db_migrate/main.py index 85efd24..1a300be 100644 --- a/src/simple_db_migrate/main.py +++ b/src/simple_db_migrate/main.py @@ -1,11 +1,12 @@ +from cli import CLI from core import SimpleDBMigrate from helpers import Lists from mysql import MySQL -from logging import Log import sys class Main(object): def __init__(self, options=None, args=None, mysql=None, db_migrate=None): + self.__cli = CLI() self.__options = options self.__args = args @@ -18,23 +19,23 @@ def __init__(self, options=None, args=None, mysql=None, db_migrate=None): self.__db_migrate = SimpleDBMigrate(self.__options.migrations_dir) def execute(self): - print "\nStarting DB migration..." + self.__cli.msg("\nStarting DB migration...") if self.__options.create_migration: self._create_migration() else: self._migrate() - print "\nDone.\n" + self.__cli.msg("\nDone.\n") def _create_migration(self): new_file = self.__db_migrate.create_migration(self.__options.create_migration) - print "- Created file '%s'" % (new_file) + self.__cli.msg("- Created file '%s'" % (new_file)) def _migrate(self): destination_version = self._get_destination_version() current_version = self.__mysql.get_current_schema_version() - print "- Current version is: %s" % current_version - print "- Destination version is: %s" % destination_version + self.__cli.msg("- Current version is: %s" % current_version) + self.__cli.msg("- Destination version is: %s" % destination_version) # if current and destination versions are the same, # will consider a migration up to execute remaining files @@ -52,7 +53,7 @@ def _get_destination_version(self): destination_version = self.__db_migrate.latest_schema_version_available() if not self.__db_migrate.check_if_version_exists(destination_version): - Log().error_and_exit("version not found (%s)" % destination_version) + self.__cli.error_and_exit("version not found (%s)" % destination_version) return destination_version @@ -68,7 +69,7 @@ def _get_migration_files_to_be_executed(self, current_version, destination_versi down_versions = [version for version in mysql_versions if version <= current_version and version > destination_version] for version in down_versions: if version not in migration_versions: - Log().error_and_exit("impossible to migrate down: one of the versions was not found (%s)" % version) + self.__cli.error_and_exit("impossible to migrate down: one of the versions was not found (%s)" % version) down_versions.reverse() return down_versions @@ -77,18 +78,18 @@ def _execute_migrations(self, current_version, destination_version, is_migration versions_to_be_executed = self._get_migration_files_to_be_executed(current_version, destination_version) if versions_to_be_executed is None or len(versions_to_be_executed) == 0: - print "\nNothing to do.\n" + self.__cli.msg("\nNothing to do.\n") sys.exit(0) up_down_label = "up" if is_migration_up else "down" - print "\nStarting migration %s!" % up_down_label - print "*** will run %s\n" % versions_to_be_executed + self.__cli.msg("\nStarting migration %s!" % up_down_label) + self.__cli.msg("*** will run %s\n" % versions_to_be_executed) sql_statements_executed = "" for migration_version in versions_to_be_executed: sql_file = self.__db_migrate.get_migration_file_name_from_version_number(migration_version) - print "===== executing %s (%s) =====" % (sql_file, up_down_label) + self.__cli.msg("===== executing %s (%s) =====" % (sql_file, up_down_label)) sql = self.__db_migrate.get_sql_command(sql_file, is_migration_up) self.__mysql.change(sql, migration_version, is_migration_up) @@ -96,6 +97,6 @@ def _execute_migrations(self, current_version, destination_version, is_migration sql_statements_executed += sql if self.__options.show_sql: - print "__________ SQL statements executed __________" - print sql_statements_executed - print "_____________________________________________\n" \ No newline at end of file + self.__cli.msg("__________ SQL statements executed __________") + self.__cli.msg(sql_statements_executed) + self.__cli.msg("_____________________________________________\n") \ No newline at end of file diff --git a/src/simple_db_migrate/mysql.py b/src/simple_db_migrate/mysql.py index 9d6b933..2964a45 100644 --- a/src/simple_db_migrate/mysql.py +++ b/src/simple_db_migrate/mysql.py @@ -1,16 +1,18 @@ -from logging import * +from cli import CLI import MySQLdb import sys class MySQL(object): def __init__(self, db_config_file="simple-db-migrate.conf", mysql_driver=MySQLdb): + self.__cli = CLI() + # read configurations try: f = open(db_config_file, "r") exec(f.read()) except IOError: - Log().error_and_exit("%s: file not found" % db_config_file) + self.__cli.error_and_exit("%s: file not found" % db_config_file) else: f.close() @@ -30,7 +32,7 @@ def __mysql_connect(self, connect_using_db_name=True): return self.__mysql_driver.connect(host=self.__mysql_host__, user=self.__mysql_user__, passwd=self.__mysql_passwd__) except Exception: - Log().error_and_exit("could not connect to database") + self.__cli.error_and_exit("could not connect to database") def __execute(self, sql): db = self.__mysql_connect() diff --git a/tests/cli_test.py b/tests/cli_test.py index fbaad09..999a56d 100644 --- a/tests/cli_test.py +++ b/tests/cli_test.py @@ -23,6 +23,22 @@ def test_it_should_configure_all_options(self): self.assertTrue(parser.has_option("--showsql")) self.assertTrue(parser.has_option("--create")) + + def test_it_should_show_error_message_and_exit(self): + cli = CLI() + try: + cli.error_and_exit("test message") + self.fail("it should not get here") + except: + pass + + def test_it_should_show_info_message_and_exit(self): + cli = CLI() + try: + cli.info_and_exit("test message") + self.fail("it should not get here") + except: + pass if __name__ == "__main__": unittest.main()