• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tensorchord / envd / 12730559639

12 Jan 2025 04:47AM UTC coverage: 44.021% (-0.06%) from 44.084%
12730559639

push

github

web-flow
feat: upgrade to python 3.11 (#1940)

6299 of 14309 relevant lines covered (44.02%)

564.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.47
/pkg/lang/ir/v0/util.go
1
// Copyright 2023 The envd Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package v0
16

17
import (
18
        "bytes"
19
        "crypto/md5"
20
        "encoding/gob"
21
        "encoding/hex"
22
        "encoding/json"
23
        "os/user"
24
        "regexp"
25
        "strconv"
26
        "strings"
27

28
        "github.com/cockroachdb/errors"
29
        "github.com/sirupsen/logrus"
30
        "github.com/spf13/viper"
31

32
        "github.com/tensorchord/envd/pkg/flag"
33
        "github.com/tensorchord/envd/pkg/lang/ir"
34
        "github.com/tensorchord/envd/pkg/util/fileutil"
35
)
36

37
func (g generalGraph) getWorkingDir() string {
42✔
38
        return fileutil.EnvdHomeDir(g.EnvironmentName)
42✔
39
}
42✔
40

41
func (g generalGraph) getExtraSourceDir() string {
×
42
        return fileutil.EnvdHomeDir("extra_source")
×
43
}
×
44

45
func parseLanguage(l string) (string, *string, error) {
22✔
46
        var language, version string
22✔
47
        if l == "" {
22✔
48
                return "", nil, errors.New("language is required")
×
49
        }
×
50

51
        // Get version from the string.
52
        re := regexp.MustCompile(`\d[\d,]*[\.]?[\d{2}]*[\.]?[\d{2}]*`)
22✔
53
        if !re.MatchString(l) {
27✔
54
                language = l
5✔
55
        } else {
22✔
56
                loc := re.FindStringIndex(l)
17✔
57
                language = l[:loc[0]]
17✔
58
                version = l[loc[0]:]
17✔
59
        }
17✔
60

61
        switch language {
22✔
62
        case "python", "r", "julia":
21✔
63
                return language, &version, nil
21✔
64
        default:
1✔
65
                return "", nil, errors.Newf("language %s is not supported", language)
1✔
66
        }
67
}
68

69
func getUIDGID() (int, int, error) {
23✔
70
        owner := viper.GetString(flag.FlagBuildOwner)
23✔
71
        if len(owner) > 0 {
23✔
72
                logrus.WithField("flag", owner).Info("use owner")
×
73
                ids := strings.Split(owner, ":")
×
74
                if len(ids) > 2 {
×
75
                        return 0, 0, errors.Newf("wrong format for owner (uid:gid): %s", owner)
×
76
                }
×
77
                uid, err := strconv.Atoi(ids[0])
×
78
                if err != nil {
×
79
                        logrus.Info(err)
×
80
                        return 0, 0, errors.Wrap(err, "failed to get uid")
×
81
                }
×
82
                // if omit gid, will use the uid as gid
83
                if len(ids) == 1 {
×
84
                        return uid, uid, nil
×
85
                }
×
86
                gid, err := strconv.Atoi(ids[1])
×
87
                if err != nil {
×
88
                        return 0, 0, errors.Wrap(err, "failed to get gid")
×
89
                }
×
90
                return uid, gid, nil
×
91
        }
92
        user, err := user.Current()
23✔
93
        if err != nil {
23✔
94
                return 0, 0, errors.Wrap(err, "failed to get uid/gid")
×
95
        }
×
96
        // Do not support windows yet.
97
        uid, err := strconv.Atoi(user.Uid)
23✔
98
        if err != nil {
23✔
99
                return 0, 0, errors.Wrap(err, "failed to get uid")
×
100
        }
×
101
        gid, err := strconv.Atoi(user.Gid)
23✔
102
        if err != nil {
23✔
103
                return 0, 0, errors.Wrap(err, "failed to get gid")
×
104
        }
×
105
        return uid, gid, nil
23✔
106
}
107

108
// A stream of gobs is self-describing. Each data item in the stream is preceded by a specification of its type, expressed in terms of a small set of predefined types. Pointers are not transmitted, but the things they point to are transmitted; that is, the values are flattened.
109
// see https://pkg.go.dev/encoding/gob#hdr-Basics
110
// we hash the blobs to determined if the graph changed.
111
func GetDefaultGraphHash() string {
23✔
112
        var b bytes.Buffer
23✔
113
        err := gob.NewEncoder(&b).Encode(DefaultGraph)
23✔
114
        if err != nil {
23✔
115
                return ""
×
116
        }
×
117
        data := b.Bytes()
23✔
118
        hashD := md5.Sum(data)
23✔
119
        return hex.EncodeToString(hashD[:])
23✔
120
}
121

122
func (g *generalGraph) Dump() (string, error) {
23✔
123
        b, err := json.Marshal(g)
23✔
124
        if err != nil {
23✔
125
                return "", err
×
126
        }
×
127
        runtimeGraphCode := string(b)
23✔
128
        return runtimeGraphCode, nil
23✔
129
}
130

131
func (g *generalGraph) Load(code []byte) error {
11✔
132
        err := json.Unmarshal(code, g)
11✔
133
        if err != nil {
11✔
134
                return errors.Wrap(err, "failed to unmarshal")
×
135
        }
×
136
        return nil
11✔
137
}
138

139
func (g generalGraph) GeneralGraphFromLabel(label []byte) (ir.Graph, error) {
11✔
140
        newg := generalGraph{}
11✔
141
        err := newg.Load(label)
11✔
142
        if err != nil {
11✔
143
                return nil, err
×
144
        }
×
145
        return &newg, nil
11✔
146
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc