예제 #1
1
func Example_buildBinaryDeb() {

	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)
		}
	}
}
예제 #2
0
func Example_genBinaryPackage() {

	ctrl := deb.NewControlDefault("testpkg", "me", "<*****@*****.**>", "Dummy package for doing nothing", "testpkg is a dummy package", true)
	for _, pkg := range ctrl.BinaryParas() {
		pkg.Set(deb.VersionFName, "0.0.2")
	}
	build := debgen.NewBuildParams()
	build.Init()
	build.IsRmtemp = false
	artifacts, err := deb.NewWriters(ctrl)
	if err != nil {
		log.Fatalf("Error building binary: %v", err)
	}
	artifacts[deb.ArchAmd64].MappedFiles = map[string]string{"/usr/bin/a": "_out/a.amd64"}
	artifacts[deb.ArchI386].MappedFiles = map[string]string{"/usr/bin/a": "_out/a.i386"}
	artifacts[deb.ArchArmhf].MappedFiles = map[string]string{"/usr/bin/a": "_out/a.armhf"}

	prep() //prepare files for packaging using some other means.

	for arch, artifact := range artifacts {
		log.Printf("generating artifact '%s'/%v", arch, artifact)
		dgen := debgen.NewDebGenerator(artifact, build)
		err = dgen.GenerateAllDefault()
		if err != nil {
			log.Fatalf("Error building for '%s': %v", arch, err)
		}
	}

	// Output:
	//
}
예제 #3
0
func Example_genDevPackage() {
	ctrl := deb.NewControlDefault("testpkg", "me", "me@a", "Dummy package for doing nothing", "testpkg is package ", true)

	spara := ctrl.GetParasByField(deb.SourceFName, "testpkg")
	bpara := ctrl.GetParasByField(deb.PackageFName, "testpkg-dev")
	nctrl := &deb.Control{spara[0], bpara[0]}

	build := debgen.NewBuildParams()
	build.IsRmtemp = false
	build.Init()
	var err error
	mappedFiles, err := debgen.GlobForGoSources(".", []string{build.TmpDir, build.DestDir})
	if err != nil {
		log.Fatalf("Error building -dev: %v", err)
	}

	artifacts, err := deb.NewWriters(nctrl)
	if err != nil {
		log.Fatalf("Error building -dev: %v", err)
	}
	for _, artifact := range artifacts {
		dgen := debgen.NewDebGenerator(artifact, build)
		dgen.DataFiles = mappedFiles
		err = dgen.GenerateAllDefault()
		if err != nil {
			log.Fatalf("Error building -dev: %v", err)
		}
	}

	// Output:
	//
}
예제 #4
0
func Test_buildBinaryDeb(t *testing.T) {

	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 {
		t.Fatalf("%v", err)
	}
	artifacts, err := deb.NewWriters(pkg)
	if err != nil {
		t.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 {
		archiveFilename := filepath.Join(deb.TempDirDefault, art.ControlArchive)
		controlTgzw, err := targz.NewWriterFromFile(archiveFilename)
		if err != nil {
			return err
		}
		controlData := []byte("Package: testpkg\n")
		//TODO add more files here ...
		header := &tar.Header{Name: "control", Size: int64(len(controlData)), Mode: int64(644), ModTime: time.Now()}
		err = controlTgzw.WriteHeader(header)
		if err != nil {
			return err
		}
		_, err = controlTgzw.Write(controlData)
		if err != nil {
			return err
		}
		err = controlTgzw.Close()
		if err != nil {
			return err
		}

		archiveFilename = filepath.Join(deb.TempDirDefault, art.DataArchive)
		dataTgzw, err := targz.NewWriterFromFile(archiveFilename)
		if err != nil {
			return err
		}
		//TODO add files here ...
		err = dataTgzw.Close()
		if err != nil {
			return err
		}
		err = os.MkdirAll(deb.DistDirDefault, 0777)
		if err != nil {
			return err
		}
		//generate artifact here ...
		err = art.Build(deb.TempDirDefault, deb.DistDirDefault)
		if err != nil {
			return err
		}
		return nil
	}
	for arch, artifact := range artifacts {
		//build binary deb here ...
		err = buildDeb(artifact)
		if err != nil {
			t.Fatalf("Error building for '%s': %v", arch, err)
		}
	}
}
예제 #5
0
//Default flow for generating a deb
func PrepareBasicDebGen(ctrl *deb.Control, build *BuildParams) ([]*DebGenerator, error) {
	if build.Version == "" {
		return nil, fmt.Errorf("Error: 'version' is a required field")
	}
	// Initiate build
	err := build.Init()
	if err != nil {
		return nil, fmt.Errorf("%v", err)
	}

	dgens := []*DebGenerator{}
	//Build ... assume just one source package
	sourcePara := ctrl.SourceParas()[0]
	for _, binPara := range ctrl.BinaryParas() {
		//TODO check -dev package here ...
		debpara := deb.CopyPara(binPara)
		debpara.Set(deb.VersionFName, build.Version)
		debpara.Set(deb.SectionFName, sourcePara.Get(deb.SectionFName))
		debpara.Set(deb.MaintainerFName, sourcePara.Get(deb.MaintainerFName))
		debpara.Set(deb.PriorityFName, sourcePara.Get(deb.PriorityFName))
		//log.Printf("debPara: %+v", debpara)

		dataFiles := map[string]string{}
		//source package. TODO: find a better way to identify a sources-only .deb package.
		if strings.HasSuffix(binPara.Get(deb.PackageFName), "-dev") {
			/*
				if build.sourcesGlob != "" {
					sources, err = filepath.Glob(build.sourcesGlob)
					if err != nil {
						return fmt.Errorf("%v", err)
					}
					log.Printf("sources matching glob: %+v", sources)
				}*/
			/*
				sourcesDestinationDir := sourcePara.Get(deb.SourceFName) + "_" + sourcePara.Get(deb.VersionFName)
				for _, sourceFinder := range build.SourceFinders {
					sourcesRelativeTo := sourceFinder.BaseDir
					var sourceDir string
					if sourceFinder.IncludeDir != "" {
						sourceDir = sourceFinder.IncludeDir
					} else {
						sourceDir = sourceFinder.BaseDir
					}
					sources, err := GlobForSources(sourcesRelativeTo, sourceDir, sourceFinder.Glob, sourceFinder.Target + sourcesDestinationDir, []string{build.TmpDir, build.DestDir})
					if err != nil {
						return nil, fmt.Errorf("Error resolving sources: %v", err)
					}
					for k, v := range sources {
						dataFiles[k] = v
					}
				}
			*/
		} else {
			// no sources
		}
		//Add changelog
		cl := filepath.Join(build.DebianDir, "changelog")
		cf, err := os.Open(cl)
		if os.IsNotExist(err) {
		} else if err != nil {
			return nil, err
		} else {
			defer cf.Close()
			changelogGz := filepath.Join(build.TmpDir, "changelog.gz")
			gzf, err := os.OpenFile(changelogGz, os.O_CREATE|os.O_WRONLY, 0644)
			defer gzf.Close()
			if err != nil {
				return nil, fmt.Errorf("Error creating temp gzip file for changelog: %v", err)
			}
			gzw, err := gzip.NewWriterLevel(gzf, gzip.BestCompression)
			if err != nil {
				return nil, fmt.Errorf("Error gzipping changelog: %v", err)
			}
			_, err = io.Copy(gzw, cf)
			if err != nil {
				return nil, fmt.Errorf("Error gzipping changelog: %v", err)
			}
			err = gzw.Close()
			if err != nil {
				return nil, fmt.Errorf("Error gzipping changelog: %v", err)
			}
			err = cf.Close()
			if err != nil {
				return nil, fmt.Errorf("Error gzipping changelog: %v", err)
			}
			err = gzf.Close()
			if err != nil {
				return nil, fmt.Errorf("Error gzipping changelog: %v", err)
			}
			//exists. Add it.
			dataFiles["/usr/share/doc/"+binPara.Get(deb.PackageFName)+"/changelog.gz"] = changelogGz
		}
		//Add copyright
		cr := filepath.Join(build.DebianDir, "copyright")
		_, err = os.Stat(cr)
		if os.IsNotExist(err) {
		} else if err != nil {
			return nil, err
		} else {
			//exists. Add it.
			dataFiles["/usr/share/doc/"+binPara.Get(deb.PackageFName)+"/copyright"] = cr
		}
		//log.Printf("Resources: %v", build.Resources)
		// TODO determine this platform
		//err = bpkg.Build(build, debgen.GenBinaryArtifact)
		artifacts, err := deb.NewWriters(&deb.Control{debpara})
		if err != nil {
			return nil, fmt.Errorf("%v", err)
		}
		for arch, artifact := range artifacts {
			required := false
			for _, requiredArch := range build.Arches {
				if arch == requiredArch {
					//yes
					required = true
				}
			}
			if !required {
				continue
			}
			dgen := NewDebGenerator(artifact, build)
			dgens = append(dgens, dgen)
			dgen.DataFiles = map[string]string{}
			for k, v := range dataFiles {
				dgen.DataFiles[k] = v
			}
			/*
				for _, source := range sources {
					//NOTE: this should not use filepath.Join because it should always use forward-slash
					dgen.DataFiles[build.sourcesDest+"/"+source] = source
				}
			*/
			//add changelog

			// add resources ...
			f := func(path string, info os.FileInfo, err2 error) error {
				if info != nil && !info.IsDir() {
					rel, err := filepath.Rel(build.ResourcesDir, path)
					if err == nil {
						dgen.DataFiles[rel] = path
					}
					return err
				}
				return nil
			}
			err = filepath.Walk(build.ResourcesDir, f)

			if err != nil {
				return nil, fmt.Errorf("%v", err)
			}
			/*
				binFinders, ok := build.BinFinders[arch]
				if ok {
					for _, binFinder := range binFinders {
						if binFinder.Glob != "" {
							glob := filepath.Join(binFinder.BaseDir, binFinder.Glob)
							bins, err := filepath.Glob(glob)
							if err != nil {
								return nil, fmt.Errorf("%v", err)
							}
							log.Printf("glob: %s, bins: %v", glob, bins)
							//log.Printf("Binaries matching glob for '%s': %+v", arch, bins)
							for _, bin := range bins {
								target := binFinder.Target + "/" + bin
								dgen.DataFiles[target] = bin
							}
						}
					}
				}
			*/
			//			log.Printf("All data files: %v", dgen.DataFiles)
		}
	}
	return dgens, nil
}