deb

package
v0.0.0-...-e53137b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2020 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Overview

Package deb provides the building blocks for reading and writing deb files.

For a template-based system for generating deb files, see the `debgen` package in this repository

**Warning: v0.3 is currently in a state of flux. Please use `go get github.com/laher/debgo-v0.2/deb` for the time-being. Ta**

Example (BuildBinaryDeb)
package main

import (
	"github.com/debber/debber-v0.3/deb"
	"log"
	"os"
	"path/filepath"
)

func main() {

	pkg := deb.NewControlDefault("testpkg", "me", "me@a", "Dummy package for doing nothing", "testpkg is package ", true)
	exesMap := map[string][]string{
		"amd64": []string{filepath.Join(deb.TempDirDefault, "/a.amd64")},
		"i386":  []string{filepath.Join(deb.TempDirDefault, "/a.i386")},
		"armhf": []string{filepath.Join(deb.TempDirDefault, "/a.armhf")}}
	err := createExes(exesMap)
	if err != nil {
		log.Fatalf("%v", err)
	}
	artifacts, err := deb.NewWriters(pkg)
	if err != nil {
		log.Fatalf("Error building binary: %v", err)
	}
	artifacts[deb.ArchAmd64].MappedFiles = map[string]string{"/usr/bin/a": filepath.Join(deb.TempDirDefault, "/a.amd64")}
	artifacts[deb.ArchI386].MappedFiles = map[string]string{"/usr/bin/a": filepath.Join(deb.TempDirDefault, "/a.i386")}
	artifacts[deb.ArchArmhf].MappedFiles = map[string]string{"/usr/bin/a": filepath.Join(deb.TempDirDefault, "/a.armhf")}
	buildDeb := func(art *deb.Writer) error {
		//generate artifact here ...
		return nil
	}
	for arch, artifact := range artifacts {
		//build binary deb here ...
		err = buildDeb(artifact)
		if err != nil {
			log.Fatalf("Error building for '%s': %v", arch, err)
		}
	}
}

func createExes(exesMap map[string][]string) error {
	for _, exes := range exesMap {
		for _, exe := range exes {
			err := os.MkdirAll(filepath.Dir(exe), 0777)
			if err != nil {
				return err
			}
			fi, err := os.Create(exe)
			if err != nil {
				return err
			}
			_, err = fi.Write([]byte("echo 1"))
			if err != nil {
				return err
			}
			err = fi.Close()
			if err != nil {
				return err
			}
		}
	}
	return nil
}
Output:

Example (BuildDevPackage)
package main

import (
	"github.com/debber/debber-v0.3/deb"
	"log"
)

func main() {

	ctrl := deb.NewControlDefault("testpkg", "me", "me@a", "Dummy package for doing nothing", "testpkg is package ", true)
	buildFunc := func(dpkg *deb.Control) error {
		// Generate files here.
		return nil
	}
	spara := ctrl.GetParasByField(deb.SourceFName, "testpkg")
	bpara := ctrl.GetParasByField(deb.PackageFName, "testpkg-dev")
	nctrl := deb.Control{spara[0], bpara[0]}
	err := buildFunc(&nctrl)
	if err != nil {
		log.Fatalf("%v", err)
	}
}
Output:

Example (BuildSourceDeb)
package main

import (
	"github.com/debber/debber-v0.3/deb"
	"github.com/debber/debber-v0.3/targz"
	"io/ioutil"
	"log"
	"path/filepath"
)

func main() {
	pkg := deb.NewControlDefault("testpkg", "me", "me@a", "Dummy package for doing nothing", "testpkg is package ", true)
	spkg := deb.NewSourcePackage(pkg)
	err := buildOrigArchive(spkg) // it's up to you how to build this
	if err != nil {
		log.Fatalf("Error building source package: %v", err)
	}
	err = buildDebianArchive(spkg) // again - do it yourself
	if err != nil {
		log.Fatalf("Error building source package: %v", err)
	}
	err = buildDscFile(spkg) // yep, same again
	if err != nil {
		log.Fatalf("Error building source package: %v", err)
	}
}

func buildOrigArchive(spkg *deb.SourcePackage) error {
	origFilePath := filepath.Join(deb.DistDirDefault, spkg.OrigFileName)
	tgzw, err := targz.NewWriterFromFile(origFilePath)
	if err != nil {
		return err
	}

	err = tgzw.Close()
	if err != nil {
		return err
	}
	return nil
}

func buildDebianArchive(spkg *deb.SourcePackage) error {
	tgzw, err := targz.NewWriterFromFile(filepath.Join(deb.DistDirDefault, spkg.DebianFileName))
	if err != nil {
		return err
	}

	err = tgzw.Close()
	if err != nil {
		return err
	}
	return nil
}

func buildDscFile(spkg *deb.SourcePackage) error {
	dscData := []byte{}
	dscFilePath := filepath.Join(deb.DistDirDefault, spkg.DscFileName)
	err := ioutil.WriteFile(dscFilePath, dscData, 0644)
	if err != nil {
		return err
	}
	return nil
}
Output:

Index

Examples

Constants

View Source
const (
	//DebianBinaryVersionDefault is the current version as specified in .deb archives (filename debian-binary)
	DebianBinaryVersionDefault = "2.0"
	//DebianCompatDefault - compatibility. Current version
	DebianCompatDefault = "9"
	//FormatDefault - the format as specified in the dsc file (3.0 quilt uses a .debian.gz file rather than a .diff.gz file)
	FormatDefault = "3.0 (quilt)"
	//FormatQuilt - (3.0 quilt uses a .debian.gz file rather than a .diff.gz file)
	FormatQuilt = "3.0 (quilt)"
	//FormatNative - (3.0 native uses a .diff.gz file)
	FormatNative = "3.0 (native)"
	// StatusDefault is unreleased by default. Change this once you're happy with it.
	StatusDefault = "unreleased"

	//SectionDefault - devel seems to be the most common value
	SectionDefault = "devel"
	//PriorityDefault - 'extra' means 'low priority'
	PriorityDefault = "extra"
	//DependsDefault - No dependencies by default
	DependsDefault = ""
	//BuildDependsDefault - debhelper recommended for any package
	BuildDependsDefault = "debhelper (>= 9.1.0)"
	//BuildDependsGoDefault - golang required
	BuildDependsGoDefault = "debhelper (>= 9.1.0), golang-go"

	//StandardsVersionDefault - standards version is specified in the control file
	StandardsVersionDefault = "3.9.4"

	//ArchitectureDefault -'any' is the default architecture for source packages - not for binary debs
	ArchitectureDefault = "any"

	//TemplateDirDefault - the place where control file templates are kept
	TemplateDirDefault = "templates"
	//ResourcesDirDefault - the place where portable files are stored.
	ResourcesDirDefault = "resources"
	//WorkingDirDefault - the directory for build process.
	WorkingDirDefault = "."

	//ExeDirDefault - the default directory for exes within the data archive
	ExeDirDefault                   = "/usr/bin"
	BinaryDataArchiveNameDefault    = "data.tar.gz"
	BinaryControlArchiveNameDefault = "control.tar.gz"

	DebianDir = "debian"
)
View Source
const (
	PackageFName     = "Package"
	VersionFName     = "Version"
	DescriptionFName = "Description"
	MaintainerFName  = "Maintainer"

	ArchitectureFName = "Architecture" // Supported values: "all", "x386", "amd64", "armhf". TODO: armel

	DependsFName    = "Depends" // Depends
	RecommendsFName = "Recommends"
	SuggestsFName   = "Suggests"
	EnhancesFName   = "Enhances"
	PreDependsFName = "PreDepends"
	ConflictsFName  = "Conflicts"
	BreaksFName     = "Breaks"
	ProvidesFName   = "Provides"
	ReplacesFName   = "Replaces"

	BuildDependsFName      = "Build-Depends" // BuildDepends is only required for "sourcedebs".
	BuildDependsIndepFName = "Build-Depends-Indep"
	ConflictsIndepFName    = "Conflicts-Indep"
	BuiltUsingFName        = "Built-Using"

	PriorityFName         = "Priority"
	StandardsVersionFName = "Standards-Version"
	SectionFName          = "Section"
	FormatFName           = "Format"
	StatusFName           = "Status"
	OtherFName            = "Other"
	SourceFName           = "Source"
)

Variables

View Source
var (
	//TempDirDefault is the default directory for intermediate files
	TempDirDefault = filepath.Join(outDirDefault, "tmp")

	//DistDirDefault is the default directory for built artifacts
	DistDirDefault = outDirDefault

	MaintainerScripts = []string{"postinst", "postrm", "prerm", "preinst"}

	//SourceFields are the fields applicable to Source packages
	//
	// see http://manpages.ubuntu.com/manpages/precise/man5/deb-src-control.5.html://manpages.ubuntu.com/manpages/precise/man5/deb-src-control.5.html
	SourceFields = []string{
		SourceFName,
		MaintainerFName,
		"Uploaders",
		StandardsVersionFName,
		"DM-Upload-Allowed",
		"Homepage",
		"Bugs",
		"Vcs-Arch",
		"Vcs-Bzr",
		"Vcs-Cvs",
		"Vcs-Darcs",
		"Vcs-Git",
		"Vcs-Hg",
		"Vcs-Mtn",
		"Vcs-Svn",
		"Vcs-Browser",
		"Origin",
		SectionFName,
		PriorityFName,
		BuildDependsFName,
		"Build-Depends-Indep",
		"Build-Conflicts",
		"Build-Conflicts-Indep",
	}

	//BinaryFields are the fields applicable to binary packages
	//
	// see http://manpages.ubuntu.com/manpages/precise/man5/deb-src-control.5.html://manpages.ubuntu.com/manpages/precise/man5/deb-src-control.5.html
	BinaryFields = []string{
		PackageFName,
		ArchitectureFName,
		"Package-Type",
		"Subarchitecture",
		"Kernel-Version",
		"Installer-Menu-Item",
		"Essential",
		"Multi-Arch",
		"Tag",
		DescriptionFName,
		DependsFName,
		PreDependsFName,
		RecommendsFName,
		SuggestsFName,
		"Breaks",
		"Enhances",
		"Replaces",
		"Conflicts",
		"Provides",
		"Built-Using",
		PriorityFName,
		SectionFName,
		"Homepage",
	}
)
View Source
var (
	Licenses = []string{
		"public-domain",
		"Apache",
		"Artistic",
		"BSD-2-clause",
		"BSD-3-clause",
		"BSD-4-clause",
		"ISC",
		"CC-BY",
		"CC-BY-SA",
		"CC-BY-ND",
		"CC-BY-NC",
		"CC-BY-NC-SA",
		"CC-BY-NC-ND",
		"CC0",
		"CDDL",
		"CPL",
		"EFL",
		"Expat",
		"GPL",
		"LGPL",
		"GFDL",
		"GFDL-NIV",
		"LPPL",
		"MPL",
		"Perl",
		"Python",
		"QPL",
		"W3C",
		"Zlib",
		"Zope",
	}
)

Keyword Meaning public-domain No license required for any purpose; the work is not subject to copyright in any jurisdiction. Apache Apache license 1.0, 2.0. Artistic Artistic license 1.0, 2.0. BSD-2-clause Berkeley software distribution license, 2-clause version. BSD-3-clause Berkeley software distribution license, 3-clause version. BSD-4-clause Berkeley software distribution license, 4-clause version. ISC Internet Software Consortium, sometimes also known as the OpenBSD License. CC-BY Creative Commons Attribution license 1.0, 2.0, 2.5, 3.0. CC-BY-SA Creative Commons Attribution Share Alike license 1.0, 2.0, 2.5, 3.0. CC-BY-ND Creative Commons Attribution No Derivatives license 1.0, 2.0, 2.5, 3.0. CC-BY-NC Creative Commons Attribution Non-Commercial license 1.0, 2.0, 2.5, 3.0. CC-BY-NC-SA Creative Commons Attribution Non-Commercial Share Alike license 1.0, 2.0, 2.5, 3.0. CC-BY-NC-ND Creative Commons Attribution Non-Commercial No Derivatives license 1.0, 2.0, 2.5, 3.0. CC0 Creative Commons Zero 1.0 Universal. Omit "Universal" from the license version when forming the short name. CDDL Common Development and Distribution License 1.0. CPL IBM Common Public License. EFL The Eiffel Forum License 1.0, 2.0. Expat The Expat license. GPL GNU General Public License 1.0, 2.0, 3.0. LGPL GNU Lesser General Public License 2.1, 3.0, or GNU Library General Public License 2.0. GFDL GNU Free Documentation License 1.0, 1.1, 1.2, or 1.3. Use GFDL-NIV instead if there are no Front-Cover or Back-Cover Texts or Invariant Sections. GFDL-NIV GNU Free Documentation License, with no Front-Cover or Back-Cover Texts or Invariant Sections. Use the same version numbers as GFDL. LPPL LaTeX Project Public License 1.0, 1.1, 1.2, 1.3c. MPL Mozilla Public License 1.1. Perl Perl license (use "GPL-1+ or Artistic-1" instead). Python Python license 2.0. QPL Q Public License 1.0. W3C W3C Software License For more information, consult the W3C Intellectual Rights FAQ. Zlib zlib/libpng license. Zope Zope Public License 1.0, 1.1, 2.0, 2.1.

Functions

func NewWriters

func NewWriters(ctrl *Control) (map[Architecture]*Writer, error)

NewWriters gets and returns an artifact for each architecture. Returns an error if the package's architecture is un-parseable

func NormaliseFieldKey

func NormaliseFieldKey(input string) string

func ParseVersion

func ParseVersion(packageVersion string) (string, string, string, error)

ParseVersion parses a debian version string into its [up to] 3 parts. Briefly, the format is: [epoch:]upstream_version[-debian_revision]

See http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version

func SetDefaults

func SetDefaults(ctrl *Control)

SetDefaults sets fields which can be initialised appropriately note that Source and Binary packages are detected by the presence of a Source or Package field, respectively.

func ValidateArchitecture

func ValidateArchitecture(archString string) error

ValidateArchitecture checks the architecture string by parsing it. Returns an error on failure. Only supported architectures are considered valid. (e.g. armel is not supported, and non-linux OSes are also not supported yet)

See https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Architecture

func ValidateControl

func ValidateControl(ctrl *Control) error

func ValidateName

func ValidateName(packageName string) error

ValidateName validates a package name for both 'Source:' and 'Package:' names

See http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Source

func ValidatePackage

func ValidatePackage(pkg *Package) error

ValidatePackage checks required fields and certain restricted values.

This can be considered a work-in-progress.

func ValidateVersion

func ValidateVersion(packageVersion string) error

ValidateVersion checks a version string against the policy manual definition.

See ParseVersion

Example
package main

import (
	"github.com/debber/debber-v0.3/deb"
	"log"
)

func main() {
	v := "1.0.1-git123"
	err := deb.ValidateVersion(v)
	if err != nil {
		log.Fatalf("Version validation broken for %v", v)
	}

}
Output:

Types

type Architecture

type Architecture string

Architecture - processor architecture (ARM/x86/AMD64) - as named by Debian. At this stage: i386, armhf, amd64 and 'all'. Note that 'any' is not valid for a binary package, and resolves to [i386, armhf, amd64] TODO: armel (Note that armhf = ARMv7 and armel = ARMv5. In Go terms, this is is is governed by the environment variable GOARM, and 7 is the default)

const (
	//ArchI386 represents x86 machines
	ArchI386 Architecture = "i386"
	//ArchArmhf represents ARMv7 (TODO: armel)
	ArchArmhf Architecture = "armhf"
	//ArchAmd64 represents 64-bit machines.
	ArchAmd64 Architecture = "amd64"
	//ArchAll is for binary packages.
	ArchAll Architecture = "all"
)

func ResolveArches

func ResolveArches(arches string) ([]Architecture, error)

ResolveArches currently parses 'Binary' arches only

type Checksum

type Checksum struct {
	Checksum string
	Size     int64
	File     string
}

Checksum stores a checksum for a file

type Checksums

type Checksums struct {
	ChecksumsMd5    []Checksum
	ChecksumsSha1   []Checksum
	ChecksumsSha256 []Checksum
}

Checksums stores the 3 required checksums for a list of files

func (*Checksums) Add

func (cs *Checksums) Add(filepath, basename string) error

Add adds checksum entries for each checksum algorithm

type Control

type Control []*Package

Control is the base unit for this library. A *Control contains one or more paragraphs

func Copy

func Copy(ctrl *Control) *Control

Copy all fields

func NewControlDefault

func NewControlDefault(name, maintainerName, maintainerEmail, shortDescription, longDescription string, addDevPackage bool) *Control

NewControlDefault is a factory for a Control. Name, Version, Maintainer and Description are mandatory.

func NewControlEmpty

func NewControlEmpty() *Control

NewControlEmpty returns a package with one empty paragraph and an empty map of ExtraData

func ParseDebMetadata

func ParseDebMetadata(rdr io.Reader) (*Control, error)

ParseDebMetadata reads an artifact's contents.

func (*Control) BinaryParas

func (ctrl *Control) BinaryParas() []*Package

BinaryParas returns all paragraphs containing a 'Package' field

func (*Control) Get

func (ctrl *Control) Get(key string) string

Get finds the first occurence of the specified value, checking each paragraph in turn

func (*Control) GetArches

func (ctrl *Control) GetArches() ([]Architecture, error)

GetArches resolves architecture(s) and return as a slice

func (*Control) GetParasByField

func (ctrl *Control) GetParasByField(key string, val string) []*Package

GetParasByField finds paragraphs containing a given field

func (*Control) SourceParas

func (ctrl *Control) SourceParas() []*Package

SourceParas returns all paragraphs containing a 'Source' field

type ControlFileReader

type ControlFileReader struct {
	Reader io.Reader
}

ControlFileReader reads a control file.

func NewControlFileReader

func NewControlFileReader(rdr io.Reader) *ControlFileReader

NewControlFileReader is a factory for reading Dsc files.

func (*ControlFileReader) Parse

func (dscr *ControlFileReader) Parse() (*Control, error)

Parse parses a stream into a package definition.

type Package

type Package struct {
	// contains filtered or unexported fields
}

Package is the base unit for this library. A *Package contains metadata.

func CopyPara

func CopyPara(pkg *Package) *Package

func NewPackage

func NewPackage() *Package

func (*Package) Get

func (pkg *Package) Get(key string) string

func (*Package) GetExtended

func (pkg *Package) GetExtended(key string) (string, string, bool)

GetExtended gets a control field by name, returning key, value & 'exists'

func (*Package) Set

func (pkg *Package) Set(key, value string)

Set sets a control field by name

type Reader

type Reader struct {
	Reader           io.Reader
	ArReader         *ar.Reader
	HasDebianVersion bool
}

Reader is a wrapper around an io.Reader and an ar.Reader.

func NewReader

func NewReader(rdr io.Reader) (*Reader, error)

NewReader is a factory for deb.Reader

func (*Reader) NextTar

func (drdr *Reader) NextTar() (string, *tar.Reader, error)

NextTar gets next tar header (for supported tar archive types - initially just tar.gz)

type SourcePackage

type SourcePackage struct {
	Package        *Control
	DscFileName    string
	OrigFileName   string
	DebianFileName string
	DebianFiles    []string
}

SourcePackage is a cross-platform package with a .dsc file.

func NewSourcePackage

func NewSourcePackage(pkg *Control) *SourcePackage

NewSourcePackage is a factory for SourcePackage. Sets up default paths.. Initialises default filenames, using .tar.gz as the archive type

type Writer

type Writer struct {
	Control             *Control
	Architecture        Architecture
	Filename            string
	DebianBinaryVersion string
	ControlArchive      string
	DataArchive         string
	MappedFiles         map[string]string
}

Writer is an architecture-specific deb

func NewWriter

func NewWriter(ctrl *Control, architecture Architecture) *Writer

NewWriter returns a Writer with defaults already set.

func (*Writer) Build

func (bdeb *Writer) Build(tempDir, destDir string) error

func (*Writer) SetDefaults

func (bdeb *Writer) SetDefaults()

SetDefaults sets some default properties

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL