From 25a2c214fa4f79ba6653dad6317aba5d0bf9d457 Mon Sep 17 00:00:00 2001 From: Manish Date: Fri, 1 May 2020 11:46:47 +1000 Subject: [PATCH] 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 --- generator/GeneralFunctions/OS.py | 18 ---------- generator/main.py | 24 ++++--------- .../DataStructureManipulations.py | 0 .../InputOutput.py | 0 .../Network.py | 0 generator/portableCode/OS.py | 36 +++++++++++++++++++ .../__init__.py | 0 7 files changed, 43 insertions(+), 35 deletions(-) delete mode 100644 generator/GeneralFunctions/OS.py rename generator/{GeneralFunctions => portableCode}/DataStructureManipulations.py (100%) rename generator/{GeneralFunctions => portableCode}/InputOutput.py (100%) rename generator/{GeneralFunctions => portableCode}/Network.py (100%) create mode 100644 generator/portableCode/OS.py rename generator/{GeneralFunctions => portableCode}/__init__.py (100%) diff --git a/generator/GeneralFunctions/OS.py b/generator/GeneralFunctions/OS.py deleted file mode 100644 index 152527e..0000000 --- a/generator/GeneralFunctions/OS.py +++ /dev/null @@ -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 diff --git a/generator/main.py b/generator/main.py index 8bf118d..144ba81 100644 --- a/generator/main.py +++ b/generator/main.py @@ -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") diff --git a/generator/GeneralFunctions/DataStructureManipulations.py b/generator/portableCode/DataStructureManipulations.py similarity index 100% rename from generator/GeneralFunctions/DataStructureManipulations.py rename to generator/portableCode/DataStructureManipulations.py diff --git a/generator/GeneralFunctions/InputOutput.py b/generator/portableCode/InputOutput.py similarity index 100% rename from generator/GeneralFunctions/InputOutput.py rename to generator/portableCode/InputOutput.py diff --git a/generator/GeneralFunctions/Network.py b/generator/portableCode/Network.py similarity index 100% rename from generator/GeneralFunctions/Network.py rename to generator/portableCode/Network.py diff --git a/generator/portableCode/OS.py b/generator/portableCode/OS.py new file mode 100644 index 0000000..bc89237 --- /dev/null +++ b/generator/portableCode/OS.py @@ -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 diff --git a/generator/GeneralFunctions/__init__.py b/generator/portableCode/__init__.py similarity index 100% rename from generator/GeneralFunctions/__init__.py rename to generator/portableCode/__init__.py