def do_import( project: Project, filename: str, format: str | None = None, options: Namespace | None = None, ) -> None: """Import project metadata from given file. :param project: the project instance :param filename: the file name :param format: the file format, or guess if not given. :param options: other options parsed to the CLI. """ if not format: for key in FORMATS: if FORMATS[key].check_fingerprint(project, filename): break else: raise PdmUsageError("Can't derive the file format automatically, " "please specify it via '-f/--format' option.") else: key = format if options is None: options = Namespace(dev=False, group=None) project_data, settings = FORMATS[key].convert(project, filename, options) pyproject = project.pyproject or atoml.document() if "tool" not in pyproject or "pdm" not in pyproject[ "tool"]: # type: ignore pyproject.setdefault("tool", {})["pdm"] = atoml.table() if "project" not in pyproject: pyproject.add("project", atoml.table()) # type: ignore pyproject["project"].add( # type: ignore atoml.comment("PEP 621 project metadata")) pyproject["project"].add( # type: ignore atoml.comment("See https://www.python.org/dev/peps/pep-0621/")) merge_dictionary(pyproject["project"], project_data) # type: ignore merge_dictionary(pyproject["tool"]["pdm"], settings) # type: ignore pyproject["build-system"] = { "requires": ["pdm-pep517"], "build-backend": "pdm.pep517.api", } project.pyproject = cast(dict, pyproject) project.write_pyproject()
def format_lockfile( mapping: dict[str, Candidate], fetched_dependencies: dict[str, list[Requirement]], summary_collection: dict[str, str], ) -> dict: """Format lock file from a dict of resolved candidates, a mapping of dependencies and a collection of package summaries. """ packages = atoml.aot() file_hashes = atoml.table() for k, v in sorted(mapping.items()): base = atoml.table() base.update(v.as_lockfile_entry()) # type: ignore base.add("summary", summary_collection[strip_extras(k)[0]]) deps = make_array(sorted(r.as_line() for r in fetched_dependencies[k]), True) if len(deps) > 0: base.add("dependencies", deps) packages.append(base) # type: ignore if v.hashes: key = f"{strip_extras(k)[0]} {v.version}" if key in file_hashes: continue array = atoml.array().multiline(True) for filename, hash_value in v.hashes.items(): inline = make_inline_table({ "file": filename, "hash": hash_value }) array.append(inline) # type: ignore if array: file_hashes.add(key, array) doc = atoml.document() doc.add("package", packages) # type: ignore metadata = atoml.table() metadata.add("files", file_hashes) doc.add("metadata", metadata) # type: ignore return cast(dict, doc)
def migrate_pyproject(project: Project) -> None: """Migrate the legacy pyproject format to PEP 621""" if project.pyproject and "project" in project.pyproject: pyproject = project.pyproject settings = {} updated_fields = [] for field in ("includes", "excludes", "build", "package-dir"): if field in pyproject["project"]: updated_fields.append(field) settings[field] = pyproject["project"].pop(field) if "dev-dependencies" in pyproject["project"]: if pyproject["project"]["dev-dependencies"]: settings["dev-dependencies"] = { "dev": pyproject["project"]["dev-dependencies"] } del pyproject["project"]["dev-dependencies"] updated_fields.append("dev-dependencies") if updated_fields: if "tool" not in pyproject or "pdm" not in pyproject["tool"]: pyproject.setdefault("tool", {})["pdm"] = atoml.table() pyproject["tool"]["pdm"].update(settings) project.pyproject = pyproject project.write_pyproject() project.core.ui.echo( f"{termui.yellow('[AUTO-MIGRATION]')} These fields are moved from " f"[project] to [tool.pdm] table: {updated_fields}", err=True, ) return if not project.pyproject_file.exists( ) or not FORMATS["legacy"].check_fingerprint(project, project.pyproject_file): return project.core.ui.echo( f"{termui.yellow('[AUTO-MIGRATION]')} Legacy pdm 0.x metadata detected, " "migrating to PEP 621...", err=True, ) do_import(project, str(project.pyproject_file), "legacy") project.core.ui.echo( termui.green("pyproject.toml") + termui.yellow( " has been migrated to PEP 621 successfully. " "Now you can safely delete the legacy metadata under [tool.pdm] table." ), err=True, )
def meta(self) -> Metadata: if not self.pyproject: self.pyproject = {"project": atoml.table()} m = Metadata(self.pyproject_file, self.pyproject.get("project", {})) m._metadata = self.pyproject.get("project", {}) return m