Example #1
0
def main(parser):
    (options, args) = parser.parse_args(args=None, values=None)

    # 当前脚本绝对路径
    abspath = util.script_abspath(inspect.currentframe())

    if not options.path:
        elog.warn("No path specified. using: -P, --path=PATH")
        exit(-1)

    # 取得配置项 options.path 的绝对路径
    root_path = util.source_abspath(APPFILE, options.path, abspath)

    # 取得文件扩展名数组
    file_exts = []
    filters = parse_strarr(options.filter)
    for filter in filters:
        if filter.startswith('.'):
            if filter not in file_exts:
                file_exts.append(filter)

        if filter in source_filters_dict.keys():
            for ext in source_filters_dict[filter]:
                if ext not in file_exts:
                    file_exts.append(ext)

    curtime = time.time()

    elog.force("path:      %r", root_path)
    elog.force("exts:      %r", file_exts)
    elog.force("recursive: %r", options.recursive)
    elog.force("timestamp: %r", curtime)

    if options.author:
        elog.force("author:    %r", options.author)

    sweep_path(root_path, file_exts, options.recursive, options.author,
               curtime)

    pass
Example #2
0
def main(parser):
    (options, args) = parser.parse_args(args=None, values=None)

    # 子进程退出后向父进程发送的信号
    ## signal.signal(signal.SIGCHLD, util.sig_chld)

    # 进程退出信号
    signal.signal(signal.SIGINT, util.sig_int)
    signal.signal(signal.SIGTERM, util.sig_term)

    # 当前脚本绝对路径
    abspath = util.script_abspath(inspect.currentframe())

    if not options.path:
        options.path = os.getcwd()
        elog.warn(
            "No path specified. using current working dir or using: --path='PATH'"
        )

    if not options.srcs:
        elog.error("No source strings specified. using: --srcs='STRINGS'")
        sys.exit(1)

    if not options.dsts:
        elog.warn("No destigation strings specified. using: --dsts='STRINGS'")

    # 取得配置项options.path的绝对路径
    root_path = util.source_abspath(APPFILE, options.path, abspath)
    srcs = parse_strarr(options.srcs)
    dsts = parse_strarr(options.dsts)

    elog.force("path: %s", root_path)
    elog.force("sour = %r", srcs)
    elog.force("dest = %r", dsts)

    founds = []
    sweep_dir(root_path, srcs, founds)
    elog.force("Total %d files found", len(founds))

    if len(founds) > 0:
        if options.replace:
            if len(srcs) == len(dsts):
                for pf in founds:
                    ctime, mtime = None, None
                    fts = file_times(pf)
                    if fts:
                        ctime, mtime = fts
                    else:
                        elog.warn("missing file: %s", pf)
                        continue

                    for i in range(0, len(srcs)):
                        srcstr = srcs[i]
                        dststr = None
                        if i < len(dsts):
                            dststr = dsts[i]

                        if dststr:
                            ds = dststr.replace('$(mtime)', mtime).replace(
                                '$(ctime)', ctime)

                            if options.whole_line:
                                cmd = "sed -i 's/%s.*/%s/g' '%s'" % (srcstr,
                                                                     ds, pf)
                            else:
                                cmd = "sed -i 's/%s/%s/g' '%s'" % (srcstr, ds,
                                                                   pf)

                            elog.debug(cmd)
                            (status, output) = commands.getstatusoutput(cmd)
                            if status != 0:
                                elog.error(
                                    "failed to command: \"%s\", output: %r",
                                    sed, output)

                elog.force("Total %d files replaced", len(founds))
            else:
                elog.error(
                    "Failed to replace for srcs(%r) mismatched with dsts(%r)",
                    srcs, dsts)
                pass
        else:
            elog.warn("No files to be replaced. Using: --replace")
            pass
    pass