File tree 3 files changed +69
-8
lines changed
3 files changed +69
-8
lines changed Original file line number Diff line number Diff line change
1
+ common --python_top=//python
1
2
build --show_loading_progress
2
3
build --show_progress
3
4
build --show_progress_rate_limit=60.0
@@ -16,5 +17,4 @@ build:ci --nouse_action_cache
16
17
build:ci --sandbox_debug
17
18
build:ci --spawn_strategy=standalone
18
19
common:ci --color=no
19
- common:ci --python_top=//python:py2
20
20
test:ci --test_strategy=standalone
Original file line number Diff line number Diff line change 1
- py_runtime (
2
- name = "py2" ,
3
- files = [],
4
- interpreter_path = "/usr/bin/python2 " ,
5
- visibility = ["//visibility:public " ],
1
+ load ( "@io_bazel_rules_go//go:def.bzl" , "go_binary" )
2
+
3
+ go_binary (
4
+ name = "wrapper " ,
5
+ srcs = ["wrapper.go " ],
6
6
)
7
7
8
8
py_runtime (
9
- name = "py3 " ,
9
+ name = "python " ,
10
10
files = [],
11
- interpreter_path = "/usr/bin/python3 " ,
11
+ interpreter = ":wrapper " ,
12
12
visibility = ["//visibility:public" ],
13
13
)
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "bufio"
5
+ "fmt"
6
+ "log"
7
+ "os"
8
+ "strings"
9
+ "syscall"
10
+ )
11
+
12
+ func main () {
13
+ args := os .Args
14
+ env := os .Environ ()
15
+ if err := wrap (args , env ); err != nil {
16
+ log .Fatal (err )
17
+ }
18
+ }
19
+
20
+ func wrap (args []string , env []string ) error {
21
+ python , _ := guess (args )
22
+
23
+ args = append ([]string {python }, args [1 :]... )
24
+ return syscall .Exec (python , args , env )
25
+ }
26
+
27
+ func guess (args []string ) (string , error ) {
28
+ python := python2 ()
29
+
30
+ if len (args ) < 2 {
31
+ return python , fmt .Errorf ("missing file argument" )
32
+ }
33
+ file , err := os .Open (os .Args [1 ])
34
+ if err != nil {
35
+ return python , err
36
+ }
37
+ defer file .Close ()
38
+
39
+ scanner := bufio .NewScanner (file )
40
+ scanner .Scan ()
41
+ shebang := scanner .Text ()
42
+ if err := scanner .Err (); err != nil {
43
+ return python , err
44
+ }
45
+
46
+ switch {
47
+ case strings .Contains (shebang , "python3" ):
48
+ python = python3 ()
49
+ default :
50
+ python = python2 ()
51
+ }
52
+ return python , nil
53
+ }
54
+
55
+ func python2 () string {
56
+ return "/usr/bin/python2"
57
+ }
58
+
59
+ func python3 () string {
60
+ return "/usr/bin/python3"
61
+ }
You can’t perform that action at this time.
0 commit comments