use crate::branch::GenericBranch;
use crate::debian::TarballKind;
use crate::workingtree::GenericWorkingTree;
use crate::{
branch::{Branch, PyBranch},
tree::PyTree,
RevisionId,
};
use pyo3::prelude::*;
use std::{collections::HashMap, path::Path, path::PathBuf};
pub struct DistributionBranchSet(Py<PyAny>);
impl<'py> IntoPyObject<'py> for DistributionBranchSet {
type Target = PyAny;
type Output = Bound<'py, PyAny>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
Ok(self.0.clone_ref(py).into_bound(py))
}
}
impl DistributionBranchSet {
pub fn new() -> Self {
Python::attach(|py| {
let m = py.import("breezy.plugins.debian.import_dsc").unwrap();
let ctr = m.getattr("DistributionBranchSet").unwrap();
DistributionBranchSet(ctr.call0().unwrap().into())
})
}
pub fn add_branch(&self, branch: &DistributionBranch) {
Python::attach(|py| {
self.0.call_method1(py, "add_branch", (&branch.0,)).unwrap();
})
}
}
pub struct DistributionBranch(Py<PyAny>);
impl<'py> IntoPyObject<'py> for DistributionBranch {
type Target = PyAny;
type Output = Bound<'py, PyAny>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
Ok(self.0.clone_ref(py).into_bound(py))
}
}
impl DistributionBranch {
pub fn new(
branch: &dyn PyBranch,
pristine_upstream_branch: &dyn PyBranch,
tree: Option<&dyn PyTree>,
pristine_upstream_tree: Option<&dyn PyTree>,
) -> Self {
Python::attach(|py| {
let m = py.import("breezy.plugins.debian.import_dsc").unwrap();
let ctr = m.getattr("DistributionBranch").unwrap();
DistributionBranch(
ctr.call1((
branch.to_object(py),
pristine_upstream_branch.to_object(py),
tree.map(|t| t.to_object(py)),
pristine_upstream_tree.map(|t| t.to_object(py)),
))
.unwrap()
.into(),
)
})
}
pub fn revid_of_version(
&self,
version: &debversion::Version,
) -> Result<RevisionId, crate::debian::error::Error> {
Ok(Python::attach(|py| -> PyResult<RevisionId> {
self.0
.call_method1(py, "revid_of_version", (version.to_string(),))?
.extract::<RevisionId>(py)
})?)
}
pub fn import_package(
&self,
dsc_path: &Path,
apply_patches: bool,
) -> Result<String, crate::debian::error::Error> {
Ok(Python::attach(|py| -> PyResult<String> {
self.0
.call_method1(
py,
"import_package",
(dsc_path.to_string_lossy().to_string(), apply_patches),
)?
.extract::<String>(py)
})?)
}
pub fn tree(&self) -> Option<GenericWorkingTree> {
Python::attach(|py| -> PyResult<Option<GenericWorkingTree>> {
let tree = self
.0
.getattr(py, "tree")?
.extract::<Option<Py<PyAny>>>(py)?;
if tree.is_none() {
return Ok(None);
}
Ok(Some(GenericWorkingTree::from(tree.unwrap())))
})
.unwrap()
}
pub fn branch(&self) -> Box<dyn Branch> {
Python::attach(|py| -> PyResult<Box<dyn Branch>> {
Ok(Box::new(GenericBranch::from(self.0.getattr(py, "branch")?)))
})
.unwrap()
}
pub fn pristine_upstream_source(&self) -> crate::debian::upstream::PristineTarSource {
Python::attach(
|py| -> PyResult<crate::debian::upstream::PristineTarSource> {
Ok(crate::debian::upstream::PristineTarSource::from(
self.0.getattr(py, "pristine_upstream_source")?,
))
},
)
.unwrap()
}
pub fn create_empty_upstream_tree(
&self,
basedir: &Path,
) -> Result<(), crate::debian::error::Error> {
Python::attach(|py| -> PyResult<()> {
self.0
.call_method1(py, "create_empty_upstream_tree", (basedir,))?;
Ok(())
})?;
Ok(())
}
pub fn extract_upstream_tree(
&self,
upstream_tips: &HashMap<TarballKind, (RevisionId, PathBuf)>,
basedir: &Path,
) -> Result<(), crate::debian::error::Error> {
Ok(Python::attach(|py| -> PyResult<()> {
self.0.call_method1(
py,
"extract_upstream_tree",
(
{
let dict = pyo3::types::PyDict::new(py);
for (k, (r, p)) in upstream_tips {
dict.set_item(k.clone(), (r.clone(), p.clone()))?;
}
dict
},
basedir,
),
)?;
Ok(())
})?)
}
}