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
import argparse
import os
import sys
import random
import shutil
import string
@@ -58,21 +59,30 @@ def parse_opts():
)
add_parser = subparsers.add_parser('add', add_help=False)
add_parser.add_argument(
"days",
"-d",
action = "store",
type = int,
help = "Days to keep files",
default = 30,
nargs = '?'
dest = 'days'
)
add_parser.add_argument(
"description",
"-m",
action = "store",
type = str,
help = "Describe share",
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.add_argument(
'--verbose', '-v',
@@ -102,6 +112,25 @@ def create_new(p, days, 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):
try:
with open(f) as fp:
@@ -140,10 +169,13 @@ def get_folder_names():
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()
print("Folder Created ToDelete InDays")
for p in names:
if filter_name is not None:
if filter_name != p:
continue
stats = get_stats(p)
sub_files = get_sub_files(p)
due = "*" if stats[3] else " "
@@ -174,17 +206,20 @@ def del_due_folders():
if __name__ == '__main__':
opts = parse_opts()
cwd = os.getcwd()
os.chdir(os.path.dirname(__file__))
if opts.command == 'add':
new_name = random_name()
while os.path.exists(new_name):
new_name = random_name()
create_new(new_name, opts.days, opts.description)
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:
list_folders(opts.root, opts.verbose == True)
if opts.command == "list" or opts.command is None:
list_folders(opts.root, verbose = opts.verbose)
if opts.command == "del":
del_due_folders()