-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
391 lines (331 loc) · 7.93 KB
/
main.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"regexp"
"strings"
"github.com/urfave/cli/v2"
)
const (
REQS_PATH = "requirements.txt"
DEV_REQS_PATH = "dev-requirements.txt"
)
type PackageSpec struct {
Name string `json:"name"`
Extras string
Version string `json:"version"`
}
var (
// TODO: Match python spec - https://peps.python.org/pep-0508/#names
// See name regexp - https://peps.python.org/pep-0508/#names
extrasRgx *regexp.Regexp = regexp.MustCompile(`\[(?:[^\d\W]\w*)(?:,[^\d\W]\w*)*\]`)
normalizeRgx *regexp.Regexp = regexp.MustCompile(`[-_.]+`)
)
func main() {
app := &cli.App{
Name: "pips",
Usage: "install pip packages and add to requirements.txt",
Commands: []*cli.Command{
{
Name: "install",
Aliases: []string{"i"},
Usage: "installs a package and updates " + REQS_PATH,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dev",
Aliases: []string{"d"},
Value: false,
Usage: "save to " + DEV_REQS_PATH,
},
},
Action: installCmd,
},
{
Name: "save",
Aliases: []string{"s"},
Usage: "saves an installed package to " + REQS_PATH,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dev",
Aliases: []string{"d"},
Value: false,
Usage: "save to " + DEV_REQS_PATH,
},
},
Action: saveCmd,
},
{
Name: "uninstall",
Aliases: []string{"remove", "rm", "u"},
Usage: "uninstalls a package and updates " + REQS_PATH,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dev",
Aliases: []string{"d"},
Value: false,
Usage: "remove from " + DEV_REQS_PATH,
},
},
Action: uninstallCmd,
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "deletes a package from " + REQS_PATH,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dev",
Aliases: []string{"d"},
Value: false,
Usage: "delete from " + DEV_REQS_PATH,
},
},
Action: unsaveCmd,
},
},
}
if err := app.Run(os.Args); err != nil {
// log.Fatal(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func installCmd(ctx *cli.Context) error {
if ctx.Args().Len() < 1 {
return fmt.Errorf("got 0 arguments")
}
args := ctx.Args().Slice()
execArgs := []string{"-m", "pip", "install"}
execArgs = append(execArgs, args...)
cmd := exec.Command("python", execArgs...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
path := REQS_PATH
if ctx.Bool("dev") {
path = DEV_REQS_PATH
}
inspection, err := pipInspect()
if err != nil {
return err
}
pkgs := parseArgs(args)
if err := savePkgs(path, inspection, pkgs); err != nil {
fmt.Println(err)
}
return nil
}
func saveCmd(ctx *cli.Context) error {
if ctx.Args().Len() < 1 {
return fmt.Errorf("got 0 arguments")
}
path := REQS_PATH
if ctx.Bool("dev") {
path = DEV_REQS_PATH
}
inspection, err := pipInspect()
if err != nil {
return err
}
pkgs := parseArgs(ctx.Args().Slice())
if err := savePkgs(path, inspection, pkgs); err != nil {
return err
}
return nil
}
// TODO: Skip truncating requirements file
// if uninstall fails
func uninstallCmd(ctx *cli.Context) error {
if ctx.Args().Len() < 1 {
return fmt.Errorf("got 0 arguments")
}
args := []string{"-m", "pip", "uninstall"}
args = append(args, ctx.Args().Slice()...)
cmd := exec.Command("python", args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
path := REQS_PATH
if ctx.Bool("dev") {
path = DEV_REQS_PATH
}
pkgs := parseArgs(ctx.Args().Slice())
if err := removePkgs(path, pkgs); err != nil {
return err
}
return nil
}
func unsaveCmd(ctx *cli.Context) error {
if ctx.Args().Len() < 1 {
return fmt.Errorf("got 0 arguments")
}
path := REQS_PATH
if ctx.Bool("dev") {
path = DEV_REQS_PATH
}
pkgs := parseArgs(ctx.Args().Slice())
if err := removePkgs(path, pkgs); err != nil {
return err
}
return nil
}
// pipInsect runs pip inspect
// and decodes the output.
func pipInspect() (*PipInspection, error) {
var stdout bytes.Buffer
// Run pip inspect. Ignore stderr and block stdin.
cmd := exec.Command("python", "-m", "pip", "inspect")
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return nil, err
}
pi := new(PipInspection)
dec := json.NewDecoder(&stdout)
if err := dec.Decode(pi); err != nil {
return nil, err
}
return pi, nil
}
// parseArgs parses a list of requirement specifiers
// returning a map of normalizedName -> extras.
func parseArgs(specifiers []string) map[string]string {
pkgs := map[string]string{}
for _, arg := range specifiers {
name, extras, _ := parseSpecifier(arg)
pkgs[name] = extras
}
return pkgs
}
// parseSpecifier parses a requirement specifier
// in the format packageName[extra1,extra2]==0.0.1
// naively to return package-name,[extras],versionspecifier.
// TODO: Parse version specifiers.
// See PEP508 - https://peps.python.org/pep-0508/.
func parseSpecifier(s string) (string, string, string) {
var name, extras, version string
if s == "" {
return "", "", ""
}
splt := strings.SplitN(s, "==", 2)
if len(splt) > 1 {
version = splt[1]
}
ne := splt[0]
if strings.Contains(ne, "[") {
loc := extrasRgx.FindStringIndex(s)
if loc != nil {
extras = ne[loc[0]:loc[1]]
name = normalizePkgName(ne[:loc[0]])
}
} else {
name = normalizePkgName(ne)
}
return name, extras, version
}
// removePkgs deletes all lines in the requirements file specified by path
// which are requirements specifications for any package in pkgs.
func removePkgs(path string, pkgs map[string]string) error {
f, err := os.OpenFile(path, os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
log.Fatal(err)
}
defer func() {
if err := f.Close(); err != nil {
log.Fatal(err)
}
}()
var buf bytes.Buffer
var e error
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
name, _, _ := parseSpecifier(line)
if _, ok := pkgs[name]; !ok {
_, err := buf.Write(scanner.Bytes())
if err != nil {
e = errors.Join(err, err)
continue
}
_, err = buf.WriteString("\n")
if err != nil {
e = errors.Join(err, err)
continue
}
}
}
if err := scanner.Err(); err != nil {
return errors.Join(err, e)
}
f.Truncate(0)
f.Seek(0, io.SeekStart)
_, err = io.Copy(f, &buf)
if err != nil {
return errors.Join(err, e)
}
return e
}
// savePkgs adds pkgs to a requirements file.
func savePkgs(path string, inspection *PipInspection, pkgs map[string]string) error {
var e error
inspHash := make(map[string]*InspectReportItem)
for i, it := range inspection.Installed {
inspHash[normalizePkgName(it.Metadata.Name)] = &inspection.Installed[i]
}
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
log.Fatal(e)
}
defer func() {
if err := f.Close(); err != nil {
log.Fatal(err)
}
}()
dups := make(map[string]bool)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
name, _, _ := parseSpecifier(line)
if _, ok := pkgs[name]; ok {
dups[name] = true
}
}
if err := scanner.Err(); err != nil {
return errors.Join(err, err)
}
for p, extras := range pkgs {
item, ok := inspHash[p]
if !ok {
e = errors.Join(e, fmt.Errorf("package %s not found", p))
continue
}
if dup := dups[p]; dup {
e = errors.Join(e, fmt.Errorf("package %s duplicate found", p))
} else {
_, err := f.WriteString(fmt.Sprintf("%s%s==%s\n", p, extras, item.Metadata.Version))
if err != nil {
errors.Join(err, fmt.Errorf("could not add %s to %s: %v", p, path, err))
continue
}
}
}
return e
}
// normalizePkgName normalizes a package name.
// See - https://peps.python.org/pep-0503/#normalized-names.
// Regex test - https://go.dev/play/p/4SazgOPaJmm
func normalizePkgName(name string) string {
return strings.ToLower(normalizeRgx.ReplaceAllLiteralString(name, "-"))
}