tiny.smg.scan.reconcile.scope
Defined in scan.reconcile.
API (2)
Actions
Public operations.
Types and contracts
Public types and contracts.
Source
Source: tools/smg/src/scan/reconcile/root.zig:1
zig
pub const scope = @import("scope.zig");Source: tools/smg/src/scan/reconcile/scope.zig
zig
const std = @import("std");const smg = @import("../../root.zig");const graph_mod = smg.graph;const model = smg.model;const scan_root = smg.scan;const core = scan_root.core;const scan_files = scan_root.scan_files;pub const Scope = struct { files: []const []const u8, trees: []const []const u8, exact: []const []const u8,};pub fn collect(allocator: std.mem.Allocator, graph: graph_mod.Graph, root: []const u8, paths: []const []const u8, options: core.Options) !Scope { var files: std.ArrayList([]const u8) = .empty; var trees: std.ArrayList([]const u8) = .empty; var exact: std.ArrayList([]const u8) = .empty; var marker_changes: std.ArrayList([]const u8) = .empty; if (paths.len == 0) { try trees.append(allocator, "."); try scan_files.collectSupported( allocator, root, root, &files, options.excludes, options.limits.scan, ); } else { for (paths) |path| { const stat = scan_files.statPath(path) catch |err| switch (err) { error.FileNotFound, error.NotDir => { const rel = try scan_files.relativePath(allocator, root, path); if (scan_files.supported(path) and !try scan_files.shouldSkip(allocator, root, path, options.excludes)) { try scan_files.appendUnique(allocator, &exact, rel); try trees.append(allocator, try treePrefix(allocator, rel)); } else if (!try scan_files.shouldSkip(allocator, root, path, options.excludes)) { try trees.append(allocator, try treePrefix(allocator, rel)); } if (packageMarker(rel)) try scan_files.appendUnique(allocator, &marker_changes, rel); continue; }, else => return err, }; const rel = try scan_files.relativePath(allocator, root, path); if (stat.kind == .directory) { if (scan_files.supported(path) and !try scan_files.shouldSkip(allocator, root, path, options.excludes)) try scan_files.appendUnique(allocator, &exact, rel); try trees.append(allocator, try treePrefix(allocator, rel)); try scan_files.collectSupported( allocator, root, path, &files, options.excludes, options.limits.scan, ); } else { try trees.append(allocator, try treePrefix(allocator, rel)); if (scan_files.supported(path) and !try scan_files.shouldSkip(allocator, root, path, options.excludes)) { if (stat.kind == .file) { try scan_files.appendUnique(allocator, &files, rel); } else { try scan_files.appendUnique(allocator, &exact, rel); } } } if (packageMarker(rel)) try scan_files.appendUnique(allocator, &marker_changes, rel); } } for (marker_changes.items) |marker| { const parent = std.fs.path.dirname(marker) orelse "."; const prefix = try treePrefix(allocator, parent); try scan_files.appendUnique(allocator, &trees, prefix); const parent_path = if (std.mem.eql(u8, parent, ".")) root else try std.fs.path.join(allocator, &.{ root, parent }); try scan_files.collectSupported( allocator, root, parent_path, &files, options.excludes, options.limits.scan, ); } try addModuleIdentityReplacements(allocator, graph, root, files.items, &exact); std.mem.sort([]const u8, files.items, {}, scan_files.lessThan); std.mem.sort([]const u8, exact.items, {}, scan_files.lessThan); return .{ .files = try files.toOwnedSlice(allocator), .trees = try trees.toOwnedSlice(allocator), .exact = try exact.toOwnedSlice(allocator), };}fn treePrefix(allocator: std.mem.Allocator, rel_raw: []const u8) ![]const u8 { const rel = std.mem.trimEnd(u8, rel_raw, "/"); if (rel.len == 0 or std.mem.eql(u8, rel, ".")) return "."; return try std.fmt.allocPrint(allocator, "{s}/", .{rel});}fn packageMarker(file: []const u8) bool { return std.mem.eql(u8, std.fs.path.basename(file), "__init__.py");}fn addModuleIdentityReplacements( allocator: std.mem.Allocator, graph: graph_mod.Graph, root: []const u8, files: []const []const u8, exact: *std.ArrayList([]const u8),) !void { var live = std.StringHashMap(void).init(allocator); defer live.deinit(); for (files) |file| try live.put(file, {}); var expected = std.StringHashMap([]const u8).init(allocator); defer expected.deinit(); for (graph.nodes.items) |node| { if (!graph_mod.isScan(node.metadata)) continue; if (!std.mem.eql(u8, node.type, model.NodeType.module) and !std.mem.eql(u8, node.type, model.NodeType.package)) continue; const file = node.file orelse continue; if (!live.contains(file)) continue; if (nestedFileModule(graph, node)) continue; const entry = try expected.getOrPut(file); if (!entry.found_existing) entry.value_ptr.* = try scan_files.moduleName(allocator, root, file); if (!std.mem.eql(u8, node.name, entry.value_ptr.*)) try scan_files.appendUnique(allocator, exact, file); }}fn nestedFileModule(graph: graph_mod.Graph, node: model.Node) bool { const file = node.file orelse return false; for (graph_mod.incomingEdges(&graph, node.name)) |edge_index| { const edge = graph.edges.items[edge_index]; if (!std.mem.eql(u8, edge.rel, model.RelType.contains) or !graph_mod.isScan(edge.metadata)) continue; const parent = graph_mod.getNode(&graph, edge.source) orelse continue; if (graph_mod.isScan(parent.metadata) and parent.file != null and std.mem.eql(u8, parent.file.?, file)) return true; } return false;}Audit
| Definitions | 3 |
|---|---|
| Public names | 3 |
| Members | 3 |
| Version | 26.7.0 |
| Revision | daab053ee433 |