/
python.go
60 lines (44 loc) · 1.66 KB
/
python.go
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
package action
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2021 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"fmt"
"os/exec"
"github.com/essentialkaos/bibop/recipe"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// PythonModule is action processor for "python-module"
func PythonModule(action *recipe.Action) error {
module, err := action.GetS(0)
if err != nil {
return err
}
if checkPythonModuleLoad(2, module) != nil {
return fmt.Errorf("Python2 module %s cannot be loaded", module)
}
return nil
}
// Python3Module is action processor for "python3-module"
func Python3Module(action *recipe.Action) error {
module, err := action.GetS(0)
if err != nil {
return err
}
if checkPythonModuleLoad(3, module) != nil {
return fmt.Errorf("Python3 module %s cannot be loaded", module)
}
return nil
}
// ////////////////////////////////////////////////////////////////////////////////// //
// checkPythonModuleLoad returns true if module can be loaded
func checkPythonModuleLoad(pythonVersion int, moduleName string) error {
pythonBinary := "python"
if pythonVersion == 3 {
pythonBinary = "python3"
}
return exec.Command(pythonBinary, "-c", "import "+moduleName).Run()
}