func hwaf_run_cmd_waf_show_flags(cmd *commander.Command, args []string) {
	var err error

	workdir, err := g_ctx.Workarea()
	handle_err(err)

	// FIXME: get actual value from waf, somehow
	pinfo_name := filepath.Join(workdir, "__build__", "c4che", "_cache.py")
	if !path_exists(pinfo_name) {
		err = fmt.Errorf(
			"no such file [%s]. did you run \"hwaf configure\" ?",
			pinfo_name,
		)
		handle_err(err)
	}

	pinfo, err := hwaflib.NewProjectInfo(pinfo_name)
	handle_err(err)

	switch len(args) {
	case 0:
		args = pinfo.Keys()
	}

	err_stack := []error{}
	for _, k := range args {
		val, err2 := pinfo.Get(k)
		if err2 != nil {
			err_stack = append(err_stack, err2)
			err = err2
			continue
		}

		fmt.Printf("%s=%s\n", k, val)
	}

	if len(err_stack) != 0 {
		for _, err := range err_stack {
			fmt.Printf("**error: %v\n", err)
		}
		handle_err(err)
	}
}
func hwaf_run_cmd_waf_show_cmtcfg(cmd *commander.Command, args []string) {
	var err error
	//n := "hwaf-" + cmd.Name()

	workdir, err := g_ctx.Workarea()
	handle_err(err)

	// FIXME: get actual value from waf, somehow
	pinfo_name := filepath.Join(workdir, "__build__", "c4che", "_cache.py")
	if !path_exists(pinfo_name) {
		err = fmt.Errorf(
			"no such file [%s]. did you run \"hwaf configure\" ?",
			pinfo_name,
		)
		handle_err(err)
	}

	pinfo, err := hwaflib.NewProjectInfo(pinfo_name)
	handle_err(err)
	cmtcfg, err := pinfo.Get("CMTCFG")
	handle_err(err)

	fmt.Printf("%s\n", cmtcfg)
}
Exemple #3
0
func hwaf_run_cmd_setup(cmd *commander.Command, args []string) {
	var err error
	n := "hwaf-" + cmd.Name()
	dirname := "."
	switch len(args) {
	case 0:
		dirname = "."
	case 1:
		dirname = args[0]
	default:
		err = fmt.Errorf("%s: you need to give a directory name", n)
		handle_err(err)
	}

	dirname = os.ExpandEnv(dirname)
	dirname = filepath.Clean(dirname)

	verbose := cmd.Flag.Lookup("v").Value.Get().(bool)
	cfg_fname := cmd.Flag.Lookup("cfg").Value.Get().(string)
	pkgdir := cmd.Flag.Lookup("pkgdir").Value.Get().(string)
	variant := cmd.Flag.Lookup("variant").Value.Get().(string)
	tags := cmd.Flag.Lookup("tags").Value.Get().(string)

	projdirs := []string{}
	const pathsep = string(os.PathListSeparator)
	for _, v := range strings.Split(cmd.Flag.Lookup("p").Value.Get().(string), pathsep) {
		if v != "" {
			v = os.ExpandEnv(v)
			v = filepath.Clean(v)
			projdirs = append(projdirs, v)
		}
	}

	if verbose {
		fmt.Printf("%s: setup workarea [%s]...\n", n, dirname)
		fmt.Printf("%s: projects=%v\n", n, projdirs)
		if cfg_fname != "" {
			fmt.Printf("%s: cfg-file=%s\n", n, cfg_fname)
		}
	}

	if cfg_fname != "" && !path_exists(cfg_fname) {
		err = fmt.Errorf("configuration file [%s] does not exist (or is not readable)", cfg_fname)
		handle_err(err)
	}

	for _, projdir := range projdirs {
		if !path_exists(projdir) {
			err = fmt.Errorf("no such directory: [%s]", projdir)
			handle_err(err)
		}

		pinfo := filepath.Join(projdir, "project.info")
		if !path_exists(pinfo) {
			err = fmt.Errorf("no such file: [%s]", pinfo)
			handle_err(err)
		}
	}

	pwd, err := os.Getwd()
	handle_err(err)
	defer os.Chdir(pwd)

	err = os.Chdir(dirname)
	handle_err(err)

	if verbose {
		fmt.Printf("%s: create local config...\n", n)
	}

	var lcfg *gocfg.Config
	lcfg_fname := "local.conf"

	// if the user provided a configuration file use that as a default
	if cfg_fname != "" && path_exists(cfg_fname) {
		lcfg, err = gocfg.ReadDefault(cfg_fname)
		handle_err(err)
	} else {
		if path_exists(lcfg_fname) {
			lcfg, err = gocfg.ReadDefault(lcfg_fname)
			handle_err(err)
		} else {
			lcfg = gocfg.NewDefault()
		}
	}

	section := "hwaf-cfg"
	if !lcfg.HasSection(section) && !lcfg.AddSection(section) {
		err = fmt.Errorf("%s: could not create section [%s] in file [%s]",
			n, section, lcfg_fname)
		handle_err(err)
	}

	// fetch a few informations from the first project.info
	cmtcfg := g_ctx.Cmtcfg()
	//projvers := time.Now().Format("20060102")
	if len(projdirs) > 0 {
		pinfo, err := hwaflib.NewProjectInfo(filepath.Join(projdirs[0], "project.info"))
		handle_err(err)
		cmtcfg, err = pinfo.Get("CMTCFG")
		handle_err(err)
	}

	if variant != "" {
		cmtcfg = variant
	}

	if tags != "" {
		tags_slice := strings.Split(tags, ",")
		tags = strings.Join(tags_slice, " ")
	}

	for k, v := range map[string]string{
		"projects": strings.Join(projdirs, pathsep),
		"pkgdir":   pkgdir,
		"cmtcfg":   cmtcfg,
		"tags":     tags,
	} {
		if lcfg.HasOption(section, k) {
			lcfg.RemoveOption(section, k)
		}
		if !lcfg.AddOption(section, k, v) {
			err := fmt.Errorf("%s: could not add option [%s] to section [%s]",
				n, k, section,
			)
			handle_err(err)
		}
	}

	err = lcfg.WriteFile(lcfg_fname, 0600, "")
	handle_err(err)

	if verbose {
		fmt.Printf("%s: setup workarea [%s]... [ok]\n", n, dirname)
	}
}
func hwaf_run_cmd_waf_bdist_rpm(cmd *commander.Command, args []string) {
	var err error
	n := "hwaf-" + cmd.Name()

	switch len(args) {
	case 0:
	default:
		err = fmt.Errorf("%s: too many arguments (%s)", n, len(args))
		handle_err(err)
	}

	verbose := cmd.Flag.Lookup("v").Value.Get().(bool)

	bdist_name := cmd.Flag.Lookup("name").Value.Get().(string)
	bdist_vers := cmd.Flag.Lookup("version").Value.Get().(string)
	bdist_release := cmd.Flag.Lookup("release").Value.Get().(string)
	bdist_cmtcfg := cmd.Flag.Lookup("cmtcfg").Value.Get().(string)
	bdist_spec := cmd.Flag.Lookup("spec").Value.Get().(string)

	bdist_url := cmd.Flag.Lookup("url").Value.Get().(string)
	if bdist_url == "" {
		bdist_url = "http://cern.ch/mana-fwk"
	}

	type RpmInfo struct {
		Name      string // RPM package name
		Vers      string // RPM package version
		Release   string // RPM package release
		CmtCfg    string // RPM CMTCFG quadruplet
		BuildRoot string // RPM build directory
		Url       string // URL home page
	}

	workdir, err := g_ctx.Workarea()
	if err != nil {
		// not a git repo... assume we are at the root, then...
		workdir, err = os.Getwd()
	}
	handle_err(err)

	if bdist_name == "" {
		bdist_name = workdir
		bdist_name = filepath.Base(bdist_name)
	}
	if bdist_vers == "" {
		bdist_vers = time.Now().Format("20060102")
	}
	if bdist_cmtcfg == "" {
		// FIXME: get actual value from waf, somehow
		pinfo_name := filepath.Join(workdir, "__build__", "c4che", "_cache.py")
		if !path_exists(pinfo_name) {
			err = fmt.Errorf(
				"no such file [%s]. did you run \"hwaf configure\" ?",
				pinfo_name,
			)
			handle_err(err)
		}
		pinfo, err := hwaflib.NewProjectInfo(pinfo_name)
		handle_err(err)
		bdist_cmtcfg, err = pinfo.Get("CMTCFG")
		handle_err(err)
	}
	fname := bdist_name + "-" + bdist_vers + "-" + bdist_cmtcfg
	rpmbldroot, err := ioutil.TempDir("", "hwaf-rpm-buildroot-")
	handle_err(err)
	defer os.RemoveAll(rpmbldroot)
	for _, dir := range []string{
		"RPMS", "SRPMS", "BUILD", "SOURCES", "SPECS", "tmp",
	} {
		err = os.MkdirAll(filepath.Join(rpmbldroot, dir), 0700)
		handle_err(err)
	}

	specfile, err := os.Create(filepath.Join(rpmbldroot, "SPECS", bdist_name+".spec"))
	handle_err(err)

	rpminfos := RpmInfo{
		Name:      bdist_name,
		Vers:      bdist_vers,
		Release:   bdist_release,
		CmtCfg:    bdist_cmtcfg,
		BuildRoot: rpmbldroot,
		Url:       bdist_url,
	}

	// get tarball from 'hwaf bdist'...
	bdist_fname := strings.Replace(fname, ".rpm", "", 1) + ".tar.gz"
	if !path_exists(bdist_fname) {
		err = fmt.Errorf("no such file [%s]. did you run \"hwaf bdist\" ?", bdist_fname)
		handle_err(err)
	}
	bdist_fname, err = filepath.Abs(bdist_fname)
	handle_err(err)
	{
		// first, massage the tar ball to something rpmbuild expects...

		// ok, now we're done.
		dst, err := os.Create(filepath.Join(rpmbldroot, "SOURCES", filepath.Base(bdist_fname)))
		handle_err(err)
		src, err := os.Open(bdist_fname)
		handle_err(err)
		_, err = io.Copy(dst, src)
		handle_err(err)
	}

	if bdist_spec != "" {
		bdist_spec = os.ExpandEnv(bdist_spec)
		bdist_spec, err = filepath.Abs(bdist_spec)
		handle_err(err)

		if !path_exists(bdist_spec) {
			err = fmt.Errorf("no such file [%s]", bdist_spec)
			handle_err(err)
		}
		user_spec, err := os.Open(bdist_spec)
		handle_err(err)
		defer user_spec.Close()

		_, err = io.Copy(specfile, user_spec)
		handle_err(err)
	} else {
		bdist_spec = specfile.Name()

		var spec_tmpl *template.Template
		spec_tmpl, err = template.New("SPEC").Parse(`
%define __spec_install_post %{nil}
%define   debug_package %{nil}
%define __os_install_post %{_dbpath}/brp-compress
%define   cmtcfg {{.CmtCfg}}
%define _topdir {{.BuildRoot}}
%define _tmppath  %{_topdir}/tmp

Summary: hwaf generated RPM for {{.Name}}
Name: {{.Name}}
Version: {{.Vers}}
Release: {{.Release}}
License: Unknown
Group: Development/Tools
SOURCE0 : %{name}-%{version}-%{cmtcfg}.tar.gz
URL: {{.Url}}

BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root

%description
%{summary}

%prep
%setup -q

%build
# Empty section.

%install
rm -rf %{buildroot}
mkdir -p  %{buildroot}

# in builddir
cp -a * %{buildroot}


%clean
rm -rf %{buildroot}


%files
%defattr(-,root,root,-)
/*
`) // */ for emacs...
		handle_err(err)

		err = spec_tmpl.Execute(specfile, rpminfos)
		handle_err(err)
	}

	err = specfile.Sync()
	handle_err(err)
	err = specfile.Close()
	handle_err(err)

	if !strings.HasSuffix(fname, ".rpm") {
		fname = fname + ".rpm"
	}

	if verbose {
		fmt.Printf("%s: building RPM [%s]...\n", n, fname)
	}

	rpmbld, err := exec.LookPath("rpmbuild")
	handle_err(err)

	rpm := exec.Command(rpmbld,
		"-bb",
		filepath.Join("SPECS", rpminfos.Name+".spec"),
	)
	rpm.Dir = rpmbldroot
	if verbose {
		rpm.Stdin = os.Stdin
		rpm.Stdout = os.Stdout
		rpm.Stderr = os.Stderr
	}
	err = rpm.Run()
	handle_err(err)

	dst, err := os.Create(fname)
	handle_err(err)
	defer dst.Close()

	rpmarch := ""
	switch runtime.GOARCH {
	case "amd64":
		rpmarch = "x86_64"
	case "386":
		rpmarch = "i386"
	default:
		err = fmt.Errorf("unhandled GOARCH [%s]", runtime.GOARCH)
		handle_err(err)
	}
	srcname := fmt.Sprintf(
		"%s-%s-%s.%s.rpm",
		rpminfos.Name,
		rpminfos.Vers,
		rpminfos.Release,
		rpmarch)

	src, err := os.Open(filepath.Join(rpmbldroot, "RPMS", rpmarch, srcname))
	handle_err(err)
	defer src.Close()

	_, err = io.Copy(dst, src)
	handle_err(err)
	err = dst.Sync()
	handle_err(err)

	if verbose {
		fmt.Printf("%s: building RPM [%s]...[ok]\n", n, fname)
	}
}
Exemple #5
0
func hwaf_run_cmd_waf_bdist(cmd *commander.Command, args []string) {
	var err error
	n := "hwaf-" + cmd.Name()

	switch len(args) {
	case 0:
	default:
		err = fmt.Errorf("%s: too many arguments (%d)", n, len(args))
		handle_err(err)
	}

	bdist_name := cmd.Flag.Lookup("name").Value.Get().(string)
	bdist_vers := cmd.Flag.Lookup("version").Value.Get().(string)
	bdist_cmtcfg := cmd.Flag.Lookup("cmtcfg").Value.Get().(string)

	workdir, err := g_ctx.Workarea()
	if err != nil {
		// not a git repo... assume we are at the root, then...
		workdir, err = os.Getwd()
	}
	handle_err(err)

	if bdist_name == "" {
		bdist_name = workdir
		bdist_name = filepath.Base(bdist_name)
	}
	if bdist_vers == "" {
		bdist_vers = time.Now().Format("20060102")
	}
	if bdist_cmtcfg == "" {
		// FIXME: get actual value from waf, somehow
		pinfo_name := filepath.Join(workdir, "__build__", "c4che", "_cache.py")
		if !path_exists(pinfo_name) {
			err = fmt.Errorf(
				"no such file [%s]. did you run \"hwaf configure\" ?",
				pinfo_name,
			)
			handle_err(err)
		}
		pinfo, err := hwaflib.NewProjectInfo(pinfo_name)
		handle_err(err)
		bdist_cmtcfg, err = pinfo.Get("CMTCFG")
		handle_err(err)
	}
	fname := bdist_name + "-" + bdist_vers + "-" + bdist_cmtcfg + ".tar.gz"

	//fmt.Printf(">> fname=[%s]\n", fname)
	fname = filepath.Join(workdir, fname)

	// FIXME: get actual value from waf, somehow
	install_area := filepath.Join(workdir, "install-area")
	if !path_exists(install_area) {
		err = fmt.Errorf(
			"no such directory [%s]. did you run \"hwaf install\" ?",
			install_area,
		)
		handle_err(err)
	}
	// the prefix to prepend inside the tar-ball
	prefix := bdist_name + "-" + bdist_vers //+ "-" + bdist_cmtcfg
	// create a temporary install-area with the correct structure:
	//  install-area/<pkgname>-<pkgvers>/...
	bdist_dir := filepath.Join(workdir, ".hwaf-bdist-install-area-"+bdist_cmtcfg)
	_ = os.RemoveAll(bdist_dir)
	err = os.MkdirAll(bdist_dir, 0700)
	handle_err(err)

	// move the install-area...
	err = os.Rename(install_area, filepath.Join(bdist_dir, prefix))
	handle_err(err)
	defer func() {
		err = os.Rename(filepath.Join(bdist_dir, prefix), install_area)
		handle_err(err)
		err = os.RemoveAll(bdist_dir)
		handle_err(err)
	}()

	err = _tar_gz(fname, bdist_dir)
	handle_err(err)
}