2019-06-12 05:41:31 +00:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2020-12-07 08:54:00 +00:00
|
|
|
from voussoirkit import interactive
|
2021-12-07 20:46:45 +00:00
|
|
|
from voussoirkit import pathclass
|
|
|
|
from voussoirkit import pipeable
|
2019-06-12 05:41:31 +00:00
|
|
|
from voussoirkit import spinal
|
2022-02-13 03:50:00 +00:00
|
|
|
from voussoirkit import winglob
|
2019-06-12 05:41:31 +00:00
|
|
|
|
2022-02-13 03:50:00 +00:00
|
|
|
def filepull(pull_from='.', globs=None, autoyes=False):
|
2021-12-07 20:46:45 +00:00
|
|
|
start = pathclass.Path(pull_from)
|
2022-02-13 03:50:00 +00:00
|
|
|
files = [
|
|
|
|
file
|
|
|
|
for d in start.listdir_directories()
|
|
|
|
for file in spinal.walk(d, glob_filenames=globs)
|
|
|
|
]
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
if len(files) == 0:
|
2021-12-07 20:46:45 +00:00
|
|
|
pipeable.stderr('No files to move')
|
|
|
|
return 1
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
duplicate_count = {}
|
|
|
|
for f in files:
|
|
|
|
basename = f.basename
|
|
|
|
duplicate_count.setdefault(basename, [])
|
|
|
|
duplicate_count[basename].append(f.absolute_path)
|
|
|
|
|
2021-08-10 00:37:19 +00:00
|
|
|
duplicates = [
|
|
|
|
'\n'.join(sorted(copies))
|
|
|
|
for (basename, copies) in duplicate_count.items()
|
|
|
|
if len(copies) > 1
|
|
|
|
]
|
2019-06-12 05:41:31 +00:00
|
|
|
duplicates = sorted(duplicates)
|
|
|
|
if len(duplicates) > 0:
|
|
|
|
raise Exception('duplicate names:\n' + '\n'.join(duplicates))
|
|
|
|
|
|
|
|
for f in files:
|
2021-12-07 20:46:45 +00:00
|
|
|
pipeable.stdout(f.basename)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
2020-12-07 08:54:00 +00:00
|
|
|
if autoyes or interactive.getpermission(f'Move {len(files)} files?'):
|
2019-06-12 05:41:31 +00:00
|
|
|
for f in files:
|
|
|
|
local = os.path.join('.', f.basename)
|
2021-12-03 03:34:28 +00:00
|
|
|
os.rename(f, local)
|
2021-12-07 20:46:45 +00:00
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return 1
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
def filepull_argparse(args):
|
2022-02-13 03:50:00 +00:00
|
|
|
return filepull(pull_from=args.pull_from, globs=args.glob, autoyes=args.autoyes)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
def main(argv):
|
2022-02-13 03:50:00 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='''
|
|
|
|
Pull all of the files in nested directories into the current directory.
|
|
|
|
''',
|
|
|
|
)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
parser.add_argument('pull_from', nargs='?', default='.')
|
2022-02-13 03:50:00 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--glob',
|
|
|
|
nargs='+',
|
|
|
|
help='''
|
|
|
|
Only pull files whose basename matches any of these glob patterns.
|
|
|
|
''',
|
|
|
|
)
|
2019-06-12 05:41:31 +00:00
|
|
|
parser.add_argument('-y', '--yes', dest='autoyes', action='store_true')
|
|
|
|
parser.set_defaults(func=filepull_argparse)
|
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
2020-02-09 01:18:50 +00:00
|
|
|
return args.func(args)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-02-09 01:18:50 +00:00
|
|
|
raise SystemExit(main(sys.argv[1:]))
|