12 use crate::{conversion::rev_pyiter_collect, exceptions::GraphError}; |
12 use crate::{conversion::rev_pyiter_collect, exceptions::GraphError}; |
13 use cpython::{PyDict, PyModule, PyObject, PyResult, Python}; |
13 use cpython::{PyDict, PyModule, PyObject, PyResult, Python}; |
14 use hg::dagops; |
14 use hg::dagops; |
15 use hg::Revision; |
15 use hg::Revision; |
16 use std::collections::HashSet; |
16 use std::collections::HashSet; |
|
17 use vcsgraph::ancestors::node_rank; |
|
18 use vcsgraph::graph::{Parents, Rank}; |
17 |
19 |
18 use crate::revlog::pyindex_to_graph; |
20 use crate::revlog::pyindex_to_graph; |
19 |
21 |
20 /// Using the the `index`, return heads out of any Python iterable of Revisions |
22 /// Using the the `index`, return heads out of any Python iterable of Revisions |
21 /// |
23 /// |
29 dagops::retain_heads(&pyindex_to_graph(py, index)?, &mut as_set) |
31 dagops::retain_heads(&pyindex_to_graph(py, index)?, &mut as_set) |
30 .map_err(|e| GraphError::pynew(py, e))?; |
32 .map_err(|e| GraphError::pynew(py, e))?; |
31 Ok(as_set) |
33 Ok(as_set) |
32 } |
34 } |
33 |
35 |
|
36 /// Computes the rank, i.e. the number of ancestors including itself, |
|
37 /// of a node represented by its parents. |
|
38 pub fn rank( |
|
39 py: Python, |
|
40 index: PyObject, |
|
41 p1r: Revision, |
|
42 p2r: Revision, |
|
43 ) -> PyResult<Rank> { |
|
44 node_rank(&pyindex_to_graph(py, index)?, &Parents([p1r, p2r])) |
|
45 .map_err(|e| GraphError::pynew_from_vcsgraph(py, e)) |
|
46 } |
|
47 |
34 /// Create the module, with `__package__` given from parent |
48 /// Create the module, with `__package__` given from parent |
35 pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> { |
49 pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> { |
36 let dotted_name = &format!("{}.dagop", package); |
50 let dotted_name = &format!("{}.dagop", package); |
37 let m = PyModule::new(py, dotted_name)?; |
51 let m = PyModule::new(py, dotted_name)?; |
38 m.add(py, "__package__", package)?; |
52 m.add(py, "__package__", package)?; |
40 m.add( |
54 m.add( |
41 py, |
55 py, |
42 "headrevs", |
56 "headrevs", |
43 py_fn!(py, headrevs(index: PyObject, revs: PyObject)), |
57 py_fn!(py, headrevs(index: PyObject, revs: PyObject)), |
44 )?; |
58 )?; |
|
59 m.add( |
|
60 py, |
|
61 "rank", |
|
62 py_fn!(py, rank(index: PyObject, p1r: Revision, p2r: Revision)), |
|
63 )?; |
45 |
64 |
46 let sys = PyModule::import(py, "sys")?; |
65 let sys = PyModule::import(py, "sys")?; |
47 let sys_modules: PyDict = sys.get(py, "modules")?.extract(py)?; |
66 let sys_modules: PyDict = sys.get(py, "modules")?.extract(py)?; |
48 sys_modules.set_item(py, dotted_name, &m)?; |
67 sys_modules.set_item(py, dotted_name, &m)?; |
49 // Example C code (see pyexpat.c and import.c) will "give away the |
68 // Example C code (see pyexpat.c and import.c) will "give away the |