Mercurial > public > mercurial-scm > hg-stable
annotate rust/hg-cpython/src/utils.rs @ 52070:28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
I suspect this will not be the last time we need to do something like this.
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Tue, 01 Oct 2024 13:45:18 +0200 |
parents | c7fb9b74e753 |
children | de317a87ea6a |
rev | line source |
---|---|
44515
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
1 use cpython::exc::ValueError; |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
2 use cpython::{PyBytes, PyDict, PyErr, PyObject, PyResult, PyTuple, Python}; |
52070
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
3 use hg::config::Config; |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
4 use hg::errors::HgError; |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
5 use hg::repo::{Repo, RepoError}; |
44515
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
6 use hg::revlog::Node; |
52070
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
7 use hg::utils::files::get_path_from_bytes; |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
8 |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
9 use crate::exceptions::FallbackError; |
43251
970978975574
rust-utils: introduce a debug util to print the python stack trace
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
10 |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
11 #[allow(unused)] |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
12 pub fn print_python_trace(py: Python) -> PyResult<PyObject> { |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
13 eprintln!("==============================="); |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
14 eprintln!("Printing Python stack from Rust"); |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
15 eprintln!("==============================="); |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
16 let traceback = py.import("traceback")?; |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
17 let sys = py.import("sys")?; |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
18 let kwargs = PyDict::new(py); |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
19 kwargs.set_item(py, "file", sys.get(py, "stderr")?)?; |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
20 traceback.call(py, "print_stack", PyTuple::new(py, &[]), Some(&kwargs)) |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
21 } |
44515
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
22 |
52070
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
23 pub fn hgerror_to_pyerr<T>( |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
24 py: Python, |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
25 error: Result<T, HgError>, |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
26 ) -> PyResult<T> { |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
27 error.map_err(|e| match e { |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
28 HgError::IoError { .. } => { |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
29 PyErr::new::<cpython::exc::IOError, _>(py, e.to_string()) |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
30 } |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
31 HgError::UnsupportedFeature(e) => { |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
32 let as_string = e.to_string(); |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
33 log::trace!("Update from null fallback: {}", as_string); |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
34 PyErr::new::<FallbackError, _>(py, &as_string) |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
35 } |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
36 HgError::RaceDetected(_) => { |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
37 unreachable!("must not surface to the user") |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
38 } |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
39 e => PyErr::new::<cpython::exc::RuntimeError, _>(py, e.to_string()), |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
40 }) |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
41 } |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
42 |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
43 pub fn repo_error_to_pyerr<T>( |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
44 py: Python, |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
45 error: Result<T, RepoError>, |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
46 ) -> PyResult<T> { |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
47 hgerror_to_pyerr(py, error.map_err(HgError::from)) |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
48 } |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
49 |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
50 /// Get a repository from a given [`PyObject`] path, and bubble up any error |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
51 /// that comes up. |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
52 pub fn repo_from_path(py: Python, repo_path: PyObject) -> Result<Repo, PyErr> { |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
53 let config = |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
54 hgerror_to_pyerr(py, Config::load_non_repo().map_err(HgError::from))?; |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
55 let py_bytes = &repo_path.extract::<PyBytes>(py)?; |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
56 let repo_path = py_bytes.data(py); |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
57 let repo = repo_error_to_pyerr( |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
58 py, |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
59 Repo::find(&config, Some(get_path_from_bytes(repo_path).to_owned())), |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
60 )?; |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
61 Ok(repo) |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
62 } |
28a0eb21ff04
rust-cpython: add a util to get a `Repo` from a python path
Rapha?l Gom?s <rgomes@octobus.net>
parents:
49749
diff
changeset
|
63 |
44515
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
64 // Necessary evil for the time being, could maybe be moved to |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
65 // a TryFrom in Node itself |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
66 const NODE_BYTES_LENGTH: usize = 20; |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
67 type NodeData = [u8; NODE_BYTES_LENGTH]; |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
68 |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
69 /// Copy incoming Python bytes given as `PyObject` into `Node`, |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
70 /// doing the necessary checks |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
71 pub fn node_from_py_object<'a>( |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
72 py: Python, |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
73 bytes: &'a PyObject, |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
74 ) -> PyResult<Node> { |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
75 let as_py_bytes: &'a PyBytes = bytes.extract(py)?; |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
76 node_from_py_bytes(py, as_py_bytes) |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
77 } |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
78 |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
79 /// Clone incoming Python bytes given as `PyBytes` as a `Node`, |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
80 /// doing the necessary checks. |
44998
26114bd6ec60
rust: do a clippy pass
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44515
diff
changeset
|
81 pub fn node_from_py_bytes(py: Python, bytes: &PyBytes) -> PyResult<Node> { |
44515
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
82 <NodeData>::try_from(bytes.data(py)) |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
83 .map_err(|_| { |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
84 PyErr::new::<ValueError, _>( |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
85 py, |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
86 format!("{}-byte hash required", NODE_BYTES_LENGTH), |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
87 ) |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
88 }) |
44998
26114bd6ec60
rust: do a clippy pass
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44515
diff
changeset
|
89 .map(Into::into) |
44515
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
90 } |