2020-07-01 03:30:11 +00:00
|
|
|
'''
|
|
|
|
This program takes a directory and does the following: If that directory
|
|
|
|
contains nothing but a single child directory, then the contents of that child
|
|
|
|
will be moved to the parent, and the empty child will be deleted.
|
|
|
|
'''
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from voussoirkit import passwordy
|
|
|
|
from voussoirkit import pathclass
|
2021-02-15 02:28:27 +00:00
|
|
|
from voussoirkit import pipeable
|
|
|
|
from voussoirkit import spinal
|
|
|
|
from voussoirkit import winglob
|
2020-07-01 03:30:11 +00:00
|
|
|
|
|
|
|
def sole_lift(starting):
|
|
|
|
starting = pathclass.Path(starting)
|
|
|
|
children = starting.listdir()
|
|
|
|
if len(children) != 1:
|
|
|
|
return
|
|
|
|
|
|
|
|
child = children[0]
|
|
|
|
|
|
|
|
if not child.is_dir:
|
|
|
|
return
|
|
|
|
|
|
|
|
temp_dir = starting.with_child(passwordy.urandom_hex(32))
|
|
|
|
os.rename(child.absolute_path, temp_dir.absolute_path)
|
|
|
|
for grandchild in temp_dir.listdir():
|
|
|
|
shutil.move(grandchild.absolute_path, starting.absolute_path)
|
|
|
|
|
|
|
|
if temp_dir.listdir():
|
|
|
|
raise Exception()
|
|
|
|
|
|
|
|
os.rmdir(temp_dir.absolute_path)
|
|
|
|
|
|
|
|
def sole_lift_argparse(args):
|
2021-02-15 02:28:27 +00:00
|
|
|
patterns = pipeable.input_many(args.patterns, skip_blank=True, strip=True)
|
|
|
|
directories = (pathclass.Path(d) for pattern in patterns for d in winglob.glob(pattern))
|
|
|
|
directories = (d for d in directories if d.is_dir)
|
|
|
|
for directory in directories:
|
|
|
|
sole_lift(directory)
|
2020-07-01 03:30:11 +00:00
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
|
2021-02-15 02:28:27 +00:00
|
|
|
parser.add_argument('patterns', nargs='+', default='.')
|
2020-07-01 03:30:11 +00:00
|
|
|
parser.set_defaults(func=sole_lift_argparse)
|
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
return args.func(args)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|