<commit_before>package rt

import (
	"flag"
	"fmt"
	"os"
	"strings"
	"sync"

	"veyron.io/veyron/veyron2"
	"veyron.io/veyron/veyron2/config"
	"veyron.io/veyron/veyron2/ipc"
	"veyron.io/veyron/veyron2/ipc/stream"
	"veyron.io/veyron/veyron2/naming"
	"veyron.io/veyron/veyron2/security"
	"veyron.io/veyron/veyron2/vlog"

	"veyron.io/veyron/veyron/lib/exec"
	"veyron.io/veyron/veyron/profiles"
	"veyron.io/veyron/veyron/runtimes/google/naming/namespace"
)

// TODO(caprita): Verrorize this, and maybe move it in the API.
var errCleaningUp = fmt.Errorf("operation rejected: runtime is being cleaned up")

type vrt struct {
	mu sync.Mutex

	profile    veyron2.Profile
	publisher  *config.Publisher
	sm         []stream.Manager // GUARDED_BY(mu)
	ns         naming.Namespace
	signals    chan os.Signal
	id         security.PrivateID
	store      security.PublicIDStore
	client     ipc.Client
	mgmt       *mgmtImpl
	debug      debugServer
	nServers   int  // GUARDED_BY(mu)
	cleaningUp bool // GUARDED_BY(mu)
}

// Implements veyron2/rt.New
func New(opts ...veyron2.ROpt) (veyron2.Runtime, error) {
	rt := &vrt{mgmt: new(mgmtImpl)}
	flag.Parse()
	rt.initHTTPDebugServer()
	nsRoots := []string{}
	for _, o := range opts {
		switch v := o.(type) {
		case veyron2.RuntimeIDOpt:
			rt.id = v.PrivateID
		case veyron2.RuntimePublicIDStoreOpt:
			rt.store = v
		case veyron2.ProfileOpt:
			rt.profile = v.Profile
		case veyron2.NamespaceRoots:
			nsRoots = v
		case veyron2.HTTPDebugOpt:
			rt.debug.addr = string(v)
		case veyron2.RuntimeOpt:
			if v.Name != "google" && v.Name != "" {
				return nil, fmt.Errorf("%q is the wrong name for this runtime", v.Name)
			}
		default:
			return nil, fmt.Errorf("option has wrong type %T", o)
		}
	}

	rt.initLogging()
	rt.initSignalHandling()

	if rt.profile == nil {
		rt.profile = profiles.New()
	}
	vlog.VI(1).Infof("Using profile %q", rt.profile.Name())

	if len(nsRoots) == 0 {
		found := false
		for _, ev := range os.Environ() {
			p := strings.SplitN(ev, "=", 2)
			if len(p) != 2 {
				continue
			}
			k, v := p[0], p[1]
			if strings.HasPrefix(k, "NAMESPACE_ROOT") {
				nsRoots = append(nsRoots, v)
				found = true
			}
		}
		if !found {
			// TODO(cnicolaou,caprita): remove this when NAMESPACE_ROOT is in use.
			if nsRoot := os.Getenv("MOUNTTABLE_ROOT"); nsRoot != "" {
				nsRoots = append(nsRoots, nsRoot)
			}<commit_msg>veyron/runtimes/google/rt: remove use of MOUNTTABLE_ROOT.<commit_after>package rt

import (
	"flag"
	"fmt"
	"os"
	"strings"
	"sync"

	"veyron.io/veyron/veyron2"
	"veyron.io/veyron/veyron2/config"
	"veyron.io/veyron/veyron2/ipc"
	"veyron.io/veyron/veyron2/ipc/stream"
	"veyron.io/veyron/veyron2/naming"
	"veyron.io/veyron/veyron2/security"
	"veyron.io/veyron/veyron2/vlog"

	"veyron.io/veyron/veyron/lib/exec"
	"veyron.io/veyron/veyron/profiles"
	"veyron.io/veyron/veyron/runtimes/google/naming/namespace"
)

// TODO(caprita): Verrorize this, and maybe move it in the API.
var errCleaningUp = fmt.Errorf("operation rejected: runtime is being cleaned up")

type vrt struct {
	mu sync.Mutex

	profile    veyron2.Profile
	publisher  *config.Publisher
	sm         []stream.Manager // GUARDED_BY(mu)
	ns         naming.Namespace
	signals    chan os.Signal
	id         security.PrivateID
	store      security.PublicIDStore
	client     ipc.Client
	mgmt       *mgmtImpl
	debug      debugServer
	nServers   int  // GUARDED_BY(mu)
	cleaningUp bool // GUARDED_BY(mu)
}

// Implements veyron2/rt.New
func New(opts ...veyron2.ROpt) (veyron2.Runtime, error) {
	rt := &vrt{mgmt: new(mgmtImpl)}
	flag.Parse()
	rt.initHTTPDebugServer()
	nsRoots := []string{}
	for _, o := range opts {
		switch v := o.(type) {
		case veyron2.RuntimeIDOpt:
			rt.id = v.PrivateID
		case veyron2.RuntimePublicIDStoreOpt:
			rt.store = v
		case veyron2.ProfileOpt:
			rt.profile = v.Profile
		case veyron2.NamespaceRoots:
			nsRoots = v
		case veyron2.HTTPDebugOpt:
			rt.debug.addr = string(v)
		case veyron2.RuntimeOpt:
			if v.Name != "google" && v.Name != "" {
				return nil, fmt.Errorf("%q is the wrong name for this runtime", v.Name)
			}
		default:
			return nil, fmt.Errorf("option has wrong type %T", o)
		}
	}

	rt.initLogging()
	rt.initSignalHandling()

	if rt.profile == nil {
		rt.profile = profiles.New()
	}
	vlog.VI(1).Infof("Using profile %q", rt.profile.Name())

	if len(nsRoots) == 0 {
		for _, ev := range os.Environ() {
			p := strings.SplitN(ev, "=", 2)
			if len(p) != 2 {
				continue
			}
			k, v := p[0], p[1]
			if strings.HasPrefix(k, "NAMESPACE_ROOT") {
				nsRoots = append(nsRoots, v)
			}
{'fraction_non_alphanumeric': 0.1410989010989011, 'fraction_numerical': 0.01010989010989011, 'mean_word_length': 3.2217068645640072, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1427322', 'n_tokens_mistral': 1957, 'n_tokens_neox': 1831, 'n_words': 369}