|
| 1 | +import os, logging |
| 2 | +import tomli, tomli_w |
| 3 | + |
| 4 | +pkg_name = 'translate-messages' |
| 5 | + |
| 6 | +logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') |
| 7 | + |
| 8 | +def update_changelog_url(): |
| 9 | + pyproject_path = os.path.join(os.path.dirname(__file__), '../pyproject.toml') |
| 10 | + logging.debug(f'Reading pyproject.toml from: {pyproject_path}') |
| 11 | + if not os.path.exists(pyproject_path): |
| 12 | + return logging.error(f'pyproject.toml file not found at {pyproject_path}') |
| 13 | + |
| 14 | + try: # load pyproject.toml |
| 15 | + with open(pyproject_path, 'rb') as file : pyproject = tomli.load(file) |
| 16 | + logging.debug('Successfully loaded pyproject.toml!') |
| 17 | + except Exception as err: |
| 18 | + return logging.exception(f'Error loading pyproject.toml: {err}') |
| 19 | + |
| 20 | + version = pyproject.get('project', {}).get('version') |
| 21 | + if not version: |
| 22 | + return logging.error('Version not found in pyproject.toml.') |
| 23 | + |
| 24 | + changelog_url = f'https://github.com/adamlui/python-utils/releases/tag/{pkg_name}-{version}' |
| 25 | + logging.debug(f'Generated changelog URL: {changelog_url}') |
| 26 | + |
| 27 | + if 'urls' not in pyproject['project']: |
| 28 | + logging.debug('Adding [project.urls] section to pyproject.toml...') |
| 29 | + pyproject['project']['urls'] = {} |
| 30 | + |
| 31 | + if 'Changelog' in pyproject['project']['urls']: |
| 32 | + logging.debug('Replacing existing Changelog URL...') |
| 33 | + else: |
| 34 | + logging.debug('Adding new Changelog URL...') |
| 35 | + pyproject['project']['urls']['Changelog'] = changelog_url |
| 36 | + |
| 37 | + try: # write to pyproject.toml |
| 38 | + with open(pyproject_path, 'wb') as file : tomli_w.dump(pyproject, file) |
| 39 | + logging.info('Updated changelog URL successfully in pyproject.toml!') |
| 40 | + except Exception as err: |
| 41 | + logging.exception(f'Error writing to pyproject.toml: {err}') |
| 42 | + |
| 43 | +update_changelog_url() |
0 commit comments