File: copyright.go

package info (click to toggle)
adequate 0.17.6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 488 kB
  • sloc: python: 254; makefile: 111; sh: 75; ansic: 29
file content (83 lines) | stat: -rw-r--r-- 1,905 bytes parent folder | download | duplicates (2)
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
// This file is part of the adequate Debian-native package, and is available
// under the Expat license. For the full terms please see debian/copyright.

package main

import (
	"fmt"
	"log"
	"os"
	"regexp"
)

const MISSING_COPYRIGHT_FILE_TAG = "missing-copyright-file"

type missingCopyrightErr struct {
	pkg  string
	path string
	err  error
}

var (
	archAwarePackageNameRE *regexp.Regexp = regexp.MustCompile("([^:]+)(:?:.*)?")
)

func (m missingCopyrightErr) Error() string {
	if m == (missingCopyrightErr{}) {
		return ""
	}
	if m.err != nil {
		return fmt.Sprintf("%q: %s %v\n", m.pkg, MISSING_COPYRIGHT_FILE_TAG, m.err)
	}
	return fmt.Sprintf("%s: %s %s", m.pkg, MISSING_COPYRIGHT_FILE_TAG, m.path)
}

type copyrightChecker struct {
	emit bool
}

func newCopyrightChecker(tags tagFilter) copyrightChecker {
	return copyrightChecker{
		emit: tags.shouldEmit(MISSING_COPYRIGHT_FILE_TAG),
	}
}

func (cc copyrightChecker) check(pkgs []string) []error {
	if !cc.emit {
		return nil
	}
	var errs []error
	for _, pkg := range pkgs {
		if err := cc.isOkay(pkg); err != (missingCopyrightErr{}) {
			errs = append(errs, err)
		}
	}
	return errs
}

func (cc copyrightChecker) isOkay(pkg string) missingCopyrightErr {
	p, ok := copyrightFileExists(pkg)
	if !ok {
		return missingCopyrightErr{
			pkg:  pkg,
			path: p,
		}
	}

	return missingCopyrightErr{}
}

// copyrightFileExists returns the expected path to the copyright file for a
// given binary package and whether it exists. It does not attempt to
// distinguish between whether a file is missing or we get an error while trying
// to determine so.
func copyrightFileExists(pkg string) (string, bool) {
	m := archAwarePackageNameRE.FindStringSubmatch(pkg)
	if len(m) < 2 {
		log.Fatalf("Unexpected package name: %q", pkg)
	}
	noArchPkg := m[1]
	p := fmt.Sprintf("/usr/share/doc/%s/copyright", noArchPkg)
	_, err := os.Stat(p)
	return p, err == nil
}