Beispiel #1
0
func SourceUnitMatchesArgs(specified []string, u *unit.SourceUnit) bool {
	var match bool
	if len(specified) == 0 {
		match = true
	} else {
		for _, unitSpec := range specified {
			if string(u.ID()) == unitSpec || u.Name == unitSpec {
				match = true
				break
			}
		}
	}

	return match
}
Beispiel #2
0
func addSourceUnitFiles(u *unit.SourceUnit, file string) {
	for _, f := range u.Files {
		if f == file {
			return
		}
	}
	u.Files = append(u.Files, file)
}
Beispiel #3
0
func addSourceUnitFilesFromData(u *unit.SourceUnit, data *graph.Output) {
	files := map[string]struct{}{}
	for _, def := range data.Defs {
		files[def.File] = struct{}{}
	}
	for _, ref := range data.Refs {
		files[ref.File] = struct{}{}
	}
	for f := range files {
		u.Files = append(u.Files, f)
	}
	sort.Strings(u.Files)
}
Beispiel #4
0
func (c *SimpleRepoCmd) genUnit(ut *unit.SourceUnit) error {
	defs := make([]*graph.Def, 0)
	refs := make([]*graph.Ref, 0)
	docs := make([]*graph.Doc, 0)

	for _, filename := range hierarchicalNames("dir", "file", ut.Name, c.NFiles) {
		ut.Files = append(ut.Files, filename)
		fileDefs, fileRefs, fileDocs, err := c.genFile(ut, filename)
		if err != nil {
			return err
		}
		defs, refs, docs = append(defs, fileDefs...), append(refs, fileRefs...), append(docs, fileDocs...)
	}

	if !c.GenSource {
		gr := graph.Output{Defs: defs, Refs: refs, Docs: docs}
		deps := make([]*dep.Resolution, 0)
		if err := writeSrclibCache(ut, &gr, deps); err != nil {
			return err
		}
	}

	return nil
}
Beispiel #5
0
func (f unitDefOffsetsFilter) SelectUnit(u *unit.SourceUnit) bool {
	_, present := f[u.ID2()]
	return present
}
Beispiel #6
0
func (c *URefsRepoCmd) genDefsFile(ut *unit.SourceUnit, filename string) (defs []*graph.Def, defRefs []*graph.Ref, err error) {
	filename = filepath.Join(ut.Name, filename)
	ut.Files = append(ut.Files, filename)

	offset := 0
	defName := "foo"

	var sourceFile *os.File
	if c.GenSource {
		err := os.MkdirAll(filepath.Dir(filename), 0700)
		if err != nil {
			return nil, nil, err
		}
		file, err := os.Create(filename)
		if err != nil {
			return nil, nil, err
		}
		sourceFile = file
	}

	for i := 0; i < c.NDefs; i++ {
		def := &graph.Def{
			DefKey: graph.DefKey{
				Repo:     ut.Repo,
				CommitID: ut.CommitID,
				UnitType: ut.Type,
				Unit:     ut.Name,
				Path:     filepath.ToSlash(filepath.Join(filename, fmt.Sprintf("method_%d", i))),
			},
			Name:     defName,
			Exported: true,
			File:     filename,
			DefStart: uint32(offset),
			DefEnd:   uint32(offset + len(defName) - 1),
		}
		if sourceFile != nil {
			_, err := sourceFile.WriteString(def.Name + "\n")
			if err != nil {
				return nil, nil, err
			}
		}
		offset += len(defName) + 1
		defs = append(defs, def)

		defRef := &graph.Ref{
			DefRepo:     def.Repo,
			DefUnitType: def.UnitType,
			DefUnit:     def.Unit,
			DefPath:     def.Path,
			Repo:        def.Repo,
			CommitID:    def.CommitID,
			UnitType:    def.UnitType,
			Unit:        def.Unit,
			Def:         true,
			File:        def.File,
			Start:       def.DefStart,
			End:         def.DefEnd,
		}
		defRefs = append(defRefs, defRef)
	}

	// Close source file
	if sourceFile != nil {
		sourceFile.WriteString("\n")
		sourceFile.Close()
	}

	return defs, defRefs, nil
}
Beispiel #7
0
// checkSourceUnitFiles warns if any files appear in graph data but
// are not in u.Files.
func (s *indexedTreeStore) checkSourceUnitFiles(u *unit.SourceUnit, data graph.Output) {
	if u == nil {
		return
	}

	graphFiles := make(map[string]struct{}, len(u.Files))
	for _, def := range data.Defs {
		graphFiles[def.File] = struct{}{}
	}
	for _, ref := range data.Refs {
		graphFiles[ref.File] = struct{}{}
	}
	for _, doc := range data.Docs {
		graphFiles[doc.File] = struct{}{}
	}
	for _, ann := range data.Anns {
		graphFiles[ann.File] = struct{}{}
	}
	delete(graphFiles, "")

	unitFiles := make(map[string]struct{}, len(u.Files))
	for _, f := range u.Files {
		unitFiles[f] = struct{}{}
	}
	if u.Dir != "" {
		unitFiles[u.Dir] = struct{}{}
	}

	var missingFiles []string
	for f := range graphFiles {
		if _, present := unitFiles[f]; !present {
			missingFiles = append(missingFiles, f)
		}
	}
	if len(missingFiles) > 0 {
		sort.Strings(missingFiles)
		log.Printf("Warning: The graph output (defs/refs/docs/anns) for source unit %+v contain %d references to files that are not present in the source unit's Files list. Indexed lookups by any of these missing files will return no results. To fix this, ensure that the source unit's Files list includes all files that appear in the graph output. The missing files are: %s.", u.ID2(), len(missingFiles), strings.Join(missingFiles, " "))
	}
}
Beispiel #8
0
// Scan a directory, listing all source units
func Scan(srcdir string, repoURI string, repoSubdir string) ([]*unit.SourceUnit, error) {
	if units, isSpecial := specialUnits[repoURI]; isSpecial {
		return units, nil
	}

	p, err := getVENVBinPath()
	if err != nil {
		return nil, err
	}

	cmd := exec.Command(filepath.Join(p, "python"), filepath.Join(p, "pydep-run.py"), "list", srcdir)

	cmd.Stderr = os.Stderr
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		return nil, err
	}
	if err := cmd.Start(); err != nil {
		return nil, err
	}

	var pkgs []*pkgInfo
	if err := json.NewDecoder(stdout).Decode(&pkgs); err != nil {
		return nil, err
	}

	if err := cmd.Wait(); err != nil {
		return nil, err
	}

	// Keep track of all files that have been successfully discovered
	discoveredScripts := make(map[string]bool)

	units := make([]*unit.SourceUnit, len(pkgs))
	for i, pkg := range pkgs {
		units[i] = pkg.SourceUnit()
		units[i].Files = pythonSourceFiles(pkg.RootDir, discoveredScripts)
		units[i].Repo = repoURI // override whatever's in the setup.py file with the actual reposit;5Dory URI

		reqs, err := requirements(pkg.RootDir)
		if err != nil {
			return nil, err
		}
		reqs_ := make([]interface{}, len(reqs))
		for i, req := range reqs {
			reqs_[i] = req
		}
		units[i].Dependencies = reqs_
	}

	// Scan for independant scripts, appending to the current set of source units
	scripts := pythonSourceFiles(srcdir, discoveredScripts)
	if len(scripts) > 0 {
		scriptsUnit := unit.SourceUnit{
			Name:  ".",
			Type:  "PythonProgram",
			Files: scripts,
			Dir:   ".",
			Ops:   map[string]*srclib.ToolRef{"depresolve": nil, "graph": nil},
		}

		reqs, err := requirements(srcdir)
		if err == nil {
			reqs_ := make([]interface{}, len(reqs))
			for i, req := range reqs {
				reqs_[i] = req
			}
			scriptsUnit.Dependencies = reqs_
		}

		units = append(units, &scriptsUnit)
	}

	return units, nil
}
Beispiel #9
0
func (f byUnitsFilter) SelectUnit(unit *unit.SourceUnit) bool {
	return (unit.Type == "" && unit.Name == "") || f.contains(unit.ID2())
}