1. Written portable CopyFile() & used in extractData.copyBundle() 2. Renamed GeneralFunctions to portableCode for better clarity that it can be used in any software and not specific to this program

This commit is contained in:
Manish 2020-05-01 11:46:47 +10:00
parent c24ea8311d
commit 25a2c214fa
7 changed files with 43 additions and 35 deletions

View File

@ -1,18 +0,0 @@
import os
def CallFuncInDir(Directory, Function, *args, **kwArgs):
CurrentDir = os.getcwd()
os.chdir(Directory)
Function(*args, **kwArgs)
os.chdir(CurrentDir)
# return True if operation succesful and False if failed
def CreateDir(Directory):
if not os.path.isfile(Directory):
if not os.path.isdir(Directory):
os.mkdir(Directory)
return True
else:
return False

View File

@ -14,14 +14,17 @@ import sys
from urllib.parse import quote as strToHtmlFmt
import zipfile
from GeneralFunctions.DataStructureManipulations import (
from portableCode.DataStructureManipulations import (
StrListToDictionary
)
from GeneralFunctions.InputOutput import (
from portableCode.InputOutput import (
WriteTextFiles,
WriteBinaryToFile
)
from GeneralFunctions.OS import CreateDir
from portableCode.OS import (
CreateDir,
CopyFile
)
""" FIXME: paths hard coded UNIX style & most likely will not work on Windows.
@ -40,20 +43,7 @@ class extractData:
def copyBundle(self, source, activityName):
destination = self.websiteDir+"bundles/"+activityName+".xo"
# move this code block to a portable function
# copyFile(source, destination)
try:
# hard link if on same partition
os.link(source, destination)
except FileExistsError:
# FIXME: create a portable function to compare if two files
# are same and use it here
pass
except OSError:
# copy if on different partition
shutil.copy2(source, destination)
except Exception as unknownError:
raise unknownError
CopyFile(source, destination)
def createDirectories(self):
assert CreateDir(self.websiteDir+"app")

View File

@ -0,0 +1,36 @@
import os
def CallFuncInDir(Directory, Function, *args, **kwArgs):
CurrentDir = os.getcwd()
os.chdir(Directory)
Function(*args, **kwArgs)
os.chdir(CurrentDir)
def CreateDir(Directory):
""" return True if operation succesful and False if failed """
if not os.path.isfile(Directory):
if not os.path.isdir(Directory):
os.mkdir(Directory)
return True
else:
return False
def CopyFile(Source, Destination):
""" raises exception if fails.
Implement exception handling else program will abort.
"""
try:
# hard link if on same partition
os.link(Source, Destination)
except FileExistsError:
# FIXME: create a function to compare if two files
# are same and use it here
pass
except OSError:
# copy if on different partition
shutil.copy2(Source, Destination)
except Exception as unknownError:
raise unknownError