Merge lp:~jelmer/brz/auto-path into lp:brz

Proposed by Jelmer Vernooij
Status: Merged
Approved by: Jelmer Vernooij
Approved revision: no longer in the source branch.
Merge reported by: The Breezy Bot
Merged at revision: not available
Proposed branch: lp:~jelmer/brz/auto-path
Merge into: lp:brz
Diff against target: 66 lines (+30/-1)
2 files modified
Cargo.toml (+1/-1)
breezy/main.rs (+29/-0)
To merge this branch: bzr merge lp:~jelmer/brz/auto-path
Reviewer Review Type Date Requested Status
Martin Packman Approve
Review via email: mp+412034@code.launchpad.net

Commit message

Update PYTHONPATH to include local breezy package when running from source.

Description of the change

Update PYTHONPATH to include local breezy package when running from source.

To post a comment you must log in.
Revision history for this message
Martin Packman (gz) wrote :

Looks good. Logic basically being if-exe-has-sibling-dir-named-breezy which seems fine.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Cargo.toml'
2--- Cargo.toml 2021-08-20 14:03:34 +0000
3+++ Cargo.toml 2021-11-17 20:21:21 +0000
4@@ -12,4 +12,4 @@
5
6 [dependencies]
7 pkg-version = "1.0.0"
8-pyo3 = "0.14.2"
9+pyo3 = "0.15.0"
10
11=== modified file 'breezy/main.rs'
12--- breezy/main.rs 2021-08-20 14:03:34 +0000
13+++ breezy/main.rs 2021-11-17 20:21:21 +0000
14@@ -1,6 +1,7 @@
15 use pkg_version::*;
16 use pyo3::prelude::*;
17 use pyo3::types::*;
18+use std::path::*;
19
20 const MAJOR: u32 = pkg_version_major!();
21 const MINOR: u32 = pkg_version_minor!();
22@@ -52,6 +53,31 @@
23 }
24 }
25
26+fn prepend_path(py: Python<'_>, el: &Path) -> PyResult<()> {
27+ let sys = PyModule::import(py, "sys")?;
28+
29+ let current_path: &pyo3::types::PyList = sys.getattr("path")?.try_into()?;
30+
31+ current_path.insert(0, el.to_str().expect("invalid local path"))?;
32+
33+ Ok(())
34+}
35+
36+// Prepend sys.path with the brz path when useful.
37+fn update_path(py: Python<'_>) -> PyResult<()> {
38+ let mut path = std::env::current_exe()?;
39+
40+ path.pop(); // Drop executable name
41+
42+ let mut package_path = path.clone();
43+ package_path.push("breezy");
44+ if package_path.is_dir() {
45+ prepend_path(py, path.as_path())?;
46+ }
47+
48+ Ok(())
49+}
50+
51 fn posix_setup(py: Python<'_>) -> PyResult<()> {
52 let os = PyModule::import(py, "os")?;
53
54@@ -72,9 +98,12 @@
55
56 fn main() -> PyResult<()> {
57 pyo3::prepare_freethreaded_python();
58+
59 Python::with_gil(|py| {
60 posix_setup(py)?;
61
62+ update_path(py)?;
63+
64 check_version(py)?;
65
66 let args: Vec<String> = std::env::args().collect();

Subscribers

People subscribed via source and target branches