2017-10-30 11:44:12 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import argparse
|
|
|
|
|
2018-01-08 12:26:49 +01:00
|
|
|
parser = argparse.ArgumentParser(description="Place a folder in ~/Documents in ~/Documents/Archives and symlink it")
|
2017-10-30 11:44:12 +01:00
|
|
|
parser.add_argument('dir', metavar='DIRECTORY', type=str, help="The directory to archive")
|
2018-01-08 12:26:49 +01:00
|
|
|
parser.add_argument('-d', '--dry', action='store_true')
|
2017-10-30 11:44:12 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Finding directories
|
|
|
|
assert('HOME' in os.environ), "Home directory unknown"
|
|
|
|
docs = os.path.realpath(os.path.join(os.environ['HOME'], 'Documents'))
|
|
|
|
assert(os.path.isdir(docs)), "Documents folder not found"
|
|
|
|
arcs = os.path.join(docs, 'Archives')
|
|
|
|
assert(os.path.isdir(arcs)), "Archives folder not found"
|
|
|
|
|
2018-01-08 12:26:49 +01:00
|
|
|
def archive(docdir):
|
|
|
|
docdir = os.path.realpath(args.dir)
|
|
|
|
assert(os.path.isdir(docdir)), docdir + " must be a directory"
|
|
|
|
|
|
|
|
assert(docdir.startswith(docs)), "Directory is not in the document folder"
|
|
|
|
assert(not docdir.startswith(arcs)), "Directory is already in the archive folder"
|
|
|
|
|
|
|
|
reldir = os.path.relpath(docdir, docs)
|
|
|
|
print("ARC", reldir)
|
|
|
|
|
|
|
|
arcdir = os.path.join(arcs, reldir)
|
|
|
|
parentArcdir = os.path.realpath(os.path.join(arcdir, '..'))
|
|
|
|
parentDocdir = os.path.realpath(os.path.join(docdir, '..'))
|
|
|
|
linkDest = os.path.relpath(arcdir, parentDocdir)
|
|
|
|
|
|
|
|
# BULLSHIT
|
|
|
|
|
|
|
|
# If the directory exists
|
|
|
|
if os.path.isdir(arcdir):
|
|
|
|
return
|
|
|
|
# for f in os.listdir(arcdir):
|
|
|
|
# assert(os.path.isdir(f)), "Something unknown in Archive dir")
|
|
|
|
# archive(os.path.join(arcdir, f))
|
|
|
|
|
|
|
|
# If the directory doesn't exist, create the directories under it and move all the folder
|
|
|
|
else:
|
|
|
|
|
|
|
|
if args.dry:
|
|
|
|
print("mkdir -p", parentArcdir)
|
|
|
|
else:
|
|
|
|
os.makedirs(parentArcdir, exist_ok=True)
|
|
|
|
|
|
|
|
if args.dry:
|
|
|
|
print("mv", docdir, arcdir)
|
|
|
|
else:
|
|
|
|
os.rename(docdir, arcdir)
|
|
|
|
|
|
|
|
if args.dry:
|
|
|
|
print("ln -s", linkDest, docdir)
|
|
|
|
else:
|
|
|
|
os.symlink(linkDest, docdir)
|
|
|
|
|
|
|
|
|
2017-10-30 11:44:12 +01:00
|
|
|
|
2018-01-08 12:26:49 +01:00
|
|
|
def unarchive(arcdir):
|
|
|
|
return
|
2017-10-30 11:44:12 +01:00
|
|
|
|
2018-01-08 12:26:49 +01:00
|
|
|
archive(args.dir)
|