Skip to content

Commit 21917c0

Browse files
committed
Generalized pkg_name init, reduced catching/logging, moved pyproject_path init to top level
1 parent e5339ed commit 21917c0

2 files changed

Lines changed: 22 additions & 44 deletions

File tree

remove-json-keys/utils/bump.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
11
import os, logging
22
import tomli, tomli_w
33

4-
pkg_name = 'remove-json-keys'
4+
pyproject_path = os.path.join(os.path.dirname(__file__), '../pyproject.toml')
5+
with open(pyproject_path, 'rb') as file:
6+
pkg_name = tomli.load(file)['project']['name']
57

68
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
79

810
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}'
11+
12+
logging.debug(f'Loading {pyproject_path}...')
13+
with open(pyproject_path, 'rb') as file : pyproject = tomli.load(file)
14+
15+
ver_tag = f"{pkg_name}-{pyproject['project']['version']}"
16+
changelog_url = f'https://github.com/adamlui/python-utils/releases/tag/{ver_tag}'
2517
logging.debug(f'Generated changelog URL: {changelog_url}')
2618

2719
if 'urls' not in pyproject['project']:
@@ -33,11 +25,8 @@ def update_changelog_url():
3325
else:
3426
logging.debug('Adding new Changelog URL...')
3527
pyproject['project']['urls']['Changelog'] = changelog_url
28+
with open(pyproject_path, 'wb') as file : tomli_w.dump(pyproject, file)
3629

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}')
30+
logging.info(f"Bumped changelog URL ver tag to '{ver_tag}'!")
4231

4332
update_changelog_url()

translate-messages/utils/bump.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
11
import os, logging
22
import tomli, tomli_w
33

4-
pkg_name = 'translate-messages'
4+
pyproject_path = os.path.join(os.path.dirname(__file__), '../pyproject.toml')
5+
with open(pyproject_path, 'rb') as file:
6+
pkg_name = tomli.load(file)['project']['name']
57

68
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
79

810
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}'
11+
12+
logging.debug(f'Loading {pyproject_path}...')
13+
with open(pyproject_path, 'rb') as file : pyproject = tomli.load(file)
14+
15+
ver_tag = f"{pkg_name}-{pyproject['project']['version']}"
16+
changelog_url = f'https://github.com/adamlui/python-utils/releases/tag/{ver_tag}'
2517
logging.debug(f'Generated changelog URL: {changelog_url}')
2618

2719
if 'urls' not in pyproject['project']:
@@ -33,11 +25,8 @@ def update_changelog_url():
3325
else:
3426
logging.debug('Adding new Changelog URL...')
3527
pyproject['project']['urls']['Changelog'] = changelog_url
28+
with open(pyproject_path, 'wb') as file : tomli_w.dump(pyproject, file)
3629

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}')
30+
logging.info(f"Bumped changelog URL ver tag to '{ver_tag}'!")
4231

4332
update_changelog_url()

0 commit comments

Comments
 (0)