Bug 1967925
| Summary: | [RFE] Provide API to set repo configuration options as string | ||
|---|---|---|---|
| Product: | [Fedora] Fedora | Reporter: | Jiri Konecny <jkonecny> |
| Component: | dnf | Assignee: | Jaroslav Rohel <jrohel> |
| Status: | CLOSED ERRATA | QA Contact: | Fedora Extras Quality Assurance <extras-qa> |
| Severity: | unspecified | Docs Contact: | |
| Priority: | unspecified | ||
| Version: | rawhide | CC: | dmach, jmracek, jrohel, mblaha, mhatina, ngompa13, packaging-team-maint, pkratoch, rpm-software-management, vmukhame, vponcova |
| Target Milestone: | --- | Keywords: | Triaged |
| Target Release: | --- | ||
| Hardware: | Unspecified | ||
| OS: | Unspecified | ||
| Whiteboard: | |||
| Fixed In Version: | dnf-4.9.0-1.fc36 | Doc Type: | If docs needed, set a value |
| Doc Text: | Story Points: | --- | |
| Clone Of: | Environment: | ||
| Last Closed: | 2021-09-23 11:52:42 UTC | Type: | Bug |
| Regression: | --- | Mount Type: | --- |
| Documentation: | --- | CRM: | |
| Verified Versions: | Category: | --- | |
| oVirt Team: | --- | RHEL 7.3 requirements from Atomic Host: | |
| Cloudforms Team: | --- | Target Upstream Version: | |
| Embargoed: | |||
| Bug Depends On: | |||
| Bug Blocks: | 1665453 | ||
|
Description
Jiri Konecny
2021-06-04 12:41:50 UTC
I'm thinking about how to achieve the required functionality with the current DNF API.
A possible solution:
dnf.repo.Repo object allows to get/set repository configuration options using getattr/setattr.
Example of a function using the existing DNF API:
```
def repo_set_opt(dnf_repo_object, option_name, option_value):
if hasattr(dnf_repo_object, option_name):
setattr(dnf_repo_object, option_name, option_value)
else:
raise Exception('Unknown option: {}'.format(option_name))
```
Example of use (description of result in comment):
repo_set_opt(repo, 'metadata_expire', '4d') # sets `metadata_expire` to 4 days
repo_set_opt(repo, 'metadata_expire', '4dr') # dnf.exceptions.ConfigError: Error parsing '4dr': could not convert '4dr' to seconds
repo_set_opt(repo, 'm_expire', '4d') # Exception: Unknown option: m_expire
repo_set_opt(repo, 'enabled', '1') # Sets boolean value `enabled` to True
repo_set_opt(repo, 'enabled', 'ewe') # dnf.exceptions.ConfigError: Error parsing 'ewe': invalid boolean value 'ewe'
repo_set_opt(repo, 'excludepkgs', 'pkg1,pkg2') # sets `excludepkgs` list
print(getattr(repo,'excludepkgs')) # test of previous action, prints list "['pkg1', 'pkg2']"
Note:
The same parser (rules for parsing values) is used as for the `--setopt` DNF argument and configuration values in the files.
`setattr` is a very powerful function. I give a safer example. Added test to prevent object methods from being overwritten.
```
def repo_set_opt(dnf_repo_object, option_name, option_value):
if hasattr(dnf_repo_object, option_name) and not callable(getattr(dnf_repo_object, option_name)):
setattr(dnf_repo_object, option_name, option_value)
else:
raise Exception('Unknown option: {}'.format(option_name))
```
It is still not a perfect solution. But it could be enough.
As I wrote, `setattr` is still not perfect. E.g. "append" type options (eg "excludepkgs") are handled as general (not append) options. So, I created an API for setting configuration options using string arguments. The API also works for setting global configuration options, not just repositories. https://github.com/rpm-software-management/dnf/pull/1779 FEDORA-2021-e14e416a38 has been pushed to the Fedora 36 stable repository. If problem still persists, please make note of it in this bug report. |