From 2fa7f56cec447a029ac612f0ff12eb8605c963af Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 27 Jul 2021 16:58:35 +0100 Subject: [PATCH] feat(package): Support package version from setup.cfg (python). (#2890) * Support package version from setup.cfg (python). Add an additional package version extraction function to parse the 'version' attribute under the 'metadata' section in a python package 'setup.cfg' file. Also add similar tests from the poetry extraction function to test the desired behaviour. This adds a dependency on ConfigParser: https://crates.io/crates/configparser. * Clean up comments * Use rust_ini over ConfigParser * Add mention to setup.cfg version parsing in docs --- docs/config/README.md | 1 + src/modules/package.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/docs/config/README.md b/docs/config/README.md index e525224c..5ba450c4 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -2096,6 +2096,7 @@ package, and shows its current version. The module currently supports `npm`, `ni - [**nimble**](https://github.com/nim-lang/nimble) - The `nimble` package version is extracted from the `*.nimble` file present in the current directory with the `nimble dump` command - [**poetry**](https://python-poetry.org/) – The `poetry` package version is extracted from the `pyproject.toml` present in the current directory +- [**python**](https://www.python.org) - The `python` package version is extracted from the `setup.cfg` present in the current directory - [**composer**](https://getcomposer.org/) – The `composer` package version is extracted from the `composer.json` present in the current directory - [**gradle**](https://gradle.org/) – The `gradle` package version is extracted from the `build.gradle` present diff --git a/src/modules/package.rs b/src/modules/package.rs index 8aba829a..2e54a7e3 100644 --- a/src/modules/package.rs +++ b/src/modules/package.rs @@ -3,6 +3,7 @@ use crate::configs::package::PackageConfig; use crate::formatter::StringFormatter; use crate::utils; +use ini::Ini; use quick_xml::events::Event as QXEvent; use quick_xml::Reader as QXReader; use regex::Regex; @@ -96,6 +97,14 @@ fn extract_poetry_version(file_contents: &str) -> Option { Some(formatted_version) } +fn extract_setup_cfg_version(file_contents: &str) -> Option { + let ini = Ini::load_from_str(file_contents).ok()?; + let raw_version = ini.get_from(Some("metadata"), "version")?; + + let formatted_version = format_version(raw_version); + Some(formatted_version) +} + fn extract_gradle_version(file_contents: &str) -> Option { let re = Regex::new(r#"(?m)^version ['"](?P[^'"]+)['"]$"#).unwrap(); let caps = re.captures(file_contents)?; @@ -217,6 +226,8 @@ fn get_package_version(context: &Context, config: &PackageConfig) -> Option io::Result<()> { + let config_name = "setup.cfg"; + let config_content = String::from( + "[metadata] + version = 0.1.0", + ); + + let project_dir = create_project_dir()?; + fill_config(&project_dir, config_name, Some(&config_content))?; + expect_output(&project_dir, Some("v0.1.0"), None); + project_dir.close() + } + + #[test] + fn test_extract_setup_cfg_version_without_version() -> io::Result<()> { + let config_name = "setup.cfg"; + let config_content = String::from("[metadata]"); + + let project_dir = create_project_dir()?; + fill_config(&project_dir, config_name, Some(&config_content))?; + expect_output(&project_dir, None, None); + project_dir.close() + } + #[test] fn test_extract_gradle_version_single_quote() -> io::Result<()> { let config_name = "build.gradle";