当前位置: 首页>>代码示例>>Python>>正文


Python File.append方法代码示例

本文整理汇总了Python中core.osutils.file.File.append方法的典型用法代码示例。如果您正苦于以下问题:Python File.append方法的具体用法?Python File.append怎么用?Python File.append使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在core.osutils.file.File的用法示例。


在下文中一共展示了File.append方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: run

# 需要导入模块: from core.osutils.file import File [as 别名]
# 或者: from core.osutils.file.File import append [as 别名]
def run(command, timeout=COMMAND_TIMEOUT, output=True, wait=True, log_level=CommandLogLevel.FULL):
    """
    Execute command in shell.
    :param command: Command to be executed.
    :param timeout: Timeout for command execution.
    :param output:
    :param wait: Specify if method should wait until command execution complete.
    :param log_level: CommandLogLevel value (SILENT, COMMAND_ONLY, FULL).
    :return: If wait=True return output of the command, else return path to file where command writes log.
    """

    def fork_it():
        """
        This function will be emulate in a parallel thread.

        You can redirect the output to one place and the errors to another:
        # dir file.xxx > output.msg 2> output.err

        You can print the errors and standard output to a single file
        by using the "&1" command to redirect the output for STDERR to STDOUT
        and then sending the output from STDOUT to a file:
        # dir file.xxx 1> output.msg 2>&1
        """

        # execute command
        # print "Thread started"
        if output:
            os.system(command + ' 1> ' + out_file + ' 2>&1')
        else:
            os.system(command)

    # If wait=False log should be writen
    out_file = OUTPUT_FILE
    if not wait:
        time_string = "_" + datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
        out_file = OUTPUT_FILE_ASYNC.replace('.', time_string + '.')
        if CURRENT_OS is OSType.WINDOWS:
            command = command + " 1> " + out_file + " 2>&1"
        elif CURRENT_OS is OSType.LINUX:
            command = command + " 1> " + out_file + " 2>&1 &"
        else:
            command = command + " &> " + out_file + " 2>&1 &"

    # remove output.txt
    try:
        File.remove(out_file)
    except OSError:
        print "Failed to delete " + out_file
        time.sleep(1)
        File.remove(out_file)

    # log command that is executed (and append to TEST_LOG file)
    if log_level is not CommandLogLevel.SILENT:
        File.append(TEST_LOG, command)
        print "##### {0} Executing command : {1}\n".format(time.strftime("%X"), command)

    # Hack for async commands on Windows
    if CURRENT_OS is OSType.WINDOWS:
        if not wait:
            timeout = 10

    # prepare command line
    thread = threading.Thread(target=fork_it)
    thread.start()

    # wait for thread to finish or timeout
    thread.join(timeout)

    # kill thread if it exceed the timeout
    if thread.is_alive():
        if wait:
            Process.kill_by_commandline(command.partition(' ')[0].rpartition(os.sep)[-1])
            thread.join()
            raise NameError('Process has timed out at ' + time.strftime("%X"))

    # get whenever exist in the pipe ?
    pipe_output = 'NOT_COLLECTED'
    if output:
        pipe_output = File.read(out_file)

    if (log_level is CommandLogLevel.FULL) and wait:
        print "##### OUTPUT BEGIN #####\n"
        print pipe_output
        print "##### OUTPUT END #####\n"

    if wait:
        return pipe_output.strip('\r\n')
    else:
        return out_file
开发者ID:NativeScript,项目名称:nativescript-cli-tests,代码行数:91,代码来源:command.py


注:本文中的core.osutils.file.File.append方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。