datetime:2024/11/19 15:12
author:nzb

ROS2源码加密混淆

  • 安装 Cython
pip install Cython
  • 创建ros1工作空间和包
mkdir -p ~/ros2_ws/src
cd ~/ros2_ws  /src
ros2 pkg create --build-type ament_python ros2_cythonize_demo
cd ros2_cythonize_demo 
touch ros2_cythonize_demo/py_node.py
  • 文件结构
ros2_cythonize_demo/
├── package.xml
├── resource
│   └── ros2_cythonize_demo
├── ros2_cythonize_demo
│   ├── __init__.py
│   └── py_node.py
├── setup.cfg
├── setup.py
└── test
    ├── test_copyright.py
    ├── test_flake8.py
    └── test_pep257.py

3 directories, 9 files
  • setup.py
from Cython.Build import cythonize
from setuptools import find_packages, setup

package_name = 'ros2_cythonize_demo'

files = package_name + "/*.py"

setup(
    ext_modules=cythonize(files, compiler_directives={'language_level': "3"}, force=True, quiet=True),
    name=package_name,
    version='0.0.0',
    packages=find_packages(exclude=['test']),
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
    ],
    install_requires=['setuptools', "wheel",  "Cython"],
    zip_safe=True,
    maintainer='blues',
    maintainer_email='nzbwork@gmail.com',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            "ros2_cythonize_demo = ros2_cythonize_demo.py_node:main"
        ],
    },
)
  • py_node.py
#! /usr/bin/env python3
import rclpy
import rclpy.logging
from rclpy.node import Node

"""
tar -zcvf install.tar.gz --exclude=**/lib/*.py install/
"""


class LogNode(Node):
    def __init__(self, name):
        super().__init__(name)
        # self.get_logger().set_level(rclpy.logging.LoggingSeverity.DEBUG)
        self.get_logger().info("init LogNode")
        self.create_timer(0.001, self.logger_cb)

    def logger_cb(self):
        self.get_logger().info("hello world [info]")
        self.get_logger().debug("hello world [debug]")


def main(args=None):
    rclpy.init(args=args)

    node = LogNode(name='node')
    # 可以在这里添加节点的逻辑
    rclpy.spin(node)
    node.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

迁移

  • cd ~/ros2_ws # 项目路径假设在~/ros2_ws
  • colcon build
  • tar -zcvf ros2_code.tar.gz --exclude=**/lib/*.py install/ # 排除源码
  • 压缩包上传新机器
    • mkdir -p ~/ros2_ws/ && cd ros2_ws
    • 压缩包解压到~/ros2_ws
    • source /opt/ros/humble/setup.bash
    • source install/setup.bash
    • ros2 run ros2_cythonize_demo ros2_cythonize_demo

ros1 闭源

results matching ""

    No results matching ""