Skip to content

Commit 066571e

Browse files
n-peugnetottok
authored andcommitted
estimate: show packages in NEW, but with a light blue color
It uses an additional query from the DAK Web API to get the packages currently in NEW, but this one returns only source packages, so we have to keep into memory the name of the source packages when we get the list of all golang packages.
1 parent f3a0410 commit 066571e

File tree

5 files changed

+76
-23
lines changed

5 files changed

+76
-23
lines changed

check_depends.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func execCheckDepends(args []string) {
9191
// parseGoModDependencies parse ALL dependencies listed in go.mod
9292
// i.e. it returns the one defined in go.mod as well as the transitively ones
9393
// TODO: this may not be the best way of doing thing since it requires the package to be converted to go module
94-
func parseGoModDependencies(directory string, goBinaries map[string]string) ([]dependency, error) {
94+
func parseGoModDependencies(directory string, goBinaries map[string]debianPackage) ([]dependency, error) {
9595
b, err := os.ReadFile(filepath.Join(directory, "go.mod"))
9696
if err != nil {
9797
return nil, err
@@ -115,7 +115,7 @@ func parseGoModDependencies(directory string, goBinaries map[string]string) ([]d
115115
}
116116

117117
if val, exists := goBinaries[rr.Root]; exists {
118-
packageName = val
118+
packageName = val.binary
119119
}
120120

121121
dependencies = append(dependencies, dependency{

check_depends_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ require (
110110
t.Fatalf("Could not create dummy Debian package: %v", err)
111111
}
112112

113-
deps, err := parseGoModDependencies(filepath.Join(tmpDir, "dummy-package"), map[string]string{
114-
"github.com/charmbracelet/glamour": "golang-github-charmbracelet-glamour-dev",
115-
"github.com/google/go-github": "golang-github-google-go-github-dev",
116-
"github.com/gregjones/httpcache": "golang-github-gregjones-httpcache-dev",
113+
deps, err := parseGoModDependencies(filepath.Join(tmpDir, "dummy-package"), map[string]debianPackage{
114+
"github.com/charmbracelet/glamour": {binary: "golang-github-charmbracelet-glamour-dev", source: "golang-github-charmbracelet-glamour"},
115+
"github.com/google/go-github": {binary: "golang-github-google-go-github-dev", source: "golang-github-google-go-github"},
116+
"github.com/gregjones/httpcache": {binary: "golang-github-gregjones-httpcache-dev", source: "golang-github-gregjones-httpcache"},
117117
})
118118
if err != nil {
119119
t.Fatalf("Could not parse go.mod dependencies: %v", err)

estimate.go

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package main
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"flag"
67
"fmt"
78
"log"
9+
"net/http"
810
"os"
911
"os/exec"
1012
"path/filepath"
@@ -15,6 +17,10 @@ import (
1517
"golang.org/x/tools/go/vcs"
1618
)
1719

20+
const (
21+
sourcesInNewURL = "https://api.ftp-master.debian.org/sources_in_suite/new"
22+
)
23+
1824
// majorVersionRegexp checks if an import path contains a major version suffix.
1925
var majorVersionRegexp = regexp.MustCompile(`([/.])v([0-9]+)$`)
2026

@@ -25,6 +31,29 @@ var moduleBlocklist = map[string]string{
2531
"github.com/Microsoft/go-winio": "Windows only",
2632
}
2733

34+
func getSourcesInNew() (map[string]string, error) {
35+
sourcesInNew := make(map[string]string)
36+
37+
resp, err := http.Get(sourcesInNewURL)
38+
if err != nil {
39+
return nil, fmt.Errorf("getting %q: %w", golangBinariesURL, err)
40+
}
41+
if got, want := resp.StatusCode, http.StatusOK; got != want {
42+
return nil, fmt.Errorf("unexpected HTTP status code: got %d, want %d", got, want)
43+
}
44+
var pkgs []struct {
45+
Source string `json:"source"`
46+
Version string `json:"version"`
47+
}
48+
if err := json.NewDecoder(resp.Body).Decode(&pkgs); err != nil {
49+
return nil, fmt.Errorf("decode: %w", err)
50+
}
51+
for _, pkg := range pkgs {
52+
sourcesInNew[pkg.Source] = pkg.Version
53+
}
54+
return sourcesInNew, nil
55+
}
56+
2857
func get(gopath, repodir, repo, rev string) error {
2958
done := make(chan struct{})
3059
defer close(done)
@@ -97,14 +126,14 @@ func otherVersions(mod string) (mods []string) {
97126
// findOtherVersion search in m for potential other versions of the given
98127
// module and returns the number of the major version found, 0 if not,
99128
// along with the corresponding package name.
100-
func findOtherVersion(m map[string]string, mod string) (int, string) {
129+
func findOtherVersion(m map[string]debianPackage, mod string) (int, debianPackage) {
101130
versions := otherVersions(mod)
102131
for i, version := range versions {
103132
if pkg, ok := m[version]; ok {
104133
return len(versions) - i, pkg
105134
}
106135
}
107-
return 0, ""
136+
return 0, debianPackage{}
108137
}
109138

110139
// trackerLink generates an OSC 8 hyperlink to the tracker for the given Debian
@@ -170,7 +199,11 @@ func estimate(importpath, revision string) error {
170199
// Retrieve already-packaged ones
171200
golangBinaries, err := getGolangBinaries()
172201
if err != nil {
173-
return nil
202+
return fmt.Errorf("get golang debian packages: %w", err)
203+
}
204+
sourcesInNew, err := getSourcesInNew()
205+
if err != nil {
206+
return fmt.Errorf("get packages in new: %w", err)
174207
}
175208

176209
// Build a graph in memory from the output of go mod graph
@@ -233,7 +266,11 @@ func estimate(importpath, revision string) error {
233266
if mod == "go" || mod == "toolchain" {
234267
return
235268
}
236-
if _, ok := golangBinaries[mod]; ok {
269+
if pkg, ok := golangBinaries[mod]; ok {
270+
if _, ok := sourcesInNew[pkg.source]; ok {
271+
line := fmt.Sprintf("%s\033[36m%s (in NEW)\033[0m", strings.Repeat(" ", indent), mod)
272+
lines = append(lines, line)
273+
}
237274
return // already packaged in Debian
238275
}
239276
var repoRoot string
@@ -250,9 +287,13 @@ func estimate(importpath, revision string) error {
250287
// Log info to indicate that it is an approximate match
251288
// but consider that it is packaged and skip the children.
252289
if v == 1 {
253-
log.Printf("%s has no version string in Debian (%s)", mod, trackerLink(pkg))
290+
log.Printf("%s has no version string in Debian (%s)", mod, trackerLink(pkg.source))
254291
} else {
255-
log.Printf("%s is v%d in Debian (%s)", mod, v, trackerLink(pkg))
292+
log.Printf("%s is v%d in Debian (%s)", mod, v, trackerLink(pkg.source))
293+
}
294+
if _, ok := sourcesInNew[pkg.source]; ok {
295+
line := fmt.Sprintf("%s\033[36m%s (in NEW)\033[0m", strings.Repeat(" ", indent), mod)
296+
lines = append(lines, line)
256297
}
257298
return
258299
}
@@ -262,7 +303,11 @@ func estimate(importpath, revision string) error {
262303
if pkg, ok := golangBinaries[repoRoot]; ok {
263304
// Log info to indicate that it is an approximate match
264305
// but consider that it is packaged and skip the children.
265-
log.Printf("%s is packaged as %s in Debian (%s)", mod, repoRoot, trackerLink(pkg))
306+
log.Printf("%s is packaged as %s in Debian (%s)", mod, repoRoot, trackerLink(pkg.source))
307+
if _, ok := sourcesInNew[pkg.source]; ok {
308+
line := fmt.Sprintf("%s\033[36m%s (in NEW)\033[0m", strings.Repeat(" ", indent), mod)
309+
lines = append(lines, line)
310+
}
266311
return
267312
}
268313
// Ignore modules from the blocklist.

make.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ func execMake(args []string, usage func()) {
932932

933933
var (
934934
eg errgroup.Group
935-
golangBinaries map[string]string // map[goImportPath]debianBinaryPackage
935+
golangBinaries map[string]debianPackage // map[goImportPath]debianPackage
936936
)
937937

938938
// TODO: also check whether there already is a git repository on salsa.
@@ -965,9 +965,9 @@ func execMake(args []string, usage func()) {
965965
log.Printf("Could not check for existing Go packages in Debian: %v", err)
966966
}
967967

968-
if debbin, ok := golangBinaries[gopkg]; ok {
968+
if debpkg, ok := golangBinaries[gopkg]; ok {
969969
log.Printf("WARNING: A package called %q is already in Debian! See https://tracker.debian.org/pkg/%s\n",
970-
debbin, debbin)
970+
debpkg.binary, debpkg.source)
971971
}
972972

973973
orig := fmt.Sprintf("%s_%s.orig.tar.%s", debsrc, u.version, u.compression)
@@ -995,12 +995,12 @@ func execMake(args []string, usage func()) {
995995
debdependencies = append(debdependencies, debianNameFromGopkg(dep, typeLibrary, "", allowUnknownHoster)+"-dev")
996996
continue
997997
}
998-
bin, ok := golangBinaries[dep]
998+
pkg, ok := golangBinaries[dep]
999999
if !ok {
10001000
log.Printf("Build-Dependency %q is not yet available in Debian, or has not yet been converted to use XS-Go-Import-Path in debian/control", dep)
10011001
continue
10021002
}
1003-
debdependencies = append(debdependencies, bin)
1003+
debdependencies = append(debdependencies, pkg.binary)
10041004
}
10051005

10061006
if err := writeTemplates(dir, gopkg, debsrc, debLib, debProg, debversion,

search.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ const (
1515
golangBinariesURL = "https://api.ftp-master.debian.org/binary/by_metadata/Go-Import-Path"
1616
)
1717

18-
func getGolangBinaries() (map[string]string, error) {
19-
golangBinaries := make(map[string]string)
18+
type debianPackage struct {
19+
binary string
20+
source string
21+
}
22+
23+
func getGolangBinaries() (map[string]debianPackage, error) {
24+
golangBinaries := make(map[string]debianPackage)
2025

2126
resp, err := http.Get(golangBinariesURL)
2227
if err != nil {
@@ -39,7 +44,10 @@ func getGolangBinaries() (map[string]string, error) {
3944
}
4045
for _, importPath := range strings.Split(pkg.XSGoImportPath, ",") {
4146
// XS-Go-Import-Path can be comma-separated and contain spaces.
42-
golangBinaries[strings.TrimSpace(importPath)] = pkg.Binary
47+
golangBinaries[strings.TrimSpace(importPath)] = debianPackage{
48+
binary: pkg.Binary,
49+
source: pkg.Source,
50+
}
4351
}
4452
}
4553
return golangBinaries, nil
@@ -74,9 +82,9 @@ func execSearch(args []string) {
7482
log.Fatal(err)
7583
}
7684

77-
for importPath, binary := range golangBinaries {
85+
for importPath, pkg := range golangBinaries {
7886
if pattern.MatchString(importPath) {
79-
fmt.Printf("%s: %s\n", binary, importPath)
87+
fmt.Printf("%s: %s\n", pkg.binary, importPath)
8088
}
8189
}
8290
}

0 commit comments

Comments
 (0)