-
Notifications
You must be signed in to change notification settings - Fork 1
/
poll.go
128 lines (103 loc) · 2.6 KB
/
poll.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
package main
import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var pollCmd = &cobra.Command{
Use: "poll",
Short: "update arXiv index",
Long: `Poll arXiv for more papers, and then compute features for them.`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
setConfig()
var err error
if onlyCompute {
err = compute()
} else {
err = pollAndCompute()
}
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
},
}
var onlyCompute bool
func init() {
pollCmd.Flags().BoolVarP(
&onlyCompute, "only-compute", "c", false, "don't poll, just update features")
}
func pollAndCompute() error {
gotNew, err := poll()
if err != nil {
return fmt.Errorf("poll arXiv: %w", err)
}
if gotNew {
err = compute()
if err != nil {
return fmt.Errorf("compute features: %w", err)
}
}
return err
}
// poll runs arxiv_daemon.py and returns true if new papers were added.
func poll() (bool, error) {
// edit file to add our query string
pollQuery := viper.GetString("poll.query")
log.Println("modifying script with new poll query", pollQuery)
rep := strings.NewReplacer(
"cat:cs.CV+OR+cat:cs.LG+OR+cat:cs.CL+OR+cat:cs.AI+OR+cat:cs.NE+OR+cat:cs.RO",
pollQuery,
)
data, err := os.ReadFile("arxiv_daemon.py")
if err != nil {
return false, err
}
newData := rep.Replace(string(data))
err = os.WriteFile("arxiv_daemon_temp.py", []byte(newData), 0o600)
if err != nil {
return false, err
}
// run the edited script
cmd := exec.Command( //nolint:gosec // The customer is always right.
"python", "-u", "arxiv_daemon_temp.py",
"--num", strconv.Itoa(viper.GetInt("poll.num")),
"--start", strconv.Itoa(viper.GetInt("poll.start")),
"--break-after", strconv.Itoa(viper.GetInt("poll.break")),
)
err = run(cmd)
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
if exitErr.ExitCode() == 1 {
log.Println("no new papers found (or polling had an error)")
return false, nil
}
}
return false, err
}
return true, nil
}
func compute() error {
cmd := exec.Command( //nolint:gosec // The customer is always right.
"python", "-u", "compute.py",
"--num", strconv.Itoa(viper.GetInt("compute.features")),
"--min_df", viper.GetString("compute.min_df"),
"--max_df", viper.GetString("compute.max_df"),
"--max_docs", strconv.Itoa(viper.GetInt("compute.max_docs")),
)
return run(cmd)
}
// run runs the command and forwards stdout and stderr.
func run(cmd *exec.Cmd) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}