summaryrefslogtreecommitdiff
blob: 8c173021a5b7e47c8145bc562eb6875dca1d9fc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
This is an adapted patch from the games/anki FreeBSD port.
https://github.com/freebsd/freebsd-ports/blob/109c3d4629b84972e660b689d169ac0761c1a519/games/anki/files/patch-build_configure_src_python.rs
https://github.com/freebsd/freebsd-ports/blob/109c3d4629b84972e660b689d169ac0761c1a519/games/anki/files/patch-build_ninja__gen_src_python.rs

Use the "PythonEnvironmentStub" function to use a native Python environment
instead of a Python venv.
* Avoid the use of Python's pip (network access).
* Remove the python_binary input for PythonEnvironmentStub since we create the
  pseudo venv manually in the ebuild. This saves us from setting an extra
  environment variable to prevent network access.
  
  The python_binary input should really only be added to PythonEnvironment, if
  the venv is not set up.
* TODO: add dev-python/mypy-protobuf to tree for fully typed Anki.

From: Lucio Sauer <watermanpaint@posteo.net>
--- a/build/configure/src/python.rs
+++ b/build/configure/src/python.rs
@@ -13,6 +13,7 @@ use ninja_gen::input::BuildInput;
 use ninja_gen::inputs;
 use ninja_gen::python::python_format;
 use ninja_gen::python::PythonEnvironment;
+use ninja_gen::python::PythonEnvironmentStub;
 use ninja_gen::python::PythonLint;
 use ninja_gen::python::PythonTypecheck;
 use ninja_gen::rsync::RsyncFiles;
@@ -81,6 +82,25 @@ pub fn setup_venv(build: &mut Build) -> Result<()> {
     Ok(())
 }
 
+pub fn setup_venv_stub(build: &mut Build) -> Result<()> {
+    build.add_action(
+        "pyenv",
+        PythonEnvironmentStub {
+            folder: "pyenv",
+            extra_binary_exports: &[
+                "mypy",     // Required in some parts of the code, but not for build
+                "black",    // dito
+                "isort",    // dito
+                "pylint",   // dito
+                "pytest",   // dito
+                "protoc-gen-mypy",
+            ],
+        },
+    )?;
+
+    Ok(())
+}
+
 pub struct GenPythonProto {
     pub proto_files: BuildInput,
 }
@@ -88,9 +108,7 @@ pub struct GenPythonProto {
 impl BuildAction for GenPythonProto {
     fn command(&self) -> &str {
         "$protoc $
-        --plugin=protoc-gen-mypy=$protoc-gen-mypy $
         --python_out=$builddir/pylib $
-        --mypy_out=$builddir/pylib $
         -Iproto $in"
     }
 
@@ -108,7 +126,6 @@ impl BuildAction for GenPythonProto {
             .collect();
         build.add_inputs("in", &self.proto_files);
         build.add_inputs("protoc", inputs![":protoc_binary"]);
-        build.add_inputs("protoc-gen-mypy", inputs![":pyenv:protoc-gen-mypy"]);
         build.add_outputs("", python_outputs);
     }
 
@@ -254,7 +271,6 @@ impl BuildAction for Sphinx {
 
     fn files(&mut self, build: &mut impl FilesHandle) {
         build.add_inputs("python", inputs![":pyenv:bin"]);
-        build.add_inputs("pip", inputs![":pyenv:pip"]);
         build.add_inputs("", &self.deps);
         build.add_output_stamp("python/sphinx/stamp");
     }
--- a/build/ninja_gen/src/python.rs
+++ b/build/ninja_gen/src/python.rs
@@ -86,6 +86,11 @@ pub struct PythonEnvironment {
     pub extra_binary_exports: &'static [&'static str],
 }
 
+pub struct PythonEnvironmentStub {
+    pub folder: &'static str,
+    pub extra_binary_exports: &'static [&'static str],
+}
+
 impl BuildAction for PythonEnvironment {
     fn command(&self) -> &str {
         "$runner pyenv $python_binary $builddir/$pyenv_folder $system_pkgs $base_requirements $requirements"
@@ -118,6 +123,34 @@ impl BuildAction for PythonEnvironment {
     }
 }
 
+impl BuildAction for PythonEnvironmentStub {
+    fn command(&self) -> &str {
+        "echo Running PythonEnvironmentStub..."
+    }
+
+    fn files(&mut self, build: &mut impl crate::build::FilesHandle) {
+        let bin_path = |binary: &str| -> Vec<String> {
+            let folder = self.folder;
+            let path = if cfg!(windows) {
+                format!("{folder}/scripts/{binary}.exe")
+            } else {
+                format!("{folder}/bin/{binary}")
+            };
+            vec![path]
+        };
+
+        build.add_variable("pyenv_folder", self.folder);
+        build.add_outputs_ext("bin", bin_path("python"), true);
+        for binary in self.extra_binary_exports {
+            build.add_outputs_ext(*binary, bin_path(binary), true);
+        }
+    }
+
+    fn check_output_timestamps(&self) -> bool {
+        true
+    }
+}
+
 pub struct PythonTypecheck {
     pub folders: &'static [&'static str],
     pub deps: BuildInput,