Exemplo n.º 1
0
func TestBackwardsCompatibility(t *testing.T) {
	profiles.Clear()

	getProfiles := func() []*profiles.Profile {
		db := []*profiles.Profile{}
		names := profiles.Profiles()
		sort.Strings(names)
		for _, name := range names {
			db = append(db, profiles.LookupProfile(name))
		}
		return db
	}

	ctx := tool.NewDefaultContext()
	if err := profiles.Read(ctx, "./testdata/legacy.xml"); err != nil {
		t.Fatal(err)
	}

	if got, want := profiles.SchemaVersion(), profiles.Original; got != want {
		t.Errorf("got %v, want %v", got, want)
	}

	oprofiles := getProfiles()
	if got, want := len(oprofiles), 5; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	filename := tmpFile()
	defer os.RemoveAll(filepath.Dir(filename))

	var t1 profiles.Target
	t1.Set("tag=cpu,os")
	profiles.AddProfileTarget("__first", t1)

	if err := profiles.Write(ctx, filename); err != nil {
		t.Fatal(err)
	}

	if err := profiles.Read(ctx, filename); err != nil {
		t.Fatal(err)
	}

	if got, want := profiles.SchemaVersion(), profiles.V3; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	nprofiles := getProfiles()
	if got, want := len(nprofiles), 6; got != want {
		t.Errorf("got %v, want %v", got, want)
	}

	if got, want := nprofiles[0].Name, "__first"; got != want {
		t.Errorf("got %v, want %v", got, want)
	}

	for i, v := range nprofiles[1:] {
		if got, want := v.Name, oprofiles[i].Name; got != want {
			t.Errorf("got %v, want %v", got, want)
		}
	}
}
Exemplo n.º 2
0
func targetAtDefaultVersion(mgr profiles.Manager, target profiles.Target) (profiles.Target, error) {
	def := target
	version, err := mgr.VersionInfo().Select(target.Version())
	if err != nil {
		return profiles.Target{}, err
	}
	def.SetVersion(version)
	return def, nil
}
Exemplo n.º 3
0
func ExampleProfileTarget() {
	var target profiles.Target
	flags := flag.NewFlagSet("test", flag.ContinueOnError)
	profiles.RegisterTargetAndEnvFlags(flags, &target)
	flags.Parse([]string{"--target=arm-linux", "--env=A=B,C=D", "--env=E=F"})
	fmt.Println(target.String())
	fmt.Println(target.DebugString())
	// Output:
	// arm-linux@
	// arm-linux@ dir: --env=A=B,C=D,E=F envvars:[]
}
Exemplo n.º 4
0
func TestReadingV0(t *testing.T) {
	pdb := profiles.NewDB()
	jirix, cleanup := jiritest.NewX(t)
	defer cleanup()

	if err := pdb.Read(jirix, "./testdata/legacy.xml"); err != nil {
		t.Fatal(err)
	}

	if got, want := pdb.SchemaVersion(), profiles.Original; got != want {
		t.Errorf("got %v, want %v", got, want)
	}

	oprofiles := pdb.Profiles()
	if got, want := len(oprofiles), 5; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	filename := tmpFile()
	defer os.RemoveAll(filepath.Dir(filename))

	var t1 profiles.Target
	t1.Set("cpu-os@1")
	pdb.InstallProfile("", "__first", "")
	if err := pdb.AddProfileTarget("", "__first", t1); err != nil {
		t.Fatal(err)
	}

	if err := pdb.Write(jirix, "", filename); err != nil {
		t.Fatal(err)
	}

	if err := pdb.Read(jirix, filename); err != nil {
		t.Fatal(err)
	}

	if got, want := pdb.SchemaVersion(), profiles.V5; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	nprofiles := pdb.Profiles()
	if got, want := len(nprofiles), 6; got != want {
		t.Fatalf("got %v, want %v", got, want)
	}

	if got, want := nprofiles[0].Name(), "__first"; got != want {
		t.Errorf("got %v, want %v", got, want)
	}

	for i, v := range nprofiles[1:] {
		if got, want := v.Name(), oprofiles[i].Name(); got != want {
			t.Errorf("got %v, want %v", got, want)
		}
	}
}
Exemplo n.º 5
0
func applyCommand(names []string, env *cmdline.Env, ctx *tool.Context, target profiles.Target, fn func(profiles.Manager, *tool.Context, profiles.Target) error) error {
	for _, n := range names {
		mgr := profiles.LookupManager(n)
		version, err := mgr.VersionInfo().Select(target.Version())
		if err != nil {
			return err
		}
		target.SetVersion(version)
		mgr.SetRoot(rootDir)
		if err := fn(mgr, ctx, target); err != nil {
			return err
		}
	}
	return nil
}
Exemplo n.º 6
0
func (eg *exampleManager) Uninstall(jirix *jiri.X, pdb *profiles.DB, root jiri.RelPath, target profiles.Target) error {
	version, err := eg.versionInfo.Select(target.Version())
	if err != nil {
		return err
	}
	dir := eg.filename(root, target).Abs(jirix)
	if err := jirix.NewSeq().WriteFile(filepath.Join(dir, "version"), []byte(version), profilesutil.DefaultFilePerm).
		WriteFile(filepath.Join(dir, version), []byte(version), profilesutil.DefaultFilePerm).
		Remove(filepath.Join(dir, version)).
		Done(); err != nil {
		return err
	}
	if pdb.RemoveProfileTarget(eg.installer, eg.name, target) {
		eg.profile = nil
	}
	return nil
}
Exemplo n.º 7
0
func (eg *exampleManager) Install(jirix *jiri.X, pdb *profiles.DB, root jiri.RelPath, target profiles.Target) error {
	version, err := eg.versionInfo.Select(target.Version())
	if err != nil {
		return err
	}
	target.SetVersion(version)
	dir := eg.filename(root, target).Abs(jirix)
	if err := jirix.NewSeq().
		MkdirAll(dir, profilesutil.DefaultDirPerm).
		WriteFile(filepath.Join(dir, "version"), []byte(version), profilesutil.DefaultFilePerm).
		WriteFile(filepath.Join(dir, version), []byte(version), profilesutil.DefaultFilePerm).
		Done(); err != nil {
		return err
	}
	eg.profile = pdb.InstallProfile(eg.installer, eg.name, string(root))
	target.InstallationDir = string(root)
	return pdb.AddProfileTarget(eg.installer, eg.name, target)
}
Exemplo n.º 8
0
func fmtInfo(jirix *jiri.X, infoFmt string, rd *profilesreader.Reader, profile *profiles.Profile, target *profiles.Target) (string, error) {
	// Populate an instance listInfo
	info := &listInfo{}
	name := profile.Name()
	installer, _ := profiles.SplitProfileName(name)
	info.SchemaVersion = rd.SchemaVersion()
	info.DBPath = rd.Path()
	if target != nil {
		info.Target.InstallationDir = jiri.NewRelPath(target.InstallationDir).Abs(jirix)
		info.Target.CommandLineEnv = target.CommandLineEnv().Vars
		info.Target.Env = target.Env.Vars
		clenv := ""
		if len(info.Target.CommandLineEnv) > 0 {
			clenv = fmt.Sprintf(" --env=\"%s\" ", strings.Join(info.Target.CommandLineEnv, ","))
		}
		if installer != "" {
			info.Target.Command = fmt.Sprintf("jiri profile install --target=%s %s%s", target, clenv, name)
		} else {
			// TODO(cnicolaou): remove this when the transition is complete.
			info.Target.Command = fmt.Sprintf("jiri v23-profile install --target=%s %s%s", target, clenv, name)
		}
	}
	if profile != nil {
		rp := jiri.NewRelPath(profile.Root())
		info.Profile.Root = rp.Abs(jirix)
		info.Profile.Name = name
		info.Profile.Installer = installer
		info.Profile.DBPath = info.DBPath
		if installer != "" {
			info.Profile.DBPath = filepath.Join(info.DBPath, installer)
		}
	}

	// Use a template to print out any field in our instance of listInfo.
	tmpl, err := template.New("list").Parse("{{ ." + infoFmt + "}}")
	if err != nil {
		return "", err
	}
	out := &bytes.Buffer{}
	if err = tmpl.Execute(out, info); err != nil {
		return "", fmt.Errorf("please specify a supported field:\n%s", infoUsage())
	}
	return out.String(), nil
}
Exemplo n.º 9
0
func RunMojoShell(mojoUrl, configFile string, configAliases map[string]string, argsFor map[string][]string, target profiles.Target) *exec.Cmd {
	// ensure the profiles are loaded
	jirix, err := jiri.NewX(cmdline.EnvFromOS())
	if err != nil {
		panic(err)
	}
	rd, err := profilesreader.NewReader(jirix, profilesreader.UseProfiles, filepath.Join(jirix.Root, ".jiri_root", "profile_db"))
	if err != nil {
		panic(err)
	}

	envslice := rd.EnvFromProfile(mojoProfileName(), target)
	env := envvar.VarsFromSlice(envslice)
	jiri.ExpandEnv(jirix, env)
	var mojoDevtools, mojoShell, mojoServices string
	for _, e := range env.ToSlice() {
		parts := strings.SplitN(e, "=", 2)
		switch parts[0] {
		case "MOJO_DEVTOOLS":
			mojoDevtools = parts[1]
		case "MOJO_SHELL":
			mojoShell = parts[1]
		case "MOJO_SERVICES":
			mojoServices = parts[1]
		}
	}
	args := []string{
		mojoUrl,
		"--config-file", configFile,
		"--shell-path", mojoShell,
		"--enable-multiprocess"}
	if target.OS() == "android" {
		args = append(args, "--android")
		args = append(args, "--origin", mojoServices)
	}
	for alias, value := range configAliases {
		args = append(args, "--config-alias", fmt.Sprintf("%s=%s", alias, value))
	}
	for key, value := range argsFor {
		args = append(args, fmt.Sprintf("--args-for=%s %s", key, strings.Join(value, " ")))
	}
	return exec.Command(filepath.Join(mojoDevtools, "mojo_run"), args...)
}
Exemplo n.º 10
0
// ensureAction ensures that the requested profile and target
// is installed/uninstalled, installing/uninstalling it if and only if necessary.
func ensureAction(jirix *jiri.X, pdb *profiles.DB, action profiles.Action, installer, profile string, root jiri.RelPath, target profiles.Target) error {
	verb := ""
	switch action {
	case profiles.Install:
		verb = "install"
	case profiles.Uninstall:
		verb = "uninstall"
	default:
		return fmt.Errorf("unrecognised action %v", action)
	}
	if jirix.Verbose() {
		fmt.Fprintf(jirix.Stdout(), "%s %v %s\n", verb, action, target)
	}
	if t := pdb.LookupProfileTarget(installer, profile, target); t != nil {
		if jirix.Verbose() {
			fmt.Fprintf(jirix.Stdout(), "%v %v is already %sed as %v\n", profile, target, verb, t)
		}
		return nil
	}
	mgr := LookupManager(profiles.QualifiedProfileName(installer, profile))
	if mgr == nil {
		return fmt.Errorf("profile %v is not supported", profile)
	}
	version, err := mgr.VersionInfo().Select(target.Version())
	if err != nil {
		return err
	}
	target.SetVersion(version)
	if jirix.Verbose() {
		fmt.Fprintf(jirix.Stdout(), "%s %s %s\n", verb, profile, target.DebugString())
	}
	if action == profiles.Install {
		return mgr.Install(jirix, pdb, root, target)
	}
	return mgr.Uninstall(jirix, pdb, root, target)
}
Exemplo n.º 11
0
func (eg *exampleManager) filename(root jiri.RelPath, target profiles.Target) jiri.RelPath {
	r := root.Join("eg")
	dir := target.TargetSpecificDirname()
	return r.Join(dir)
}
Exemplo n.º 12
0
func fmtHeader(name string, target *profiles.Target) string {
	if target == nil {
		return name
	}
	return name + " " + target.String()
}