aboutsummaryrefslogtreecommitdiff
path: root/gazelle/python/std_modules.go
blob: 8a016afed6fdc4de55a217a46e591095fcbcd0f9 (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
// Copyright 2023 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package python

import (
	"bufio"
	"context"
	_ "embed"
	"fmt"
	"io"
	"log"
	"os"
	"os/exec"
	"strconv"
	"strings"
	"sync"
)

var (
	stdModulesCmd    *exec.Cmd
	stdModulesStdin  io.WriteCloser
	stdModulesStdout io.Reader
	stdModulesMutex  sync.Mutex
	stdModulesSeen   map[string]struct{}
)

func startStdModuleProcess(ctx context.Context) {
	stdModulesSeen = make(map[string]struct{})

	// due to #691, we need a system interpreter to boostrap, part of which is
	// to locate the hermetic interpreter.
	stdModulesCmd = exec.CommandContext(ctx, "python3", helperPath, "std_modules")
	stdModulesCmd.Stderr = os.Stderr
	// All userland site-packages should be ignored.
	stdModulesCmd.Env = []string{"PYTHONNOUSERSITE=1"}

	stdin, err := stdModulesCmd.StdinPipe()
	if err != nil {
		log.Printf("failed to initialize std_modules: %v\n", err)
		os.Exit(1)
	}
	stdModulesStdin = stdin

	stdout, err := stdModulesCmd.StdoutPipe()
	if err != nil {
		log.Printf("failed to initialize std_modules: %v\n", err)
		os.Exit(1)
	}
	stdModulesStdout = stdout

	if err := stdModulesCmd.Start(); err != nil {
		log.Printf("failed to initialize std_modules: %v\n", err)
		os.Exit(1)
	}
}

func shutdownStdModuleProcess() {
	if err := stdModulesStdin.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "error closing std module: %v", err)
	}

	if err := stdModulesCmd.Wait(); err != nil {
		log.Printf("failed to wait for std_modules: %v\n", err)
	}
}

func isStdModule(m module) (bool, error) {
	if _, seen := stdModulesSeen[m.Name]; seen {
		return true, nil
	}
	stdModulesMutex.Lock()
	defer stdModulesMutex.Unlock()

	fmt.Fprintf(stdModulesStdin, "%s\n", m.Name)

	stdoutReader := bufio.NewReader(stdModulesStdout)
	line, err := stdoutReader.ReadString('\n')
	if err != nil {
		return false, err
	}
	if len(line) == 0 {
		return false, fmt.Errorf("unexpected empty output from std_modules")
	}

	isStd, err := strconv.ParseBool(strings.TrimSpace(line))
	if err != nil {
		return false, err
	}

	if isStd {
		stdModulesSeen[m.Name] = struct{}{}
		return true, nil
	}
	return false, nil
}