-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor to avoid duplication of config-file source of metadata #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
17cf57c
0da5b14
a44fc5d
390d673
e28a8fa
2f3dff4
b9df9b8
8ed2442
a570f43
995399c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,10 @@ | |
|
|
||
| from autorelease.scripts.vendor import vendor_actions | ||
| from autorelease.scripts.check import run_checks | ||
| from autorelease.utils import split_setup_cfg_path | ||
| from autorelease.version import ( | ||
| ConfigParserError, get_setup_cfg, get_setup_name, get_setup_version | ||
| ) | ||
| # from autorelease import ReleaseNoteWriter | ||
| from autorelease.gh_api4.notes4 import NotesWriter, prs_since_latest_release | ||
|
|
||
|
|
@@ -77,6 +81,52 @@ def auth(auth): | |
| auth = load_auth(auth) | ||
| pprint(auth) | ||
|
|
||
| @cli.group() | ||
| def metadata(): | ||
| pass | ||
|
|
||
|
|
||
| @metadata.command(name="version") | ||
| @click.option("-c", "--conf", type=str, default="setup.cfg", | ||
| help="setup.cfg file to use") | ||
| def metadata_version(conf): | ||
| directory, filename = split_setup_cfg_path(conf) | ||
| try: | ||
| setup_cfg = get_setup_cfg(directory=directory, filename=filename) | ||
| except ConfigParserError as exc: | ||
| raise click.ClickException( | ||
| f"Unable to parse setup config: {conf}" | ||
| ) from exc | ||
| value = get_setup_version(setup_cfg, default_version=None) | ||
| field = "version" | ||
|
|
||
| if value is None: | ||
| raise click.ClickException( | ||
| f"Missing [metadata] {field} in {conf}" | ||
| ) | ||
|
dwhswenson marked this conversation as resolved.
dwhswenson marked this conversation as resolved.
|
||
| click.echo(value) | ||
|
|
||
|
|
||
| @metadata.command(name="name") | ||
| @click.option("-c", "--conf", type=str, default="setup.cfg", | ||
| help="setup.cfg file to use") | ||
| def metadata_name(conf): | ||
| directory, filename = split_setup_cfg_path(conf) | ||
| try: | ||
| setup_cfg = get_setup_cfg(directory=directory, filename=filename) | ||
| except ConfigParserError as exc: | ||
| raise click.ClickException( | ||
| f"Unable to parse setup config: {conf}" | ||
| ) from exc | ||
| value = get_setup_name(setup_cfg, default_name=None) | ||
| field = "name" | ||
|
|
||
| if value is None: | ||
| raise click.ClickException( | ||
| f"Missing [metadata] {field} in {conf}" | ||
| ) | ||
|
dwhswenson marked this conversation as resolved.
dwhswenson marked this conversation as resolved.
dwhswenson marked this conversation as resolved.
|
||
| click.echo(value) | ||
|
Comment on lines
+89
to
+136
|
||
|
|
||
|
|
||
| @cli.command() | ||
| @click.option('--conf', type=click.File('r')) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,14 @@ | |
| import subprocess | ||
|
|
||
| try: | ||
| from configparser import ConfigParser, NoSectionError, NoOptionError | ||
| from configparser import ( | ||
| ConfigParser, Error as ConfigParserError, NoSectionError, NoOptionError | ||
| ) | ||
| except ImportError: | ||
| # py2 | ||
| from ConfigParser import ConfigParser, NoSectionError, NoOptionError | ||
| from ConfigParser import ( | ||
| ConfigParser, Error as ConfigParserError, NoSectionError, NoOptionError | ||
| ) | ||
|
|
||
| try: | ||
| from ._installed_version import _installed_version | ||
|
|
@@ -98,6 +102,11 @@ def get_setup_cfg(directory, filename="setup.cfg"): | |
| directory for setup.cfg, relative to cwd; default '.' | ||
| filename : str | ||
| filename for setup.cfg; default 'setup.cfg' | ||
|
||
|
|
||
| Raises | ||
| ------ | ||
| ConfigParserError | ||
| if setup.cfg exists but cannot be parsed | ||
| """ | ||
| if isinstance(directory, int): | ||
| rel_path = _find_rel_path_for_file(directory, filename) | ||
|
|
@@ -114,20 +123,44 @@ def get_setup_cfg(directory, filename="setup.cfg"): | |
| return conf | ||
|
|
||
|
|
||
| def get_setup_version(default_version, directory, filename="setup.cfg"): | ||
| version = default_version | ||
| conf = get_setup_cfg(directory, filename) | ||
| def get_setup_value(conf, option, default=None, section='metadata'): | ||
| """Get a setup.cfg value or return the provided default. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| conf : ConfigParser | ||
| loaded setup.cfg content to query | ||
| option : str | ||
| field name to retrieve from section | ||
| default | ||
| value to return when setup.cfg is missing or does not define field | ||
| section : str | ||
| section name to query; default 'metadata' | ||
| """ | ||
| value = default | ||
| try: | ||
| version = conf.get('metadata', 'version') | ||
| value = conf.get(section, option) | ||
| except (NoSectionError, NoOptionError): | ||
| pass # version (or metadata) not defined in setup.cfg | ||
| pass # option (or section) not defined in setup.cfg | ||
| except AttributeError: | ||
| pass # no setup.cfg found (conf is None) | ||
| return version | ||
| return value | ||
|
|
||
|
|
||
| def get_setup_version(conf, default_version=None): | ||
| return get_setup_value(conf, option='version', section='metadata', | ||
| default=default_version) | ||
|
|
||
|
|
||
| def get_setup_name(conf, default_name=None): | ||
| return get_setup_value(conf, option='name', section='metadata', | ||
| default=default_name) | ||
|
|
||
|
|
||
| short_version = get_setup_version(_installed_version, | ||
| directory=_version_setup_depth) | ||
| short_version = get_setup_version( | ||
| get_setup_cfg(directory=_version_setup_depth), | ||
| default_version=_installed_version, | ||
| ) | ||
| _git_version = get_git_version() | ||
| _is_repo = (_git_version != '' and _git_version != "Unknown") | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.