Пример #1
0
def pairs(stat_dir, return_path=True):
    # type: (string) -> list
    pairs_s = [x for x in os.listdir(expu(stat_dir))]
    pair_dirs = [pjoin(stat_dir, x) for x in pairs_s]
    if return_path:
        # return [x for x in pair_dirs if os.path.isdir(expu(x)) and '_' in x]
        return [x for x in pair_dirs if os.path.isdir(expu(x))]
    else:
        return [x[1] for x in zip(pair_dirs, pairs_s) \
                if os.path.isdir(expu(x[0]))]
Пример #2
0
def get_stats(stat_file: str,
              targets: list,
              insts: int = 200 * (10**6),
              re_targets=False) -> dict:
    assert (os.path.isfile(expu(stat_file)))
    lines = get_raw_stats_around(stat_file, insts)

    patterns = {}
    if re_targets:
        meta_pattern = re.compile('.*\((.+)\).*')
        for t in targets:
            meta = meta_pattern.search(t).group(1)
            patterns[meta] = re.compile(t + '\s+(\d+\.?\d*)\s+#')
    else:
        for t in targets:
            patterns[t] = re.compile(t + '\s+(\d+\.?\d*)\s+#')

    # print(patterns)
    stats = {}

    for line in lines:
        for k in patterns:
            m = patterns[k].search(line)
            if not m is None:
                if re_targets:
                    stats[m.group(1)] = to_num(m.group(2))
                else:
                    stats[k] = to_num(m.group(1))
    return stats
Пример #3
0
def stat_filt(pairs, dirs):
    # type: (list) -> list
    new_pairs = []
    new_paths = []
    for pair, path in zip(pairs, dirs):
        if os.path.isfile(expu(pjoin(path, 'stats.txt'))):
            new_pairs.append(pair)
            new_paths.append(path)
    return new_pairs, new_paths
Пример #4
0
def rm(rm_args=None):
    global c
    global evaledpaths
    args = ''
    paths = []
    evalpaths = []
    option_end = False
    if not rm_args:
        rm_args = argv[1:]
    for arg in rm_args:
        if arg == '--':
            option_end = True
        elif (arg.startswith("-") and not option_end) or arg in c.invalid:
            pass
        else:
            path = abspath(expv(expu(arg)))
            file_evalpaths = gen_evalpaths(path)
            evalpath = gen_eval(path)
            if c.suffix in arg:
                pprint(path + " is a protection file")
                if ask_in(q="Do you want to remove it? (y/n) ", a="Yesyes"):
                    args += arg + ' '
                else:
                    pprint(path + " will not be removed")
                continue
            if exists(evalpath):
                if ask(evalpath):
                    paths.append(path)
                    evalpaths.append(evalpath)
                else:
                    continue
            if not parent_clear(file_evalpaths, path):
                continue
            if isdir(path):
                find_exec = "find " + path + " -name " + "\".*" + c.suffix + "\"" + " -print"
                out, err = Popen(find_exec,
                                 shell=True,
                                 stdout=PIPE,
                                 stderr=PIPE,
                                 universal_newlines=True).communicate()
                for pfile in iter(out.splitlines()):
                    pprint("A protected file or directory is found inside " +
                           path)
                    if not ask(pfile):
                        pprint(
                            "Terminated due to potentially dangerous action")
                        exit(1)
        args += bash_path(arg) + ' '
    Popen("rm " + args, shell=True).wait()
    remove_protection_files = ''
    for evalpath, path in zip(evalpaths, paths):
        if exists(evalpath) and not exists(path):
            remove_protection_files += bash_path(evalpath) + ' '
    if remove_protection_files:
        Popen("rm " + remove_protection_files, shell=True).wait()
    evaledpaths = []
Пример #5
0
def protect(protect_args=None):
    c = Config()
    if not protect_args:
        protect_args = argv[1:]
    for arg in protect_args:
        if arg in c.invalid:
            print("\".\" and \"..\" may not be protected")
        else:
            path = abspath(expv(expu(arg)))
            evalpath = dirname(path) + "/." + basename(path) + c.suffix
            if not exists(path):
                print("Warning: " + path + " does not exist")
            with open(evalpath, "w") as f:
                question = input("Question for " + path + ": ")
                answer = input("Answer: ")
                f.write(question + "\n" + answer)
Пример #6
0
def protect(protect_args=None):
    global c
    flags = ''
    option_end = False
    if not protect_args:
        protect_args = argv[1:]
    for arg in protect_args:
        if arg == '--':
            option_end = True
        elif (arg.startswith("-") and not option_end):
            flags = flags + arg[arg.rfind('-') + 1:]
        elif arg in c.invalid:
            pprint('"." and ".." may not be protected')
        else:
            path = abspath(expv(expu(arg)))
            evalpath = dirname(path) + "/." + basename(path) + c.suffix
            if not exists(path):
                pprint("Warning: " + path + " does not exist")
            with open(evalpath, "w") as f:
                question = input("Question for " + path + ": ")
                answer = input("Answer: ")
                f.write(question + "\n" + answer + "\n" + flags.upper())
Пример #7
0
def get_conn():
    conn = sqlite3.connect(expu("~/.timy.db"))
    create_tables(conn)
    return conn
Пример #8
0
def pair_to_full_path(path, pairs):
    return [expu(pjoin(path, p)) for p in pairs]
Пример #9
0
def get_raw_stats_around(stat_file: str,
                         insts: int = 200 * (10**6),
                         cycles: int = None) -> list:
    old_buff = []
    buff = []

    old_insts = None
    old_cycles = None
    new_insts = None
    new_cycles = None

    p_insts = re.compile('switch_cpus.committedInsts\s+(\d+)\s+#')
    p_cycles = re.compile('switch_cpus.numCycles\s+(\d+)\s+#')

    if insts > 500 * (10**6):
        for line in reverse_readline(expu(stat_file)):
            buff.append(line)

            if line.startswith(
                    '---------- Begin Simulation Statistics ----------'):
                if cycles:
                    pass
                else:
                    if insts >= new_insts:
                        if old_insts is None:
                            return buff
                        elif abs(insts - old_insts) > abs(new_insts - insts):
                            return buff
                        else:
                            return old_buff
                    else:
                        old_insts = new_insts
                        old_cycles = new_cylces
                        old_buff = deepcopy(buff)
                        buff.clear()

            elif line.startswith('system.switch_cpus.committedInsts'):
                new_insts = int(p_insts.search(line).group(1))
            elif line.startswith('system.switch_cpus.numCycles'):
                new_cylces = int(p_cycles.search(line).group(1))

    else:
        with open(expu(stat_file)) as f:
            for line in f:
                buff.append(line)

                if line.startswith(
                        '---------- End Simulation Statistics   ----------'):
                    if cycles:
                        pass
                    else:
                        if insts <= new_insts:
                            if old_insts is None:
                                return buff
                            elif abs(insts - old_insts) > abs(new_insts -
                                                              insts):
                                return buff
                            else:
                                return old_buff
                        else:
                            old_insts = new_insts
                            old_cycles = new_cylces
                            old_buff = deepcopy(buff)
                            buff.clear()

                elif line.startswith('system.switch_cpus.committedInsts'):
                    new_insts = int(p_insts.search(line).group(1))
                elif line.startswith('system.switch_cpus.numCycles'):
                    new_cylces = int(p_cycles.search(line).group(1))

    return old_buff
Пример #10
0
 def newer_than_gem5(d):
     stat = pjoin(expu(d), 'stats.txt')
     return left_is_older(pjoin(os.environ['gem5_build'], 'gem5.fast'),
                          stat)