From b3ca06d08f4bba7af522a085e942d523c697a684 Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Wed, 10 Jan 2018 02:49:20 +0000 Subject: first commit --- d1pkgweb.go | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++ deb822/deb822_decl.go | 7 ++++ deb822/package.go | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++ deb822/parse.go | 80 +++++++++++++++++++++++++++++++++++++ 4 files changed, 303 insertions(+) create mode 100644 d1pkgweb.go create mode 100644 deb822/deb822_decl.go create mode 100644 deb822/package.go create mode 100644 deb822/parse.go diff --git a/d1pkgweb.go b/d1pkgweb.go new file mode 100644 index 0000000..d416e27 --- /dev/null +++ b/d1pkgweb.go @@ -0,0 +1,108 @@ +package main + +import ( + "bufio" + "d1pkgweb/deb822" + "fmt" + "os" +) + +var templ = ` + +{{.Name}}-{{.Version}} + +
{{.Name}} {{.Version}}
+
+{{.Description}} +
+
+{{.LongDescription}} +
+ +
+
+Depends: + +
+
+Maintainer: {{.Maintainer}} +
+ + +` + +func main() { + + args := os.Args + + if len(args) < 2 { + fmt.Printf("Usage: %s \n", args[0]) + return + } + + fnames := args[1:] + for _, fname := range fnames { + f, err := os.Open(fname) + if err != nil { + defer f.Close() + } + if err != nil { + fmt.Printf("Error opening file %s\n", fname) + + } else { + r := bufio.NewScanner(f) + + if r != nil { + for s, err := deb822.ScanStanza(r); s["Package"] != ""; s, err = deb822.ScanStanza(r) { + if err == nil { + deb822.Stanza2HtmlPage(s, templ, ".") + } else { + fmt.Printf("error: %s\n", err) + } + //WriteFiles(s, num, "./files/") + } + } + } + } +} diff --git a/deb822/deb822_decl.go b/deb822/deb822_decl.go new file mode 100644 index 0000000..82dcdbb --- /dev/null +++ b/deb822/deb822_decl.go @@ -0,0 +1,7 @@ +package deb822 + +/*Stanza map containing the values of fields in an RFC822 stanza */ +type Stanza map[string]string + +/*Stanzas array of Stanza*/ +type Stanzas []map[string]string diff --git a/deb822/package.go b/deb822/package.go new file mode 100644 index 0000000..df444ea --- /dev/null +++ b/deb822/package.go @@ -0,0 +1,108 @@ +package deb822 + +import ( + "fmt" + "html/template" + "io" + "log" + "os" + "regexp" + "strings" +) + +type Package struct { + Name string + Version string + Description string + LongDescription string + Depends []string + Maintainer string + Filename 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.Depends = strings.Split(s["Depends"], ",") + + 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) error { + + fname := s["Filename"] + if fname == "" { + return fmt.Errorf("No Filename provided") + } + p, err := NewPackage(s) + if err != nil { + log.Fatal("empty package!!!") + } + nameVersion := fmt.Sprintf("%s_%s", p.Name, p.Version) + fmt.Printf(" nameVersion: %s\n", nameVersion) + fmt.Printf(" baseDir: %s\n", baseDir) + + fname = regexpRemove.ReplaceAllString(fname, "") + fmt.Printf(" fname: %s\n", fname) + + /// FIXME!!!! ERROR IN DETECTION OF PACKAGE NAME!!!! + dirName := fmt.Sprintf("%s/%s", baseDir, strings.Split(fname, nameVersion)[0]) + + err = os.MkdirAll(dirName, 0755) + if err == nil { + fmt.Printf(" dirName: %s\n Package: %s\n Version: %s\n", dirName, p.Name, p.Version) + htmlFile := fmt.Sprintf("%s%s_%s.html", dirName, p.Name, p.Version) + fmt.Printf("Creating file: %s\n---------\n", htmlFile) + if f, err := os.Open(htmlFile); err == nil { + f.Close() + fmt.Printf("File %s exists -- skipping\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 +} diff --git a/deb822/parse.go b/deb822/parse.go new file mode 100644 index 0000000..a8a02e6 --- /dev/null +++ b/deb822/parse.go @@ -0,0 +1,80 @@ +package deb822 + +import ( + "bufio" + "fmt" + "strings" +) + +/*ParseStream parses a stream and returns an array of maps, where +/* each map contains all the fields of an RFC822 stanza */ +func ParseStream(stream *bufio.Scanner) (Stanzas, int) { + + s := make(Stanzas, 10) + curSize := 10 + for i := 0; i < 10; i++ { + s[i] = make(map[string]string) + } + curStanza := 0 + curField := "" + for stream.Scan() { + if strings.TrimSpace(stream.Text()) == "" { + if curField == "" { + continue + } + curStanza++ + curField = "" + fmt.Printf("==== New stanza ==== (Nr. %d/%d)\n", curStanza, curSize) + if curStanza >= curSize { + newS := make(Stanzas, curSize*2) + for i := curSize; i < curSize*2; i++ { + newS[i] = make(map[string]string) + } + curSize *= 2 + copy(newS, s) + s = newS + } + continue + } + + //fmt.Printf("line: %s\n", stream.Text()) + field := strings.SplitN(stream.Text(), ":", 2) + //fmt.Printf("field: %s\n", field) + if len(field) > 1 { + /* This is a new field*/ + curField = field[0] + s[curStanza][curField] = strings.TrimSpace(field[1]) + } else { + /* this is the continuation of the last field */ + s[curStanza][curField] += "\n" + field[0] + } + } + return s, curStanza +} + +/*ScanStanza reads a single RFC822 stanza from a stream and returns +/* it as a map */ +func ScanStanza(stream *bufio.Scanner) (Stanza, error) { + + s := make(Stanza) + + curField := "" + for stream.Scan() { + if strings.TrimSpace(stream.Text()) == "" { + return s, nil + } + + field := strings.SplitN(stream.Text(), ":", 2) + if len(field) > 1 { + /* This is a new field*/ + curField = field[0] + s[curField] = strings.TrimSpace(field[1]) + } else { + /* this is the continuation of the last field */ + s[curField] += "\n" + field[0] + } + } + + err := stream.Err() + return s, err +} -- cgit v1.2.3