Converting package back to stanza.

This commit is contained in:
Andrey Smirnov
2013-12-24 01:17:05 +04:00
parent 2e44d23e4d
commit e37bcf2ea0
3 changed files with 55 additions and 8 deletions
+5 -1
View File
@@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
. "launchpad.net/gocheck" . "launchpad.net/gocheck"
"strings"
) )
type ControlFileSuite struct { type ControlFileSuite struct {
@@ -110,11 +111,14 @@ func (s *ControlFileSuite) TestReadWriteStanza(c *C) {
err = w.Flush() err = w.Flush()
c.Assert(err, IsNil) c.Assert(err, IsNil)
r = NewControlFileReader(bytes.NewBuffer(buf.Bytes())) str := buf.String()
r = NewControlFileReader(buf)
stanza2, err := r.ReadStanza() stanza2, err := r.ReadStanza()
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(stanza2, DeepEquals, stanza) c.Assert(stanza2, DeepEquals, stanza)
c.Assert(strings.HasPrefix(str, "Package: "), Equals, true)
} }
func (s *ControlFileSuite) BenchmarkReadStanza(c *C) { func (s *ControlFileSuite) BenchmarkReadStanza(c *C) {
+36
View File
@@ -103,6 +103,42 @@ func (p *Package) String() string {
return fmt.Sprintf("%s-%s_%s", p.Name, p.Version, p.Architecture) return fmt.Sprintf("%s-%s_%s", p.Name, p.Version, p.Architecture)
} }
// Stanza creates original stanza from package
func (p *Package) Stanza() (result Stanza) {
result = p.Extra.Copy()
result["Package"] = p.Name
result["Version"] = p.Version
result["Filename"] = p.Filename
result["Architecture"] = p.Architecture
if p.HashMD5 != "" {
result["MD5sum"] = p.HashMD5
}
if p.HashSHA1 != "" {
result["SHA1"] = p.HashSHA1
}
if p.HashSHA256 != "" {
result["SHA256"] = p.HashSHA256
}
if p.Depends != nil {
result["Depends"] = strings.Join(p.Depends, ", ")
}
if p.PreDepends != nil {
result["Pre-Depends"] = strings.Join(p.PreDepends, ", ")
}
if p.Suggests != nil {
result["Suggests"] = strings.Join(p.Suggests, ", ")
}
if p.Recommends != nil {
result["Recommends"] = strings.Join(p.Recommends, ", ")
}
result["Size"] = fmt.Sprintf("%d", p.Filesize)
return
}
// Equals compares two packages to be identical // Equals compares two packages to be identical
func (p *Package) Equals(p2 *Package) bool { func (p *Package) Equals(p2 *Package) bool {
return p.Name == p2.Name && p.Version == p2.Version && p.Filename == p2.Filename && return p.Name == p2.Name && p.Version == p2.Version && p.Filename == p2.Filename &&
+14 -7
View File
@@ -8,17 +8,17 @@ import (
var packageStanza = Stanza{"Source": "alien-arena", "Depends": "libc6 (>= 2.7), alien-arena-data (>= 7.40)", "Filename": "pool/contrib/a/alien-arena/alien-arena-common_7.40-2_i386.deb", "SHA1": "46955e48cad27410a83740a21d766ce362364024", "SHA256": "eb4afb9885cba6dc70cccd05b910b2dbccc02c5900578be5e99f0d3dbf9d76a5", "Priority": "extra", "Maintainer": "Debian Games Team <pkg-games-devel@lists.alioth.debian.org>", "Description": "Common files for Alien Arena client and server ALIEN ARENA is a standalone 3D first person online deathmatch shooter\n crafted from the original source code of Quake II and Quake III, released\n by id Software under the GPL license. With features including 32 bit\n graphics, new particle engine and effects, light blooms, reflective water,\n hi resolution textures and skins, hi poly models, stain maps, ALIEN ARENA\n pushes the envelope of graphical beauty rivaling today's top games.\n .\n This package installs the common files for Alien Arena.\n", "Homepage": "http://red.planetarena.org", "Tag": "role::app-data, role::shared-lib, special::auto-inst-parts", "Installed-Size": "456", "Version": "7.40-2", "Replaces": "alien-arena (<< 7.33-1)", "Size": "187518", "MD5sum": "1e8cba92c41420aa7baa8a5718d67122", "Package": "alien-arena-common", "Section": "contrib/games", "Architecture": "i386"} var packageStanza = Stanza{"Source": "alien-arena", "Depends": "libc6 (>= 2.7), alien-arena-data (>= 7.40)", "Filename": "pool/contrib/a/alien-arena/alien-arena-common_7.40-2_i386.deb", "SHA1": "46955e48cad27410a83740a21d766ce362364024", "SHA256": "eb4afb9885cba6dc70cccd05b910b2dbccc02c5900578be5e99f0d3dbf9d76a5", "Priority": "extra", "Maintainer": "Debian Games Team <pkg-games-devel@lists.alioth.debian.org>", "Description": "Common files for Alien Arena client and server ALIEN ARENA is a standalone 3D first person online deathmatch shooter\n crafted from the original source code of Quake II and Quake III, released\n by id Software under the GPL license. With features including 32 bit\n graphics, new particle engine and effects, light blooms, reflective water,\n hi resolution textures and skins, hi poly models, stain maps, ALIEN ARENA\n pushes the envelope of graphical beauty rivaling today's top games.\n .\n This package installs the common files for Alien Arena.\n", "Homepage": "http://red.planetarena.org", "Tag": "role::app-data, role::shared-lib, special::auto-inst-parts", "Installed-Size": "456", "Version": "7.40-2", "Replaces": "alien-arena (<< 7.33-1)", "Size": "187518", "MD5sum": "1e8cba92c41420aa7baa8a5718d67122", "Package": "alien-arena-common", "Section": "contrib/games", "Architecture": "i386"}
type PackageSuite struct { type PackageSuite struct {
para Stanza stanza Stanza
} }
var _ = Suite(&PackageSuite{}) var _ = Suite(&PackageSuite{})
func (s *PackageSuite) SetUpTest(c *C) { func (s *PackageSuite) SetUpTest(c *C) {
s.para = packageStanza.Copy() s.stanza = packageStanza.Copy()
} }
func (s *PackageSuite) TestNewFromPara(c *C) { func (s *PackageSuite) TestNewFromPara(c *C) {
p := NewPackageFromControlFile(s.para) p := NewPackageFromControlFile(s.stanza)
c.Check(p.Name, Equals, "alien-arena-common") c.Check(p.Name, Equals, "alien-arena-common")
c.Check(p.Version, Equals, "7.40-2") c.Check(p.Version, Equals, "7.40-2")
@@ -30,13 +30,13 @@ func (s *PackageSuite) TestNewFromPara(c *C) {
} }
func (s *PackageSuite) TestKey(c *C) { func (s *PackageSuite) TestKey(c *C) {
p := NewPackageFromControlFile(s.para) p := NewPackageFromControlFile(s.stanza)
c.Check(p.Key(), DeepEquals, []byte("Palien-arena-common 7.40-2 i386")) c.Check(p.Key(), DeepEquals, []byte("Palien-arena-common 7.40-2 i386"))
} }
func (s *PackageSuite) TestEncodeDecode(c *C) { func (s *PackageSuite) TestEncodeDecode(c *C) {
p := NewPackageFromControlFile(s.para) p := NewPackageFromControlFile(s.stanza)
encoded := p.Encode() encoded := p.Encode()
p2 := &Package{} p2 := &Package{}
err := p2.Decode(encoded) err := p2.Decode(encoded)
@@ -45,13 +45,20 @@ func (s *PackageSuite) TestEncodeDecode(c *C) {
c.Assert(p2, DeepEquals, p) c.Assert(p2, DeepEquals, p)
} }
func (s *PackageSuite) TestStanza(c *C) {
p := NewPackageFromControlFile(s.stanza.Copy())
stanza := p.Stanza()
c.Assert(stanza, DeepEquals, s.stanza)
}
func (s *PackageSuite) TestString(c *C) { func (s *PackageSuite) TestString(c *C) {
p := NewPackageFromControlFile(s.para) p := NewPackageFromControlFile(s.stanza)
c.Assert(p.String(), Equals, "alien-arena-common-7.40-2_i386") c.Assert(p.String(), Equals, "alien-arena-common-7.40-2_i386")
} }
func (s *PackageSuite) TestEquals(c *C) { func (s *PackageSuite) TestEquals(c *C) {
p := NewPackageFromControlFile(s.para) p := NewPackageFromControlFile(s.stanza)
stanza2 := packageStanza.Copy() stanza2 := packageStanza.Copy()
p2 := NewPackageFromControlFile(stanza2) p2 := NewPackageFromControlFile(stanza2)