def create_links(): if not ask("Create symlinks?"): return print("Creating symlinks") print("Dotfiles path:", DOTFILES_DIR) for src, dest in LINKS.items(): src_path = DOTFILES_DIR / src dest_path = HOME_DIR / dest dest_dir = dest_path.parent if not dest_dir.exists(): print(f"Creating {dest_dir}") dest_dir.mkdir(parents=True) print(f"Linking {dest_path} -> {src_path}") if dest_path.is_symlink(): dest_links_to = dest_path.resolve() if dest_links_to != src_path and ask( f"\t{dest_path} is a symlink to {dest_links_to}, overwrite?" ): link(src_path, dest_path, unlink=True) elif dest_path.is_file(): if ask(f"\t{dest_path} is a regular file, overwrite?"): link(src_path, dest_path, unlink=True) else: link(src_path, dest_path)
def import_migrations_from_app(app): migrations_dir = lib.dirs(settings, app=app, format=["migrations"], check=True) if not migrations_dir: return print("[ %s ]" % app) existant_migrations = [] for m in sorted(os.listdir(dest_dir)): if not m.startswith('_') and m.endswith('.py'): mp = os.path.join(dest_dir, m) with open(mp) as f: if "# {} module".format(app) in f.read(): existant_migrations.append(mp) if existant_migrations: rc = lib.ask("The migrations for {} alerady installed.\nDo You wanna remove its?".format(app)) if rc == 'n': return for m in existant_migrations: print("removing " + m) os.unlink(m) print("") migrations = [(os.path.join(migrations_dir, m), re.sub(r"^[\d_]+", "", m)[:-3]) for m in sorted(os.listdir(migrations_dir)) if not m.startswith('_') and m.endswith('.py')] for path, name in migrations: ts = datetime.utcnow().strftime("%Y_%m_%d_%H%M%S%f") dest_file = os.path.join(dest_dir, ts + "_" + name + ".py") print("creating " + dest_file) shutil.copy2(path, dest_file) lib.insert_in_python(dest_file, ["from"], ["# {} module. please dont remove this line".format(app)], in_end=True, ignore_pass=True)
def execute(argv, argv0, engine): if '--help' in argv or '-h' in argv: usage(argv0) os.environ["AIOWEB_ENV"] = "test" environment = os.getenv("AIOWEB_ENV", "development") os.environ.setdefault("AIOWEB_SETTINGS_MODULE", "settings") import lib, inflection from aioweb import settings tests_dir = lib.dirs(settings, format=["tests_controllers"], check=True) if not tests_dir: print("No controller found!") sys.exit(0) controllers = [m[:-3] for m in os.listdir(tests_dir) if m.endswith(".py") and not m.startswith("__")] if '--list' in argv: [print(m[:-3]) for m in os.listdir(tests_dir) if m.endswith(".py") and not m.startswith("__")] sys.exit(0) for ctrl in controllers: print(ctrl) test_file = os.path.join(tests_dir, ctrl + ".py") print("testing " + ctrl) rc = None if os.system("python3 " + test_file) != 0 and rc != 'a': rc = lib.ask("Do You wanna continue?", ['y', 'n', 'a']) if rc == 'n': sys.exit(1)
def execute(argv, argv0, engine): import lib, inflection os.environ.setdefault("AIOWEB_SETTINGS_MODULE", "settings") from aioweb import settings sys.path.append(os.getcwd()) if len(argv) < 2: usage(argv0) controller_name = inflection.camelize(argv[0]) + "Controller" controller_file_name = inflection.underscore(argv[0]) + ".py" methods = argv[1:] dest_file = os.path.join(lib.dirs(settings, format=["controllers"]), controller_file_name) os.makedirs(os.path.dirname(dest_file), exist_ok=True) template = lib.get_template("controllers/simple_controller.py", settings) if os.path.exists(dest_file): if lib.ask("{} already exists!\nDo you wanna replace it?".format(dest_file)) == 'n': print("Controller generation was aborted!") return print("creating {}".format(dest_file)) with open(template, "r") as f: controller_code = f.read().replace("CLASS", controller_name) with open(dest_file, "w") as df: df.write(controller_code) for method in methods: engine["commands"]["generate"]["controller_method"]([argv[0], method], argv0, engine) engine["commands"]["generate"]["test"]["controller"](argv, argv0, engine)
def execute(argv, argv0, engine): import lib os.environ.setdefault("AIOWEB_SETTINGS_MODULE", "settings") from aioweb import settings sys.path.append(os.getcwd()) if len(argv) < 1 or '-h' in argv or '--help' in argv: usage(argv0) seeds_dir = lib.dirs(settings, format=["seeds"]) seed_name = argv[0] try: seeds = [os.path.join(seeds_dir, f) for f in os.listdir(seeds_dir) if seed_name in f] except FileNotFoundError: seeds = [] rc = None for m in seeds: if rc not in ['a', 'all']: rc = lib.ask( "\n{} already exists.\nDo you wanna remove it before create migration?".format(m), ['y', 'n', 'e', 'exit', 'all', 'a', 'skip all']) if rc in ['skip all']: break if rc in ['e', 'exit']: print("migration aborted") sys.exit(1) if rc in ['a', 'y', 'all']: print("delete " + m) os.unlink(m) os.system("orator make:seed {} -p {}".format(seed_name, seeds_dir))
def execute(argv, argv0, engine): if not argv: usage(argv0) import lib os.environ.setdefault("AIOWEB_SETTINGS_MODULE", "settings") from aioweb import settings table, model_name, model_class = lib.names(argv[0], ["table", "model", "class"]) dest_path = os.path.join(lib.dirs(settings, format=["tests"]), "models/" + model_name + ".py") os.makedirs(os.path.dirname(dest_path), exist_ok=True) if os.path.exists(dest_path): if lib.ask("{} already exists!\nDo You wanna rewrite it?".format( dest_path)) == 'n': return template = lib.get_template("tests/model.py", settings) with open(template, "r") as f: print("generating " + dest_path) content = f.read().replace("MODEL", model_name).replace( "CLASS", model_class).replace("TABLE", table) with open(dest_path, "w") as df: df.write(content)
def execute(argv, argv0, engine): import lib, inflection os.environ.setdefault("AIOWEB_SETTINGS_MODULE", "settings") from aioweb import settings if len(argv) == 0: usage(argv0) module_full_path = argv chase = engine["commands"] for i, m in enumerate(module_full_path): rc = chase.get(m) if not rc: chase = None break elif type(rc) == str: module_full_path[i] = rc chase = chase.get(rc) else: chase = rc dest_file = os.path.join("wyrm", "modules", *module_full_path) + ".py" if type(chase) == dict: print("CONFLICT: " + '"' + ' '.join(module_full_path) + '" is a module collection!') sys.exit(1) elif os.path.exists(dest_file): rc = lib.ask(dest_file + " already exists!\nDo You wanna rewrite it???") if rc == 'n': sys.exit(0) elif callable(chase): rc = lib.ask("This module already exists! Do You wanna replace it?") if rc == 'n': sys.exit(0) print("generating " + '"' + ' '.join(module_full_path) + '"') os.makedirs(os.path.dirname(dest_file), exist_ok=True) template = lib.get_template("wyrm/module.py", settings) with open(template, "r") as f: module_code = f.read().replace("MODULE_FULL_NAME", ' '.join(module_full_path)) with open(dest_file, "w") as df: df.write(module_code)
def install(groups): for group_name, packages in groups.items(): print(f"{group_name} packages:") for pkg in packages: p = pkg.split("/")[1] print(f"\t- {p}") if ask(f"Install {group_name} packages?"): yay_packages = " ".join(packages) print(f"yay -S --needed {yay_packages}") os.system(f"yay -S --needed {yay_packages}") print()
def execute(argv, argv0, engine): import lib os.environ.setdefault("AIOWEB_SETTINGS_MODULE", "settings") from aioweb import settings sys.path.append(os.getcwd()) if len(argv) <= 1 or '-h' in argv or '--help' in argv: usage(argv0) additional_fields = lib.get_fields_from_argv(argv[2:], usage, argv0) migrations_dir = lib.dirs(settings, format=["migrations"]) table_name = argv[0] migration_name = argv[1] try: migrations = [ os.path.join(migrations_dir, f) for f in os.listdir(migrations_dir) if migration_name in f ] except FileNotFoundError: migrations = [] rc = None for m in migrations: if rc not in ['a', 'all']: rc = lib.ask( "\n{} already exists.\nDo you wanna remove it before create migration?" .format(m), ['y', 'n', 'e', 'exit', 'all', 'a', 'skip all']) if rc in ['skip all']: break if rc in ['e', 'exit']: print("migration aborted") sys.exit(1) if rc in ['a', 'y', 'all']: print("delete " + m) os.unlink(m) os.system("orator make:migration {} -p {} -t {}".format( migration_name, migrations_dir, table_name)) file_name = os.path.join(migrations_dir, [ f for f in sorted(os.listdir(migrations_dir)) if migration_name in f ][-1]) print("patching " + file_name) lib.insert_in_python( file_name, ["def up", "as table:"], ["table.{}('{}')".format(tp, name) for tp, name in additional_fields]) lib.insert_in_python(file_name, ["def down", "as table:"], [ "table.drop_column('{}')".format(name) for tp, name in additional_fields ])
def execute(argv, argv0, engine): if not argv: usage(argv0) import lib os.environ.setdefault("AIOWEB_SETTINGS_MODULE", "settings") from aioweb import settings additional_fields = lib.get_fields_from_argv(argv[1:], usage, argv0) if not additional_fields: additional_fields = [("string", "somefield")] table, model_name, model_class = lib.names(argv[0], ["table", "model", "class"]) factories_dir = lib.dirs(settings, format=["factories"]) os.makedirs(factories_dir, exist_ok=True) dest_path = os.path.join(factories_dir, '{}_factory.py'.format(table)) if os.path.exists(dest_path): if lib.ask(dest_path + " exists\nDo You wanna rewrite it?") == 'n': print("factory generation cancelled") return template = lib.get_template("tests/factory.py", settings) with open(template, "r") as f: print("generating " + dest_path) content = f.read().replace("MODEL", model_name).replace( "CLASS", model_class).replace("TABLE", table) with open(dest_path, "w") as df: df.write(content) lib.insert_in_python( dest_path, ["import"], ['from app.models.{} import {}'.format(model_name, model_class)]) lib.insert_in_python( dest_path, ["return"], map(lambda prm: ' "{}": None, '.format(prm[1]), additional_fields))
def execute(argv, argv0, engine): import lib, inflection, re os.environ.setdefault("AIOWEB_SETTINGS_MODULE", "settings") from aioweb import settings sys.path.append(os.getcwd()) if len(argv) == 0 or '-h' in argv or '--help' in argv or argv[0].startswith('-'): usage(argv0) additional_fields = lib.get_fields_from_argv(argv[1:], usage, argv0) migrations_dir, models_dir = lib.dirs(settings, format=["migrations", "models"]) table, model_name, model_class = lib.names(argv[0], ["table", "model", "class"]) migration_name = "create_{}_table".format(table) model_file = os.path.join(models_dir, "{}.py".format(model_name)) rewrite = True if os.path.exists(model_file): if lib.ask("{} already exists. Do you wanna rewrite it?".format(model_file)) == 'n': rewrite = False if rewrite: print("delete " + model_file) os.unlink(model_file) if rewrite: os.system("orator make:model {} -p app/models".format(inflection.singularize(table).capitalize())) rewrite = True try: rc = [f for f in os.listdir(migrations_dir) if migration_name in f] except FileNotFoundError: rc = [] if len(rc) > 0: if lib.ask( "{} already exists.\nDo you wanna rewrite it?".format("db/migrations/" + rc[0])) == 'n': rewrite = False if rewrite: for f in rc: print("delete " + "db/migrations/" + f) os.unlink(os.path.join(migrations_dir, f)) if rewrite: os.system("orator make:migration {} -p db/migrations/ -C -t {}".format(migration_name, table)) file_name = os.path.join(migrations_dir, [f for f in sorted(os.listdir(migrations_dir)) if migration_name in f][-1]) print("patching " + file_name) lib.insert_in_python(file_name, ["def up", "as table:"], ["table.{}('{}').nullable()".format(tp, name) for tp, name in additional_fields], in_end=True) print("patching " + model_file) with open(model_file, "r") as fr: content = fr.read() wrong_class_name = re.search(r"class ([_a-zA-Z0-9]+)", content).group(1) right_class_name = inflection.camelize(wrong_class_name.lower()) with open(model_file, "w") as f: f.write(re.sub(wrong_class_name, right_class_name, content)) fillable = "__fillable__ = [{}]".format(", ".join( ['"{}"'.format(name) for tp, name in additional_fields] )) lib.insert_in_python(model_file, ["class"], [fillable] + ["# {}{} - {}".format(name, (25 - len(name)) * ' ', tp) for tp, name in additional_fields], ignore_pass=True) print("generating factory") engine["commands"]["generate"]["test"]["factory"](argv, argv0, engine) engine["commands"]["generate"]["test"]["model"](argv[:1], argv0, engine)
def execute(argv, argv0, engine): import lib, inflection, re from aioweb import settings if len(argv) == 0 or '-h' in argv or '--help' in argv: usage(argv0) additional_fields = lib.get_fields_from_argv(argv[1:], usage, argv0) controllers_dir, models_dir, views_dir, configs_dir = lib.dirs(settings, format=["controllers", "models", "views", "config"]) table, model_name, model_class, controller_class = lib.names(argv[0], ["table", "model", "class", 'crud_class']) controller_file = os.path.join(controllers_dir, "{}.py".format(table)) views_dir= os.path.join(views_dir, table) routes_file = os.path.join(configs_dir, "routes.py") rewrite=False replacements= { 'MODEL': model_name, 'MODEL_CLASS': model_class, 'CONTROLLER_CLASS': controller_class, 'TABLE': table, 'MODEL_FIELD_NAMES': ', '.join(list(map(lambda type_name: '"' + type_name[1] + '"', additional_fields))), } controller_code = lib.read_template("crud/controller.py", settings=settings, replacements=replacements) os.makedirs(controllers_dir, exist_ok=True) if os.path.exists(controller_file): if lib.ask(controller_file + " already exists.\nDo you wanna rewrite it?") == 'n': sys.exit(1) print("creating {}...".format(controller_file)) with open(controller_file, "w") as f: f.write(controller_code) with open(routes_file, "r") as f: if not re.search(r"['\"]{}['\"]".format(table), f.read()): print("patching {}".format(routes_file)) lib.insert_in_python(routes_file, ["setup(router)"], ["router.resource('{}', '{}')".format(table, table)], in_end=True) print("") if os.path.exists(views_dir): if lib.ask(views_dir + "/ already exists.\nDo you wanna wipe out it?") == 'n': sys.exit(1) form_fields = "" for tp, name in additional_fields: form_fields+='<div class="field">\n' form_fields+=' <label>{}\n'.format(name) form_fields+=' <input' if tp in ['decimal', 'double', 'integer', 'big_integer', 'float']: form_fields += ' type="number" ' else: form_fields += ' type="text" ' form_fields+='name="{}[{}]" '.format(model_name, name) form_fields+= '{% if model.' + name + ' != None %} ' + 'value="{{ model.' + name + ' }}" ' + '{% endif %}' form_fields+='>' + "</input>" form_fields+="\n" form_fields+=' </label>\n</div>' replacements={ 'MODEL': model_name, 'TABLE': table, 'FORM_FIELDS': form_fields, } os.makedirs(views_dir, exist_ok=True) for view_name in ["index.html", "get.html", "form.html", "edit_page.html", "add.html", "add_page.html", "edit.html", "delete.html"]: view_file = os.path.join(views_dir, view_name) print("creating {}...".format(view_file)) view_code = lib.read_template("crud/{}".format(view_name), settings=settings, replacements=replacements) with open(view_file, "w") as f: f.write(view_code) engine["commands"]["generate"]["test"]["crud"](argv, argv0, engine)
help='threads count', default=config.threads_count) parser.add_argument('-y', help="yes all", default=False, action='store_true') return parser.parse_args() if __name__ == "__main__": """ usage: python bot.py --threads=2 --workflow=SyncWorkflow """ start = time.time() ids = fetch() args = parse_args() threads_count = int(args.threads) workflow = args.workflow print "syncing {num} records with workflow: {workflow}, threads: {threads_count}".format( num=len(ids), workflow=workflow, threads_count=threads_count) if lib.ask(args.y): pool = ThreadPool(threads_count) for _ in tqdm.tqdm(pool.imap(sync, ids), total=len(ids)): pass print "Finished. Took: " + str(time.time() - start) + "s"
def execute(argv, argv0, engine): import lib if len(argv) == 0 or len(argv) > 2 or argv[0] in [ '-h', '--help' ] or argv[-1] in ['-h', '--help']: usage(argv0) sys.exit(1) name = argv[0] dirname = argv[-1] appdir = dirname if len(argv) >= 2: appdir = os.path.join(dirname, name) gen_path = lib.get_template("new") if not gen_path: print("ERROR: can't locate generator templates!") sys.exit(1) if os.path.exists(appdir): print("cleaning {}".format(os.path.abspath(appdir))) if os.listdir(appdir): rc = lib.ask( "{} is not empty.\nDo you wanna empty this folder?".format( os.path.abspath(appdir))) if rc != 'y': print("Generation was aborted.") sys.exit(1) for entry in os.listdir(appdir): path = os.path.join(appdir, entry) if os.path.isdir(path): shutil.rmtree(path) else: os.unlink(path) print("coping files...") for entry in os.listdir(gen_path): src = os.path.join(gen_path, entry) dest = os.path.join(appdir, entry) if os.path.isdir(src): shutil.copytree(src, dest) else: shutil.copy2(src, dest) else: if appdir != dirname: os.makedirs(dirname, exist_ok=True) print("coping files...") shutil.copytree(gen_path, appdir) os.makedirs(os.path.join(appdir, "wyrm/generators"), exist_ok=True) os.makedirs(os.path.join(appdir, "wyrm/modules"), exist_ok=True) if appdir != dirname: print("patching setup.py") print(lib.get_template("setup.py")) shutil.copy2(lib.get_template("setup.cfg"), os.path.join(dirname, "setup.cfg")) shutil.copy2(lib.get_template("setup.py"), os.path.join(dirname, "setup.py")) with open(os.path.join(dirname, "setup.py"), "r") as f: setup_py = f.read().replace("APP_NAME", name) with open(os.path.join(dirname, "setup.py"), "w") as f: f.write(setup_py)
def add_user_to_groups(): if not ask("Add user to groups?"): return for group in GROUPS: os.system(f"sudo gpasswd -a {USERNAME} {group}")