def _validate_and_prepare(self, properties, arguments): """ Prepare the version list here. All other preparation is handled by the plugins in the chain. """ prepared_args = { 'default_version': properties.get('default_version'), 'update_versions': [], } target_version = arguments.get('version') # If the target version is the default, we stop here because create takes care of this if prepared_args['default_version'] == target_version: return prepared_args # Here we determine the initial version versions = Versions() version_loader = VersionLoader(versions) version_loader.load(properties) versions.sort() # retrieve all versions for version in versions.list(): # First add this version if it's not the default one, because that's handled by create if version.get_value() != prepared_args['default_version']: prepared_args['update_versions'].append(version.get_value()) # Then if we reached the target version, we break if version.get_value() == target_version: break return prepared_args
def fail_on_unknown_version(version, properties): versions = Versions() version_loader = VersionLoader(versions) version_loader.load(properties) versions.sort() if not versions.exists(Version(version)): message = "the given version is not valid for this project" raise PluginException(message)
def fail_on_unknown_version(version, properties): """Verify that ``version`` is valid for this project""" versions = Versions() version_loader = VersionLoader(versions) version_loader.load(properties) versions.sort() if not versions.exists(Version(version)): raise UnknownVersionException( "version {} is not valid for this project".format(version))
def fail_on_invalid_version(self, connector, executor, version, properties): plugin_dir = properties.get('plugin.dir') versions = Versions() version_loader = VersionLoader(versions) version_loader.load(properties) versions.sort() v = Version(version) previous = versions.previous(v).get_value() properties['previous'] = previous script = File(os.path.join(plugin_dir, 'mysql', 'update', 'checkversion.sql')) executor['script'] = script connector.execute(executor, properties) if "(Code 1329)" in connector.get_result(): raise InvalidVersionException("invalid version: {}".format(previous))
def fail_on_invalid_version(self, properties, connector, executor, version): plugin_dir = properties.get('plugin.dir') versions = Versions() version_loader = VersionLoader(versions) version_loader.load(properties) versions.sort() v = Version(version) previous = versions.previous(v).get_value() properties['previous'] = previous script = File( os.path.join(plugin_dir, 'mssql', 'update', 'checkversion.sql')) executor['script'] = script connector.execute(executor, properties)
def testCheckVersion(self): properties = Properties() properties.set_property("current.dir", os.path.abspath('.')) properties.set_property('create.dir', os.path.join(properties.get_property('current.dir'), 'create')) properties.set_property('alter.dir', os.path.join(properties.get_property('current.dir'), 'alter')) properties.set_property('default_version', '1.0.0') versions = Versions() version_loader = VersionLoader(versions) version_loader.load(properties) versions.sort() previous = versions.previous(Version('1.0.2')) self.assertEqual(previous.get_value(), '1.0.1', 'invalid version') previous = versions.previous(Version('1.0.1')) self.assertEqual(previous.get_value(), '1.0.0', 'invalid version') # this is the odd one. Given is the first version, because off the fact that there is no previous version, # the first version is returned. previous = versions.previous(Version('1.0.0')) self.assertEqual(previous.get_value(), '1.0.0', 'invalid version')
def execute(self, arguments, properties): project = '' version = None if arguments.v: version = arguments.v current_dir = properties.get_property('current.dir') project_file = properties.get_property('project.file') config_file = File(os.path.join(current_dir, project_file)) if not config_file.exists(): database = raw_input('database : ') project = database + "-db" host = raw_input('host [localhost] : ') host = Ora.nvl(host, "localhost") username = raw_input('username : '******'password : '******'version [1.0.0]: ') version = Ora.nvl(version, "1.0.0") os.mkdir(project) template_dir = os.path.join(properties.get_property('plugin.dir'), 'mysql', 'generate', 'templates') template_file = os.path.join(template_dir, project_file) f = open(template_file) stream = f.read() f.close() stream = stream.replace('{host}', host) stream = stream.replace('{database}', database) stream = stream.replace('{username}', username) stream = stream.replace('{password}', password) stream = stream.replace('{version}', version) config_file = os.path.join(current_dir, project, project_file) f = open(config_file, 'w') f.write(stream) f.close() properties.set_property('alter.dir', os.path.join(current_dir, project, 'alter')) properties.set_property('create.dir', os.path.join(current_dir, project, 'create')) config_file = os.path.join(current_dir, project, project_file) f = open(config_file) data = json.load(f) for key in data.keys(): properties.set_property(key, data[key]) f.close() versions = Versions() version_loader = VersionLoader(versions) version_loader.load(properties) versions.sort() version_guesser = VersionGuesser(properties, versions) next_version = version_guesser.guess(version).to_string() version_dir = version_guesser.to_folder(next_version) # create the version folder os.makedirs(version_dir) databases = properties.get_property('databases') version_database = properties.get_property('version_database') default_version = properties.get_property('default_version') environments = properties.get_property('environments') objects = properties.get_property('create_objects') for database in databases: # create the scheme folder database_dir = os.path.join(version_dir, database) os.mkdir(database_dir) # create the dat folder dat_dir = os.path.join(database_dir, 'dat') os.mkdir(dat_dir) # create the version script in the dat folder if database == version_database: version_file = os.path.join(dat_dir, 'version.sql') f = open(version_file, 'w') if next_version == default_version: stream = properties.get_property('version_insert_statement') else: stream = properties.get_property('version_update_statement') stream = stream.replace('<version>', next_version) f.write(stream) f.close() # sqlScript=self.getSqlVersionStatement(versions, version) # projectHelper.writeFile(datFolder+os.sep+'version.sql', sqlScript) # create the environment folders in the dat folder for environment in environments: os.mkdir(os.path.join(dat_dir, environment)) # create the environment script in the dat folder. if database == version_database and next_version == default_version: environment_file = os.path.join(dat_dir, environment, 'environment.sql') f = open(environment_file, 'w') stream = properties.get_property('environment_insert_statement') stream = stream.replace('<environment>', environment) f.write(stream) f.close() # create the ddl folder ddl_dir = os.path.join(database_dir, 'ddl') os.mkdir(ddl_dir) # create the object folders in the ddl folder for object in objects: os.mkdir(os.path.join(ddl_dir, object)) # create the template code on create. if database == version_database and next_version == default_version: for object in objects: object_dir = os.path.join(template_dir, object) if os.path.exists(object_dir): files = Files.list(File(object_dir)) for file in files: shutil.copyfile(file.get_url(), os.path.join(ddl_dir, object, file.tail())) print "version " + next_version + " created."
def execute(self, arguments, properties): properties.set_property('create.dir', os.path.join(properties.get_property('current.dir'), 'create')) properties.set_property('alter.dir', os.path.join(properties.get_property('current.dir'), 'alter')) version = arguments.v Fail.fail_on_no_version(version) Fail.fail_on_unknown_version(version, properties) default_databases = properties.get_property('databases') databases = Ora.nvl(arguments.d, default_databases) Fail.fail_on_invalid_database(arguments.d, properties) current_dir = properties.get_property('current.dir') component_name = properties.get_property('component_name') target_dir = os.path.join(current_dir, properties.get_property('component_target_folder')) objects = properties.get_property('create_objects') build_dir = App.build_dir(version, properties) # exclude the file 'version.sql', this file is excluded from the dat listing below. component_excluded_files = properties.get_property('component_excluded_files') excluded_files = properties.get_property('excluded_files') excluded_files.extend(component_excluded_files) # create the target folder, if not present. if not File(target_dir).exists(): os.makedirs(target_dir) print "building component with version '" + version + "'" zip_file = os.path.join(target_dir, component_name + '_' + version + '.zip') zip_handle = ZipFile(zip_file, 'w') for database in databases: for object in objects: if not object == 'lib': # global ddl objects folder = File(os.path.join(build_dir, database, 'ddl', object)) zip_dir = File(os.path.join(component_name + '_' + version, 'ddl', object)) files = Files.list_filtered(folder, properties) for file in files: print file.get_url() target_file = File(os.path.join(zip_dir.get_url(), file.tail())) zip_handle.write(file.get_url(), target_file.get_url(), ZIP_DEFLATED) # global dat files folder = File(os.path.join(build_dir, database, 'dat')) zip_dir = File(os.path.join(component_name + '_' + version, 'dat')) files = Files.list_filtered(folder, properties) for file in files: print file.get_url() target_file = File(os.path.join(zip_dir.get_url(), file.tail())) zip_handle.write(file.get_url(), target_file.get_url(), ZIP_DEFLATED) # create the version script in the dat folder version_statement = self.version_statement(version, properties) version_statement = version_statement.replace('<version>', version) version_statement = version_statement.replace('<name>', component_name) f = open('version.sql', 'w') f.write(version_statement) f.close() zip_dir = File(os.path.join(component_name + '_' + version, 'dat')) target_file = File(os.path.join(zip_dir.get_url(), 'version.sql')) zip_handle.write('version.sql', target_file.get_url()) # remove the version.sql file. os.remove('version.sql') # create the checkversion script in the root folder # first retrieve the previous version for the checkversion script versions = Versions() version_loader = VersionLoader(versions) version_loader.load(properties) versions.sort() previous = versions.previous(Version(version)) component_select_statement = properties.get_property("component_select_statement") component_select_statement = component_select_statement.replace('<name>', component_name) component_select_statement = component_select_statement.replace('<previous>', previous.get_value()) print component_select_statement f = open('checkversion.sql', 'w') f.write(component_select_statement) f.close() zip_dir = File(os.path.join(component_name + '_' + version)) target_file = File(os.path.join(zip_dir.get_url(), 'checkversion.sql')) zip_handle.write('checkversion.sql', target_file.get_url()) # remove the version.sql file. os.remove('checkversion.sql') zip_handle.close() print "component with version " + version + " created."
def execute(self, properties, arguments): """ Build the package after verifying version is valid, and database, if provided. :param properties: The project properties :param arguments: A dict of { 'version': 'The version to package for' 'database': 'The database to package for (optional)' } """ prepared_args = self._validate_and_prepare(properties, arguments) version = prepared_args['version'] databases = prepared_args['databases'] current_dir = properties.get('current.dir') component_name = properties.get('component_name') target_dir = os.path.join(current_dir, properties.get('component_target_folder')) objects = properties.get('create_objects') if version == properties.get("default_version"): build_dir = properties.get("create.dir") else: build_dir = os.path.join(properties.get("alter.dir"), version) # exclude the file 'version.sql', this file is excluded from the dat listing below. component_excluded_files = properties.get('component_excluded_files') excluded_files = properties.get('excluded_files') excluded_files.extend(component_excluded_files) # create the target folder, if not present. if not File(target_dir).exists(): os.makedirs(target_dir) print("building component with version '{}'".format(version)) zip_file = os.path.join(target_dir, component_name + '_' + version + '.zip') zip_handle = ZipFile(zip_file, 'w') for database in databases: for obj in objects: if not obj == 'lib': # global ddl objects folder = File(os.path.join(build_dir, database, 'ddl', obj)) zip_dir = File(os.path.join(component_name + '_' + version, 'ddl', obj)) files = Files.list_filtered(folder, properties) for file in files: print(file.get_url()) target_file = File(os.path.join(zip_dir.get_url(), file.tail())) zip_handle.write(file.get_url(), target_file.get_url(), ZIP_DEFLATED) # global dat files folder = File(os.path.join(build_dir, database, 'dat')) zip_dir = File(os.path.join(component_name + '_' + version, 'dat')) files = Files.list_filtered(folder, properties) for file in files: print(file.get_url()) target_file = File(os.path.join(zip_dir.get_url(), file.tail())) zip_handle.write(file.get_url(), target_file.get_url(), ZIP_DEFLATED) # create the version script in the dat folder if version == properties.get("default_version"): version_statement = properties.get("component_insert_statement") else: version_statement = properties.get("component_update_statement") version_statement = version_statement.replace('<version>', version) version_statement = version_statement.replace('<name>', component_name) f = open('version.sql', 'w') f.write(version_statement) f.close() zip_dir = File(os.path.join(component_name + '_' + version, 'dat')) target_file = File(os.path.join(zip_dir.get_url(), 'version.sql')) zip_handle.write('version.sql', target_file.get_url()) # remove the version.sql file. os.remove('version.sql') # create the checkversion script in the root folder # first retrieve the previous version for the checkversion script versions = Versions() version_loader = VersionLoader(versions) version_loader.load(properties) versions.sort() previous = versions.previous(Version(version)) component_select_statement = properties.get("component_select_statement") component_select_statement = component_select_statement.replace('<name>', component_name) component_select_statement = component_select_statement.replace( '<previous>', previous.get_value()) print(component_select_statement) f = open('checkversion.sql', 'w') f.write(component_select_statement) f.close() zip_dir = File(os.path.join(component_name + '_' + version)) target_file = File(os.path.join(zip_dir.get_url(), 'checkversion.sql')) zip_handle.write('checkversion.sql', target_file.get_url()) # remove the version.sql file. os.remove('checkversion.sql') zip_handle.close() print("component with version {} created.".format(version))
def execute(self, arguments, properties): properties.set_property('create.dir', os.path.join(properties.get_property('current.dir'), 'create')) properties.set_property('alter.dir', os.path.join(properties.get_property('current.dir'), 'alter')) # find the drop plugin. plugin = self.find_plugin(properties, 'drop') # create the drop arguments and execute the plugin parser = argparse.ArgumentParser(add_help=False) parser.add_argument('-h', type=str, help='host', required=False) parser.add_argument('-s', type=str, help='schema', required=False) parser.add_argument('-e', type=str, help='environment', required=False) parser.add_argument('-a', type=str, help='alias', required=False) commands = [] if arguments.h: commands.append('-h='+arguments.h) if arguments.s: commands.append('-s='+arguments.d) if arguments.e: commands.append('-e='+arguments.e) if arguments.a: commands.append('-a='+arguments.a) drop_arguments = parser.parse_args(commands) plugin.execute(drop_arguments, properties) # find the create plugin. plugin = self.find_plugin(properties, 'create') # create the drop arguments and execute the plugin parser = argparse.ArgumentParser(add_help=False) parser.add_argument('-h', type=str, help='host', required=False) parser.add_argument('-s', type=str, help='schema', required=False) parser.add_argument('-e', type=str, help='environment', required=False) parser.add_argument('-a', type=str, help='alias', required=False) commands = [] if arguments.h: commands.append('-h='+arguments.h) if arguments.s: commands.append('-s='+arguments.s) if arguments.e: commands.append('-e='+arguments.e) if arguments.a: commands.append('-a='+arguments.a) create_arguments = parser.parse_args(commands) plugin.execute(create_arguments, properties) versions = Versions() version_loader = VersionLoader(versions) version_loader.load(properties) versions.sort() # remove the default_version from the version list # by doing so, this reflects the present alters in the project folder alter_versions = [] for version in versions.list(): if version.get_value() != properties.get_property('default_version'): alter_versions.append(version.get_value()) # create a list of versions up to the given version # if no version is given, the last available version in the project folder is assumed. if arguments.v: update_versions = [] for version in alter_versions: update_versions.append(version) if version == arguments.v: break alter_versions = update_versions # find the update plugin. plugin = self.find_plugin(properties, 'update') # create the arguments and execute the plugin for version in alter_versions: parser = argparse.ArgumentParser(add_help=False) parser.add_argument('-h', type=str, help='host', required=False) parser.add_argument('-s', type=str, help='schema', required=False) parser.add_argument('-e', type=str, help='environment', required=False) parser.add_argument('-a', type=str, help='alias', required=False) parser.add_argument('-v', type=str, help='version', required=False) commands = [] if arguments.h: commands.append('-h='+arguments.h) if arguments.s: commands.append('-s='+arguments.s) if arguments.e: commands.append('-e='+arguments.e) if arguments.a: commands.append('-a='+arguments.a) commands.append('-v='+version) update_arguments = parser.parse_args(commands) plugin.execute(update_arguments, properties)
# def weight(value): # i = 0 # result = 0 # print value.split('.') # for item in value.split('.'): # # result = result + float(item) / 1000**i # print result # i = i + 1 # return result # # # print weight('1.0.3.2') versions = Versions() version = Version("1.0.2") versions.add(version) version = Version("1.0.3") versions.add(version) version = Version("1.0.3.1") versions.add(version) version = Version("1.0.3.2") versions.add(version) version = Version("1.0.4") versions.add(version)
def execute(self, properties, arguments): """ Create or upgrade a project. The version is always required, the rest only when generating an entirely new project. :type properties: system.Properties.Properties :param properties: The project properties :type arguments: dict :param arguments: This dict contains the plugin arguments: * **version**: (Initial) project version to generate for; * **host**: The hostname for the new project. If not provided, an upgrade is assumed (optional); * **port**: Port to connect to (optional); * **database**: Name of the database (optional); * **schema**: Name of the schema (optional); * **username**: Database username (optional); * **password**: Database password (optional). """ prepared_args = self._validate_and_prepare(properties, arguments) template_dir = os.path.join(properties.get('plugin.dir'), 'mssql', 'generate', 'templates') versions = Versions() version_loader = VersionLoader(versions) version_loader.load(properties) versions.sort() version_guesser = VersionGuesser(properties, versions) next_version = version_guesser.guess( prepared_args.get('version')).to_string() version_dir = version_guesser.to_folder(next_version) # create the version folder os.makedirs(version_dir) schemes = properties.get('schemes') version_schema = properties.get('version_schema') default_version = properties.get('default_version') environments = properties.get('environments') objects = properties.get('create_objects') for schema in schemes: # create the scheme folder schema_dir = os.path.join(version_dir, schema) os.mkdir(schema_dir) # create the dat folder dat_dir = os.path.join(schema_dir, 'dat') os.mkdir(dat_dir) # create the version script in the dat folder if schema == version_schema: version_file = os.path.join(dat_dir, 'version.sql') f = open(version_file, 'w') if next_version == default_version: stream = properties.get('version_insert_statement') else: stream = properties.get('version_update_statement') stream = stream.replace('<version>', next_version) f.write(stream) f.close() # FIXME: remove? # sqlScript=self.getSqlVersionStatement(versions, version) # projectHelper.writeFile(datFolder+os.sep+'version.sql', sqlScript) # create the environment folders in the dat folder for environment in environments: os.mkdir(os.path.join(dat_dir, environment)) # create the environment script in the dat folder. if schema == version_schema and next_version == default_version: environment_file = os.path.join(dat_dir, environment, 'environment.sql') f = open(environment_file, 'w') stream = properties.get('environment_insert_statement') stream = stream.replace('<environment>', environment) f.write(stream) f.close() # create the ddl folder ddl_dir = os.path.join(schema_dir, 'ddl') os.mkdir(ddl_dir) # create the object folders in the ddl folder for obj in objects: os.mkdir(os.path.join(ddl_dir, obj)) # create the template code on create. if schema == version_schema and next_version == default_version: for obj in objects: object_dir = os.path.join(template_dir, obj) if os.path.exists(object_dir): files = Files.list(File(object_dir)) for file in files: shutil.copyfile( file.get_url(), os.path.join(ddl_dir, obj, file.tail())) print("version {} created.".format(next_version))