golang-godebiancontrol-dev/0000755000014500017510000000000012267021464015541 5ustar michaelstaffgolang-godebiancontrol-dev/debiancontrol_test.go0000644000014500017510000001031412267021434021746 0ustar michaelstaff// vim:ts=4:sw=4:noexpandtab // © 2012-2014 Michael Stapelberg (see also: LICENSE) package godebiancontrol_test import ( "bytes" "fmt" "github.com/stapelberg/godebiancontrol" "log" "os" "testing" ) func TestParse(t *testing.T) { contents := bytes.NewBufferString(`Package: bti Binary: bti Version: 032-1 Maintainer: gregor herrmann Uploaders: tony mancill Build-Depends: debhelper (>= 8), bash-completion (>= 1:1.1-3), libcurl4-nss-dev, libreadline-dev, libxml2-dev, libpcre3-dev, liboauth-dev, xsltproc, docbook-xsl, docbook-xml, dh-autoreconf Architecture: any Standards-Version: 3.9.2 Format: 3.0 (quilt) Files: 3d5f65778bf3f89be03c313b0024b62c 1980 bti_032-1.dsc 1e0d0b693fdeebec268004ba41701baf 59773 bti_032.orig.tar.gz ac1229a6d685023aeb8fcb0806324aa8 5065 bti_032-1.debian.tar.gz Vcs-Browser: http://svn.toastfreeware.priv.at/wsvn/ToastfreewareDebian/bti/trunk/ Vcs-Svn: http://svn.toastfreeware.priv.at/debian/bti/trunk/ Checksums-Sha1: 3da2c5a42138c884a7d9524b9706dc56c0d6d46e 1980 bti_032-1.dsc 22061e3f56074703be415d65abc9ca27ef775c6a 59773 bti_032.orig.tar.gz 66ae7f56a3c1f0ebe0638d0ec0599a819d72baea 5065 bti_032-1.debian.tar.gz Checksums-Sha256: ed6015b79693f270d0a826c695b40e4d8eb4307942cac81a98f1fda479f74215 1980 bti_032-1.dsc feeabec98a89040a53283d798f7d55eb4311a854f17312a177dc45919883746a 59773 bti_032.orig.tar.gz f025da42efaf57db5e71a14cb8be27eb802ad23e7ab02b7ce2252454a86ac1d9 5065 bti_032-1.debian.tar.gz Homepage: http://gregkh.github.com/bti/ Package-List: bti deb net extra Directory: pool/main/b/bti Priority: source Section: net Package: i3-wm Version: 4.2-1 Installed-Size: 1573 Maintainer: Michael Stapelberg Architecture: amd64 Provides: x-window-manager Depends: libc6 (>= 2.8), libev4 (>= 1:4.04), libpcre3 (>= 8.10), libstartup-notification0 (>= 0.10), libx11-6, libxcb-icccm4 (>= 0.3.8), libxcb-keysyms1 (>= 0.3.8), libxcb-randr0 (>= 1.3), libxcb-util0 (>= 0.3.8), libxcb-xinerama0, libxcb1, libxcursor1 (>> 1.1.2), libyajl2 (>= 2.0.4), perl, x11-utils Recommends: xfonts-base Suggests: rxvt-unicode | x-terminal-emulator Description-en: improved dynamic tiling window manager Key features of i3 are good documentation, reasonable defaults (changeable in a simple configuration file) and good multi-monitor support. The user interface is designed for power users and emphasizes keyboard usage. i3 uses XCB for asynchronous communication with X11 and aims to be fast and light-weight. . Please be aware i3 is primarily targeted at advanced users and developers. Homepage: http://i3wm.org/ Description-md5: 2be7e62f455351435b1e055745d3e81c Tag: implemented-in::c, interface::x11, role::program, uitoolkit::TODO, works-with::unicode, x11::window-manager Section: x11 Priority: extra Filename: pool/main/i/i3-wm/i3-wm_4.2-1_amd64.deb Size: 798186 MD5sum: 3c7dbecd76d5c271401860967563fa8c SHA1: 2e94f3faa5d4d617061f94076b2537d15fbff73f SHA256: 2894bc999b3982c4e57f100fa31e21b52e14c5f3bc7ad5345f46842fcdab0db7`) paragraphs, err := godebiancontrol.Parse(contents) if err != nil { t.Fatal(err) } if len(paragraphs) != 2 { t.Fatal("Expected exactly two paragraphs") } if paragraphs[0]["Format"] != "3.0 (quilt)" { t.Fatal(`"Format" (simple) was not parsed correctly`) } if paragraphs[0]["Build-Depends"] != "debhelper (>= 8),bash-completion (>= 1:1.1-3),libcurl4-nss-dev, libreadline-dev, libxml2-dev, libpcre3-dev, liboauth-dev, xsltproc, docbook-xsl, docbook-xml, dh-autoreconf" { t.Fatal(`"Build-Depends" (folder) was not parsed correctly`) } expectedFiles := ` 3d5f65778bf3f89be03c313b0024b62c 1980 bti_032-1.dsc 1e0d0b693fdeebec268004ba41701baf 59773 bti_032.orig.tar.gz ac1229a6d685023aeb8fcb0806324aa8 5065 bti_032-1.debian.tar.gz ` if paragraphs[0]["Files"] != expectedFiles { t.Fatal(`"Files" (multiline) was not parsed correctly`) } } func ExampleParse() { file, err := os.Open("debian-mirror/dists/sid/main/source/Sources") if err != nil { log.Fatal(err) } defer file.Close() paragraphs, err := godebiancontrol.Parse(file) if err != nil { log.Fatal(err) } // Print a list of which source package uses which package format. for _, pkg := range paragraphs { fmt.Printf("%s uses %s\n", pkg["Package"], pkg["Format"]) } } golang-godebiancontrol-dev/debiancontrol.go0000644000014500017510000000435612267021464020723 0ustar michaelstaff// vim:ts=4:sw=4:noexpandtab // © 2012-2014 Michael Stapelberg (see also: LICENSE) // Package debiancontrol implements a parser for Debian control files. package godebiancontrol import ( "bufio" "io" "strings" "unicode" ) type FieldType int const ( Simple FieldType = iota Folded Multiline ) var fieldType = make(map[string]FieldType) type Paragraph map[string]string func init() { fieldType["Description"] = Multiline fieldType["Files"] = Multiline fieldType["Changes"] = Multiline fieldType["Checksums-Sha1"] = Multiline fieldType["Checksums-Sha256"] = Multiline fieldType["Package-List"] = Multiline } // Parses a Debian control file and returns a slice of Paragraphs. // // Implemented according to chapter 5.1 (Syntax of control files) of the Debian // Policy Manual: // http://www.debian.org/doc/debian-policy/ch-controlfields.html func Parse(input io.Reader) (paragraphs []Paragraph, err error) { reader := bufio.NewReader(input) lastkey := "" var paragraph Paragraph = make(Paragraph) for { line, err := reader.ReadString('\n') if err != nil { if err == io.EOF { break } return nil, err } trimmed := strings.TrimSpace(line) // Check if the line is empty (or consists only of whitespace). This // marks a new paragraph. if trimmed == "" { if len(paragraph) > 0 { paragraphs = append(paragraphs, paragraph) } paragraph = make(Paragraph) continue } // folded and multiline fields must start with a space or tab. if line[0] == ' ' || line[0] == '\t' { if fieldType[lastkey] == Multiline { // Whitespace, including newlines, is significant in the values // of multiline fields, therefore we just append the line // as-is. paragraph[lastkey] += line } else { // For folded lines we strip whitespace before appending. paragraph[lastkey] += trimmed } } else { split := strings.Split(trimmed, ":") key := split[0] value := strings.TrimLeftFunc(trimmed[len(key)+1:], unicode.IsSpace) paragraph[key] = value lastkey = key } } // Append last paragraph, but only if it is non-empty. // The case of an empty last paragraph happens when the file ends with a // blank line. if len(paragraph) > 0 { paragraphs = append(paragraphs, paragraph) } return } golang-godebiancontrol-dev/README.md0000644000014500017510000000122112267021451017010 0ustar michaelstaffInstall: go get github.com/stapelberg/godebiancontrol …and then use it in your code: ```go package main import ( "github.com/stapelberg/godebiancontrol" "os" "log" ) func main() { file, err := os.Open("/var/lib/apt/lists/http.debian.net_debian_dists_testing_main_binary-amd64_Packages") if err != nil { log.Fatal(err) } defer file.Close() p, err := godebiancontrol.Parse(file) if err != nil { log.Fatal(err) } log.Printf("The first package in the list is %s\n", p[0]["Package"]) } ``` Find the documentation at: http://go.pkgdoc.org/github.com/stapelberg/godebiancontrol golang-godebiancontrol-dev/LICENSE0000644000014500017510000000274312267021441016547 0ustar michaelstaffCopyright © 2012-2014, Michael Stapelberg and contributors All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Michael Stapelberg nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY Michael Stapelberg ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Michael Stapelberg BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.