Exemplo n.º 1
0
def main(argv):
    check_dependencies()

    move_files = False
    date_regex = None
    log_fname = None
    sha_rename = False
    dir_format = os.path.sep.join(['%Y', '%m', '%d'])

    try:
        opts, args = getopt.getopt(argv[2:], "d:r:l:mhs", ["date=", "regex=", "log=", "move", "help","sha-rename"])
    except getopt.GetoptError as e:
        print(e)
        help_info()

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            print('Printing help:')
            help_info()

        if opt in ("-d", "--date"):
            if not arg:
                print('Date format cannot be empty')
                sys.exit(0)
            dir_format = parse_date_format(arg)

        if opt in ("-l", "--log"):
            if not arg:
                print("log file name can't be empty")
                sys.exit(0)
            log_fname = arg

        if opt in ("-m", "--move"):
            move_files = True
            print('Using move strategy!')

        if opt in ("-s", "--sha-rename"):
            sha_rename = True
            print('Renaming files to SHA256 name!')

        if opt in ("-r", "--regex"):
            try:
                date_regex = re.compile(arg)
            except:
                error("Provided regex is invalid!")

    if len(argv) < 2:
        print('ERROR: Number of arguments are less than 2')
        print(argv)
        help_info()

    inputdir = os.path.expanduser(argv[0])
    outputdir = os.path.expanduser(argv[1])

    if not os.path.isdir(inputdir) or not os.path.exists(inputdir):
        error('Input directory "%s" does not exist or cannot be accessed' % inputdir)
    if not os.path.exists(outputdir):
        print('Output directory "%s" does not exist, creating now' % outputdir)
        try:
            os.makedirs(outputdir)
        except Exception:
            print('Cannot create output directory. No write access!')
            sys.exit(0)

    if log_fname:
        logging.basicConfig(filename=log_fname, level='DEBUG')
    else:
        logging.basicConfig(level='CRITICAL')

    ignored_files = ('.DS_Store', 'Thumbs.db')
    error_list = list()
    count = dict(copied=0, moved=0, duplicate=0, error=0, other=0 )
    total_count=0
    for _, _, files in os.walk(inputdir): total_count += len(files)
    bar = ProgressBar(total_count, count.keys())
    for root, _, files in os.walk(inputdir):
        for filename in files:
            try:
                if filename in ignored_files:
                    continue
                if not sha_rename:
                    status = handle_file(os.path.join(root, filename), outputdir, dir_format, move_files, date_regex)
                else:
                    status = handle_file2(os.path.join(root, filename), outputdir, dir_format, move_files, date_regex)
                count[status] += 1
                bar.increment(status)
            except KeyboardInterrupt:
                print('\n Exiting...')
                print_summary(count)
                sys.exit(0)
            except Exception as e :
                logging.error('Error skipping %s:%s' % (filename,repr(e)))
                error_list.append(os.path.join(root, filename))
                count['error'] += 1
                bar.increment('error')
    bar.done()
    logging.info('===Files with Errors===')
    for fn in error_list:
        logging.info(fn)
    logging.info('===End Files with Errors===')
    print_summary(count)
Exemplo n.º 2
0
output = args.output

with open(output, 'w') as f:

    progress = ProgressBar(rows)
    for i in range(rows):

        x = np.random.uniform(min_float, max_float, cols)
        d = np.random.randint(min_digits, max_digits + 1, cols)
        t = np.random.choice(list(args.formatting), cols)

        s = [
            ' ' * i
            for i in np.random.randint(min_prefix_ws, max_prefix_ws + 1, cols)
        ]
        e = [
            ' ' * i
            for i in np.random.randint(min_suffix_ws, max_suffix_ws + 1, cols)
        ]

        fmts = [
            '{xi:.{di}{ti}}'.format(xi=xi, di=di, ti=ti)
            for xi, di, ti in zip(x, d, t)
        ]

        f.write(','.join([si + fmt + ei for si, ei, fmt in zip(s, e, fmts)]))
        f.write('\n')

        progress.increment()