##// END OF EJS Templates
tests: use sha256line.py instead of /dev/random in test-censor.t (issue6858)...
tests: use sha256line.py instead of /dev/random in test-censor.t (issue6858) Sometimes the systems that run our test suite don't have enough entropy and they cannot produce target file of the expected size using /dev/random, which results in test failures. Switching to /dev/urandom would give us way more available data at the cost of it being less "random", but we don't really need to use entropy for this task at all, since we only care if the file size after compression is big enough to not be stored inline in the revlog. So let's use something that we already have used to generate this kind of data in other tests.

File last commit:

r52148:24d32981 default
r52255:e7be2ddf stable
Show More
discovery.rs
173 lines | 6.2 KiB | application/rls-services+xml | RustLexer
Georges Racinet
rust-discovery: cpython bindings for the core logic...
r42356 // discovery.rs
//
// Copyright 2018 Georges Racinet <gracinet@anybox.fr>
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
//! Bindings for the `hg::discovery` module provided by the
//! `hg-core` crate. From Python, this will be seen as `rustext.discovery`
//!
//! # Classes visible from Python:
//! - [`PartialDiscover`] is the Rust implementation of
//! `mercurial.setdiscovery.partialdiscovery`.
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 use crate::PyRevision;
Raphaël Gomès
rust: switch hg-core and hg-cpython to rust 2018 edition...
r42828 use crate::{
Georges Racinet
rust-cpython: removed now useless py_set() conversion...
r43563 cindex::Index, conversion::rev_pyiter_collect, exceptions::GraphError,
Raphaël Gomès
rust: switch hg-core and hg-cpython to rust 2018 edition...
r42828 };
Georges Racinet
rust-discovery: implementing and exposing stats()...
r42357 use cpython::{
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 ObjectProtocol, PyClone, PyDict, PyModule, PyObject, PyResult, PyTuple,
Python, PythonObject, ToPyObject,
Georges Racinet
rust-discovery: implementing and exposing stats()...
r42357 };
Georges Racinet
rust-discovery: cpython bindings for the core logic...
r42356 use hg::discovery::PartialDiscovery as CorePartialDiscovery;
use hg::Revision;
Georges Racinet
rust-cpython: removed now useless py_set() conversion...
r43563 use std::collections::HashSet;
Georges Racinet
rust-discovery: cpython bindings for the core logic...
r42356
use std::cell::RefCell;
rust-index: add a function to convert PyObject index for hg-core...
r44398 use crate::revlog::pyindex_to_graph;
Georges Racinet
rust-discovery: cpython bindings for the core logic...
r42356 py_class!(pub class PartialDiscovery |py| {
data inner: RefCell<Box<CorePartialDiscovery<Index>>>;
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 data index: RefCell<Index>;
Georges Racinet
rust-discovery: cpython bindings for the core logic...
r42356
Georges Racinet
rust-discovery: accept the new 'respectsize' init arg...
r42963 // `_respectsize` is currently only here to replicate the Python API and
// will be used in future patches inside methods that are yet to be
// implemented.
Georges Racinet
rust-discovery: cpython bindings for the core logic...
r42356 def __new__(
_cls,
Georges Racinet
rust-discovery: read the index from a repo passed at init...
r42964 repo: PyObject,
Georges Racinet
rust-discovery: accept the new 'respectsize' init arg...
r42963 targetheads: PyObject,
Georges Racinet
rust-discovery: optionally don't randomize at all, for tests...
r42968 respectsize: bool,
randomize: bool = true
Georges Racinet
rust-discovery: cpython bindings for the core logic...
r42356 ) -> PyResult<PartialDiscovery> {
Georges Racinet
rust-discovery: read the index from a repo passed at init...
r42964 let index = repo.getattr(py, "changelog")?.getattr(py, "index")?;
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 let index = pyindex_to_graph(py, index)?;
let target_heads = rev_pyiter_collect(py, &targetheads, &index)?;
Georges Racinet
rust-discovery: cpython bindings for the core logic...
r42356 Self::create_instance(
py,
RefCell::new(Box::new(CorePartialDiscovery::new(
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 index.clone_ref(py),
target_heads,
Georges Racinet
rust-discovery: optionally don't randomize at all, for tests...
r42968 respectsize,
randomize,
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 ))),
RefCell::new(index),
Georges Racinet
rust-discovery: cpython bindings for the core logic...
r42356 )
}
def addcommons(&self, commons: PyObject) -> PyResult<PyObject> {
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 let index = self.index(py).borrow();
let commons_vec: Vec<_> = rev_pyiter_collect(py, &commons, &*index)?;
Georges Racinet
rust-discovery: cpython bindings for the core logic...
r42356 let mut inner = self.inner(py).borrow_mut();
inner.add_common_revisions(commons_vec)
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 .map_err(|e| GraphError::pynew(py, e))?;
Ok(py.None())
}
Georges Racinet
rust-discovery: cpython bindings for the core logic...
r42356
def addmissings(&self, missings: PyObject) -> PyResult<PyObject> {
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 let index = self.index(py).borrow();
let missings_vec: Vec<_> = rev_pyiter_collect(py, &missings, &*index)?;
Georges Racinet
rust-discovery: cpython bindings for the core logic...
r42356 let mut inner = self.inner(py).borrow_mut();
inner.add_missing_revisions(missings_vec)
.map_err(|e| GraphError::pynew(py, e))?;
Ok(py.None())
}
def addinfo(&self, sample: PyObject) -> PyResult<PyObject> {
let mut missing: Vec<Revision> = Vec::new();
let mut common: Vec<Revision> = Vec::new();
for info in sample.iter(py)? { // info is a pair (Revision, bool)
let mut revknown = info?.iter(py)?;
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 let rev: PyRevision = revknown.next().unwrap()?.extract(py)?;
// This is fine since we're just using revisions as integers
// for the purposes of discovery
let rev = Revision(rev.0);
Georges Racinet
rust-discovery: cpython bindings for the core logic...
r42356 let known: bool = revknown.next().unwrap()?.extract(py)?;
if known {
common.push(rev);
} else {
missing.push(rev);
}
}
let mut inner = self.inner(py).borrow_mut();
inner.add_common_revisions(common)
.map_err(|e| GraphError::pynew(py, e))?;
inner.add_missing_revisions(missing)
.map_err(|e| GraphError::pynew(py, e))?;
Ok(py.None())
}
def hasinfo(&self) -> PyResult<bool> {
Ok(self.inner(py).borrow().has_info())
}
def iscomplete(&self) -> PyResult<bool> {
Ok(self.inner(py).borrow().is_complete())
}
Georges Racinet
rust-discovery: implementing and exposing stats()...
r42357 def stats(&self) -> PyResult<PyDict> {
let stats = self.inner(py).borrow().stats();
let as_dict: PyDict = PyDict::new(py);
as_dict.set_item(py, "undecided",
Georges Racinet
rust-python3: compatibility fix for integer conversion...
r42519 stats.undecided.map(
|l| l.to_py_object(py).into_object())
.unwrap_or_else(|| py.None()))?;
Georges Racinet
rust-discovery: implementing and exposing stats()...
r42357 Ok(as_dict)
}
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 def commonheads(&self) -> PyResult<HashSet<PyRevision>> {
let res = self.inner(py).borrow().common_heads()
.map_err(|e| GraphError::pynew(py, e))?;
Ok(res.into_iter().map(Into::into).collect())
Georges Racinet
rust-discovery: cpython bindings for the core logic...
r42356 }
Georges Racinet
rust-discovery: exposing sampling to python...
r42967
def takefullsample(&self, _headrevs: PyObject,
size: usize) -> PyResult<PyObject> {
let mut inner = self.inner(py).borrow_mut();
let sample = inner.take_full_sample(size)
.map_err(|e| GraphError::pynew(py, e))?;
let as_vec: Vec<PyObject> = sample
.iter()
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 .map(|rev| PyRevision(rev.0).to_py_object(py).into_object())
Georges Racinet
rust-discovery: exposing sampling to python...
r42967 .collect();
Ok(PyTuple::new(py, as_vec.as_slice()).into_object())
}
def takequicksample(&self, headrevs: PyObject,
size: usize) -> PyResult<PyObject> {
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 let index = self.index(py).borrow();
Georges Racinet
rust-discovery: exposing sampling to python...
r42967 let mut inner = self.inner(py).borrow_mut();
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 let revsvec: Vec<_> = rev_pyiter_collect(py, &headrevs, &*index)?;
Georges Racinet
rust-discovery: exposing sampling to python...
r42967 let sample = inner.take_quick_sample(revsvec, size)
.map_err(|e| GraphError::pynew(py, e))?;
let as_vec: Vec<PyObject> = sample
.iter()
Raphaël Gomès
rust: make `Revision` a newtype...
r51872 .map(|rev| PyRevision(rev.0).to_py_object(py).into_object())
Georges Racinet
rust-discovery: exposing sampling to python...
r42967 .collect();
Ok(PyTuple::new(py, as_vec.as_slice()).into_object())
}
Georges Racinet
rust-discovery: cpython bindings for the core logic...
r42356 });
/// Create the module, with __package__ given from parent
pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> {
let dotted_name = &format!("{}.discovery", package);
let m = PyModule::new(py, dotted_name)?;
m.add(py, "__package__", package)?;
m.add(
py,
"__doc__",
"Discovery of common node sets - Rust implementation",
)?;
m.add_class::<PartialDiscovery>(py)?;
let sys = PyModule::import(py, "sys")?;
let sys_modules: PyDict = sys.get(py, "modules")?.extract(py)?;
sys_modules.set_item(py, dotted_name, &m)?;
// Example C code (see pyexpat.c and import.c) will "give away the
// reference", but we won't because it will be consumed once the
// Rust PyObject is dropped.
Ok(m)
}