-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprepare_pypi_release.py
More file actions
132 lines (108 loc) · 4.98 KB
/
prepare_pypi_release.py
File metadata and controls
132 lines (108 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os
import sys
import io
import shutil
import subprocess
import zipfile
import requests
# Download SDK directly to zip object
def download_sdk_zip(url):
response = requests.get(url, timeout=30)
if response.status_code == 200:
return zipfile.ZipFile(io.BytesIO(response.content))
else:
raise Exception(f"Failed to download SDK zip from {url}")
def download_file(url):
response = requests.get(url, timeout=30)
if response.status_code == 200:
return response.text
else:
raise Exception(f"Failed to download file from {url}")
# Clean up and remove unnecessary stuff from lib3mf directory
def clean_lib3mf_directory(lib3mf_dir):
valid_extensions = {'.so', '.dylib', '.dll', 'Lib3MF.py', '__init__.py'}
before_path = os.getcwd()
os.chdir(lib3mf_dir)
try:
for item in os.listdir():
if any(item.endswith(ext) for ext in valid_extensions):
continue
if os.path.isdir(item):
shutil.rmtree(item)
else:
os.unlink(item)
finally:
os.chdir(before_path)
print("Remove unnecessary files and cleaned up the lib3mf directory")
# Extract the libraries and necessary files to the lib3mf directory
def extract_bin_and_bindings(zip_file, lib3mf_dir):
bin_dir = None
bindings_python_file = None
for file in zip_file.namelist():
if 'Bin/' in file and bin_dir is None:
bin_dir = os.path.dirname(file)
if 'Bindings/Python/Lib3MF.py' in file:
bindings_python_file = file
if bin_dir is None or bindings_python_file is None:
raise FileNotFoundError("Required directories or files not found in the SDK zip")
for file in zip_file.namelist():
if file.startswith(bin_dir) and not file.endswith('/'):
destination_path = os.path.join(lib3mf_dir, os.path.basename(file))
with zip_file.open(file) as source, open(destination_path, "wb") as target:
shutil.copyfileobj(source, target)
if str(file).endswith('Lib3MF.py'):
destination_path = os.path.join(lib3mf_dir, os.path.basename(file))
with zip_file.open(file) as source, open(destination_path, "wb") as target:
shutil.copyfileobj(source, target)
print(f"Copied the latest version of bindings to the lib3mf directory")
clean_lib3mf_directory(lib3mf_dir)
# Update the "real" README file
def update_readme(version):
# Download the README file
readme_file = "README-base.md"
readme_url = f"https://raw.githubusercontent.com/3MFConsortium/lib3mf/release/{version}/README.md"
readme_content = download_file(readme_url)
# Read the example script content
with open("examples/create_cube_example_complete.py", "r", encoding="utf-8") as file:
create_cube_example_complete = file.read()
# Insert the example before the documentation section
marker = "## Documentation"
if marker in readme_content:
readme_parts = readme_content.split(marker, 1)
readme_content = readme_parts[0] + "\n\n## Example (Create Cube)\n\n```python\n" + create_cube_example_complete + "\n```\n\n" + marker + readme_parts[1]
else:
# Fallback: append the example to the end
readme_content = readme_content + "\n\n## Example (Create Cube)\n\n```python\n" + create_cube_example_complete + "\n```\n"
with open(readme_file, "w", encoding="utf-8") as file:
file.write(readme_content)
print(f"Updated {readme_file} with version {version} from README URL {readme_url}")
# Is it there already ?
def check_version_exists_on_pypi(package_name, version):
url = f"https://pypi.org/pypi/{package_name}/{version}/json"
response = requests.get(url)
return response.status_code == 200
# Deal with git also here
def git_commit_and_push(version, update_existing=False):
try:
commit_message = f"Updating Release version {version}" if update_existing else f"Release version {version}"
subprocess.run(['git', 'add', '.'], check=True)
subprocess.run(['git', 'commit', '-m', commit_message], check=True)
subprocess.run(['git', 'push'], check=True)
print(f"Successfully committed and pushed: {commit_message}")
except subprocess.CalledProcessError as e:
print(f"An error occurred while trying to commit and push: {e}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python create_pypi_release.py <version>")
sys.exit(1)
version = sys.argv[1]
update_readme(version)
sdk_url = f"https://github.com/3MFConsortium/lib3mf/releases/download/v{version}/lib3mf_sdk_v{version}.zip"
zip_file = download_sdk_zip(sdk_url)
lib3mf_dir = os.path.join(os.path.dirname(__file__), 'lib3mf')
if not os.path.exists(lib3mf_dir):
os.makedirs(lib3mf_dir)
extract_bin_and_bindings(zip_file, lib3mf_dir)
package_name = "lib3mf"
update_existing = check_version_exists_on_pypi(package_name, version)
git_commit_and_push(version, update_existing)