| 19 | |
| 20 | |
| 21 | def create_dist_metadata( |
| 22 | project_name, # type: str |
| 23 | version, # type: str |
| 24 | requires_python=None, # type: Optional[str] |
| 25 | requires_dists=(), # type: Iterable[str] |
| 26 | location=None, # type: Optional[str] |
| 27 | metadata_type=MetadataType.DIST_INFO, # type: MetadataType.Value |
| 28 | ): |
| 29 | # type: (...) -> DistMetadata |
| 30 | |
| 31 | pkg_info = Message() |
| 32 | pkg_info.add_header("Name", project_name) |
| 33 | pkg_info.add_header("Version", version) |
| 34 | if requires_python: |
| 35 | pkg_info.add_header("Requires-Python", requires_python) |
| 36 | for requirement in requires_dists: |
| 37 | pkg_info.add_header("Requires-Dist", requirement) |
| 38 | |
| 39 | resolved_location = location or safe_mkdtemp() |
| 40 | metadata_dir = "{project_name}-{version}.{suffix}".format( |
| 41 | project_name=project_name, |
| 42 | version=version, |
| 43 | suffix="dist-info" if metadata_type is MetadataType.DIST_INFO else "egg-info", |
| 44 | ) |
| 45 | metadata_file_name = "METADATA" if metadata_type is MetadataType.DIST_INFO else "PKG-INFO" |
| 46 | |
| 47 | additional_metadata_files = () # type: Tuple[Text, ...] |
| 48 | read_function = None # type: Optional[Callable[[Text], bytes]] |
| 49 | if os.path.isdir(resolved_location): |
| 50 | additional_metadata_files = tuple( |
| 51 | os.path.relpath(metadata_path, resolved_location) |
| 52 | for metadata_path in glob.glob(os.path.join(resolved_location, metadata_dir, "*")) |
| 53 | if os.path.basename(metadata_path) != metadata_file_name |
| 54 | ) |
| 55 | if additional_metadata_files: |
| 56 | |
| 57 | def read_function(rel_path): |
| 58 | # type: (Text) -> bytes |
| 59 | with open(os.path.join(resolved_location, rel_path), "rb") as fp: |
| 60 | return fp.read() |
| 61 | |
| 62 | return DistMetadata.from_metadata_files( |
| 63 | MetadataFiles( |
| 64 | metadata=DistMetadataFile( |
| 65 | type=metadata_type, |
| 66 | location=resolved_location, |
| 67 | rel_path=os.path.join(metadata_dir, metadata_file_name), |
| 68 | project_name=ProjectName(project_name), |
| 69 | version=Version(version), |
| 70 | pkg_info=pkg_info, |
| 71 | ), |
| 72 | additional_metadata_files=additional_metadata_files, |
| 73 | read_function=read_function, |
| 74 | ) |
| 75 | ) |