177 lines
5.4 KiB
Python
177 lines
5.4 KiB
Python
|
import os
|
||
|
import stat
|
||
|
import time
|
||
|
import shutil
|
||
|
import platform
|
||
|
import configparser
|
||
|
|
||
|
M_DEFAULTCONFIG = "./Settings.cfg"
|
||
|
M_CONFIGPARSER = configparser.ConfigParser()
|
||
|
|
||
|
def normalizePath(path):
|
||
|
return os.path.normcase(path)
|
||
|
|
||
|
def doesFileExist(path):
|
||
|
return os.path.isfile(path)
|
||
|
|
||
|
def doesFolderExist(path):
|
||
|
path = normalizePath(path)
|
||
|
if os.path.exists(path):
|
||
|
return not doesFileExist(path)
|
||
|
return False
|
||
|
|
||
|
def getFileSize(path):
|
||
|
path = normalizePath(path)
|
||
|
if doesFileExist(path):
|
||
|
return os.path.getsize(path)
|
||
|
else:
|
||
|
return 0
|
||
|
|
||
|
def checkSizeChange(path):
|
||
|
path = normalizePath(path)
|
||
|
if doesFileExist(path):
|
||
|
tmp_size = getFileSize(path)
|
||
|
time.sleep(10)
|
||
|
return (tmp_size == getFileSize(path))
|
||
|
return False
|
||
|
|
||
|
def createFolder(path):
|
||
|
path = normalizePath(path)
|
||
|
if doesFolderExist(path):
|
||
|
return True
|
||
|
else:
|
||
|
try:
|
||
|
os.makedirs(path)
|
||
|
return True
|
||
|
except OSError:
|
||
|
return False
|
||
|
|
||
|
def deleteDirectory(path, attempt=True):
|
||
|
path = normalizePath(path)
|
||
|
if doesFileExist(path):
|
||
|
try:
|
||
|
os.remove(path)
|
||
|
except:
|
||
|
if attempt:
|
||
|
os.chmod(path, stat.S_IWRITE)
|
||
|
os.unlink(path)
|
||
|
deleteDirectory(path, False)
|
||
|
else:
|
||
|
return False
|
||
|
elif doesFolderExist(path):
|
||
|
try:
|
||
|
os.rmdir(path)
|
||
|
except:
|
||
|
if attempt:
|
||
|
os.chmod(path, stat.S_IWRITE)
|
||
|
os.unlink(path)
|
||
|
deleteDirectory(path, False)
|
||
|
else:
|
||
|
return False
|
||
|
return True
|
||
|
|
||
|
def moveFile(input_path, output_path, copy=False):
|
||
|
input_path = normalizePath(input_path)
|
||
|
output_path = normalizePath(output_path)
|
||
|
folder_created = False
|
||
|
if platform.system() == 'Windows':
|
||
|
folder_created = createFolder(output_path.rsplit("\\", 1)[0])
|
||
|
else:
|
||
|
folder_created = createFolder(output_path.rsplit("/", 1)[0])
|
||
|
if folder_created:
|
||
|
try:
|
||
|
if copy:
|
||
|
shutil.copyfile(input_path, output_path)
|
||
|
else:
|
||
|
shutil.move(input_path, output_path)
|
||
|
return True
|
||
|
except shutil.Error:
|
||
|
if copy:
|
||
|
print("Copying failed...")
|
||
|
else:
|
||
|
print("Moving failed...")
|
||
|
return False
|
||
|
except:
|
||
|
if doesFileExist(output_path):
|
||
|
if getFileSize(input_path) == getFileSize(output_path):
|
||
|
if not copy:
|
||
|
try:
|
||
|
deleteDirectory(input_path)
|
||
|
except:
|
||
|
print("Successfully copied file but could not delete input file...")
|
||
|
else:
|
||
|
if copy:
|
||
|
print("Failed to copy file")
|
||
|
else:
|
||
|
print("Failed to move file")
|
||
|
return False
|
||
|
|
||
|
def listAllFiles(path):
|
||
|
path = normalizePath(path)
|
||
|
try:
|
||
|
tmp_paths = os.listdir(path)
|
||
|
tmp_files = []
|
||
|
for files in tmp_paths:
|
||
|
tmp_current = os.path.join(path, files)
|
||
|
if doesFolderExist(tmp_current):
|
||
|
tmp_files = tmp_files + listAllFiles(tmp_current)
|
||
|
elif doesFileExist(tmp_current):
|
||
|
tmp_files.append(tmp_current)
|
||
|
return tmp_files
|
||
|
except OSError:
|
||
|
return None
|
||
|
|
||
|
def deleteEmptyFolders(path, delroot=False):
|
||
|
path = normalizePath(path)
|
||
|
if not os.path.isdir(path):
|
||
|
return
|
||
|
tmp_files = os.listdir(path)
|
||
|
if tmp_files:
|
||
|
for files in tmp_files:
|
||
|
tmp_fullpath = os.path.join(path, files)
|
||
|
if os.path.isdir(tmp_fullpath):
|
||
|
deleteEmptyFolders(tmp_fullpath, True)
|
||
|
tmp_files = os.listdir(path)
|
||
|
if not tmp_files and delroot:
|
||
|
os.rmdir(path)
|
||
|
|
||
|
def addConfiguration(section, option, key=""):
|
||
|
if not doesFileExist(M_DEFAULTCONFIG):
|
||
|
M_CONFIGPARSER.add_section(section)
|
||
|
M_CONFIGPARSER.set(section, option, key)
|
||
|
else:
|
||
|
M_CONFIGPARSER.read(M_DEFAULTCONFIG)
|
||
|
if not M_CONFIGPARSER.has_section(section):
|
||
|
M_CONFIGPARSER.add_section(section)
|
||
|
if not M_CONFIGPARSER.has_option(section, option):
|
||
|
M_CONFIGPARSER.set(section, option, key)
|
||
|
with open(M_DEFAULTCONFIG, 'w') as tmp:
|
||
|
M_CONFIGPARSER.write(tmp)
|
||
|
|
||
|
def setConfiguration(section, option, key=""):
|
||
|
if not doesFileExist(M_DEFAULTCONFIG):
|
||
|
addConfiguration(section, option, key)
|
||
|
else:
|
||
|
M_CONFIGPARSER.read(M_CONFIGPARSER)
|
||
|
if not M_CONFIGPARSER.has_section(section):
|
||
|
M_CONFIGPARSER.add_section(section)
|
||
|
M_CONFIGPARSER.set(section, option, key)
|
||
|
with open(M_DEFAULTCONFIG, 'w') as tmp:
|
||
|
M_CONFIGPARSER.write(tmp)
|
||
|
|
||
|
def getConfigurationStr(section, option):
|
||
|
if doesFileExist(M_DEFAULTCONFIG):
|
||
|
M_CONFIGPARSER.read(M_DEFAULTCONFIG)
|
||
|
if M_CONFIGPARSER.has_section(section) and M_CONFIGPARSER.has_option(section, option):
|
||
|
tmp_key = M_CONFIGPARSER.get(section, option)
|
||
|
if tmp_key != "" or tmp_key.lower().find("none") != -1:
|
||
|
return tmp_key
|
||
|
return None
|
||
|
|
||
|
def getConfigurationBool(section, option):
|
||
|
if doesFileExist(M_DEFAULTCONFIG):
|
||
|
M_CONFIGPARSER.read(M_DEFAULTCONFIG)
|
||
|
if M_CONFIGPARSER.has_section(section) and M_CONFIGPARSER.has_option(section, option):
|
||
|
return M_CONFIGPARSER.getboolean(section, option)
|
||
|
return None
|