Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /aibaba-ai_aibaba-ai /libs /cli /scripts /generate_migrations.py
| # type: ignore | |
| """Script to generate migrations for the migration script.""" | |
| import json | |
| import os | |
| import pkgutil | |
| import click | |
| from aibaba_ai_cli.namespaces.migrate.generate.generic import ( | |
| generate_simplified_migrations, | |
| ) | |
| from aibaba_ai_cli.namespaces.migrate.generate.grit import ( | |
| dump_migrations_as_grit, | |
| ) | |
| from aibaba_ai_cli.namespaces.migrate.generate.partner import ( | |
| get_migrations_for_partner_package, | |
| ) | |
| def cli(): | |
| """Migration scripts management.""" | |
| pass | |
| def generic( | |
| pkg1: str, pkg2: str, output: str, filter_by_all: bool, format: str | |
| ) -> None: | |
| """Generate a migration script.""" | |
| click.echo("Migration script generated.") | |
| migrations = generate_simplified_migrations(pkg1, pkg2, filter_by_all=filter_by_all) | |
| if output is not None: | |
| name = output.removesuffix(".json").removesuffix(".grit") | |
| else: | |
| name = f"{pkg1}_to_{pkg2}" | |
| if output is None: | |
| output = f"{name}.json" if format == "json" else f"{name}.grit" | |
| if format == "json": | |
| dumped = json.dumps(migrations, indent=2, sort_keys=True) | |
| else: | |
| dumped = dump_migrations_as_grit(name, migrations) | |
| with open(output, "w") as f: | |
| f.write(dumped) | |
| def handle_partner(pkg: str, output: str = None): | |
| migrations = get_migrations_for_partner_package(pkg) | |
| # Run with python 3.9+ | |
| name = pkg.removeprefix("langchain_") | |
| data = dump_migrations_as_grit(name, migrations) | |
| output_name = f"{name}.grit" if output is None else output | |
| if migrations: | |
| with open(output_name, "w") as f: | |
| f.write(data) | |
| click.secho(f"Aibaba AI migration script saved to {output_name}") | |
| else: | |
| click.secho(f"No migrations found for {pkg}", fg="yellow") | |
| def partner(pkg: str, output: str) -> None: | |
| """Generate migration scripts specifically for Aibaba AI modules.""" | |
| click.echo("Migration script for Aibaba AI generated.") | |
| handle_partner(pkg, output) | |
| def json_to_grit(json_file: str) -> None: | |
| """Generate a Grit migration from an old JSON migration file.""" | |
| with open(json_file, "r") as f: | |
| migrations = json.load(f) | |
| name = os.path.basename(json_file).removesuffix(".json").removesuffix(".grit") | |
| data = dump_migrations_as_grit(name, migrations) | |
| output_name = f"{name}.grit" | |
| with open(output_name, "w") as f: | |
| f.write(data) | |
| click.secho(f"GritQL migration script saved to {output_name}") | |
| def all_installed_partner_pkgs() -> None: | |
| """Generate migration scripts for all Aibaba AI modules.""" | |
| # Will generate migrations for all partner packages. | |
| # Define as "langchain_<partner_name>". | |
| # First let's determine which packages are installed in the environment | |
| # and then generate migrations for them. | |
| langchain_pkgs = [ | |
| name | |
| for _, name, _ in pkgutil.iter_modules() | |
| if name.startswith("langchain_") | |
| and name not in {"aibaba_ai_core", "langchain_cli", "aibaba_ai_community"} | |
| ] | |
| for pkg in langchain_pkgs: | |
| handle_partner(pkg) | |
| if __name__ == "__main__": | |
| cli() | |
Xet Storage Details
- Size:
- 3.83 kB
- Xet hash:
- f4ff808fa5c2c68385c449b141662a06ee656a65cf9b4ffc6367215de427147c
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.