ros2kit 0.2.1

Rust utility library for ROS 2 workspace management and ament environment discovery
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

use ignore::WalkBuilder;

/// Layout type of a ROS 2 workspace.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WorkspaceLayout {
    /// Standard colcon layout with `src/` containing packages.
    Standard,
    /// Pixi-managed workspace with `pixi.toml` at root. Packages may be anywhere.
    Pixi,
}

/// A ROS 2 workspace directory containing source packages and optionally build artifacts.
#[derive(Debug, Clone)]
pub struct Workspace {
    /// Root directory of the workspace.
    pub root: PathBuf,
}

impl Workspace {
    /// Returns the workspace display name (last path component, or `"."` for cwd).
    pub fn name(&self) -> &str {
        self.root
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or(".")
    }

    /// Detects the workspace layout by checking for `pixi.toml` or `pixi.lock`.
    pub fn layout(&self) -> WorkspaceLayout {
        if self.root.join("pixi.toml").is_file() || self.root.join("pixi.lock").is_file() {
            WorkspaceLayout::Pixi
        } else {
            WorkspaceLayout::Standard
        }
    }

    pub fn is_workspace(&self) -> bool {
        self.root.join("src").is_dir()
            || self.root.join("install").is_dir()
            || self.root.join("pixi.toml").is_file()
    }

    /// Returns `true` if an `install/` directory exists in this workspace.
    pub fn has_install(&self) -> bool {
        self.root.join("install").is_dir()
    }

    /// Returns `true` if the workspace contains source packages.
    ///
    /// For standard workspaces, checks that `src/` exists and is non-empty.
    /// For pixi workspaces, scans for `package.xml` files anywhere under the root.
    pub fn has_source(&self) -> bool {
        match self.layout() {
            WorkspaceLayout::Standard => {
                let src = self.root.join("src");
                if !src.is_dir() {
                    return false;
                }
                if let Ok(mut entries) = std::fs::read_dir(&src) {
                    entries.any(|e| e.is_ok())
                } else {
                    false
                }
            }
            WorkspaceLayout::Pixi => self.package_count() > 0,
        }
    }

    /// Returns `true` if at least one package has been built and installed
    /// (i.e. has an ament index entry under `install/`).
    pub fn is_built(&self) -> bool {
        let install_dir = self.root.join("install");
        if !install_dir.is_dir() {
            return false;
        }
        let Ok(entries) = std::fs::read_dir(&install_dir) else {
            return false;
        };
        entries.flatten().any(|e| {
            let path = e.path();
            path.is_dir()
                && path
                    .join("share")
                    .join("ament_index")
                    .join("resource_index")
                    .join("packages")
                    .is_dir()
        })
    }

    /// Returns `true` if colcon build artifacts exist (`log/` or `build/` directories).
    pub fn has_logs(&self) -> bool {
        self.root.join("log").is_dir() || self.root.join("build").is_dir()
    }

    /// Returns the number of source packages found by scanning for `package.xml` files.
    pub fn package_count(&self) -> usize {
        scan(&self.root).len()
    }

    /// Returns the number of installed packages with an ament index under `install/`.
    pub fn install_package_count(&self) -> usize {
        let install_dir = self.root.join("install");
        if !install_dir.is_dir() {
            return 0;
        }
        let Ok(entries) = std::fs::read_dir(&install_dir) else {
            return 0;
        };
        entries
            .flatten()
            .filter(|e| {
                let path = e.path();
                path.is_dir()
                    && path
                        .join("share")
                        .join("ament_index")
                        .join("resource_index")
                        .join("packages")
                        .is_dir()
            })
            .count()
    }

    /// Returns canonicalized ament prefix paths from `install/` subdirectories.
    pub fn install_prefixes(&self) -> Vec<PathBuf> {
        self.overlay_paths().ament_prefixes
    }

    /// Collects all environment overlay paths from the workspace's `install/` directory.
    ///
    /// Returns ament prefixes, Python site-packages paths, and library paths
    /// needed to overlay this workspace's installed packages onto the environment.
    pub fn overlay_paths(&self) -> OverlayPaths {
        let install_dir = self.root.join("install");
        if !install_dir.is_dir() {
            return OverlayPaths::default();
        }

        let canonical = install_dir
            .canonicalize()
            .unwrap_or_else(|_| install_dir.clone());

        let mut paths = OverlayPaths::default();

        let Ok(entries) = std::fs::read_dir(&canonical) else {
            return paths;
        };

        for entry in entries.flatten() {
            let sub = entry.path();
            if !sub.is_dir() {
                continue;
            }
            if !sub
                .join("share")
                .join("ament_index")
                .join("resource_index")
                .join("packages")
                .is_dir()
            {
                continue;
            }
            collect_prefix_paths(&sub, &mut paths);
        }

        if paths.ament_prefixes.is_empty() {
            collect_prefix_paths(&canonical, &mut paths);
        }

        paths
    }
}

/// Environment paths collected from a workspace's `install/` directory for overlay.
#[derive(Debug, Clone, Default)]
pub struct OverlayPaths {
    /// Ament prefix paths (`AMENT_PREFIX_PATH` entries).
    pub ament_prefixes: Vec<PathBuf>,
    /// Python site-packages paths (`PYTHONPATH` entries).
    pub python_paths: Vec<PathBuf>,
    /// Library paths (`LD_LIBRARY_PATH` / `DYLD_LIBRARY_PATH` entries).
    pub lib_paths: Vec<PathBuf>,
}

fn collect_prefix_paths(prefix: &Path, paths: &mut OverlayPaths) {
    paths.ament_prefixes.push(prefix.to_path_buf());
    let lib_dir = prefix.join("lib");
    if !lib_dir.is_dir() {
        return;
    }
    paths.lib_paths.push(lib_dir.clone());
    for py_dir in &["python3.12", "python3.11", "python3.10", "python3.9"] {
        let site_packages = lib_dir.join(py_dir).join("site-packages");
        if site_packages.is_dir() {
            paths.python_paths.push(site_packages.clone());
            if let Ok(entries) = std::fs::read_dir(&site_packages) {
                for entry in entries.flatten() {
                    if entry.file_name().to_string_lossy().ends_with(".egg-link")
                        && let Ok(content) = std::fs::read_to_string(entry.path())
                        && let Some(line) = content.lines().next()
                    {
                        let target = PathBuf::from(line.trim());
                        if target.is_dir() {
                            paths.python_paths.push(target);
                        }
                    }
                }
            }
            break;
        }
    }
}

/// Build system type declared in a package's `package.xml` under `<export><build_type>`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PackageBuildType {
    AmentCmake,
    AmentPython,
    Cmake,
    AmentCargo,
    Unknown,
}

/// A ROS 2 source package discovered by scanning a workspace for `package.xml` files.
#[derive(Debug, Clone)]
pub struct WorkspacePackage {
    /// Package name from `<name>` in `package.xml`.
    pub name: String,
    /// Package version from `<version>` in `package.xml`.
    pub version: String,
    /// Package description from `<description>` in `package.xml`.
    pub description: String,
    /// Path to the package directory (parent of `package.xml`).
    pub path: PathBuf,
    /// The workspace this package belongs to.
    pub workspace: Arc<Workspace>,
    /// Build system type from `<export><build_type>` in `package.xml`.
    pub build_type: PackageBuildType,
    /// Deduplicated, sorted list of all dependency package names from `package.xml`.
    pub dependencies: Vec<String>,
}

/// Build state of a workspace package relative to its install directory.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PackageBuildStatus {
    /// No ament index entry exists in `install/`.
    NotBuilt,
    /// Installed and up-to-date (install marker is newer than all source files).
    Built,
    /// Source files have been modified since the last build.
    Dirty,
}

impl WorkspacePackage {
    /// Returns the path to this package's `package.xml`.
    pub fn package_xml(&self) -> PathBuf {
        self.path.join("package.xml")
    }

    /// Returns the path to `CMakeLists.txt` if it exists in the package directory.
    pub fn cmake_lists(&self) -> Option<PathBuf> {
        let path = self.path.join("CMakeLists.txt");
        if path.is_file() { Some(path) } else { None }
    }

    /// Returns the path to `setup.py` if it exists in the package directory.
    pub fn setup_py(&self) -> Option<PathBuf> {
        let path = self.path.join("setup.py");
        if path.is_file() { Some(path) } else { None }
    }

    /// Returns the path to `Cargo.toml` if it exists in the package directory.
    pub fn cargo_toml(&self) -> Option<PathBuf> {
        let path = self.path.join("Cargo.toml");
        if path.is_file() { Some(path) } else { None }
    }

    pub fn latest_build_log(&self) -> Option<String> {
        let log_file = self
            .workspace
            .root
            .join("log")
            .join("latest_build")
            .join(&self.name)
            .join("stdout_stderr.log");
        std::fs::read_to_string(&log_file).ok()
    }

    pub fn build_status(&self) -> PackageBuildStatus {
        let build_marker = self
            .workspace
            .root
            .join("build")
            .join(&self.name)
            .join("colcon_build.rc");

        if !build_marker.is_file() {
            return PackageBuildStatus::NotBuilt;
        }

        let rc = std::fs::read_to_string(&build_marker).unwrap_or_default();
        if rc.trim() != "0" {
            return PackageBuildStatus::Dirty;
        }

        let build_mtime = build_marker.metadata().and_then(|m| m.modified()).ok();

        let source_mtime = newest_source_mtime(&self.path);

        match (source_mtime, build_mtime) {
            (Some(src), Some(built)) if src > built => PackageBuildStatus::Dirty,
            _ => PackageBuildStatus::Built,
        }
    }
}

fn newest_source_mtime(dir: &Path) -> Option<std::time::SystemTime> {
    let mut newest = None;

    let walker = WalkBuilder::new(dir)
        .hidden(true)
        .git_ignore(true)
        .git_global(false)
        .git_exclude(true)
        .build();

    for entry in walker.flatten() {
        if !entry.file_type().is_some_and(|ft| ft.is_file()) {
            continue;
        }
        if let Ok(meta) = entry.path().metadata()
            && let Ok(mtime) = meta.modified()
        {
            newest = Some(match newest {
                Some(current) if mtime > current => mtime,
                Some(current) => current,
                None => mtime,
            });
        }
    }

    newest
}

/// Scans a single directory for ROS 2 source packages. Delegates to [`scan_multiple`].
pub fn scan(root: &Path) -> Vec<WorkspacePackage> {
    scan_multiple(&[root.to_path_buf()])
}

/// Scans multiple workspace directories for ROS 2 source packages in parallel.
///
/// Uses a single `ignore` walker across all roots. Duplicate roots (by canonical path)
/// are skipped. Results are sorted alphabetically by package name.
pub fn scan_multiple(roots: &[PathBuf]) -> Vec<WorkspacePackage> {
    if roots.is_empty() {
        return Vec::new();
    }

    let mut seen = HashSet::new();
    let workspaces: Vec<Arc<Workspace>> = roots
        .iter()
        .filter(|r| {
            let canonical = r.canonicalize().unwrap_or_else(|_| r.to_path_buf());
            seen.insert(canonical)
        })
        .map(|r| Arc::new(Workspace { root: r.clone() }))
        .collect();

    if workspaces.is_empty() {
        return Vec::new();
    }

    let packages = Mutex::new(Vec::new());

    let mut builder = WalkBuilder::new(&workspaces[0].root);
    for ws in &workspaces[1..] {
        builder.add(&ws.root);
    }

    builder
        .hidden(true)
        .follow_links(true)
        .git_ignore(true)
        .git_global(false)
        .git_exclude(true)
        .filter_entry(|entry| {
            if !entry.file_type().is_some_and(|ft| ft.is_dir()) {
                return true;
            }
            let name = entry.file_name().to_string_lossy();
            !matches!(
                name.as_ref(),
                "target" | "build" | "install" | "log" | "node_modules"
            )
        })
        .build_parallel()
        .run(|| {
            Box::new(|entry| {
                let entry = match entry {
                    Ok(e) => e,
                    Err(_) => return ignore::WalkState::Continue,
                };

                if !entry.file_type().is_some_and(|ft| ft.is_file()) {
                    return ignore::WalkState::Continue;
                }

                if entry.file_name() != "package.xml" {
                    return ignore::WalkState::Continue;
                }

                let path = entry.path();
                if let Some(dir) = path.parent()
                    && let Some(info) = parse_package_xml(path)
                {
                    let workspace = workspaces
                        .iter()
                        .find(|ws| path.starts_with(&ws.root))
                        .cloned()
                        .unwrap_or_else(|| {
                            Arc::new(Workspace {
                                root: dir.to_path_buf(),
                            })
                        });
                    packages.lock().unwrap().push(WorkspacePackage {
                        name: info.name,
                        version: info.version,
                        description: info.description,
                        path: dir.to_path_buf(),
                        workspace,
                        build_type: info.build_type,
                        dependencies: info.dependencies,
                    });
                }

                ignore::WalkState::Continue
            })
        });

    let mut packages = packages.into_inner().unwrap();
    packages.sort_by(|a, b| a.name.cmp(&b.name));
    packages
}

struct PackageXmlInfo {
    name: String,
    version: String,
    description: String,
    build_type: PackageBuildType,
    dependencies: Vec<String>,
}

const DEP_TAGS: &[&str] = &[
    "depend",
    "build_depend",
    "build_export_depend",
    "exec_depend",
    "buildtool_depend",
    "test_depend",
];

fn parse_package_xml(package_xml: &Path) -> Option<PackageXmlInfo> {
    use quick_xml::events::Event;
    use quick_xml::reader::Reader;

    let content = std::fs::read_to_string(package_xml).ok()?;
    let mut reader = Reader::from_str(&content);

    let mut name = None;
    let mut version = None;
    let mut description = None;
    let mut build_type = None;
    let mut dependencies = Vec::new();
    let mut seen_deps = HashSet::new();
    let mut in_export = false;
    let mut current_tag = String::new();

    loop {
        match reader.read_event() {
            Ok(Event::Start(e)) => {
                let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
                if tag == "export" {
                    in_export = true;
                }
                current_tag = tag;
            }
            Ok(Event::End(e)) => {
                if e.name().as_ref() == b"export" {
                    in_export = false;
                }
                current_tag.clear();
            }
            Ok(Event::Text(e)) => {
                let text = e.unescape().ok();
                match current_tag.as_str() {
                    "name" if name.is_none() => {
                        name = text.map(|t| t.trim().to_string());
                    }
                    "version" if version.is_none() => {
                        version = text.map(|t| t.trim().to_string());
                    }
                    "description" if description.is_none() => {
                        description = text.map(|t| t.trim().to_string());
                    }
                    "build_type" if in_export => {
                        build_type = text.map(|t| match t.trim() {
                            "ament_cmake" => PackageBuildType::AmentCmake,
                            "ament_python" => PackageBuildType::AmentPython,
                            "cmake" => PackageBuildType::Cmake,
                            "ament_cargo" => PackageBuildType::AmentCargo,
                            _ => PackageBuildType::Unknown,
                        });
                    }
                    tag if DEP_TAGS.contains(&tag) => {
                        if let Some(dep) = text.map(|t| t.trim().to_string())
                            && !dep.is_empty()
                            && seen_deps.insert(dep.clone())
                        {
                            dependencies.push(dep);
                        }
                    }
                    _ => {}
                }
            }
            Ok(Event::Eof) => break,
            Err(_) => return None,
            _ => {}
        }
    }

    dependencies.sort();

    Some(PackageXmlInfo {
        name: name?,
        version: version.unwrap_or_default(),
        description: description.unwrap_or_default(),
        build_type: build_type.unwrap_or(PackageBuildType::Unknown),
        dependencies,
    })
}