Exemple #1
0
// parseRepoConfigFile parses the xyz.repo file and returns a map of reponame/repourl
func (yum *Client) parseRepoConfigFile(fname string) (map[string]string, error) {
	var err error
	repos := make(map[string]string)

	cfg, err := gocfg.ReadDefault(fname)
	if err != nil {
		return nil, err
	}

	for _, section := range cfg.Sections() {
		if !cfg.HasOption(section, "baseurl") {
			continue
		}
		repourl, err := cfg.String(section, "baseurl")
		if err != nil {
			return nil, err
		}
		if strings.HasPrefix(repourl, "/") {
			repourl = "file://" + repourl
		}
		yum.msg.Debugf("adding repo=%q url=%q from file [%s]\n", section, repourl, fname)
		repos[section] = repourl
	}
	return repos, err
}
Exemple #2
0
func (ctx *Context) GlobalCfg() (*gocfg.Config, error) {
	var err error
	if ctx.gcfg != nil {
		return ctx.gcfg, nil
	}

	gcfg := gocfg.NewDefault()
	// aggregate all configurations. last one wins.
	for _, fname := range []string{
		filepath.Join(string(os.PathSeparator), "etc", "hwaf.conf"),
		filepath.Join(ctx.Root, "etc", "hwaf.conf"),
		filepath.Join("${HOME}", ".config", "hwaf", "local.conf"),
	} {
		fname = os.ExpandEnv(fname)
		if !path_exists(fname) {
			continue
		}
		cfg, err := gocfg.ReadDefault(fname)
		if err != nil {
			return nil, err
		}
		for _, section := range cfg.Sections() {
			if !gcfg.HasSection(section) {
				if !gcfg.AddSection(section) {
					err = fmt.Errorf("hwaf: could not add section [%s] to global config", section)
					return nil, err
				}
			}
			options, err := cfg.Options(section)
			if err != nil {
				return nil, err
			}
			for _, option := range options {
				if gcfg.HasOption(section, option) {
					if !gcfg.RemoveOption(section, option) {
						err = fmt.Errorf("hwaf: could not remove option [%s] from section [%s] from global config", option, section)
						return nil, err
					}
				}
				v, err := cfg.RawString(section, option)
				if err != nil {
					return nil, err
				}
				if !gcfg.AddOption(section, option, v) {
					err = fmt.Errorf("hwaf: could not add option [%s] in section [%s] in global config (value=%s)", option, section, v)
					return nil, err
				}
			}
		}
	}

	ctx.gcfg = gcfg
	return ctx.gcfg, err
}
func NewProjectInfo(name string) (*ProjectInfo, error) {
	var err error
	fname := filepath.Clean(name)
	if !path_exists(fname) {
		err = fmt.Errorf("could not find project info [%s]", fname)
		return nil, err
	}
	cfg, err := gocfg.ReadDefault(fname)
	if err != nil {
		return nil, err
	}
	//fmt.Printf("cfg [%s]\nsections: %s\n", fname, cfg.Sections())
	return &ProjectInfo{cfg}, nil
}
Exemple #4
0
func (ctx *Context) LocalCfg() (*gocfg.Config, error) {

	workdir, err := ctx.Workarea()
	if err != nil {
		return nil, err
	}

	cfg_fname := filepath.Join(workdir, "local.conf")
	if !path_exists(cfg_fname) {
		err = fmt.Errorf("could not find local config [%s]", cfg_fname)
		return nil, err
	}

	ctx.lcfg, err = gocfg.ReadDefault(cfg_fname)

	return ctx.lcfg, err
}
Exemple #5
0
// ListRepositories lists all repositories.
func (ctx *Context) ListRepositories() error {
	var err error
	reposdir, err := os.Open(ctx.yumreposd)
	if err != nil {
		return err
	}
	defer reposdir.Close()

	dirs, err := reposdir.Readdir(-1)
	if err != nil {
		return err
	}

	for _, fi := range dirs {
		fname := filepath.Join(ctx.yumreposd, fi.Name())
		cfg, err := config.ReadDefault(fname)
		if err != nil {
			return err
		}
		for _, section := range cfg.Sections() {
			if section == config.DEFAULT_SECTION {
				continue
			}
			name, err := cfg.String(section, "name")
			if err != nil {
				return err
			}
			baseurl, err := cfg.String(section, "baseurl")
			if err != nil {
				return err
			}
			isEnabled, err := cfg.Bool(section, "enabled")
			if err != nil {
				return err
			}
			enabled := "disabled"
			if isEnabled {
				enabled = "enabled"
			}
			fmt.Printf("%s: %q (%s)\n", name, baseurl, enabled)
		}
	}
	return err
}
Exemple #6
0
func init() {
	cfgdir := filepath.Dir(CfgFname)
	if !path_exists(cfgdir) {
		err := os.MkdirAll(cfgdir, 0700)
		if err != nil {
			panic(err.Error())
		}
	}

	fname := CfgFname

	if !path_exists(fname) {
		section := "go-octogit"
		if !Cfg.AddSection(section) {
			err := fmt.Errorf("go-octogit: could not create section [%s] in file [%s]", section, fname)
			panic(err.Error())
		}
		for k, v := range map[string]string{
			"username": "",
			"password": "",
			"token":    "",
		} {
			if !Cfg.AddOption(section, k, v) {
				err := fmt.Errorf("go-octogit: could not add option [%s] to section [%s]", k, section)
				panic(err.Error())
			}
		}
		err := Cfg.WriteFile(CfgFname, 0600, "")
		if err != nil {
			panic(err.Error())
		}
	} else {
		cfg, err := gocfg.ReadDefault(fname)
		if err != nil {
			panic(err.Error())
		}
		Cfg = cfg
	}
}
Exemple #7
0
// RemoveRepository removes the repository named name.
func (ctx *Context) RemoveRepository(name string) error {
	var err error
	fname := filepath.Join(ctx.yumreposd, name+".repo")
	if !path_exists(fname) {
		return fmt.Errorf("lbpkr: no such repo %q", name)
	}

	cfg, err := config.ReadDefault(fname)
	if err != nil {
		return err
	}
	v, err := cfg.String(name, "name")
	if err != nil {
		return err
	}
	if v != name {
		return fmt.Errorf("lbpkr: invalid repo name (got=%q. want=%q)", v, name)
	}
	err = os.Remove(fname)
	if err != nil {
		return err
	}
	return err
}
Exemple #8
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)
	}
}
Exemple #9
0
func (a *asetup_t) run() error {
	var err error

	n := "hwaf-" + a.cmd.Name()

	if len(a.args) == 0 {
		if a.verbose {
			a.ctx.Infof("re-using previously asetup'ed workarea...\n")
		}
		// case where we reuse a previously already asetup'ed workarea
		_, err = a.ctx.LocalCfg()
		if err == nil {
			if a.verbose {
				a.ctx.Infof("re-using previously asetup'ed workarea... [done]\n")
			}
			return nil
		}
		err = fmt.Errorf("%v\n'hwaf asetup' called with no argument in a pristine workarea is NOT valid.", err)
		if err != nil {
			return err
		}
	}

	args := make([]string, 0, len(a.args))
	for _, arg := range a.args {
		subarg := strings.Split(arg, ",")
		for _, sarg := range subarg {
			if sarg != "" {
				args = append(args, sarg)
			}
		}
	}

	dirname, err := os.Getwd()
	if err != nil {
		return err
	}

	dirname, err = filepath.Abs(dirname)
	if err != nil {
		return err
	}

	// make sure 'hwaf init' was run at least once in this directory...
	for _, dir := range []string{
		filepath.Join(dirname, ".hwaf", "bin"),
		filepath.Join(dirname, ".hwaf", "tools"),
	} {
		err = os.RemoveAll(dir)
		if err != nil {
			return err
		}
	}
	{
		sub := exec.Command("hwaf", "init", fmt.Sprintf("-v=%v", a.verbose), dirname)
		sub.Stdin = os.Stdin
		sub.Stdout = os.Stdout
		sub.Stderr = os.Stderr
		err = sub.Run()
		if err != nil {
			return err
		}
	}

	err = a.process(args)
	if err != nil {
		return err
	}

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

	subcmd := exec.Command(
		"hwaf", "setup",
		fmt.Sprintf("-v=%v", a.verbose),
		"-p", a.opts.projdir,
	)
	subcmd.Stdin = os.Stdin
	subcmd.Stdout = os.Stdout
	subcmd.Stderr = os.Stderr
	err = subcmd.Run()
	if err != nil {
		return err
	}

	lcfg_fname := "local.conf"
	if !path_exists(lcfg_fname) {
		err = fmt.Errorf("%s: no such file [%s]", n, lcfg_fname)
		if err != nil {
			return err
		}
	}

	lcfg, err := gocfg.ReadDefault(lcfg_fname)
	if err != nil {
		return err
	}

	err = a.handle_toolchain_section(lcfg, lcfg_fname)
	if err != nil {
		return err
	}

	err = a.handle_env_section(lcfg, lcfg_fname, dirname)
	if err != nil {
		return err
	}

	err = lcfg.WriteFile(lcfg_fname, 0600, "")
	if err != nil {
		return err
	}

	if a.verbose {
		fmt.Printf("%s: asetup workarea [%s]... [ok]\n", n, dirname)
	}
	return err
}
Exemple #10
0
func parse_dist_files(fnames []string) (distname, distvers string, err error) {
	distname = ""
	distvers = ""

	re_release_filename := regexp.MustCompile(`(\w+)(-release|_version)`)
	re_lsb_release_version := regexp.MustCompile(`(.+) release ([\d.]+)[^(]*(?:\((.+)\))?`)
	re_release_version := regexp.MustCompile(`([^0-9]+)(?: release )?([\d.]+)[^(]*(?:\((.+)\))?`)

	if !sort.StringsAreSorted(fnames) {
		sort.Strings(fnames)
	}

	for _, fname := range fnames {
		m := re_release_filename.FindStringSubmatch(fname)
		if m == nil {
			continue
		}
		distname = m[1]
		//fmt.Printf(">>> [%s]\n    [%v] => %q\n", fname, m, distname)
		distfile := fname

		f, err2 := os.Open(distfile)
		defer f.Close()
		if err2 != nil {
			continue
		}

		var line string
		line, err2 = bufio.NewReader(f).ReadString('\n')
		if err2 != nil {
			continue
		}
		//distid := ""

		m = re_lsb_release_version.FindStringSubmatch(line)
		if m == nil {
			// pre-LSB.
			m = re_release_version.FindStringSubmatch(line)
			if m == nil {
				// not supported.
				//fmt.Printf("** [%s] wrong format (%v)\n", distfile, strings.Trim(line, " \r\n"))
				continue
			}
		}
		// LSB format: "distro release x.x (codename)"
		distfullname := m[1]
		distvers = m[2]
		//distid = m[3]
		//fmt.Printf(">>> %v (%q %q)\n", m, distfullname, distvers)
		match := func(pattern string) bool {
			matched, err := regexp.MatchString(pattern, distfullname)
			if err != nil {
				return false
			}
			return matched
		}

		if match(`Red.*?Hat.*?Enterprise.*?Linux.*?`) {
			// rel := strings.Split(distvers, ".")
			// major := rel[0]
			// //minor := rel[1]
			// distver = major
			distname = "rhel"

		} else if match(`Red.*?Hat.*?Linux.*?`) {
			// rel := strings.Split(distvers, ".")
			// major := rel[0]
			// //minor := rel[1]
			// distver = major
			distname = "rh"

		} else if match(`CERN.*?E.*?Linux.*?`) {
			distname = "cel"

		} else if match(`Scientific.*?Linux.*?CERN.*?`) {

			// rel := strings.Split(distvers, ".")
			// major := rel[0]
			// //minor := rel[1]
			// distver = major
			distname = "slc"
		} else if match(`Scientific.*?Linux.*?`) {

			// rel := strings.Split(distvers, ".")
			// major := rel[0]
			// //minor := rel[1]
			// distver = major
			distname = "sl"

		} else if match(`Fedora.*?`) {
			distname = "fedora"

		} else {
			// not a supported distribution ?
			continue
		}
		//fmt.Printf("dist=%q distvers=%v\n", distname, distvers)
		return distname, distvers, nil
	}

	for _, args := range [][]string{
		{"/etc/os-release", "ID", "VERSION_ID"},
		{"/etc/lsb-release", "DISTRIB_ID", "DISTRIB_RELEASE"},
	} {
		fname := args[0]
		id_str := args[1]
		vers_str := args[2]
		//fmt.Printf("--> [%s]\n", fname)
		idx := sort.SearchStrings(fnames, fname)
		if !(idx < len(fnames) && fnames[idx] == fname) {
			continue
		}

		cfg, err := gocfg.ReadDefault(fname)
		if err != nil {
			//fmt.Printf("++ %v\n", err)
			continue
		}

		d_name, err := cfg.RawString("DEFAULT", id_str)
		if err == nil {
			distname = strings.Trim(d_name, `"'`)
		} else {
			//fmt.Printf("++ %v\n", err)
			continue
		}
		d_vers, err := cfg.RawString("DEFAULT", vers_str)
		if err == nil {
			distvers = strings.Trim(d_vers, `"'`)
		} else {
			// distvers is optional...

			//fmt.Printf("++ %v\n", err)
			//continue
		}
		return distname, distvers, nil
	}

	return "", "", fmt.Errorf("platform: unsupported linux distribution")
}