def check_cpp(argv, prog): CMD_TPL1='for d in {{dirs}};do find $d -name "*.cc" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" -o -name "*.c" | xargs -I {} cpplint {}; done' CMD_TPL2='for d in {{dirs}};do find $d -name "*.cc" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" -o -name "*.c" | xargs -I {} cpplint {} >> {{out}}/cpplint.txt 2>&1;done' argparser = argparse.ArgumentParser(description="octopus ", prog=prog) argparser.add_argument('-o', dest='output_dir', help='文件输出目录', default='') argparser.add_argument( 'dirs', metavar='N', nargs='*', help='要检查的目录') args = argparser.parse_args(argv) if args.output_dir: local('[ -d {out} ] || mkdir -p {out}'.format(out=args.output_dir)) local('if [ -f {out}/cpplint.txt ];then rm -f {out}/cpplint.txt; fi'.format( out=args.output_dir)) cmd = Template(CMD_TPL2.decode('utf-8')); cmd = cmd.render(out=args.output_dir, dirs=' '.join(args.dirs)) cmd = cmd.encode('utf-8') pyshell.shell(cmd, warn_only=True) converter = CpplintConverter() converter.parse(args.output_dir+'/cpplint.txt') converter.write(args.output_dir+'/cpplint.xml') else: cmd = Template(CMD_TPL1.decode('utf-8')); cmd = cmd.render(out=args.output_dir, dirs=' '.join(args.dirs)) cmd = cmd.encode('utf-8') pyshell.shell(cmd, warn_only=True)
def set_user(host, user): """ 添加或者修改用户信息""" cmd = 'ssh -t {host} "sudo /usr/sbin/useradd {name}"'.format( host=host, name=user['name']) pyshell.shell(cmd) if user['passwd']: change_passwd(host, user['name'], user['passwd']) cmd_add_group = 'sudo /usr/sbin/groupadd {group}; sudo /usr/sbin/usermod -a -G {group} {name};' cmd = '' if user['groups']: for group in user['groups']: cmd = cmd + cmd_add_group.format(group = group, name=user['name']) cmd = 'ssh -t {host} "' + cmd + '"' cmd = cmd.format(host = host) pyshell.shell(cmd)
def info(self): pret = shell('LANG=en_US svn info', timeout=10, capture=True) result = {} for line in pret.stdout.split('\n'): line = line.rstrip() parts = line.split(':', 1) if len(parts) == 2: if parts[0] == 'Revision': result['revision'] = int(parts[1]) elif parts[0] == 'URL': result['svn'] = parts[1] return result
def run(): """控制脚本的执行 Args: Returns: Raises: KeyError: 如果配置文件没有设置会抛异常 """ use_env = options.use_env if use_env: fp = open("ruce.conf.yml.tmp", "w") with open("ruce.conf.yml") as conf_file: config = yaml.load(conf_file) if use_env in config["env"]: config["env"]["use_env"] = use_env yaml.dump(config, stream=fp, default_flow_style=False) fp.close() ret = shell("mv ruce.conf.yml.tmp ruce.conf.yml", capture=False) return current_dir = os.path.dirname(os.path.abspath(__file__)) + "/tpls" j2_env = Environment(loader=FileSystemLoader(current_dir), trim_blocks=True) case_name = options.gen_tpl if case_name: try: if os.path.exists("test_{}.py".format(case_name)): print "File test_{}.py has already existed".format(case_name) return new_case = j2_env.get_template("test_basic.tpl").render(name=case_name) create_file = open("test_{}.py".format(case_name), "w") create_file.write(new_case) create_file.close() print "create test_{}.py ok".format(case_name) except Exception as e: print ("gen_tpl params error\n" "--gen_tpl=case_name") return if options.gen_conf: try: env_name, host_port = options.gen_conf.split("=") host, port = host_port.split(":") new_conf = j2_env.get_template("basic_conf.tpl").render(env_name=env_name, host=host, port=port) create_file = open("ruce.conf.yml", "w") create_file.write(new_conf) create_file.close() print "create ruce.conf.yml ok" except Exception as e: print ("gen_conf params error\n" "--gen_conf='env_name=host:port'") return if options.name != "all": name_list = options.name.split(",") for name in name_list: file_name = "test_{}.py".format(name.strip()) if os.path.exists(file_name): ret = shell(r"python {}".format(file_name), capture=True, debug=False) print ret.stdout print ret.stderr else: result_record = defaultdict(dict) name_list = os.listdir(".") print separator for name in name_list: if name.startswith("test_") and name.endswith(".py"): ret = shell(r"python {}".format(name), capture=True, debug=False) output = ret.stdout + ret.stderr parsed_result = parse_case(output) if parsed_result["passed"]: print_success("{} ---- passed".format(name)) else: print_error("{} ---- failed".format(name)) print "\r" result_record[name] = parsed_result parse_record(result_record) return