Exemplo n.º 1
0
def get_group_manifest_file(group: str,
                            need_exist=True,
                            check_backup=True) -> Path:
    """
    Get the path of the file storing the backup manifest for the group.

    Arguments:
        group: group for which to get manifest file
        need_exist: if True, dies if manifest does not exist.
        check_backup: if we would die because of need_*s, check backup first.
            If a backup file exists, prompt to restore.
    """

    group = canonicalize_group_name(group)

    fp = CONFIG_DIR.joinpath(f"{group}.txt")
    fp_bkp = get_backup_fp(fp)

    # conditions not met
    if not fp.exists():
        if check_backup and fp_bkp.exists():
            msg = (f'The group "{group}" was not found, but a backup file ' +
                   "was. Would you like to restore the backup file?")

            if click.confirm(msg, default=True):
                fcopy(fp_bkp.as_posix(), fp.as_posix())
                return fp

        elif need_exist:
            die(f'Group "{group}" does not exist.')

    return fp
Exemplo n.º 2
0
def stash(dname, subdir="bicIC"):
    '''
    Copy all the PST files found in directory "dname" to the indicated
    subdirectory of the project directory. This function creates container
    directories with the same name as the files (minus extension)

    '''
    for fn in os.listdir(dname):
        if fn.endswith('.pst'):
            ffn = os.path.join(dname, fn)
            dn = os.path.join(BASEDIR, subdir, os.path.splitext(fn)[0])
            if not os.path.isdir(dn):
                os.mkdir(dn)
            fcopy(ffn, dn)
Exemplo n.º 3
0
def load_config_file(cfg_path, log_dir=None, backup=True) -> dict:
    assert os.path.isfile(cfg_path)
    if backup:
        bkup_path = os.path.join(
            log_dir, f"config-{datetime.fromtimestamp(time.time())}.py")
        fcopy(cfg_path, bkup_path)
        cfg = load_module(bkup_path, "cfg")
        check_cfg = load_module(bkup_path, "check_cfg")
        modify_cfg = load_module(bkup_path, "modify_cfg")
    else:
        cfg = load_module(cfg_path, "cfg")
        check_cfg = load_module(cfg_path, "check_cfg")
        modify_cfg = load_module(cfg_path, "modify_cfg")
    modify_cfg(cfg)
    assert check_cfg(cfg)
    return cfg
Exemplo n.º 4
0
def commit_group_rps(group: str, rps: Iterable[RichPath]) -> None:
    """
    Atomically commit the passed rps as the new contents of the group file.

    Does NOT append or merge, that is the responsibility of the caller.

    Does not write empty files - empty rps is a noop.
    """

    fp = get_group_manifest_file(group, need_exist=False, check_backup=False)
    fp_bkp = get_backup_fp(fp)

    # if the file already exists, back it up
    if fp.exists() and fp.stat().st_size > 0:
        fcopy(str(fp), str(fp_bkp))

    with tmp.NamedTemporaryFile(mode="w") as tf:
        for rp in sorted(set(rps)):
            if rp.is_glob or rp.sticky or osp.exists(rp.str):
                tf.write(str(rp) + "\n")

        tf.flush()
        fcopy(tf.name, str(fp))

    # if a backup does not exist, initialize it to the fresh file contents
    if not fp_bkp.exists() and fp.stat().st_size > 0:
        fcopy(str(fp), str(fp_bkp))
Exemplo n.º 5
0
    in2_not1 = [elem for elem in comp2_list if elem not in comp1_list]
    in1_not2 = [elem for elem in comp1_list if elem not in comp2_list]

    both = [elem for elem in comp1_list if elem in comp2_list]
    # print out statistics

    print(" Number of summary files listed in both directories: " +
          str(len(both)))
    print(" Number of summary files listed only in " + str(comp1_path) + ": " +
          str(len(in1_not2)))
    print(" Number of summary files listed only in " + str(comp2_path) + ": " +
          str(len(in2_not1)))

    ncop = 0
    for file in in1_not2:
        fcopy(os.path.join(comp1_path, file), os.path.join(merge_path, file))
        ncop = ncop + 1
    for file in in2_not1:
        fcopy(os.path.join(comp2_path, file), os.path.join(merge_path, file))
        ncop = ncop + 1
    print(str(ncop) + " files copied to merge directory")

    # tested and okay until here

    # only files that are in both comp direcories are compared
    # extract filenames from the 'both' list

    for fn in both:
        print("Checking file: ", fn)

        in1_not2, in2_not1 = compare(os.path.join(comp1_path, fn),
Exemplo n.º 6
0
            flawed_data += 1
            print('there is a problem with the data in text file: ' +
                  os.path.basename(txt_file))
            f.close()
            continue
        if not os.path.isfile(files_path + data[0]):
            flawed_data += 1
            print('there is no image file -' + data[0] + '- in this directory')
            f.close()
            continue

        #if not os.path.isfile(txt_file):
        #	fcopy(txt_file, folders_path + 'Output/')

        if not os.path.isfile(folders_path + 'Output/' + data[0]):
            fcopy(files_path + data[0], folders_path + 'Output/')
            fcopy(txt_file, folders_path + 'Output/')

        else:
            extra_num = 0
            old_name = data[0]
            while os.path.isfile(folders_path + 'Output/' + data[0]):
                img_name_list = data[0].split('.')
                new_img_name = img_name_list[0] + str(
                    extra_num) + '.' + img_name_list[1]
                data[0] = new_img_name
                extra_num += 1

            new_line = ' '.join(data)
            f.close()
            new_file_name = folders_path + 'Output/' + data[0].split(