修掉两个"存在但跑不起来"的脚本:CRLF 行尾 + 新增 .gitattributes 统一为 LF

症状(没有报错,只有"文件在、文档说能用、一跑就死")
    $ bash scripts/run_hole_detect.sh --arm 89 --preview
    scripts/run_hole_detect.sh: 行 11: $'\r': 未找到命令
    scripts/run_hole_detect.sh: 第 12 行: set: pipefail: 无效的选项名
  另一处 $'\r': 未找到命令 来自 scripts/run_dual_gripper.sh(同样症状)。

根因
  这两个 .sh 是 100% CRLF 行尾。bash 会把行尾的 \r 当成标识符的一部分,
  于是 `case "$1" in\r`、`set -eo pipefail\r` 全部变成非法记号。
  而文档在五处告诉用户跑它:README.md、docs/WORKSPACE.md、docs/ARCHITECTURE.md、
  docs/SITE_CONFIG.md、docs/HOLE_DETECT.md(HOLE_DETECT.md 直接给了命令)。
  即"文档齐全、文件存在、命令一敲就死",报错信息里的那个 \r 又完全看不出来。

为什么之前没人发现(这是本次真正的教训)
  bash -n 会漏报:对 run_dual_gripper.sh 它返回 OK,可实际执行立刻死在第 6 行。
  所以"静态检查通过"不能当成"脚本能用"。验脚本要么真跑 --help,要么用
  file/bat 看行尾。这条已写进 .gitattributes 的注释里。

改了什么
  1. scripts/run_hole_detect.sh、scripts/run_dual_gripper.sh 由 CRLF 改为 LF。
     去行尾符后与改前逐字节相同 —— 只动行尾,一个字符内容都没动。
     已验证:两个脚本 bash -n 通过,且 run_hole_detect.sh --help 能正常打印中文帮助。
  2. 新增 .gitattributes:
       * text=auto eol=lf      —— 本仓库自己的文件一律 LF
       /src/flexiv_*/** -text  —— 厂商上游代码内容与行尾都不动(R4.4)
       /thirdparty/** -text
       *.onnx/*.engine/*.trt 等二进制显式标 binary,避免被当成文本转换
  3. 顺带把本仓库其余 19 个仍是 CRLF 的文件(.py 9 个、.yaml 2 个、.launch.py 2 个、
     .msg 5 个、.srv 1 个)统一为 LF。这些文件在 Python/YAML 下本来能正常读,
     所以此前没有症状;但同一仓库混着两种行尾,下次再有一个 .sh 中招就是必然。
     已核对:去掉行尾符后与 HEAD 逐字节相同 —— 内容零变化。

验收
  21 个文件:git diff --ignore-cr-at-eol -w 只列出 .gitattributes(零内容变化)
  scripts/*.sh scripts/lib/*.sh:bash -n 失败数 0(改前是 2)
  单测:608 passed,与改动前一致

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
arm_dev
2026-09-23 17:41:06 +08:00
parent 72353f3773
commit 806efe4df1
22 changed files with 2382 additions and 2345 deletions
@@ -1,70 +1,70 @@
#!/usr/bin/env python3
"""在当前机械臂的彩色话题上启动 hole_detect。"""
from __future__ import annotations
import os
from pathlib import Path
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, LogInfo, OpaqueFunction
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node
def _ws_root() -> Path:
for key in ("ROS2_WS", "SITE_ROS2_ROOT"):
v = os.environ.get(key, "").strip()
if v:
return Path(v)
return Path.home() / "ros2"
def _setup(context, *args, **kwargs):
root = _ws_root()
backend = LaunchConfiguration("backend").perform(context) or os.environ.get("SITE_HOLE_BACKEND") or "two_stage"
weights = LaunchConfiguration("weights").perform(context) or os.environ.get("SITE_HOLE_WEIGHTS") or ""
cfg = LaunchConfiguration("config_file").perform(context) or os.environ.get("SITE_HOLE_CONFIG") or ""
cam = os.environ.get("SITE_CAM_NAME") or "d405"
preview = LaunchConfiguration("preview").perform(context).lower() in ("1", "true", "yes")
if not cfg:
if backend in ("two_stage", "hough_roi"):
cfg = str(root / "src" / "arm_application" / "config" / "hole_detect.yaml")
else:
cfg = str(root / "src" / "arm_application" / "config" / "hole_insert.yaml")
if not weights:
if backend in ("two_stage", "hough_roi"):
weights = os.environ.get("SITE_HOLE_WORKPIECE_WEIGHTS") or os.environ.get("SITE_YOLO_WEIGHTS") or ""
else:
weights = os.environ.get("SITE_HOLE_WEIGHTS") or str(root / "models" / "hole_insert" / "detect_best_1.onnx")
return [
LogInfo(msg=f"[hole_detect] cam={cam} backend={backend} weights={weights} cfg={cfg}"),
Node(
package="arm_application",
executable="hole_detect",
name="hole_detect",
output="screen",
parameters=[
{
"camera_source": cam,
"weights": weights,
"config_file": cfg,
"backend": backend,
"device": os.environ.get("SITE_YOLO_DEVICE") or "0",
"preview": preview,
}
],
),
]
def generate_launch_description() -> LaunchDescription:
return LaunchDescription(
[
DeclareLaunchArgument("backend", default_value=""),
DeclareLaunchArgument("weights", default_value=""),
DeclareLaunchArgument("config_file", default_value=""),
DeclareLaunchArgument("preview", default_value="false"),
OpaqueFunction(function=_setup),
]
)
#!/usr/bin/env python3
"""在当前机械臂的彩色话题上启动 hole_detect。"""
from __future__ import annotations
import os
from pathlib import Path
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, LogInfo, OpaqueFunction
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node
def _ws_root() -> Path:
for key in ("ROS2_WS", "SITE_ROS2_ROOT"):
v = os.environ.get(key, "").strip()
if v:
return Path(v)
return Path.home() / "ros2"
def _setup(context, *args, **kwargs):
root = _ws_root()
backend = LaunchConfiguration("backend").perform(context) or os.environ.get("SITE_HOLE_BACKEND") or "two_stage"
weights = LaunchConfiguration("weights").perform(context) or os.environ.get("SITE_HOLE_WEIGHTS") or ""
cfg = LaunchConfiguration("config_file").perform(context) or os.environ.get("SITE_HOLE_CONFIG") or ""
cam = os.environ.get("SITE_CAM_NAME") or "d405"
preview = LaunchConfiguration("preview").perform(context).lower() in ("1", "true", "yes")
if not cfg:
if backend in ("two_stage", "hough_roi"):
cfg = str(root / "src" / "arm_application" / "config" / "hole_detect.yaml")
else:
cfg = str(root / "src" / "arm_application" / "config" / "hole_insert.yaml")
if not weights:
if backend in ("two_stage", "hough_roi"):
weights = os.environ.get("SITE_HOLE_WORKPIECE_WEIGHTS") or os.environ.get("SITE_YOLO_WEIGHTS") or ""
else:
weights = os.environ.get("SITE_HOLE_WEIGHTS") or str(root / "models" / "hole_insert" / "detect_best_1.onnx")
return [
LogInfo(msg=f"[hole_detect] cam={cam} backend={backend} weights={weights} cfg={cfg}"),
Node(
package="arm_application",
executable="hole_detect",
name="hole_detect",
output="screen",
parameters=[
{
"camera_source": cam,
"weights": weights,
"config_file": cfg,
"backend": backend,
"device": os.environ.get("SITE_YOLO_DEVICE") or "0",
"preview": preview,
}
],
),
]
def generate_launch_description() -> LaunchDescription:
return LaunchDescription(
[
DeclareLaunchArgument("backend", default_value=""),
DeclareLaunchArgument("weights", default_value=""),
DeclareLaunchArgument("config_file", default_value=""),
DeclareLaunchArgument("preview", default_value="false"),
OpaqueFunction(function=_setup),
]
)