Skip to content

Commit 1d894a6

Browse files
updated CI to inject setuptools section in pyproject toml
1 parent ea0d27a commit 1d894a6

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

.github/actions/validator_pypi_publish/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ runs:
6464
cd validator
6565
CONCATANATED_NAME=$(python concat_name.py ${{ inputs.validator }})
6666
NEW_PEP_PACKAGE_NAME=$(python package_name_normalization.py $CONCATANATED_NAME)
67-
python add_build_prefix.py ./pyproject.toml $CONCATANATED_NAME
6867
mv ./validator ./$NEW_PEP_PACKAGE_NAME
68+
python add_build_prefix.py ./pyproject.toml $NEW_PEP_PACKAGE_NAME
6969
7070
- name: Build & Upload
7171
shell: bash
Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,71 @@
1-
import toml
21
import re
32
import sys
43

54

6-
def add_package_name_prefix(pyproject_path, validator_name):
5+
def add_package_name_prefix(pyproject_path, pep_503_new_package_name):
6+
# Read the existing pyproject.toml file
77
with open(pyproject_path, "r") as f:
88
content = f.read()
99

1010
try:
11-
toml_data = toml.loads(content)
11+
# Check for the presence of the project.name key using a regular expression
12+
project_name_match = re.search(
13+
r'^name\s*=\s*"(.*?)"', content, flags=re.MULTILINE
14+
)
15+
if not project_name_match:
16+
print(f"Could not find the 'project.name' in {pyproject_path}.")
17+
sys.exit(1)
18+
existing_name = project_name_match.group(1)
1219
except Exception as e:
13-
print(f"Failed to parse {pyproject_path}: {e}")
20+
print(f"Failed to parse project name in {pyproject_path}: {e}")
1421
sys.exit(1)
1522

16-
existing_name = toml_data.get("project", {}).get("name")
17-
if not existing_name:
18-
print(f"Could not find the 'project.name' in {pyproject_path}.")
19-
sys.exit(1)
20-
21-
validator_name = validator_name.split("/")
22-
new_name = f"{validator_name[0]}-grhub-{validator_name[1]}"
23-
23+
# Update the project name to the new PEP 503-compliant name
2424
updated_content = re.sub(
2525
rf'(^name\s*=\s*")({re.escape(existing_name)})(")',
26-
rf"\1{new_name}\3",
26+
rf"\1{pep_503_new_package_name}\3",
2727
content,
2828
flags=re.MULTILINE,
2929
)
3030

31+
# Now we manually add the [tool.setuptools] section with the new folder name
32+
# If the section already exists, we append the correct package name
33+
setuptools_section = f"""
34+
35+
[tool.setuptools]
36+
packages = ["{pep_503_new_package_name}"]
37+
38+
"""
39+
40+
# Check if the [tool.setuptools] section already exists
41+
if "[tool.setuptools]" in updated_content:
42+
# If it exists, update the packages value
43+
updated_content = re.sub(
44+
r"(^\[tool\.setuptools\].*?^packages\s*=\s*\[.*?\])",
45+
f'[tool.setuptools]\npackages = ["{pep_503_new_package_name}"]',
46+
updated_content,
47+
flags=re.DOTALL | re.MULTILINE,
48+
)
49+
else:
50+
# If the section doesn't exist, append it at the end of the file
51+
updated_content += setuptools_section
52+
53+
# Write the modified content back to the pyproject.toml file
3154
with open(pyproject_path, "w") as f:
3255
f.write(updated_content)
3356

34-
print(f"Updated project name to '{new_name}' in {pyproject_path}")
57+
print(
58+
"Updated project name to "
59+
"'{pep_503_new_package_name}' and added package folder in {pyproject_path}"
60+
)
3561

3662

3763
if __name__ == "__main__":
3864
if len(sys.argv) < 3:
39-
print("Usage: python script.py <pyproject_path> <validator-name>")
65+
print("Usage: python script.py <pyproject_path> <pep_503_new_package_name>")
4066
sys.exit(1)
4167

4268
pyproject_path = sys.argv[1]
43-
validator_name = sys.argv[2]
69+
pep_503_new_package_name = sys.argv[2]
4470

45-
add_package_name_prefix(pyproject_path, validator_name)
71+
add_package_name_prefix(pyproject_path, pep_503_new_package_name)

0 commit comments

Comments
 (0)