/
synthesis.go
127 lines (101 loc) · 2.63 KB
/
synthesis.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
// Copyright (C) 2015 Alexander Sokolov <sokoloff.a@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
gzip "github.com/klauspost/pgzip"
"fmt"
"io"
"os"
"strconv"
"strings"
)
func splitLine(line string, out ...*string) error {
items := strings.Split(line, "@")
if len(items) < len(out)+2 {
return fmt.Errorf("Can't parse synthesis line %v: expected %v fields got %v", line, len(out)+2, len(items))
}
for i := 0; i < len(out); i++ {
if out[i] != nil {
*out[i] = items[i+2]
}
}
return nil
}
func ReadSynthesisFile(repo Repository, file string, out chan<- Package) error {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
gz, err := gzip.NewReader(f)
if err != nil {
return err
}
defer gz.Close()
r := bufio.NewReader(gz)
cur := Package{}
for err == nil {
line, err := r.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("can't read synthesis file: %v", err)
}
line = strings.Trim(line, "\n\r")
// @provides@libvo-amrwbenc.so.0@libvo-amrwbenc0[== 0.1.2-1:2014.1]
// @requires@libc.so.6@libc.so.6(GLIBC_2.0)@libc.so.6(GLIBC_2.1.3)
// @summary@VisualOn AMR-WB encoder library
// @filesize@68793
// @info@libvo-amrwbenc0-0.1.2-1-rosa2014.1.i586@0@143156@System/Libraries@rosa@2014.1
if strings.HasPrefix(line, "@info") {
var size string
err = splitLine(line,
&cur.FileName,
nil,
&size,
&cur.Group,
&cur.Disttag,
&cur.Distepoch)
if err != nil {
return err
}
cur.Repository = repo.Name
cur.Size, err = strconv.Atoi(size)
if err != nil {
return fmt.Errorf("Can't read synthesis file: incorrect filesize '%v': %v", size, err)
}
items := strings.Split(cur.FileName, "-")
if len(items) < 4 {
return fmt.Errorf("Can't parse package filename %s", cur.FileName)
}
s := items[len(items)-1]
n := strings.LastIndex(s, ".")
cur.Arch = s[n+1:]
cur.Version = items[len(items)-3] + "-" + items[len(items)-2]
cur.Name = strings.Join(items[:len(items)-3], "-")
out <- cur
cur = Package{}
continue
}
if strings.HasPrefix(line, "@summary@") {
cur.Summary = line[9:]
continue
}
if strings.HasPrefix(line, "@filesize@") {
items := strings.Split(line, "@")
if len(items) < 3 {
return fmt.Errorf("can't read synthesis file: package filesize not found %v", line)
}
cur.RPMSize, err = strconv.Atoi(items[2])
if err != nil {
return fmt.Errorf("Can't read synthesis file: incorrect RPM size %v: %v", items[2], err)
}
continue
}
}
return nil
}