possibiliy to add files

This commit is contained in:
Ville Rantanen
2020-11-18 13:58:49 +02:00
parent 24dd87219c
commit fa4335abc7

53
flit.py
View File

@@ -3,6 +3,7 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
import argparse import argparse
import os import os
import sys
import random import random
import shutil import shutil
import string import string
@@ -58,21 +59,30 @@ def parse_opts():
) )
add_parser = subparsers.add_parser('add', add_help=False) add_parser = subparsers.add_parser('add', add_help=False)
add_parser.add_argument( add_parser.add_argument(
"days", "-d",
action = "store", action = "store",
type = int, type = int,
help = "Days to keep files", help = "Days to keep files",
default = 30, default = 30,
nargs = '?' dest = 'days'
) )
add_parser.add_argument( add_parser.add_argument(
"description", "-m",
action = "store", action = "store",
type = str, type = str,
help = "Describe share", help = "Describe share",
default = "-", default = "-",
nargs = '?' dest = 'description'
) )
add_parser.add_argument(
"files",
action = "store",
type = str,
help = "Copy files/folders under the new share",
default = [],
nargs = '*'
)
list_parser = subparsers.add_parser('list', add_help=False) list_parser = subparsers.add_parser('list', add_help=False)
list_parser.add_argument( list_parser.add_argument(
'--verbose', '-v', '--verbose', '-v',
@@ -102,6 +112,25 @@ def create_new(p, days, description):
fp.write(description) fp.write(description)
def copy_files(cwd, new_name, files):
target = os.path.abspath(new_name)
for f in opts.files:
print(f"Copying: {f}")
source = os.path.join(cwd, f)
if os.path.isfile(source):
shutil.copy2(
source,
target
)
else:
shutil.copytree(
source,
os.path.join(target,f),
symlinks = True,
)
def get_description(f): def get_description(f):
try: try:
with open(f) as fp: with open(f) as fp:
@@ -140,10 +169,13 @@ def get_folder_names():
return [p[0] for p in dir_list] return [p[0] for p in dir_list]
def list_folders(root_folder, verbose = False): def list_folders(root_folder, verbose = False, filter_name = None):
names = get_folder_names() names = get_folder_names()
print("Folder Created ToDelete InDays") print("Folder Created ToDelete InDays")
for p in names: for p in names:
if filter_name is not None:
if filter_name != p:
continue
stats = get_stats(p) stats = get_stats(p)
sub_files = get_sub_files(p) sub_files = get_sub_files(p)
due = "*" if stats[3] else " " due = "*" if stats[3] else " "
@@ -174,17 +206,20 @@ def del_due_folders():
if __name__ == '__main__': if __name__ == '__main__':
opts = parse_opts() opts = parse_opts()
cwd = os.getcwd()
os.chdir(os.path.dirname(__file__))
if opts.command == 'add': if opts.command == 'add':
new_name = random_name() new_name = random_name()
while os.path.exists(new_name): while os.path.exists(new_name):
new_name = random_name() new_name = random_name()
create_new(new_name, opts.days, opts.description) create_new(new_name, opts.days, opts.description)
print(os.path.abspath(new_name)) print(os.path.abspath(new_name))
print(opts.root + "/" + new_name) print("")
copy_files(cwd, new_name, opts.files)
list_folders(opts.root, verbose = True, filter_name = new_name)
if opts.command == "list" or opts.command == None: if opts.command == "list" or opts.command is None:
list_folders(opts.root, opts.verbose == True) list_folders(opts.root, verbose = opts.verbose)
if opts.command == "del": if opts.command == "del":
del_due_folders() del_due_folders()