-
Notifications
You must be signed in to change notification settings - Fork 1
/
version.go
157 lines (140 loc) · 3.97 KB
/
version.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
// Copyright 2023 Tomas Machalek <[email protected]>
// Copyright 2023 Institute of the Czech National Corpus,
// Faculty of Arts, Charles University
// This file is part of CNC-MASM.
//
// CNC-MASM is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// CNC-MASM is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with CNC-MASM. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"github.com/czcorpus/cnc-gokit/collections"
"github.com/czcorpus/cnc-gokit/fs"
)
var (
VerSrchPtrn = regexp.MustCompile(`open-([^\s]+)`)
)
type Version struct {
Major int
Minor int
Patch int
Variant string
}
func (v Version) IsZero() bool {
return v.Major == 0 && v.Minor == 0 && v.Patch == 0
}
func (v Version) Semver() string {
return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
}
func (v Version) String() string {
var vs string
if v.Variant != "" {
vs = "-" + vs
}
return fmt.Sprintf(
"manatee-open-%d.%d.%d%s", v.Major, v.Minor, v.Patch, vs)
}
func (v Version) Ge(other Version) bool {
if v.Major != other.Major {
return v.Major > other.Major
}
if v.Minor != other.Minor {
return v.Minor > other.Minor
}
return v.Patch > other.Patch
}
func (v Version) Eq(other Version) bool {
return v.Major == other.Major && v.Minor == other.Minor && v.Patch == other.Patch
}
func ParseManateeVersion(v string) (Version, error) {
v = strings.TrimSuffix(v, "-cnc")
items := strings.Split(v, ".")
if len(items) < 2 || len(items) > 3 {
return Version{}, fmt.Errorf("invalid version specifier: %s", v)
}
var err error
var ans Version
ans.Major, err = strconv.Atoi(items[0])
if err != nil {
return ans, err
}
ans.Minor, err = strconv.Atoi(items[1])
if err != nil {
return ans, err
}
if len(items) == 3 {
ans.Patch, err = strconv.Atoi(items[2])
return ans, err
}
return ans, nil
}
func AutodetectManateeVersion(specPath string, knownVersions []string) (Version, error) {
libPath := DefaultManateeLibPath
if specPath != "" {
libPath = path.Join(specPath, "libmanatee.so")
}
if fs.PathExists(libPath) {
cmd := exec.Command("strings", libPath)
out, err := cmd.CombinedOutput()
if err == nil {
srch := VerSrchPtrn.FindStringSubmatch(string(out))
return ParseManateeVersion(srch[1])
} else {
return Version{}, fmt.Errorf("failed to run `strings %s`", libPath)
}
} else {
return findLatestManateeInOpt(knownVersions)
}
}
func findLatestManateeInOpt(knownVersions []string) (Version, error) {
entries, err := os.ReadDir("/opt/manatee")
if err != nil {
return Version{}, fmt.Errorf("no default Manatee found and failed to list manatee versions is /opt/manatee: %w", err)
}
foundVersions := make([]Version, 0, 10)
for _, ent := range entries {
if v, err := ParseManateeVersion(ent.Name()); err == nil {
if collections.SliceContains(knownVersions, v.Semver()) {
foundVersions = append(foundVersions, v)
}
}
}
if len(foundVersions) > 0 {
sort.SliceStable(foundVersions, func(i, j int) bool {
return foundVersions[j].Ge(foundVersions[i])
})
return foundVersions[len(foundVersions)-1], nil
}
return Version{}, nil
}
func findManatee(version Version) string {
if fs.PathExists("/usr/lib/libmanatee.so") {
return "/usr/lib"
}
if fs.PathExists("/usr/local/lib/libmanatee.so") {
return "/usr/local/lib"
}
optInstPath := fmt.Sprintf("/opt/manatee/%s/lib", version.Semver())
if fs.PathExists(filepath.Join(optInstPath, "libmanatee.so")) {
return optInstPath
}
return ""
}