summaryrefslogtreecommitdiff
path: root/deb822/package.go
blob: 525f112ca7ca91c5b1a5bde8f3a2be45936a9075 (plain)
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
package deb822

import (
	"fmt"
	"html/template"
	"io"
	"log"
	"os"
	"regexp"
	"strings"
)

type Package struct {
	Name            string
	Version         string
	Description     string
	LongDescription string
	Provides        []string
	Depends         []string
	Recommends      []string
	Maintainer      string
	Filename        string
	Suite           string
	Component       string
	Section         string
	Priority        string
}

var regexpRemove = regexp.MustCompile("(DEVUAN/|DEBIAN/|[0-9]+:)")

var regexpDots = regexp.MustCompilePOSIX("(^[[:blank:]]+.[[:blank:]]*$)")

func NewPackage(s Stanza) (Package, error) {

	var p Package

	if s["Package"] == "" || s["Version"] == "" {
		return p, fmt.Errorf("empty package")
	}

	p.Name = s["Package"]
	p.Version = regexpRemove.ReplaceAllString(s["Version"], "")
	descr := strings.SplitN(s["Description"], "\n", 2)
	p.Description = descr[0]
	if len(descr) > 1 {
		p.LongDescription = regexpDots.ReplaceAllString(descr[1], "")
	}
	p.Maintainer = s["Maintainer"]
	p.Provides = strings.Split(s["Provides"], ",")
	if len(p.Provides) == 0 {
		p.Provides = nil
	}

	p.Depends = strings.Split(s["Depends"], ",")
	if len(p.Depends) == 0 {
		p.Depends = nil
	}
	p.Recommends = strings.Split(s["Recommends"], ",")
	if len(p.Recommends) == 0 {
		p.Recommends = nil
	}
	p.Section = s["Section"]
	p.Priority = s["Priority"]
	return p, nil

}

func PrintPackage(p Package, templ string, out io.Writer) {

	t, err := template.New("webpage").Parse(templ)

	if err != nil {
		log.Fatal("Error in template")
	}

	t.Execute(out, p)
}

/*Stanza2HtmlPage Render the html webpage of a package and save it in the
/* corresponding "pool" directory.
*/
func Stanza2HtmlPage(s Stanza, templ string, baseDir string, release string, suite string, component string) error {

	fname := s["Filename"]
	if fname == "" {
		return fmt.Errorf("No Filename provided")
	}
	p, err := NewPackage(s)
	p.Suite = suite
	p.Component = component
	if err != nil {
		log.Fatal("empty package!!!")
	}
	//nameVersion := fmt.Sprintf("%s_%s", p.Name, p.Version)

	fname = regexpRemove.ReplaceAllString(fname, "")

	//dirName := fmt.Sprintf("%s/%s", baseDir, strings.Split(fname, nameVersion)[0])
	dirName := fmt.Sprintf("%s/%s/%s/", baseDir, release, suite)
	err = os.MkdirAll(dirName, 0755)
	if err == nil {
		//htmlFile := fmt.Sprintf("%s%s_%s.html", dirName, p.Name, p.Version)
		htmlFile := fmt.Sprintf("%s%s_%s.html", dirName, p.Name, p.Version)

		if f, err := os.Open(htmlFile); err == nil {
			f.Close()
			fmt.Fprintf(os.Stderr, "  Skipping %s -- it exists\n", htmlFile)
			return err
		}
		file, err := os.Create(htmlFile)
		if err != nil {
			err = fmt.Errorf("Error creating file: %s", err)
			return err
		} else {
			PrintPackage(p, templ, file)
			file.Close()
			return nil
		}
	} else {
		err = fmt.Errorf("Error creating path: %s", err)
		return err
	}

	return nil
}