// Resolve dependencies in a dependency list // Resolves dependencies in order they occured originally func (d *DependencyList) Resolve() error { if d == nil { log.Debug("No dependencies to resolve") return nil } log.Debug("Resolving dependencies") errs := make([]string, 0) for _, dep := range d.Dependencies { log.Debug("Resolving module dependency: %s", dep) if err := dep.Resolve(d.Parent); err != nil { log.Error("Error resolving module dependencies [%s]: %s", dep, err) errs = append(errs, dep.String()) break } } if len(errs) > 0 { log.Error("Failed to find dependencies:") for _, err := range errs { log.Error("=> %s", err) } return errors.New("Failed to find dependencies") } return nil }
func ParseCPANLines(lines []string) (*CPANFile, error) { cpanfile := &CPANFile{} for _, l := range lines { if len(l) == 0 { continue } log.Trace("Parsing line: %s", l) dep, err := ParseCPANLine(l) if err != nil { log.Error("=> Error parsing line: %s", err) continue } if dep != nil { log.Info("=> Found dependency: %s", dep) cpanfile.AddDependency(dep) continue } log.Trace("=> No error and no dependency found") } log.Info("Found %d dependencies in cpanfile", len(cpanfile.Dependencies)) return cpanfile, nil }
func SaveIndex(index string, indexes map[string]*Source) { // TODO append, but needs to know which stuff is new //out, err := os.OpenFile(".gopancache/index", os.O_RDWR|os.O_APPEND, 0660) out, err := os.Create(index) if err != nil { log.Error("Error creating index: %s", err.Error()) } for _, source := range indexes { out.Write([]byte(source.Name + " [" + source.URL + "]\n")) log.Trace(source.Name) for _, author := range source.Authors { out.Write([]byte(" " + author.Name + " [" + author.URL + "]\n")) log.Trace(" %s", author.Name) for _, pkg := range author.Packages { out.Write([]byte(" " + pkg.Name + " => " + pkg.URL + "\n")) log.Trace(" %s => %s", pkg.Name, pkg.URL) for p, pk := range pkg.Provides { out.Write([]byte(" " + p + " (" + pk.Version + "): " + pk.File + "\n")) log.Trace(" %s (%s): %s", p, pk.Version, pk.File) } } } } out.Close() }
func WatchConfig(configWrapper iConfigWrapper, configPath string) { watcherForConfigFile, err := fsnotify.NewWatcher() CheckError(err) go func() { for { select { case e := <-watcherForConfigFile.Event: if lastConfigModifiedTime.Add(1 * time.Second).After(time.Now()) { continue } lastConfigModifiedTime = time.Now() // log.Warn("Config '%s' modified, but NOT RELOADING YET.", e.Name) log.Warn("Config '%s' modified, now reloading (most of the) config settings.", e.Name) configWrapper.SetConfig(loadFile(configPath)) configWrapper.ConfigReloaded() break case err := <-watcherForConfigFile.Error: log.Error("Error: %+v", err) break } } }() err = watcherForConfigFile.Watch(configPath) CheckError(err) }
// NetworkName returns the SSID of the wifi network. func NetworkName(nif string) string { if nif == "" { nif = C.GoString(C.guessWifiInterfaceName()) } if nif == "" { log.Debug("Could not find Wi-Fi network interface") return "" } active := C.getWifiActive(C.CString(nif)) powerOn := C.getWifiPowerOn(C.CString(nif)) if !active { log.Debug("Wi-Fi network interface is not active") return "" } if !powerOn { log.Debug("Wi-Fi network interface is not powered on") return "" } ssid := C.GoString(C.getWifiSSID(C.CString(nif))) if ssid == "" { log.Error("Wi-Fi network interface ssid empty") } return ssid }
func (session *Session) render(asset string) error { asset = "assets/templates/" + asset var t *template.Template c, ok := session.Config.Cache["template:"+asset] if !ok { log.Trace("Loading asset: %s", asset) a, err := session.Config.AssetLoader(asset) log.Trace("Creating template: %s", asset) t = template.New(asset) t.Delims(session.Config.LeftDelim, session.Config.RightDelim) if err != nil || a == nil { log.Error("Failed loading template %s: %s", asset, err) return err } log.Trace("Parsing template: %s", asset) _, err = t.Parse(string(a)) if err != nil { log.Error("Failed parsing template %s: %s", asset, err) return err } log.Trace("Template parsed successfully: %s", asset) session.Config.Cache["template:"+asset] = t } else { t = c.(*template.Template) log.Trace("Template loaded from cache: %s", asset) } var b bytes.Buffer err := t.Execute(&b, session.Stash) if err != nil { log.Error("Failed executing template %s: %s", asset, err) return err } _, err = session.Response.Write(b.Bytes()) if err != nil { log.Error("Error writing output for template %s: %s", asset, err) return err } return nil }
func (session *Session) RenderException(status int, err error) { key := uuid.NewUUID().String() session.Response.Status = status session.Stash["Gotcha"].(map[string]interface{})["Error"] = err.Error() session.Stash["Gotcha"].(map[string]interface{})["ErrorId"] = key buf := make([]byte, 1<<16) n := runtime.Stack(buf, true) session.Stash["Gotcha"].(map[string]interface{})["Stack"] = fmt.Sprintf("%s", buf[:n]) log.Error("Exception %s: %s\n%s", key, err.Error(), session.Stash["Gotcha"].(map[string]interface{})["Stack"]) e := session.render("error.html") if e != nil { log.Error("Error rendering error page: %s", e) session.Response.Write([]byte("[" + key + "] Internal Server Error: " + err.Error() + "\n")) } }
func (session *Session) RenderNotFound() { session.Stash["Gotcha"].(map[string]interface{})["Error"] = "Not found" session.Response.Status = 404 e := session.render("notfound.html") if e != nil { log.Error("Error rendering not found page: %s", e) } }
func RemoveModule(index string, source *Source, author *Author, pkg *Package) { out, err := os.OpenFile(index, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660) if err != nil { log.Error("Error opening index: %s", err.Error()) return } out.Write([]byte(source.Name + " [" + source.URL + "]\n")) out.Write([]byte(" " + author.Name + " [" + author.URL + "]\n")) out.Write([]byte(" -" + pkg.Name + " => " + pkg.URL + "\n")) out.Close() }
func LoadConfig(global string, workspaces []string) { // Load global environment log.Info("Loading global environment file: %s", global) cfg, err := LoadConfigFile(global) if err != nil { log.Error("Error loading global configuration: %s", err.Error()) } if cfg != nil { GlobalConfigWorkspace = cfg } // Load workspaces for _, conf := range workspaces { log.Info("Loading workspace file: %s", conf) cfg, err := LoadConfigFile(conf) if err != nil { log.Error("Error loading workspace: %s", err.Error()) } if cfg != nil { ConfigWorkspaces[cfg.Name] = cfg } } }
func AppendToIndex(index string, source *Source, author *Author, pkg *Package) { out, err := os.OpenFile(index, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660) if err != nil { log.Error("Error opening index: %s", err.Error()) return } out.Write([]byte(source.Name + " [" + source.URL + "]\n")) out.Write([]byte(" " + author.Name + " [" + author.URL + "]\n")) out.Write([]byte(" " + pkg.Name + " => " + pkg.URL + "\n")) for p, pk := range pkg.Provides { out.Write([]byte(" " + p + " (" + pk.Version + "): " + pk.File + "\n")) } out.Close() }
func HasJSONXS() bool { cmd := exec.Command("perl", "-MJSON::XS", "-e", "") _, err := cmd.Output() if err != nil { log.Error("Error executing command: %s", err) return false } ps := cmd.ProcessState sy := ps.Sys().(syscall.WaitStatus) exit := sy.ExitStatus() if exit != 0 { return false } return true }
func ParseCPANLine(line string) (*Dependency, error) { if len(line) == 0 { return nil, nil } matches := re.FindStringSubmatch(line) if len(matches) == 0 { log.Trace("Unable to parse line: %s", line) return nil, nil } module := matches[2] version := strings.Replace(matches[4], " ", "", -1) comment := matches[5] dependency, err := DependencyFromString(module, version) if strings.HasPrefix(strings.Trim(comment, " "), "# REQS: ") { comment = strings.TrimPrefix(strings.Trim(comment, " "), "# REQS: ") log.Trace("Found additional dependencies: %s", comment) for _, req := range strings.Split(comment, ";") { req = strings.Trim(req, " ") bits := strings.Split(req, "-") new_dep, err := DependencyFromString(bits[0], bits[1]) if err != nil { log.Error("Error parsing REQS dependency: %s", req) continue } log.Trace("Added dependency: %s", new_dep) dependency.Additional = append(dependency.Additional, new_dep) } } if err != nil { return nil, err } log.Info("%s (%s %s)", module, dependency.Modifier, dependency.Version) return dependency, err }
func (s *Source) Load() error { log.Debug("Loading source: %s", s) switch s.Type { case "CPAN": log.Debug("=> Got CPAN source") return s.loadCPANSource() case "BackPAN": log.Debug("=> Got BackPAN source") return s.loadBackPANSource() case "SmartPAN": log.Debug("=> Got SmartPAN source") return nil case "MetaCPAN": log.Debug("=> Got MetaCPAN source") return nil default: log.Error("Unrecognised source type: %s", s.Type) return errors.New(fmt.Sprintf("Unrecognised source: %s", s)) } }
func HasPerl() (bool, string) { cmd := exec.Command("perl", "-V::version:") out, err := cmd.Output() if err != nil { log.Error("Error executing command: %s", err) return false, "" } ps := cmd.ProcessState sy := ps.Sys().(syscall.WaitStatus) exit := sy.ExitStatus() if exit != 0 { return false, "" } s := string(out) s = strings.TrimSpace(s) l := strings.Split(s, "\n") v := l[0] return true, v }
func main() { config = getpan.Configure() config.Dump() mods := flag.Args() if len(mods) == 0 { if _, err := os.Stat(config.CPANFile); os.IsNotExist(err) { log.Error("cpanfile not found: %s", config.CPANFile) os.Exit(1) } } if len(mods) > 0 && mods[0] == "exec" { log.Debug("getpan exec => " + strings.Join(mods[1:], " ")) cmd := exec.Command(mods[1], mods[2:]...) cmd.Env = os.Environ() cmd.Env = append(cmd.Env, "PERL5LIB="+config.InstallDir+"/lib/perl5") cmd.Env = append(cmd.Env, "PATH="+os.Getenv("PATH")+":"+config.InstallDir+"/bin") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err := cmd.Run() if err != nil { // debug so it doesn't show up in stdout/stderr unless -loglevel is used log.Debug("Error in exec: %s", err.Error()) os.Exit(10) } return } for _, source := range config.Sources { err := source.Load() if err != nil { log.Error("Error loading sources: %s", err) os.Exit(1) return } } var deps *getpan.DependencyList if len(mods) == 0 { log.Info("Installing from cpanfile: %s", config.CPANFile) d, err := getpan.ParseCPANFile(config.CPANFile) if err != nil { log.Error("Error parsing cpanfile: %s", err) os.Exit(2) return } deps = &d.DependencyList } else { log.Info("Installing from command line args") deps = &getpan.DependencyList{ Dependencies: make([]*getpan.Dependency, 0), } for _, arg := range mods { dependency, err := getpan.DependencyFromString(arg, "") if err != nil { log.Error("Unable to parse input: %s", arg) continue } deps.AddDependency(dependency) } } err := deps.Resolve() if err != nil { log.Error("Error resolving dependencies: %s", err) os.Exit(3) return } if false == config.NoDepdump { log.Info("Resolved dependency tree:") deps.PrintDeps(0) } if config.NoInstall { log.Info("Skipping installation phase") return } _, err = deps.Install() if err != nil { log.Error("Error installing dependencies: %s", err) os.Exit(4) return } // FIXME hacky, need a better way of tracking installed deps log.Info("Successfully installed %d modules", deps.UniqueInstalled()) }
func (m *Module) Install() (int, error) { log.Debug("Installing module: %s", m) n := 0 if m.Deps != nil { log.Trace("Installing module dependencies for %s", m) <-install_semaphore o, err := m.Deps.Install() install_semaphore <- 1 n += o if err != nil { log.Error("Error installing module dependencies for %s: %s", m, err) return n, err } } var c *exec.Cmd var stdout *bytes.Buffer var stderr *bytes.Buffer cpanm_cache_dir, err := filepath.Abs(config.CacheDir) if err != nil { log.Error("Failed to get absolute path of gopan cache directory: %s", err) return n, err } os.Setenv("PERL_CPANM_HOME", cpanm_cache_dir) done := false attempts := 0 for !done { time.Sleep(time.Duration(100) * time.Millisecond) c = m.getCmd() stdout = new(bytes.Buffer) stderr = new(bytes.Buffer) c.Stderr = stderr c.Stdout = stdout // brute force cpanm text file busy errors attempts++ if err := c.Start(); err != nil { if attempts > 10 { log.Error("Error installing module %s: %s", m, err) return n, err } } else { done = true } } if err := c.Wait(); err != nil { if !strings.HasPrefix(strings.ToLower(stderr.String()), "plenv: cannot rehash:") && !strings.Contains(strings.ToLower(stderr.String()), "text file busy") && !strings.HasPrefix(strings.ToLower(stdout.String()), "plenv: cannot rehash:") && !strings.Contains(strings.ToLower(stdout.String()), "text file busy") { log.Error(m.Name + "-" + m.Version + " failed to install") log.Error("Error installing %s %s: %s\nSTDERR:\n%sSTDOUT:\n%s", m.Name, m.Version, err, stderr.String(), stdout.String()) return n, err } } n++ log.Printf("Installed " + m.Name + " (" + m.Version + ")") return n, nil }
func (tr *TaskRun) Start(exitCh chan int) { tr.Started = time.Now() stdout, err := tr.Cmd.StdoutPipe() if err != nil { tr.Error = err exitCh <- 1 return } stderr, err := tr.Cmd.StderrPipe() if err != nil { tr.Error = err exitCh <- 1 return } if len(tr.Stdout) > 0 { wr, err := NewFileLogWriter(tr.Stdout) if err != nil { log.Error("Unable to open file %s: %s", tr.Stdout, err.Error()) tr.StdoutBuf = NewInMemoryLogWriter() } else { tr.StdoutBuf = wr } } else { tr.StdoutBuf = NewInMemoryLogWriter() } if len(tr.Stderr) > 0 { wr, err := NewFileLogWriter(tr.Stderr) if err != nil { log.Error("Unable to open file %s: %s", tr.Stderr, err.Error()) tr.StderrBuf = NewInMemoryLogWriter() } else { tr.StderrBuf = wr } } else { tr.StderrBuf = NewInMemoryLogWriter() } if len(tr.Pwd) > 0 { log.Info("Setting pwd: %s", tr.Pwd) tr.Cmd.Dir = tr.Pwd } for k, v := range tr.Environment { log.Info("Adding env var %s = %s", k, v) tr.Cmd.Env = append(tr.Cmd.Env, k+"="+v) } err = tr.Cmd.Start() if tr.Cmd.Process != nil { ev := &Event{time.Now(), fmt.Sprintf("Process %d started: %s", tr.Cmd.Process.Pid, tr.Command)} log.Info(ev.Message) tr.Events = append(tr.Events, ev) } if err != nil { tr.Error = err log.Error(err.Error()) tr.StdoutBuf.Close() tr.StderrBuf.Close() exitCh <- 1 return } go func() { go io.Copy(tr.StdoutBuf, stdout) go io.Copy(tr.StderrBuf, stderr) tr.Cmd.Wait() tr.StdoutBuf.Close() tr.StderrBuf.Close() log.Trace("STDOUT: %s", tr.StdoutBuf.String()) log.Trace("STDERR: %s", tr.StderrBuf.String()) ps := tr.Cmd.ProcessState sy := ps.Sys().(syscall.WaitStatus) ev := &Event{time.Now(), fmt.Sprintf("Process %d exited with status %d", ps.Pid(), sy.ExitStatus())} log.Info(ev.Message) tr.Events = append(tr.Events, ev) log.Info(ps.String()) tr.Stopped = time.Now() exitCh <- 1 }() }
func download(session *http.Session) { if _, ok := session.Stash["repo"]; !ok { session.RenderNotFound() return } if _, ok := session.Stash["file"]; !ok { session.RenderNotFound() return } repo := session.Stash["repo"].(string) file := session.Stash["file"].(string) if repo == "SmartPAN" { if _, ok := filemap[file]; !ok { log.Debug("SmartPAN repo - file [%s] not found in any index", file) session.RenderNotFound() return } repo = filemap[file] log.Debug("SmartPAN repo - file [%s] found in [%s]", file, repo) } log.Debug("Repo [%s], file [%s]", repo, file) nfile := config.CacheDir + "/" + repo + "/" + file if _, err := os.Stat(nfile); err != nil { log.Debug("File not found on disk, considering readthrough") for fn, _ := range indexes { log.Debug("Trying file: %s", fn) if src, ok := indexes[fn][repo]; ok { log.Debug("Found matching repo") if strings.HasPrefix(src.URL, "http:") { log.Debug("Found HTTP URL, trying: %s", src.URL+"/"+file) res, err := nethttp.Get(src.URL + "/" + file) if err != nil { log.Debug("Error on readthrough: %s", err.Error()) continue } defer res.Body.Close() b, err := ioutil.ReadAll(res.Body) if err != nil { log.Debug("Error reading body: %s", err.Error()) continue } session.Response.Write(b) return } } } log.Debug("No readthrough available") session.RenderNotFound() return } f, err := os.Open(nfile) if err != nil { log.Error(err.Error()) session.RenderNotFound() return } defer f.Close() b, err := ioutil.ReadAll(f) if err != nil { log.Error(err.Error()) session.RenderNotFound() return } session.Response.Write(b) }
func (m *Module) loadDependencies() error { yml, err := ioutil.ReadFile(m.Extracted + "/META.yml") if err != nil { // TODO this isnt an error (it shouldnt make build fail) log.Error("Error opening META.yml for %s: %s", m.Name, err) // return nil to prevent build fail return nil } meta := make(map[interface{}]interface{}) err = yaml.Unmarshal(yml, &meta) if err != nil { // TODO this isnt a real error, probably log.Error("Error parsing YAML: %s", err) // return nil to prevent build fail return nil } if reqs, ok := meta["requires"]; ok { log.Debug("Found dependencies for module %s", m.Name) switch reqs.(type) { case map[interface{}]interface{}: for req, ver := range reqs.(map[interface{}]interface{}) { v := float64(0) switch ver.(type) { case string: v = gopan.VersionFromString(ver.(string)) case int: v = float64(ver.(int)) } log.Printf("=> %s (%f)", req, v) dep, err := DependencyFromString(req.(string), fmt.Sprintf("%f", ver)) if err != nil { log.Error("Error parsing dependency: %s", err) continue } if _, ok := perl_core[dep.Name]; ok { log.Trace("Module is from perl core: %s", dep.Name) continue } m.Deps.AddDependency(dep) } } log.Debug("Resolving module dependency list") if err := m.Deps.Resolve(); err != nil { log.Error("Error resolving dependency list [%s]: %s", m.Name, err) return err } return nil } // FIXME repeat of block above, just with more nested levels if p, ok := meta["prereqs"]; ok { if r, ok := p.(map[interface{}]interface{})["runtime"]; ok { if reqs, ok := r.(map[interface{}]interface{})["requires"]; ok { log.Debug("Found dependencies for module %s", m.Name) switch reqs.(type) { case map[interface{}]interface{}: for req, ver := range reqs.(map[interface{}]interface{}) { v := float64(0) switch ver.(type) { case string: v = gopan.VersionFromString(ver.(string)) case int: v = float64(ver.(int)) } log.Printf("=> %s (%f)", req, v) dep, err := DependencyFromString(req.(string), fmt.Sprintf("%f", ver)) if err != nil { log.Error("Error parsing dependency: %s", err) continue } if _, ok := perl_core[dep.Name]; ok { log.Trace("Module is from perl core: %s", dep.Name) continue } m.Deps.AddDependency(dep) } } } } if t, ok := p.(map[interface{}]interface{})["test"]; ok { if reqs, ok := t.(map[interface{}]interface{})["requires"]; ok { log.Debug("Found dependencies for module %s", m.Name) switch reqs.(type) { case map[interface{}]interface{}: for req, ver := range reqs.(map[interface{}]interface{}) { v := float64(0) switch ver.(type) { case string: v = gopan.VersionFromString(ver.(string)) case int: v = float64(ver.(int)) } log.Printf("=> %s (%f)", req, v) dep, err := DependencyFromString(req.(string), fmt.Sprintf("%f", ver)) if err != nil { log.Error("Error parsing dependency: %s", err) continue } if _, ok := perl_core[dep.Name]; ok { log.Trace("Module is from perl core: %s", dep.Name) continue } m.Deps.AddDependency(dep) } } } } log.Debug("Resolving module dependency list") if err := m.Deps.Resolve(); err != nil { log.Error("Error resolving dependency list: %s", err) return err } return nil } log.Debug("No dependencies for module %s", m.Name) return nil }
func Provides(pkg *gopan.Package, tgzpath string, extpath string, dirpath string) error { // not required? path should already exist os.MkdirAll(dirpath, 0770) var stdout1 bytes.Buffer var stderr1 bytes.Buffer extract := exec.Command("tar", "-zxf", tgzpath, "-C", dirpath) extract.Stdout = &stdout1 extract.Stderr = &stderr1 if err := extract.Run(); err != nil { log.Error("Extract run: %s", err.Error()) log.Trace(stdout1.String()) log.Error(stderr1.String()) return err } log.Trace(stdout1.String()) log.Trace(stderr1.String()) defer func() { var stdout3 bytes.Buffer var stderr3 bytes.Buffer clean := exec.Command("rm", "-rf", extpath) clean.Stdout = &stdout3 clean.Stderr = &stderr3 if err := clean.Run(); err != nil { log.Error("Clean run: %s", err.Error()) } log.Trace(stdout3.String()) log.Trace(stderr3.String()) }() //var stdout2 bytes.Buffer var stderr2 bytes.Buffer if len(pldArgs) == 0 { if len(os.Getenv("GOPAN_ALLOW_DEV_VERSIONS")) > 0 { pldArgs = "({ALLOW_DEV_VERSION=>1})" } else { pldArgs = "()" } } log.Trace("pldArgs: %s", pldArgs) //cmd := exec.Command("perl", "-MModule::Metadata", "-MJSON::XS", "-e", "print encode_json(Module::Metadata->provides(version => 2, prefix => \"\", dir => $ARGV[0]))", extpath) cmd := exec.Command("perl", "-MParse::LocalDistribution", "-MJSON::XS", "-e", "print encode_json(Parse::LocalDistribution->new"+pldArgs+"->parse($ARGV[0]))", extpath) //cmd.Stdout = &stdout2 cmd.Stderr = &stderr2 stdout, err := cmd.StdoutPipe() defer stdout.Close() if err != nil { log.Error("StdoutPipe: %s", err.Error()) return err } if err := cmd.Start(); err != nil { log.Error("Start: %s", err.Error()) return err } var pld PLD if err := json.NewDecoder(stdout).Decode(&pld); err != nil { log.Error("JSON decoder error: %s", err.Error()) return err } if err := cmd.Wait(); err != nil { log.Error("Wait: %s", err.Error()) return err } //log.Trace(stdout2.String()) log.Trace(stderr2.String()) pkg.Provides = make(map[string]*gopan.PerlPackage) for p, pk := range pld { pp := &gopan.PerlPackage{ Package: pkg, Name: p, Version: pk.Version, File: pk.Infile, } pkg.Provides[p] = pp log.Trace("%s: %s %s", p, pp.Version, pp.File) } log.Debug("%s provides %d packages", pkg, len(pkg.Provides)) return nil }
func (s *settings) OnWatchReloadError(err error) { log.Error("Error: %+v", err) }
func (r *Response) EventStream() chan []byte { c := make(chan []byte) r.IsEventStream = true r.Headers.Add("Content-Type", "text/event-stream") r.Headers.Add("Cache-Control", "no-cache") r.Headers.Add("Connection", "keep-alive") //r.Write([]byte("\n\n")) r.Send() hj, ok := r.writer.(nethttp.Hijacker) if !ok { log.Warn("Connection unsuitable for hijack") return nil } conn, bufrw, err := hj.Hijack() if err != nil { log.Warn("Connection hijack failed") return nil } r.esBufrw = bufrw r.esConn = conn go func() { for b := range c { if len(b) == 0 { log.Trace("Event stream ended") r.esConn.Close() break } lines := strings.Split(string(b), "\n") data := "" for _, l := range lines { data += "data: " + l + "\n" } data += "\n" sz := len(data) + 1 log.Info("Event stream message is %d bytes", sz) size := fmt.Sprintf("%X", sz) r.esBufrw.Write([]byte(size + "\r\n")) lines = strings.Split(data, "\n") for _, ln := range lines { r.esBufrw.Write([]byte(ln + "\n")) } _, err := r.esBufrw.Write([]byte("\r\n")) if err != nil { log.Error("Error writing to connection: %s\n", err) r.esConn.Close() break } err = r.esBufrw.Flush() if err != nil { log.Error("Error flushing buffer: %s\n", err) r.esConn.Close() break } } }() return c }
func getPackages() int { newpkg := 0 var pl func(*html.Node, *gopan.Source, *gopan.Author) pl = func(n *html.Node, source *gopan.Source, author *gopan.Author) { log.Trace("NODE: %s [%s, %s, %s]", n.DataAtom, n.Type, n.Data) if n.Type == html.ElementNode && n.Data == "a" { //log.Info("NODE IS ELEMENTNODE") for _, attr := range n.Attr { // FIXME stuff that isn't .tar.gz? if attr.Key == "href" && strings.HasSuffix(attr.Val, ".tar.gz") { log.Trace("==> HREF: %s", n.FirstChild.Data) pkg := strings.TrimSuffix(n.FirstChild.Data, "/") if _, ok := author.Packages[pkg]; !ok { author.Packages[pkg] = &gopan.Package{ Name: pkg, Author: author, URL: author.URL + "/" + pkg, } newpkg++ log.Debug("Found package: %s", pkg) } } } //log.Info("%s", n.Data) } for c := n.FirstChild; c != nil; c = c.NextSibling { pl(c, source, author) } } log.Info("Building package list") for fname, _ := range indexes { for _, source := range indexes[fname] { log.Debug("Index: %s", source) wg.Add(1) go func(source *gopan.Source) { defer wg.Done() for _, author := range source.Authors { wg.Add(1) go func(author *gopan.Author) { defer wg.Done() sem <- 1 log.Trace("=> %s", author) url := source.URL + "/" + author.Name[:1] + "/" + author.Name[:2] + "/" + author.Name + "/" log.Trace("Getting URL: %s", url) res, err := http.Get(url) if err != nil { log.Error("HTTP GET - %s", err.Error()) <-sem return } doc, err := html.Parse(res.Body) if err != nil { log.Error("HTML PARSE - %s", err.Error()) <-sem return } pl(doc, source, author) <-sem }(author) } }(source) } } wg.Wait() log.Info("Finished building package list") return newpkg }
func (d *DependencyList) Install() (int, error) { if d == nil { log.Debug("No dependencies to install") return 0, nil } n := 0 if install_semaphore == nil { install_semaphore = make(chan int, config.CPUs) } var wg sync.WaitGroup var errorLock sync.Mutex errs := make([]string, 0) for _, dep := range d.Dependencies { log.Debug("Installing dependency: %s", dep) wg.Add(1) go func(dep *Dependency) { defer wg.Done() defer func(mod *Module) { if mod != nil { log.Debug("Resuming installation of %s", mod) } }(d.Parent) _, ok1 := global_installed[dep.Module.Cached] _, ok2 := global_installed[dep.Module.Name+"-"+dep.Module.Version] if ok1 || ok2 { log.Trace("Module is already installed: %s", dep.Module) return } log.Trace("Aquiring install lock for module %s", dep.Module) install_lock.Lock() if mt, ok := install_mutex[dep.Module.Cached]; ok { install_lock.Unlock() log.Trace("Waiting on existing installation for %s", dep.Module) log.Trace("Path: %s", dep.Module.Path()) mt.Lock() mt.Unlock() log.Trace("Existing installation complete for %s", dep.Module) return } log.Trace("Creating new installation lock for module %s", dep.Module) install_mutex[dep.Module.Cached] = new(sync.Mutex) install_mutex[dep.Module.Cached].Lock() //log.Trace("%s:: Sending semaphore", dep.module) install_semaphore <- 1 install_lock.Unlock() o, err := dep.Module.Install() //log.Trace("%s:: Waiting on semaphore", dep.module) <-install_semaphore //log.Trace("%s:: Got semaphore", dep.module) global_installed[dep.Module.Name+"-"+dep.Module.Version] = dep.Module global_installed[dep.Module.Cached] = dep.Module global_unique[dep.Module.Name] = 1 n += o if err != nil { log.Error("Error installing module: %s", err) errorLock.Lock() errs = append(errs, dep.Module.String()) errorLock.Unlock() } install_lock.Lock() install_mutex[dep.Module.Cached].Unlock() install_lock.Unlock() n++ }(dep) } wg.Wait() if len(errs) > 0 { log.Error("Failed to install dependencies:") for _, err := range errs { log.Error("=> %s", err) } return n, errors.New("Failed to install dependencies") } return n, nil }
func (s *Source) Find(d *Dependency) (*Module, error) { log.Debug("Finding dependency: %s", d) switch s.Type { case "SmartPAN": log.Debug("=> Using SmartPAN source") url := s.URL if !strings.HasSuffix(s.URL, "/") { url += "/" } url += "where/" + d.Name + "/" + d.Modifier + d.Version log.Info("Query: %s", url) res, err := http.Get(url) if err != nil { log.Error("Error querying SmartPAN: %s", err.Error()) return nil, err } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) log.Trace("Got response: %s", string(body)) if res.StatusCode != http.StatusOK { log.Info("Module not found in SmartPAN: %s", d.Name) return nil, nil } var v *WhereOutput if err = json.Unmarshal(body, &v); err != nil { log.Error("Error parsing JSON: %s", err.Error()) return nil, err } log.Trace("Found module %s", v.Module) if len(v.Versions) == 0 { log.Info("Found module but no versions returned") return nil, nil } var lv *VersionOutput for _, ver := range v.Versions { if ver.Version == v.Latest { log.Info("Using latest version of %s: %f", v.Module, ver.Version) lv = ver break } } if lv == nil { log.Info("Couldn't find latest version, selecting first available") lv = v.Versions[0] } return &Module{ Name: d.Name, Version: fmt.Sprintf("%f", lv.Version), Source: s, Url: lv.URL, }, nil case "CPAN": log.Debug("=> Using CPAN source") if mod, ok := s.ModuleList[d.Name]; ok { log.Trace("=> Found in source: %s", mod) if d.Matches(mod) { log.Trace("=> Version (%s) matches dependency: %s", mod.Version, d) return mod, nil } log.Trace("=> Version (%s) doesn't match dependency: %s", mod.Version, d) return nil, nil } case "BackPAN": log.Debug("=> Using BackPAN source") // TODO better version matching - new backpan index? if mod, ok := s.ModuleList[d.Name+"-"+d.Version]; ok { log.Trace("=> Found in source: %s", mod) if d.Matches(mod) { log.Trace("=> Version (%s) matches dependency: %s", mod.Version, d) return mod, nil } log.Trace("=> Version (%s) doesn't match dependency: %s", mod.Version, d) return nil, nil } case "MetaCPAN": log.Debug("=> Using MetaCPAN source") var sout, serr bytes.Buffer var cpanm_args string = fmt.Sprintf("-L %s --info %s~\"%s%s\"", config.InstallDir, d.Name, d.Modifier, d.Version) cpanm_cache_dir, err := filepath.Abs(config.CacheDir) if err != nil { log.Error("Failed to get absolute path of gopan cache directory: %s", err) return nil, err } log.Trace("About to exec: cpanm %s", cpanm_args) os.Setenv("CPANM_INFO_ARGS", cpanm_args) os.Setenv("PERL_CPANM_HOME", cpanm_cache_dir) cmd := exec.Command("bash", "-c", `eval cpanm $CPANM_INFO_ARGS`) cmd.Stdout = &sout cmd.Stderr = &serr if err := cmd.Run(); err != nil { log.Error("cpanm %s: %s,\n%s\n", cpanm_args, err, serr.String()) return nil, nil } if 0 == len(sout.String()) { log.Warn("No author/module from cpanm") return nil, nil } author_module := strings.TrimRight(sout.String(), "\n") mematches := metacpanRe.FindStringSubmatch(author_module) if nil == mematches { log.Error("Match failed for: %s", author_module) return nil, nil } log.Trace("Resolved: %s", author_module) for _, mesource := range config.MetaSources { meurl := fmt.Sprintf("authors/id/%s/%s/%s", mematches[1][0:1], mematches[1][0:2], mematches[0]) archive_url := fmt.Sprintf("%s/%s", mesource.URL, meurl) log.Trace("Checking: " + archive_url) resp, err := http.Head(archive_url) if err != nil { log.Trace(err) continue } log.Trace("HEAD status code: %d", resp.StatusCode) if 200 == resp.StatusCode { // No module/version check since 'cpanm --info' may resolve to // archive and version that may not match source return &Module{ Name: mematches[2], Version: mematches[3], Source: mesource, Url: meurl, }, nil } } log.Error("Could not get archive URL via 'cpanm %s'", cpanm_args) return nil, nil default: log.Error("Unrecognised source type: %s", s.Type) return nil, errors.New(fmt.Sprintf("Unrecognised source: %s", s)) } log.Trace("=> Not found in source") return nil, nil }
func main() { configure() log.Logger().SetLevel(log.Stol(config.LogLevel)) log.Info("Using log level: %s", config.LogLevel) indexes = make(map[string]map[string]*gopan.Source) if !config.NoCache { indexes[config.Index] = gopan.LoadIndex(config.CacheDir + "/" + config.Index) } if config.NoCache || config.Update { for _, s := range config.Sources { b := strings.SplitN(s, "=", 2) if len(b) < 2 { log.Error("Expected Name=URL pair, got: %s", s) return } if idx, ok := indexes[config.Index][b[0]]; ok { log.Warn("Index [%s] already exists with URL [%s], updating to [%s]", idx.URL, b[1]) idx.URL = b[1] } else { indexes[config.Index][b[0]] = &gopan.Source{ Name: b[0], URL: b[1], Authors: make(map[string]*gopan.Author, 0), } } } if len(config.Sources) == 0 && !config.CPAN && !config.BackPAN { log.Debug("No -source, -cpan, -backpan parameters, adding default CPAN/BackPAN") config.CPAN = true config.BackPAN = true } if config.CPAN { if _, ok := indexes[config.Index]["CPAN"]; !ok { log.Debug("Adding CPAN index") indexes[config.Index]["CPAN"] = gopan.CPANSource() } else { log.Debug("CPAN index already exists") } } if config.BackPAN { if _, ok := indexes[config.Index]["BackPAN"]; !ok { log.Debug("Adding BackPAN index") indexes[config.Index]["BackPAN"] = gopan.BackPANSource() } else { log.Debug("BackPAN index already exists") } } log.Info("Using sources:") for fname, _ := range indexes { log.Info("From %s", fname) for _, source := range indexes[fname] { log.Info("=> %s", source.String()) } } newAuthors := getAuthors() newPackages := getPackages() os.MkdirAll(config.CacheDir, 0777) if !config.NoCache { gopan.SaveIndex(config.CacheDir+"/"+config.Index, indexes[config.Index]) } if config.Update { log.Info("Found %d new packages by %d new authors", newAuthors, newPackages) } } nsrc, nauth, nmod, npkg := gopan.CountIndex(indexes) log.Info("Found %d packages in %d modules by %d authors from %d sources", npkg, nmod, nauth, nsrc) if !config.NoMirror { mirrorPan() } }
func mirrorPan() { log.Info("Mirroring *PAN") // FIXME inefficient _, _, npkg, _ := gopan.CountIndex(indexes) mirrored := 0 var pc = func() int { return mirrored / npkg * 100 } for fname, _ := range indexes { log.Debug("File: %s", fname) for _, source := range indexes[fname] { log.Debug("Index: %s", source) wg.Add(1) go func(source *gopan.Source) { defer wg.Done() for _, author := range source.Authors { log.Debug("=> %s", author) wg.Add(1) go func(author *gopan.Author) { cachedir := config.CacheDir + "/" + source.Name + "/" + author.Name[:1] + "/" + author.Name[:2] + "/" + author.Name + "/" os.MkdirAll(cachedir, 0777) defer wg.Done() for _, pkg := range author.Packages { wg.Add(1) go func(pkg *gopan.Package) { defer wg.Done() cache := cachedir + pkg.Name log.Trace(" - Caching to: %s", cache) if _, err := os.Stat(cache); err == nil { log.Debug("%d%% |> %s", pc(), pkg) log.Trace(" - Already exists in cache") mirrored++ return } sem <- 1 mirrored++ log.Debug("%d%% => %s", pc(), pkg) url := source.URL + "/" + author.Name[:1] + "/" + author.Name[:2] + "/" + author.Name + "/" + pkg.Name log.Trace(" - From URL: %s", url) out, err := os.Create(cache) defer out.Close() if err != nil { log.Error("CREATE - %s", err.Error()) <-sem return } resp, err := http.Get(url) if err != nil { log.Error("HTTP GET - %s", err.Error()) <-sem return } _, err = io.Copy(out, resp.Body) if err != nil { log.Error("IO COPY - %s", err.Error()) } <-sem }(pkg) } }(author) } }(source) } } wg.Wait() log.Info("Finished mirroring *PAN") }
func main() { configure() indexes = make(map[string]map[string]*gopan.Source) indexes[config.InputIndex] = gopan.LoadIndex(config.CacheDir + "/" + config.InputIndex) log.Logger().SetLevel(log.Stol(config.LogLevel)) log.Info("Using log level: %s", config.LogLevel) // FIXME inefficient _, _, tpkg, _ := gopan.CountIndex(indexes) npkg := 0 nmod := 0 var pc = func() float64 { return float64(nmod) / float64(tpkg) * 100 } log.Info("Writing packages index file") out, err := os.Create(config.CacheDir + "/" + config.OutputIndex) if err != nil { log.Error("Error creating packages index: %s", err.Error()) return } for fname, _ := range indexes { log.Debug("File: %s", fname) for _, idx := range indexes[fname] { log.Debug("Index: %s", idx) out.Write([]byte(idx.Name + " [" + idx.URL + "]\n")) for _, auth := range idx.Authors { log.Debug("Author %s", auth) out.Write([]byte(" " + auth.Name + " [" + auth.URL + "]\n")) for _, pkg := range auth.Packages { out.Write([]byte(" " + pkg.Name + " => " + pkg.URL + "\n")) log.Debug("Package: %s", pkg) if !config.Flatten { if len(pkg.Provides) == 0 { // TODO better handling of filenames modnm := strings.TrimSuffix(pkg.Name, ".tar.gz") tgzpath := config.CacheDir + "/" + idx.Name + "/" + auth.Name[:1] + "/" + auth.Name[:2] + "/" + auth.Name + "/" + pkg.Name if _, err := os.Stat(tgzpath); err != nil { log.Error("File not found: %s", tgzpath) return } extpath := config.ExtDir + "/" + idx.Name + "/" + auth.Name[:1] + "/" + auth.Name[:2] + "/" + auth.Name + "/" + modnm dirpath := config.ExtDir + "/" + idx.Name + "/" + auth.Name[:1] + "/" + auth.Name[:2] + "/" + auth.Name log.Trace("=> tgzpath: %s", tgzpath) log.Trace(" > extpath: %s", extpath) log.Trace(" > dirpath: %s", dirpath) // Only index packages if they don't already exist if err := pandex.Provides(pkg, tgzpath, extpath, dirpath); err != nil { log.Error("Error retrieving package list: %s", err) continue } } npkg += len(pkg.Provides) nmod += 1 for p, pk := range pkg.Provides { out.Write([]byte(" " + p + " (" + pk.Version + "): " + pk.File + "\n")) } if nmod > 0 && nmod%100 == 0 { log.Info("%f%% Done %d/%d packages (%d provided so far)", pc(), nmod, tpkg, npkg) } } } } } } out.Close() log.Info("Found %d packages from %d modules", npkg, nmod) }
// Resolve a dependency (i.e. one module), trying all sources func (d *Dependency) Resolve(p *Module) error { if gm, ok := global_modules[d.Name+"-"+d.Version]; ok { log.Trace("Dependency %s already resolved (S1): %s", d, gm) d.Module = gm return nil } log.Trace("Resolving dependency: %s", d) for _, s := range config.Sources { log.Trace("=> Trying source: %s", s) m, err := s.Find(d) if err != nil { log.Trace("=> Error from source: %s", err) continue } if m != nil { log.Trace("=> Resolved dependency: %s", m) d.Module = m break } } if d.Module == nil { log.Error("Failed to resolve dependency: %s", d) return fmt.Errorf("Dependency not found from any source: %s", d) } if gm, ok := global_modules[d.Module.Name+"-"+d.Module.Version+"~"+d.Module.Source.URL]; ok { log.Trace("Dependency %s already resolved (S2): %s", d, gm) d.Module = gm } else if gm, ok := global_modules[d.Module.Name]; ok { log.Trace("Dependency %s already resolved (S3): %s", d, gm) // See if the already resolved version is acceptable if !d.MatchesVersion(gm.Version) { errstr := fmt.Sprintf("Version conflict in dependency tree: %s => %s", d, gm) log.Error(errstr) return errors.New(errstr) } log.Trace("Version %s matches %s", d.Module, gm.Version) // TODO See if downloading a new version would be better d.Module = gm } else { log.Debug("Downloading: %s", d.Module) if err := d.Module.Download(); err != nil { log.Error("Error downloading module %s: %s", d.Module, err) return err } if p != nil { if p.IsCircular(d.Module) { log.Error("Detected circular dependency %s from module %s", d.Module, p) return fmt.Errorf("Detected circular dependency %s from module %s", d.Module, p) } } // module can't exist because of global_lock global_modules[d.Module.Name] = d.Module global_modules[d.Module.Name+"-"+d.Module.Version] = d.Module global_modules[d.Module.Name+"-"+d.Module.Version+"~"+d.Module.Source.URL] = d.Module log.Debug("Resolving module dependencies: %s", d.Module) d.Module.Deps = &DependencyList{ Parent: d.Module, Dependencies: make([]*Dependency, 0), } if d.Additional != nil && len(d.Additional) > 0 { log.Trace("Adding cpanfile additional REQS") for _, additional := range d.Additional { log.Trace("Adding additional dependency from cpanfile: %s", additional) d.Module.Deps.AddDependency(additional) } } if err := d.Module.loadDependencies(); err != nil { return err } } return nil }