-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdlsorter_os.py
More file actions
114 lines (94 loc) · 4.31 KB
/
dlsorter_os.py
File metadata and controls
114 lines (94 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import dlsorter_properties
import logging
import os
import re
import shutil
# rarpartexp = "^.*\.part(?!1\.|01\.|001\.)[0-9]{1,3}.*$"
rarpartexp = "^.*\.(?:part(?!1\.|01\.|001\.)[0-9]{1,3}|subs|sample).*$"
# Create directory if it does not already exist
# directory = absolute path to directory to create
def create_directory(directory):
if not os.path.exists(directory):
logging.debug("Create directory: " + directory)
os.makedirs(directory)
# Delete directory and all contents
# directory = absolute path to any existing directory
def delete_directory(directory):
if os.path.exists(directory):
logging.debug("Remove directory: " + directory)
shutil.rmtree(directory, ignore_errors=True)
# Recursively search directory for files with extension and build list to return
# directory = absolute path to directory to start recursive search from
# extension = file extension of what you want to find (".rar") or (".mkv")
# outlist = return list of absolute path files
def search_for_files(directory, extension):
outlist = []
for root, dirs, files in os.walk(os.path.abspath(directory)):
for f in files:
if f.endswith(extension) and not os.path.islink(os.path.join(root, f)):
abs_f = os.path.join(root, f)
sz = os.path.getsize(abs_f)
if extension == '.mkv' and sz >= dlsorter_properties.ossearchmkvminsz:
outlist.append(abs_f)
logging.info("Found: " + os.path.basename(abs_f))
logging.debug("Found: " + abs_f)
elif extension == '.rar':
if not re.match(rarpartexp, abs_f): # Match rars like: part1, part01, part001
outlist.append(abs_f)
logging.info("Found: " + os.path.basename(abs_f))
logging.debug("Found: " + abs_f)
return outlist
# Recursively search directory for directories that end with string
# directory = absolute path to directory to start recursive search from
# endswithstr = string you want to look for at end of directory name ("_tmp")
# outlist = return list of absolute path directories
def search_for_folders(directory, endswithstr):
outlist = []
if os.path.isdir(directory) and directory not in dlsorter_properties.downloaddirs:
for root, dirs, files in os.walk(os.path.abspath(directory)):
for d in dirs:
if d.endswith(endswithstr):
abs_d = os.path.join(root, d)
outlist.append(abs_d)
return outlist
# Copy file
# infile = absolute path original file
# destination = absolute path destination
def copy_file(infile, destination):
if os.path.exists(destination):
for d in dlsorter_properties.downloaddirs:
if d in infile:
logging.info("Copy file: " + os.path.basename(infile) + " -> " + destination.replace(d, ".."))
logging.debug("Copy file: " + infile + " -> " + destination)
shutil.copy2(infile, destination)
# Move file
# infile = absolute path original file
# destination = absolute path destination
def move_file(infile, destination):
if os.path.exists(destination):
for d in dlsorter_properties.downloaddirs:
if d in infile:
logging.info("Move file: " + os.path.basename(infile) + " -> " + destination.replace(d, ".."))
logging.debug("Move file: " + infile + " -> " + destination)
shutil.move(infile, destination)
# Rename file
# infile = absolute path current file
# newname = absolute path new file name
def rename_file(infile, newname):
if not os.path.exists(newname):
logging.info("Rename file: " + os.path.basename(infile) + " -> " + os.path.basename(newname))
logging.debug("Rename file: " + infile + " -> " + newname)
os.rename(infile, newname)
# Create symlink
# src = absolute path to source file
# dst = absolute path to desired symlink destination file
def create_symlink(src, dst):
if os.path.exists(src) and not os.path.exists(dst):
logging.debug("Create symlink: " + src + " -> " + dst)
os.symlink(src, dst)
# Delete symlink
# inlink = absolute path to symlink
def delete_symlink(inlink):
if os.path.islink(inlink):
logging.debug("Delete symlink: " + inlink)
os.unlink(inlink)