|
2 | 2 | try: |
3 | 3 | import pathlib |
4 | 4 | except ImportError: # py2 |
5 | | - import pathlib2 |
6 | | -import pkg_resources |
| 5 | + import pathlib2 as pathlib |
| 6 | +try: |
| 7 | + from importlib import resources as importlib_resources |
| 8 | +except ImportError: # py < 3.7 |
| 9 | + import importlib_resources |
7 | 10 | from packaging.version import Version |
8 | 11 | import autorelease |
9 | 12 |
|
10 | 13 | import git |
11 | 14 | import re |
12 | 15 |
|
13 | 16 | def extract_github_owner_and_repo(url): |
14 | | - github_url_re = ".*github.com:(?P<owner>.*)/(?P<repo>.*)" |
| 17 | + github_url_re = r".*github.com:(?P<owner>.*)/(?P<repo>.*)" |
15 | 18 | m = re.match(github_url_re, url) |
| 19 | + if m is None: |
| 20 | + raise ValueError(f"Unable to parse GitHub URL: {url}") |
16 | 21 | owner = m.group('owner') |
17 | 22 | repo = m.group('repo') |
18 | 23 | if repo.endswith('.git'): |
@@ -70,23 +75,34 @@ def get_substitution_mapping(config=None): |
70 | 75 |
|
71 | 76 | def vendor(resources, base_path, relative_target_dir, substitutions): |
72 | 77 | for resource in resources: |
73 | | - orig_loc = pkg_resources.resource_filename('autorelease', resource) |
74 | | - name = pathlib.Path(orig_loc).name |
| 78 | + # Use importlib.resources to get the resource content |
| 79 | + try: |
| 80 | + # For Python 3.9+ |
| 81 | + resource_files = importlib_resources.files('autorelease') |
| 82 | + resource_path = resource_files / resource |
| 83 | + with resource_path.open('r') as rfile: |
| 84 | + template_content = rfile.read() |
| 85 | + except AttributeError: |
| 86 | + # For Python 3.7-3.8 |
| 87 | + with importlib_resources.path('autorelease', resource) as resource_path: |
| 88 | + with open(resource_path, mode='r') as rfile: |
| 89 | + template_content = rfile.read() |
| 90 | + |
| 91 | + name = pathlib.Path(resource).name |
75 | 92 | target_dir = base_path / relative_target_dir |
76 | 93 | target_dir.mkdir(parents=True, exist_ok=True) |
77 | 94 | target_loc = base_path / relative_target_dir / name |
78 | | - # print(f"cp {orig_loc} {target_loc}") |
79 | | - with open(orig_loc, mode='r') as rfile: |
80 | | - template = string.Template(rfile.read()) |
| 95 | + # print(f"cp {resource} {target_loc}") |
| 96 | + template = string.Template(template_content) |
81 | 97 |
|
82 | 98 | with open(target_loc, mode='w') as wfile: |
83 | 99 | wfile.write(template.substitute(**substitutions)) |
84 | 100 |
|
| 101 | + |
85 | 102 | def vendor_actions(base_path): |
86 | 103 | resources = ['autorelease-default-env.sh', 'autorelease-prep.yml', |
87 | 104 | 'autorelease-gh-rel.yml', 'autorelease-deploy.yml'] |
88 | 105 | resources = ['gh_actions_stages/' + res for res in resources] |
89 | 106 | target_dir = pathlib.Path('.github/workflows') |
90 | 107 | substitutions = get_substitution_mapping() |
91 | 108 | vendor(resources, base_path, target_dir, substitutions) |
92 | | - |
|
0 commit comments