2020import textwrap
2121import zipfile
2222
23+
2324TESTS_DIR = pathlib .Path (__file__ ).parent
2425FIXTURES_DIR = os .path .join (TESTS_DIR , "fixtures" )
2526SDIST_FIXTURE = os .path .join (FIXTURES_DIR , "twine-1.5.0.tar.gz" )
@@ -44,10 +45,54 @@ def build_archive(path, name, archive_format, files):
4445 archive .addfile (member , io .BytesIO (data ))
4546 return filepath
4647
47- if archive_format == "zip" :
48+ if archive_format == "zip" or archive_format == "whl" :
4849 with zipfile .ZipFile (filepath , mode = "w" ) as archive :
4950 for mname , content in files .items ():
5051 archive .writestr (mname , textwrap .dedent (content ))
5152 return filepath
5253
5354 raise ValueError (format )
55+
56+
57+ def build_sdist (path , name , version , files ):
58+ content = {
59+ f"{ name } -{ version } /README" : "README" ,
60+ f"{ name } -{ version } /PKG-INFO" : f"""\
61+ Metadata-Version: 1.1
62+ Name: { name }
63+ Version: { version }
64+ """ ,
65+ }
66+
67+ # Remap file paths to be under archive root folder.
68+ root = f"{ name } -{ version } "
69+ for key , value in files .items ():
70+ content [f"{ root } /{ key } " ] = value
71+
72+ return build_archive (path , f"{ name } -{ version } " , "tar.gz" , content )
73+
74+
75+ def build_wheel (path , name , version , files ):
76+ # This does not generate valid a wheel file: the archive lacks the
77+ # .dist-info/RECORD member with the content listing. However, the
78+ # generated files pass validation done by twine and are sufficient
79+ # for testing purposes.
80+
81+ content = {
82+ f"{ name } -{ version } .dist-info/WHEEL" : """\
83+ Wheel-Version: 1.0
84+ Generator: test
85+ Root-Is-Purelib: true
86+ Tag: py3-none-any
87+ """ ,
88+ f"{ name } -{ version } .dist-info/METADATA" : f"""\
89+ Metadata-Version: 1.1
90+ Name: { name }
91+ Version: { version }
92+ """ ,
93+ }
94+
95+ # Add wheel content.
96+ content .update (files )
97+
98+ return build_archive (path , f"{ name } -{ version } -py3-none-any" , "whl" , content )
0 commit comments