|
1 | | -import toml |
2 | 1 | import re |
3 | 2 | import sys |
4 | 3 |
|
5 | 4 |
|
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 |
7 | 7 | with open(pyproject_path, "r") as f: |
8 | 8 | content = f.read() |
9 | 9 |
|
10 | 10 | 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) |
12 | 19 | 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}") |
14 | 21 | sys.exit(1) |
15 | 22 |
|
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 |
24 | 24 | updated_content = re.sub( |
25 | 25 | rf'(^name\s*=\s*")({re.escape(existing_name)})(")', |
26 | | - rf"\1{new_name}\3", |
| 26 | + rf"\1{pep_503_new_package_name}\3", |
27 | 27 | content, |
28 | 28 | flags=re.MULTILINE, |
29 | 29 | ) |
30 | 30 |
|
| 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 |
31 | 54 | with open(pyproject_path, "w") as f: |
32 | 55 | f.write(updated_content) |
33 | 56 |
|
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 | + ) |
35 | 61 |
|
36 | 62 |
|
37 | 63 | if __name__ == "__main__": |
38 | 64 | 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>") |
40 | 66 | sys.exit(1) |
41 | 67 |
|
42 | 68 | pyproject_path = sys.argv[1] |
43 | | - validator_name = sys.argv[2] |
| 69 | + pep_503_new_package_name = sys.argv[2] |
44 | 70 |
|
45 | | - add_package_name_prefix(pyproject_path, validator_name) |
| 71 | + add_package_name_prefix(pyproject_path, pep_503_new_package_name) |
0 commit comments