-
Notifications
You must be signed in to change notification settings - Fork 303
/
os.go
224 lines (194 loc) · 5.73 KB
/
os.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package os
import (
"fmt"
"os"
"path/filepath"
"runtime"
"go.starlark.net/starlark"
"github.com/tilt-dev/tilt/internal/tiltfile/starkit"
"github.com/tilt-dev/tilt/internal/tiltfile/value"
)
// The starlark OS module.
// Modeled after Bazel's repository_os
// https://docs.bazel.build/versions/master/skylark/lib/repository_os.html
// and Python's OS module
// https://docs.python.org/3/library/os.html
type Extension struct {
}
func NewExtension() Extension {
return Extension{}
}
func (e Extension) OnStart(env *starkit.Environment) error {
err := env.AddBuiltin("os.getcwd", cwd)
if err != nil {
return err
}
err = addPathBuiltin(env, "os.path.abspath", abspath)
if err != nil {
return err
}
err = env.AddBuiltin("os.path.relpath", relpath)
if err != nil {
return err
}
err = addPathBuiltin(env, "os.path.basename", basename)
if err != nil {
return err
}
err = addPathBuiltin(env, "os.path.dirname", dirname)
if err != nil {
return err
}
err = addPathBuiltin(env, "os.path.exists", exists)
if err != nil {
return err
}
err = env.AddBuiltin("os.path.join", join)
if err != nil {
return err
}
err = addPathBuiltin(env, "os.path.realpath", realpath)
if err != nil {
return err
}
err = env.AddValue("os.environ", Environ{})
if err != nil {
return err
}
err = env.AddBuiltin("os.getenv", getenv)
if err != nil {
return err
}
err = env.AddBuiltin("os.putenv", putenv)
if err != nil {
return err
}
err = env.AddBuiltin("os.unsetenv", unsetenv)
if err != nil {
return err
}
return env.AddValue("os.name", starlark.String(osName()))
}
// For consistency with
// https://docs.python.org/3/library/os.html#os.name
func osName() string {
if runtime.GOOS == "windows" {
return "nt"
}
return "posix"
}
func getenv(t *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var key value.Stringable
var defaultVal starlark.Value = starlark.None
err := starkit.UnpackArgs(t, fn.Name(), args, kwargs,
"key", &key,
"default?", &defaultVal,
)
if err != nil {
return nil, err
}
envVal, found := os.LookupEnv(key.Value)
if !found {
return defaultVal, nil
}
return starlark.String(envVal), nil
}
func putenv(t *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var key, v value.Stringable
err := starkit.UnpackArgs(t, fn.Name(), args, kwargs,
"key", &key,
"value", &v,
)
if err != nil {
return nil, err
}
os.Setenv(key.Value, v.Value)
return starlark.None, nil
}
func unsetenv(t *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var key value.Stringable
err := starkit.UnpackArgs(t, fn.Name(), args, kwargs,
"key", &key,
)
if err != nil {
return nil, err
}
os.Unsetenv(key.Value)
return starlark.None, nil
}
// Fetch the working directory of current Tiltfile execution.
// All built-ins will be executed relative to this directory (e.g., local(), docker_build(), etc)
// Intended to mirror the API of Python's getcwd
// https://docs.python.org/3/library/os.html#os.getcwd
func cwd(t *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
err := starkit.UnpackArgs(t, fn.Name(), args, kwargs)
if err != nil {
return nil, err
}
dir := starkit.AbsWorkingDir(t)
return starlark.String(dir), nil
}
// Add a function that takes exactly one parameter, a path string.
func addPathBuiltin(env *starkit.Environment, name string,
f func(t *starlark.Thread, s string) (starlark.Value, error)) error {
builtin := func(t *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var path string
err := starkit.UnpackArgs(t, fn.Name(), args, kwargs,
"path", &path,
)
if err != nil {
return nil, err
}
return f(t, path)
}
return env.AddBuiltin(name, starkit.Function(builtin))
}
func abspath(t *starlark.Thread, path string) (starlark.Value, error) {
return starlark.String(starkit.AbsPath(t, path)), nil
}
func relpath(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var targpath string
basepath := starkit.AbsWorkingDir(thread) // by default, relative to CWD
// Matches syntax of Python's os.path.relpath()
if err := starkit.UnpackArgs(thread, fn.Name(), args, kwargs,
"targpath", &targpath,
"basepath?", &basepath); err != nil {
return nil, err
}
relPath, err := filepath.Rel(basepath, targpath)
return starlark.String(relPath), err
}
func basename(t *starlark.Thread, path string) (starlark.Value, error) {
return starlark.String(filepath.Base(path)), nil
}
func dirname(t *starlark.Thread, path string) (starlark.Value, error) {
return starlark.String(filepath.Dir(path)), nil
}
func exists(t *starlark.Thread, path string) (starlark.Value, error) {
absPath := starkit.AbsPath(t, path)
_, err := os.Stat(absPath)
if err != nil {
// Return false on error (either not found errors or permission denied
// errors), for consistency with the python version.
return starlark.Bool(false), nil
}
return starlark.Bool(true), nil
}
func join(t *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
parts := []string{}
for i, arg := range args {
s, ok := starlark.AsString(arg)
if !ok {
return nil, fmt.Errorf("os.path.join() only accepts strings. Argument #%d: %s", i, arg)
}
parts = append(parts, s)
}
return starlark.String(filepath.Join(parts...)), nil
}
func realpath(t *starlark.Thread, path string) (starlark.Value, error) {
realPath, err := filepath.EvalSymlinks(starkit.AbsPath(t, path))
if err != nil {
return nil, err
}
return starlark.String(realPath), nil
}