def get_filelist_from_graph(fileindex, filepath): dg = DelphiGraph.from_file(filepath) df = dg.rootnode if not projectroot_path: projectroot_path = dg.abspath projectroot_path = os.path.realpath(projectroot_path) nodes = df.collect_nodes(DelphiFile.NodePredicates.path_not_in(dg.stdlibpath)) # extract path to project file filepaths = map(lambda n: os.path.join(n.path, n.filename), nodes) # make absolute oldcwd = os.getcwd() io.safechdir(dg.abspath) filepaths = map(lambda fp: os.path.abspath(fp), filepaths) io.safechdir(oldcwd) # make relative to project root filepaths = map(lambda fp: io.relpath(fp, relative_to=projectroot_path), filepaths) for fp in filepaths: fileindex.set(fp) return projectroot_path
except: pass # print('KEEP : %s' % fp) if __name__ == '__main__': fps = sys.argv[1:] if not fps: print("Usage: %s <graph>(;<graph>)*" % sys.argv[0]) sys.exit(1) fps = reduce(lambda x,y: x+y, map(lambda p: p.split(';'), fps)) fps = filter(lambda p: p != '', fps) fileindex = FileIndex() projectroot_paths = [] for fp in fps: projectroot_path = get_filelist_from_graph(fileindex, fp) projectroot_paths.append(projectroot_path) test = all(map(lambda p: p == projectroot_paths[0], projectroot_paths)) if not test: print("Project root paths are not all the same, not safe to prune:\n%s" % "\n".join(projectroot_paths)) else: projectroot_path = projectroot_paths[0] oldcwd = os.getcwd() io.safechdir(projectroot_path) do_prune(projectroot_path, fileindex) io.safechdir(oldcwd)
def trace(self, filepath, maxdepth=None, projview=False): def trace_deps(filepath, searchpath, stdlibpath, unitaliases, depth=0, maxdepth=None): # don't prevent calls on filepaths already in index so as to resolve # cycles properly, but catch these here to return a valid object # for a filepath already known if self.index_has(filepath): return self.index_get(filepath) io.write_next_action("Tracing %s" % os.path.basename(filepath), indent=depth) filepath, exists, filetype = classify_file(filepath, searchpath, stdlibpath) delphifile = self.new_delphifile(filepath, (filepath, exists, filetype)) if exists: searchpath = collect_searchpath(searchpath, delphifile, filepath) unitaliases = collect_unitaliases(unitaliases, delphifile, filepath) finder_function = finder_dispatch.get(filetype) if finder_function: fps = finder_function(open(filepath).read(), parent=delphifile) io.write_result('Found [%s]' % ', '.join(fps), indent=depth) fps = process_filepaths(delphifile.path, unitaliases, fps) if not maxdepth or maxdepth >= depth+1: for fp in fps: delphifile.add_node(trace_deps(fp, searchpath, stdlibpath, unitaliases, depth=depth+1, maxdepth=maxdepth)) return delphifile path, filename = os.path.split(filepath) abspath = os.path.abspath(path) searchpath = [] stdlibpath = DelphiCompiler.get_stdlibpath(relative_to=(path or '.')) finder_dispatch = { FileTypes.DelphiProjectGroup: findProjects, FileTypes.DelphiProject: group(findMainSource, findDelphiCompilerFlags), FileTypes.Program: group(findUses, findIncludes, findResources), FileTypes.Library: group(findUses, findIncludes, findResources), FileTypes.Package: group(findContains, findIncludes, findResources), FileTypes.Unit: group(findUses, findIncludes, findResources), FileTypes.FileInclude: group(findUses, findIncludes, findResources), } if projview: finder_dispatch = { FileTypes.DelphiProjectGroup: findProjects, FileTypes.DelphiProject: findMainSource, } # change cwd to where the file is so that no matter what . is at # runtime the paths of files in the graph will be constant oldcwd = os.getcwd() try: io.safechdir(path) self.index_clear() df = trace_deps(filename, searchpath, stdlibpath, {}, maxdepth=maxdepth) finally: io.safechdir(oldcwd) return DelphiGraph(df, abspath, searchpath, stdlibpath)