Skip to content

Commit

Permalink
feat: 优化用户检测方法,支持多目录
Browse files Browse the repository at this point in the history
  • Loading branch information
fishros committed Sep 6, 2024
1 parent 38a2086 commit 59a9928
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
25 changes: 16 additions & 9 deletions tools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1174,16 +1174,23 @@ def exists(path):
@staticmethod
def getusers():
"""
优先home,没有home提供root
优先获取有home目录的普通用户, 没有普通用户则返回root
"""
users = CmdTask("users", 0).run()
if users[0]!=0: return ['root']
# TODO 使用ls再次获取用户名
users = users[1][0].split(" ")
if len(users[0])==0:
user = input(tr.tr("请手动输入你的用户名>>"))
users.clear()
users.append(user)
users = []

# 遍历 /etc/passwd 文件来获取用户名和UID
with open('/etc/passwd', 'r') as passwd_file:
for line in passwd_file:
user_info = line.split(':')
username = user_info[0]
home_dir = user_info[5]
uid = int(user_info[2])

# 过滤出有home目录且UID大于等于1000的普通用户
if home_dir.startswith('/home') and uid >= 1000:
users.append(username)

users.append('root')
return users

@staticmethod
Expand Down
45 changes: 24 additions & 21 deletions tools/tool_config_rosenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from .base import PrintUtils,CmdTask,FileUtils,AptUtils,ChooseTask
from .base import osversion
from .base import run_tool_file
import os

class Tool(BaseTool):
def __init__(self):
self.type = BaseTool.TYPE_CONFIG
self.name = "一键配置ROS开发环境"
self.author = '小鱼'

def config_rosenv(self):
shell = FileUtils.get_shell()

Expand All @@ -19,32 +21,33 @@ def get_source_command(dic):
for i in range(len(dic)):
count += 1
choose += "{}) source {};;\n".format(count,dic[i])
tips += dic[i].replace("/opt/ros/","").replace(f"/setup.{shell}","")+"("+str(count)+") "
tips += dic[i].replace("/opt/ros/","").replace("/setup.{}".format(shell),"")+"("+str(count)+") "
return choose.replace('<tips>', tips)+"esac"

# check and append source
result = CmdTask(f"ls /opt/ros/*/setup.{shell}", 0).run()
shellrc_result = CmdTask(f"ls /home/*/.{shell}rc", 0).run()
if shellrc_result[0]!=0: shellrc_result = CmdTask(f"ls /root/.{shell}rc", 0).run()
if len(result[1])>1:
PrintUtils.print_info(f'检测到系统有多个ROS环境,已为您配置未启动终端选择,修改~/.{shell}rc可关闭')
data = get_source_command(result[1])
for shellrc in shellrc_result[1]:
FileUtils.find_replace(shellrc,f"source\s+/opt/ros/[A-Za-z]+/setup.{shell}","")
FileUtils.find_replace_sub(shellrc,"# >>> fishros initialize >>>","# <<< fishros initialize <<<", "")
FileUtils.append(shellrc,"# >>> fishros initialize >>>\n"+data+"\n# <<< fishros initialize <<<\n")
return True
elif len(result[1])==1 and len(result[1][0])>2:
PrintUtils.print_info(f'检测到系统有1个ROS环境,已为您配置未启动终端自动激活,修改~/.{shell}rc可关闭')
for shellrc in shellrc_result[1]:
FileUtils.find_replace(shellrc,f"source\s+/opt/ros/[A-Za-z]+/setup.{shell}","")
FileUtils.find_replace_sub(shellrc,"# >>> fishros initialize >>>","# <<< fishros initialize <<<", "")
FileUtils.append(shellrc,'# >>> fishros initialize >>>\n source {} \n# <<< fishros initialize <<<\n'.format(result[1][0]))
return True
else:
result = CmdTask("ls /opt/ros/*/setup.{}".format(shell), 0).run()
ros_count = len(result[1])
ros_sourcefile = result[1]
if ros_count==0:
PrintUtils.print_error("当前系统并没有安装ROS,请使用一键安装安装~")
return False


usershomes = FileUtils.getusershome()
shell_file = ".{}rc".format(shell)
for userhome in usershomes:
shell_path = os.path.join(userhome,shell_file)
PrintUtils.print_delay('正在准备配置用户目录:{}'.format(userhome))
if FileUtils.exists(shell_path):
if ros_count>1:
PrintUtils.print_info('当前系统包含{}个ROS,已为您完成启动终端自动激活ROS环境,修改{}可关闭'.format(ros_count,shell_path))
if ros_count>1:
data = get_source_command(ros_sourcefile)
else:
data = 'source {}'.format(ros_sourcefile[0])
FileUtils.find_replace(shell_path,"source\s+/opt/ros/[A-Za-z]+/setup.{}".format(shell),"") # 删除旧source
FileUtils.find_replace_sub(shell_path,"# >>> fishros initialize >>>","# <<< fishros initialize <<<", "") # 替换
FileUtils.append(shell_path,"# >>> fishros initialize >>>\n"+data+"\n# <<< fishros initialize <<<\n") # 添加
PrintUtils.print_text("")

def run(self):
self.config_rosenv()
5 changes: 3 additions & 2 deletions tools/tool_install_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ def install_docker(self):

# Post-installation steps
CmdTask('sudo groupadd docker', 10,os_command=True).run()
user = FileUtils.getusers()[0]
CmdTask('sudo gpasswd -a {} docker'.format(user), 10,os_command=True).run()
users = FileUtils.getusers()
for user in users:
CmdTask('sudo gpasswd -a {} docker'.format(user), 10,os_command=True).run()

PrintUtils.print_info("安装完成,接下来你可以尝试使用docker --version指令测试是有正常回显~")

Expand Down

0 comments on commit 59a9928

Please sign in to comment.