def copy_initial_migration_scripts(args): """Will copy the initial db migration scripts into the alembic versions folder of the application. """ dst_path = [] dst_path.append(get_app_location(args.app)) dst_path.append(args.app) dst_path.append("alembic") dst_path.append("versions") src_path = [] src_path.append(get_app_location(args.base)) src_path.append(args.base) src_path.append("alembic") src_path.append("versions") src = os.path.join(*src_path) dst = os.path.join(*dst_path) dst_files = os.listdir(dst) src_files = os.listdir(src) # Only copy the initial files if the directory is empty and src and # dst isn't the same directory (Happens when installing ringo # itself). if (src == dst) or (len(dst_files) > 1): return for file_name in src_files: full_file_name = os.path.join(src, file_name) if (os.path.isfile(full_file_name)): shutil.copy(full_file_name, dst)
def handle_modul_fields_command(args): """Will print generated SA fields ready to be pasted in the model based on the given form configuration. :args: command args """ path = [] path.append(get_app_location(args.app)) path.append(args.app) path.append("views") path.append("forms") path.append("%s.xml" % args.name) formbar_path = [] formbar_path.append(get_app_location("formbar")) formbar_path.append("contrib") formbar_path.append("generate.py") print "\nGenerating fields for the %s model..." % args.name out = subprocess.check_output([ "python", os.path.join(*formbar_path), "model", os.path.join(*path) ]) print "\n", out
def handle_app_init_command(args): # Create a folder for the application os.makedirs(args.name) print("Created new application folder %s" % args.name) # Get template for the configuration file. template_path = [] template_path.append(get_app_location(args.app)) template_path.append(args.app) template_path.append("scaffolds") template_path.append("basic") template_path.append("development.ini_tmpl") with open(os.path.join(*template_path)) as f: template = f.read() config_file = "production.ini" config_path = [] config_path.append(args.name) config_path.append(config_file) with open(os.path.join(*config_path), "w") as c: template = template.replace("{{package}}", args.app) #template = template.replace("app.title = ringo", "app.title = %s" # % args.name) c.write(template) print("Created new configuration file %s in %s" % (config_file, args.name)) print("\n") print("Next steps:") print("0. cd %s" % args.name) print("1. Adjust settings in your config") print("2. %s-admin db init --config %s" % (args.app, config_file)) print("3. %s-admin fixtures load --config %s" % (args.app, config_file)) print("4. Start application: pserve production.ini")
def handle_modul_add_command(args): """@todo: Docstring for add_modul_add_command. :args: @todo :returns: @todo """ path = [] path.append(get_app_location(args.app)) path.append(args.config) session = get_session(os.path.join(*path)) package = args.app name = args.name clazz = name.capitalize() modul_id = get_next_modulid(package, session) add_model_file(package, name, modul_id, clazz, args.mixin) add_form_file(package, name) add_table_file(package, name) msg = "Added %s modul" % name path = create_new_revision(args, msg) replace_sql_in_revision(path, package, name, session) print "" print "Ready. Next steps:" print "" print "touch %s" % path print "%s-admin db upgrade" % args.app
def add_model_file(package, modul, id, clazz, mixins): target_file = os.path.join(get_app_location(package), package, 'model', '%s.py' % modul) print 'Adding new model file "%s"... ' % target_file, # Build mixins mixinclasses = [] for mixin in mixins: mixinclasses.append(mixinmap[mixin]) try: tablename = modul + 's' label = modul.capitalize() label_plural = label + "s" clazzpath = ".".join([package, 'model', modul, label]) values = { 'modul': modul, 'clazzpath': clazzpath, 'label': label, 'label_plural': label_plural, 'modul': modul, 'table': tablename, 'id': id, 'clazz': clazz, 'mixins': mixinclasses } template = template_lookup.get_template("model.mako") generated = template.render(**values) outfile = open(target_file, 'w+') outfile.write(generated) outfile.close() # Import the new model file. This is needed to be able to # generate the migration script properly. dynamic_import(clazzpath) print 'Ok.' except Exception: print 'Failed.'
def add_fixtures(package, name, session): """Will add the fixtures for the new modul to the fixture files. :package: @todo :name: @todo :session: @todo :returns: @todo """ path = os.path.join(get_app_location(package), package, 'fixtures') modul_fixture = get_modul_fixture(name, package, session) modul_file = _get_fixture_file(path, "*_modules.json") with open(modul_file, "r+") as mf: print 'Adding data to fixture "%s"... ' % modul_file, modul_json = json.load(mf) modul_json.append(modul_fixture) mf.seek(0) mf.write(json.dumps(modul_json, indent=4)) print 'Ok' modul_id = modul_fixture["id"] action_fixtures = get_action_fixtures(session, modul_id) action_file = _get_fixture_file(path, "*_actions.json") with open(action_file, "r+") as af: print 'Adding data to fixture "%s"... ' % action_file, action_json = json.load(af) action_json.extend(action_fixtures) af.seek(0) af.write(json.dumps(action_json, indent=4)) print 'Ok'
def handle_db_fixsequence_command(args): """Will fix the sequences of the primary keys in all tables of a PostgresSQL Database. For each table in the database the function will determine the highest value for the id and set the nextval to the max+1. Background. When inserting items per fixtures the fixture may contain an id for the new item. In this case the id will be set in the database but the internal sequence counter is not updated.""" # Load the SQL Script to fix the sequence sql_path = [] sql_path.append(get_app_location("ringo")) sql_path.append("ringo/scripts/generate_fix_sequence_stmnts.sql") with open(os.path.join(*sql_path)) as sqlfile: sql = sqlfile.read() # Get DB connection and apply loaded SQL statements. path = [] path.append(args.config) engine = get_engine(os.path.join(*path)) conn = engine.connect() result = conn.execute(sql) print "Fixing sequences ... ", for r in result: conn.execute(r[0]) conn.close() print "OK"
def del_model_file(package, modul): target_file = os.path.join(get_app_location(package), package, 'model', '%s.py' % modul) print 'Deleting model file "%s"... ' % target_file, try: os.remove(target_file) print 'Ok.' except Exception: print 'Failed.'
def del_form_file(package, modul): filename = modul + "s" target_file = os.path.join(get_app_location(package), package, 'views', 'forms', '%s.xml' % filename) print 'Deleting form configuration file "%s"... ' % target_file, try: os.remove(target_file) print 'Ok.' except: print 'Failed.'
def load_file(data): if data.startswith("@"): # Load the data from the filesystem. # Path is @package:/path/to/file/relative/to/package/file app = get_app_location(data.split(":")[0].strip("@")) rel_path = data.split(":")[1] full_path = os.path.join(app, rel_path) with open(full_path, "r") as tf: data = tf.read() return data
def get_last_revision_file(args): """Will return the name of the latest revision file of the application""" script_path = [] script_path.append(get_app_location(args.app)) script_path.append(args.app) script_path.append("alembic") script_path.append("versions") cwd = os.getcwd() os.chdir(os.path.join(*script_path)) script_path.append(max([fn for fn in os.listdir(".") if fn.endswith("py")], key=os.path.getctime)) os.chdir(cwd) return os.path.join(*script_path)
def get_last_revision_file(args): """Will return the name of the latest revision file of the application""" script_path = [] script_path.append(get_app_location(args.app)) script_path.append(args.app) script_path.append("alembic") script_path.append("versions") cwd = os.getcwd() os.chdir(os.path.join(*script_path)) script_path.append( max([fn for fn in os.listdir(".") if fn.endswith("py")], key=os.path.getctime)) os.chdir(cwd) return os.path.join(*script_path)
def add_form_file(package, modul): filename = modul + "s" target_file = os.path.join(get_app_location(package), package, 'views', 'forms', '%s.xml' % filename) print 'Adding new form configuration file "%s"... ' % target_file, try: values = {} template = template_lookup.get_template("form.mako") generated = template.render(**values) outfile = open(target_file, 'w+') outfile.write(generated) outfile.close() print 'Ok.' except: print 'Failed.'
def handle_ext_delete_command(args): path = [] path.append(get_app_location(args.app)) path.append(args.config) session = get_session(os.path.join(*path)) modul = _load_modul_by_name(session, args.name.replace("ringo_", "")) if modul: if args.name in extensions: session.delete(modul) transaction.commit() print "Extension %s deleted!" % args.name else: print "Extension %s already deleted!" % args.name else: print "Extension %s not found!" % args.name
def handle_ext_delete_command(args): path = [] path.append(get_app_location(args.app)) path.append(args.config) session = get_session(os.path.join(*path)) modul = get_modul_by_name(args.name.replace("ringo_", ""), session) if modul: if args.name in extensions: session.delete(modul) transaction.commit() print "Extension %s deleted!" % args.name else: print "Extension %s already deleted!" % args.name else: print "Extension %s not found!" % args.name
def handle_ext_init_command(args): path = [] path.append(get_app_location(args.app)) path.append(args.config) session = get_session(os.path.join(*path)) for ext in extensions: if ext == args.name: if get_modul_by_name(ext.replace("ringo_", ""), session): print "Extension %s already added!" % args.name else: extension = dynamic_import("%s." % args.name) _add_modul(extension.modul_config, None, session) print "Extension %s added!" % args.name break else: print "Extension %s not found!" % args.name
def handle_ext_init_command(args): path = [] path.append(get_app_location(args.app)) path.append(args.config) session = get_session(os.path.join(*path)) for ext in extensions: if ext == args.name: if _load_modul_by_name(session, ext.replace("ringo_", "")): print "Extension %s already added!" % args.name else: extension = dynamic_import("%s." % args.name) _add_modul(extension.modul_config, None, session) print "Extension %s added!" % args.name break else: print "Extension %s not found!" % args.name
def handle_modul_delete_command(args): """@todo: Docstring for add_modul_add_command. :args: @todo :returns: @todo """ path = [] path.append(get_app_location(args.app)) path.append(args.config) session = get_session(os.path.join(*path)) package = args.app name = args.name remove_db_entry(name, session) del_model_file(package, name) del_form_file(package, name) del_table_file(package, name)
def handle_user_passwd_command(args): if args.password: password = args.password else: password = password_generator() encrypted_password = encrypt_password(password) path = [] path.append(get_app_location(args.app)) path.append(args.config) session = get_session(os.path.join(*path)) try: user = session.query(User).filter(User.login == args.user).all()[0] except: print "User %s not found in system. You could only alter existing user's passwords" % args.user else: user.password = encrypted_password print "OK! Password for '%s' changed to '%s'" % (args.user, password) finally: transaction.commit()
def get_alembic_config(args, app=None): """Return a alembic configuration :app: Name of the application :returns: Alembic configuration """ config_path = [] config_path.append(get_app_location(args.app)) config_path.append(args.app) config_path.append("alembic") alembic_dir = os.path.join(*config_path) config_path.append("alembic.ini") cfg = Config(os.path.join(*config_path)) if args.config: app_config = get_appsettings_(args.config) cfg.set_main_option("sqlalchemy.url", app_config.get('sqlalchemy.url')) cfg.set_main_option("script_location", alembic_dir) cfg.set_main_option("app_config", args.config) return cfg
def get_alembic_config(args, app=None): """Return a alembic configuration :app: Name of the application :returns: Alembic configuration """ config_path = [] config_path.append(get_app_location(args.app)) config_path.append(args.app) config_path.append("alembic") alembic_dir = os.path.join(*config_path) config_path.append("alembic.ini") cfg = Config(os.path.join(*config_path)) if args.config: app_config = get_appsettings(args.config) cfg.set_main_option("sqlalchemy.url", app_config.get('sqlalchemy.url')) cfg.set_main_option("script_location", alembic_dir) cfg.set_main_option("app_config", args.config) return cfg
def test_get_app_location(): import pkg_resources from ringo.lib.helpers import get_app_location location = pkg_resources.get_distribution("ringo").location result = get_app_location() assert result == location