diff --git a/README.md b/README.md index 370dbf5..83748f0 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,16 @@ wget http://fishros.com/install -O fishros && . fishros ``` +## 如何自动选择 + +目前一键安装支持从配置文件自动输入选项,你需要手动运行一次一键安装,使用完毕后会自动产生 `/tmp/fishros.yaml`。 + +使用下面的指令将配置文件拷贝到当前终端即可。 + +``` +cp /tmp//tmp/fishros.yaml ./ +``` + ## 贡献指南 diff --git a/install.py b/install.py index be2d4e0..dcb9989 100644 --- a/install.py +++ b/install.py @@ -28,6 +28,7 @@ def main(): from tools.base import CmdTask,FileUtils,PrintUtils,ChooseTask from tools.base import encoding_utf8,osversion,osarch from tools.base import run_tool_file,download_tools + from tools.base import config_helper # check base config if not encoding_utf8: @@ -77,6 +78,7 @@ def main(): download_tools(code,tools) run_tool_file(tools[code]['tool'].replace(url_prefix,'').replace("/",".")) + config_helper.gen_config_file() PrintUtils.print_delay("欢迎加入机器人学习交流QQ群:139707339(入群口令:一键安装)",0.1) PrintUtils.print_success("如在使用过程中遇到问题,请打开:https://fishros.org.cn/forum 进行反馈",0.001) diff --git a/tools/base.py b/tools/base.py index fcd7e61..2382f16 100644 --- a/tools/base.py +++ b/tools/base.py @@ -1,17 +1,86 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import print_function +from calendar import c import os import re import sys import time import subprocess import locale +from queue import Queue +import yaml + encoding = locale.getpreferredencoding() encoding_utf8 = encoding.find("UTF")>-1 +class ConfigHelper(): + def __init__(self,record_file=None): + self.record_input_queue = Queue() + + self.record_file = record_file + if self.record_file==None: self.record_file = "./fish_install.yaml" + self.default_input_queue = self.get_default_queue(self.record_file) + + def record_input(self,item): + self.record_input_queue.put(item) + + def gen_config_file(self): + """生产配置文件 + """ + config_yaml = {} + + chooses = [] + + while self.record_input_queue.qsize()>0: + chooses.append(self.record_input_queue.get()) + + config_yaml['chooses'] = chooses + config_yaml['time'] = str(time.time()) + + with open("/tmp/fish_install.yaml", "w", encoding="utf-8") as f: + yaml.dump(config_yaml, f,allow_unicode=True) + + def get_input_value(self): + if self.default_input_queue.qsize()>0: + return self.default_input_queue.get() + + def record_choose(self,data): + self.record_input_queue.put(data) + + def get_default_queue(self,param_file_path): + """获取默认的配置 + + Args: + param_file_path (string, optional): 参数文件路径. Defaults to None. + + Returns: + Queue: 数据队列 + """ + config_data = None + choose_queue = Queue() + + if not os.path.exists(param_file_path): + return choose_queue + + with open(param_file_path,"r",encoding="utf-8") as f: + config_data = f.read() + + if config_data == None: return choose_queue + config_yaml = yaml.load(config_data,Loader=yaml.FullLoader) + for choose in config_yaml['chooses']: + choose_queue.put(choose) + + return choose_queue + +config_helper = ConfigHelper() +# config_helper.record_choose({"choose":1,"desc":"一键安装ROS"}) +# config_helper.record_choose({"choose":2,"desc":"还愿"}) +# print(config_helper.get_input_value()) +# config_helper.gen_config_file() + def GetOsVersion(): """ Library for detecting the current OS, including detecting specific @@ -883,12 +952,23 @@ def __choose(data,tips,array): choose = -1 for key in dic: PrintUtils.print_delay('[{}]:{}'.format(key,dic[key]),0.005) + + choose = None + choose_item = config_helper.get_input_value() + while True: - choose = input("请输入[]内的数字以选择:") + if choose_item: + choose = str(choose_item['choose']) + print("为您从配置文件找到默认选项:",choose_item) + else: + choose = input("请输入[]内的数字以选择:") + choose_item = None + # Input From Queue if choose.isdecimal() : if (int(choose) in dic.keys() ) or (int(choose)==0): choose = int(choose) break + config_helper.record_choose({"choose":choose,"desc":dic[choose]}) PrintUtils.print_fish() return choose,dic[choose] @@ -1105,6 +1185,4 @@ def download_tools(id,tools): - - osarch = AptUtils.getArch() \ No newline at end of file diff --git a/tools/tool_install_ros.py b/tools/tool_install_ros.py index bdfec7f..37fae41 100644 --- a/tools/tool_install_ros.py +++ b/tools/tool_install_ros.py @@ -223,10 +223,18 @@ def choose_and_install_ros(self): for a in dic_base.keys(): ros_name[RosVersions.get_version_string(a)] = a - _,rosname = ChooseTask(ros_name.keys(),"请选择你要安装的ROS版本名称(请注意ROS1和ROS2区别):",True).run() + code,rosname = ChooseTask(ros_name.keys(),"请选择你要安装的ROS版本名称(请注意ROS1和ROS2区别):",True).run() + if code==0: + print("你选择退出。。。。") + return + version_dic = {1:rosname+"桌面版",2:rosname+"基础版(小)"} code,name = ChooseTask(version_dic,"请选择安装的具体版本(如果不知道怎么选,请选1桌面版):",False).run() + if code==0: + print("你选择退出。。。。") + return + install_tool = 'aptitude' if osversion.get_version() == "16.04": install_tool = 'apt'