Skip to content

Commit

Permalink
refactor: add python object (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra authored Dec 6, 2023
1 parent eb3de7f commit 1832dd2
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 47 deletions.
7 changes: 4 additions & 3 deletions docs/recipe_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,10 @@ The following example creates a Python entry point named "bsdiff4" that calls

```yaml
build:
entry_points:
- bsdiff4 = bsdiff4.cli:main_bsdiff4
- bspatch4 = bsdiff4.cli:main_bspatch4
python:
entry_points:
- bsdiff4 = bsdiff4.cli:main_bsdiff4
- bspatch4 = bsdiff4.cli:main_bspatch4
```


Expand Down
25 changes: 13 additions & 12 deletions examples/mamba/recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ outputs:
commands:
- if: unix
then:
- test -d ${PREFIX}/include/mamba # [unix]
- test -f ${PREFIX}/include/mamba/version.hpp # [unix]
- test -f ${PREFIX}/lib/cmake/libmamba/libmambaConfig.cmake # [unix]
- test -f ${PREFIX}/lib/cmake/libmamba/libmambaConfigVersion.cmake # [unix]
- test -e ${PREFIX}/lib/libmamba${SHLIB_EXT} # [unix]
- test -d ${PREFIX}/include/mamba # [unix]
- test -f ${PREFIX}/include/mamba/version.hpp # [unix]
- test -f ${PREFIX}/lib/cmake/libmamba/libmambaConfig.cmake # [unix]
- test -f ${PREFIX}/lib/cmake/libmamba/libmambaConfigVersion.cmake # [unix]
- test -e ${PREFIX}/lib/libmamba${SHLIB_EXT} # [unix]
else:
- if not exist %LIBRARY_PREFIX%\include\mamba\version.hpp (exit 1) # [win]
- if not exist %LIBRARY_PREFIX%\lib\cmake\libmamba\libmambaConfig.cmake (exit 1) # [win]
- if not exist %LIBRARY_PREFIX%\lib\cmake\libmamba\libmambaConfigVersion.cmake (exit 1) # [win]
- if not exist %LIBRARY_PREFIX%\bin\libmamba.dll (exit 1) # [win]
- if not exist %LIBRARY_PREFIX%\lib\libmamba.lib (exit 1) # [win]
- if not exist %LIBRARY_PREFIX%\include\mamba\version.hpp (exit 1) # [win]
- if not exist %LIBRARY_PREFIX%\lib\cmake\libmamba\libmambaConfig.cmake (exit 1) # [win]
- if not exist %LIBRARY_PREFIX%\lib\cmake\libmamba\libmambaConfigVersion.cmake (exit 1) # [win]
- if not exist %LIBRARY_PREFIX%\bin\libmamba.dll (exit 1) # [win]
- if not exist %LIBRARY_PREFIX%\lib\libmamba.lib (exit 1) # [win]
# - cat $PREFIX/include/mamba/version.hpp | grep "LIBMAMBA_VERSION_MAJOR ${{ libmamba_version_split[0] }}" # [unix]
# - cat $PREFIX/include/mamba/version.hpp | grep "LIBMAMBA_VERSION_MINOR ${{ libmamba_version_split[1] }}" # [unix]
# - cat $PREFIX/include/mamba/version.hpp | grep "LIBMAMBA_VERSION_PATCH ${{ libmamba_version_split[2] }}" # [unix]
Expand Down Expand Up @@ -129,8 +129,9 @@ outputs:
- ${{ "build_mamba.sh" if unix }}
- ${{ "build_mamba.bat" if win }}
string: py${{ python | version_to_buildstring }}h${{ hash }}_${{ build_number }}
entry_points:
- mamba = mamba.mamba:main
python:
entry_points:
- mamba = mamba.mamba:main
requirements:
build:
- if: build_platform != target_platform
Expand Down
18 changes: 13 additions & 5 deletions src/packaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ fn write_to_dest(
/// This function creates a link.json file for the given output.
fn create_link_json(output: &Output) -> Result<Option<String>, PackagingError> {
let noarch_links = PythonEntryPoints {
entry_points: output.recipe.build().entry_points().to_owned(),
entry_points: output.recipe.build().python().entry_points().to_owned(),
};

let link_json = LinkJson {
Expand Down Expand Up @@ -816,6 +816,7 @@ pub fn package_conda(
if output
.recipe
.build()
.python()
.entry_points()
.iter()
.any(|ep| ep.command == name.to_string_lossy())
Expand All @@ -827,10 +828,17 @@ pub fn package_conda(
// Windows
else if stripped.starts_with("Scripts") {
if let Some(name) = stripped.file_name() {
if output.recipe.build().entry_points().iter().any(|ep| {
format!("{}.exe", ep.command) == name.to_string_lossy()
|| format!("{}-script.py", ep.command) == name.to_string_lossy()
}) {
if output
.recipe
.build()
.python()
.entry_points()
.iter()
.any(|ep| {
format!("{}.exe", ep.command) == name.to_string_lossy()
|| format!("{}-script.py", ep.command) == name.to_string_lossy()
})
{
continue;
}
}
Expand Down
78 changes: 61 additions & 17 deletions src/recipe/parser/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::str::FromStr;

use rattler_conda_types::{package::EntryPoint, NoArchKind, NoArchType};
use rattler_conda_types::{package::EntryPoint, NoArchType};
use serde::{Deserialize, Serialize};

use super::Dependency;
Expand Down Expand Up @@ -31,9 +31,9 @@ pub struct Build {
pub(super) script: Script,
/// A noarch package runs on any platform. It can be either a python package or a generic package.
pub(super) noarch: NoArchType,
/// For a Python noarch package to have executables it is necessary to specify the python entry points.
/// These contain the name of the executable and the module + function that should be executed.
pub(super) entry_points: Vec<EntryPoint>,
/// Python specific build configuration
#[serde(default, skip_serializing_if = "Python::is_default")]
pub(super) python: Python,
// TODO: Add and parse the rest of the fields
}

Expand Down Expand Up @@ -63,9 +63,9 @@ impl Build {
&self.noarch
}

/// Get the entry points.
pub fn entry_points(&self) -> &[EntryPoint] {
self.entry_points.as_slice()
/// Python specific build configuration.
pub const fn python(&self) -> &Python {
&self.python
}

/// Check if the build should be skipped.
Expand Down Expand Up @@ -104,16 +104,8 @@ impl TryConvertNode<Build> for RenderedMappingNode {
"noarch" => {
build.noarch = value.try_convert(key_str)?;
}
"entry_points" => {
if let Some(NoArchKind::Generic) = build.noarch.kind() {
return Err(_partialerror!(
*key.span(),
ErrorKind::Other,
label = "`entry_points` are only allowed for `python` noarch packages"
));
}

build.entry_points = value.try_convert(key_str)?;
"python" => {
build.python = value.try_convert(key_str)?;
}
invalid => {
return Err(_partialerror!(
Expand All @@ -128,6 +120,58 @@ impl TryConvertNode<Build> for RenderedMappingNode {
}
}

/// Python specific build configuration
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Python {
/// For a Python noarch package to have executables it is necessary to specify the python entry points.
/// These contain the name of the executable and the module + function that should be executed.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub(super) entry_points: Vec<EntryPoint>,
}

impl Python {
/// Get the entry points.
pub fn entry_points(&self) -> &[EntryPoint] {
self.entry_points.as_slice()
}

/// Returns true if this is the default python configuration.
pub fn is_default(&self) -> bool {
self.entry_points.is_empty()
}
}

impl TryConvertNode<Python> for RenderedNode {
fn try_convert(&self, name: &str) -> Result<Python, PartialParsingError> {
self.as_mapping()
.ok_or_else(|| _partialerror!(*self.span(), ErrorKind::ExpectedMapping))
.and_then(|m| m.try_convert(name))
}
}

impl TryConvertNode<Python> for RenderedMappingNode {
fn try_convert(&self, _name: &str) -> Result<Python, PartialParsingError> {
let mut python = Python::default();

for (key, value) in self.iter() {
let key_str = key.as_str();
match key_str {
"entry_points" => {
python.entry_points = value.try_convert(key_str)?;
}
invalid => {
return Err(_partialerror!(
*key.span(),
ErrorKind::InvalidField(invalid.to_string().into()),
));
}
}
}

Ok(python)
}
}

/// Run exports are applied to downstream packages that depend on this package.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct RunExports {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ Recipe {
noarch: NoArchType(
None,
),
entry_points: [],
python: Python {
entry_points: [],
},
},
requirements: Requirements {
build: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ build:
script:
- test succeeded
noarch: false
entry_points: []
requirements: {}
test:
imports: []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ Recipe {
noarch: NoArchType(
None,
),
entry_points: [],
python: Python {
entry_points: [],
},
},
requirements: Requirements {
build: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ build:
number: 0
string: h12341234_0
noarch: false
entry_points: []
requirements:
build: []
host: []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ recipe:
skip: false
script: []
noarch: false
entry_points: []
requirements:
build:
- __COMPILER c
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ recipe:
script:
- python -m pip install . -vv --no-deps --no-build-isolation
noarch: python
entry_points: []
requirements:
host:
- pip
Expand Down
5 changes: 3 additions & 2 deletions test-data/recipes/flask/recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ source:
build:
number: 0
script: python -m pip install . -vv --no-deps --no-build-isolation
entry_points:
- flask = flask.cli:main
python:
entry_points:
- flask = flask.cli:main
noarch: python

requirements:
Expand Down
1 change: 0 additions & 1 deletion test-data/rendered_recipes/curl_recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ recipe:
skip: false
script: []
noarch: false
entry_points: []
requirements:
build:
- __COMPILER c
Expand Down
1 change: 0 additions & 1 deletion test-data/rendered_recipes/rich_recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ recipe:
script:
- python -m pip install . -vv --no-deps --no-build-isolation
noarch: python
entry_points: []
requirements:
build: []
host:
Expand Down

0 comments on commit 1832dd2

Please sign in to comment.