dotfiles/scripts/archive
2018-01-08 12:26:49 +01:00

66 lines
2 KiB
Python
Executable file

#!/usr/bin/env python3
import os
import argparse
parser = argparse.ArgumentParser(description="Place a folder in ~/Documents in ~/Documents/Archives and symlink it")
parser.add_argument('dir', metavar='DIRECTORY', type=str, help="The directory to archive")
parser.add_argument('-d', '--dry', action='store_true')
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"
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)
def unarchive(arcdir):
return
archive(args.dir)