def transport(**kwargs): srcPath = os.path.dirname(kwargs["packetZeroPath"]) baseFileName = os.path.basename(kwargs["packetZeroPath"])[:-2] def scpStr(index): s = "scp -P " + kwargs["port"] + " " + kwargs["user"] + "@" + kwargs["host"] + ":" s += os.path.join(srcPath, baseFileName + "." + str(index)) s += " " + kwargs["dstPath"] return s if not os.path.exists(os.path.join(kwargs["dstPath"], baseFileName + ".0")): pUtils.createDirectory(kwargs["dstPath"]) cmd = scpStr(0) t = pUtils.runProgram(cmd, True) if t["returnCode"] != 0: return {"retCode": 2, "errMsg": "Unable to retrieve packet zero", "debug": t, "cmd": cmd} fileFullPath = os.path.join(kwargs["dstPath"], baseFileName + ".0") manifestData = json.loads(pUtils.quickFileRead(fileFullPath)) sliceAmount = manifestData["sliceAmount"] checksumDict = manifestData["checksumDict"] for i in range(1, sliceAmount + 1): fileFullPath = os.path.join(kwargs["dstPath"], baseFileName + "." + str(i)) if os.path.exists(fileFullPath): fileSha1 = pUtils.getFileSha1(fileFullPath) if fileSha1 == checksumDict[baseFileName + "." + str(i)]: continue cmd = scpStr(i) t = None retryCounter = 0 while True: retryCounter += 1 if retryCounter > int(kwargs["try"]): return {"retCode": 1, "errMsg": "Max retry reached", "debug": t, "cmd": cmd} t = pUtils.runProgram(cmd, True) if t["returnCode"] != 0: continue fileSha1 = pUtils.getFileSha1(fileFullPath) if fileSha1 != checksumDict[baseFileName + "." + str(i)]: continue break return {"retCode": 0, "errMsg": None}
def test_fileOperations_1(self): testDirFullPath = tempfile.mkdtemp('','unitTest_pUtils_') self.assertEqual(os.path.exists(testDirFullPath),True) string = 'print \'Hello World!\'' pUtils.quickFileWrite(os.path.join(testDirFullPath,'hw.py'),string) data = pUtils.quickFileRead(os.path.join(testDirFullPath,'hw.py')) self.assertEqual(string,data) self.assertEqual(pUtils.getFileSha1(os.path.join(testDirFullPath,'hw.py')),'a849bee4b303051f907d64b6c461ee6c699c3e79') pUtils.pSlice(os.path.join(testDirFullPath,'hw.py'),testDirFullPath,1) self.assertEqual(len(pUtils.filterListByRegex(os.listdir(testDirFullPath),r'hw\.py\.[0-9]+')),21) os.remove(os.path.join(testDirFullPath,'hw.py')) self.assertEqual(os.path.exists(os.path.join(testDirFullPath,'hw.py')),False) pUtils.pUnSlice(os.path.join(testDirFullPath,'hw.py.0'),os.path.join(testDirFullPath,'hw.py')) self.assertEqual(os.path.exists(os.path.join(testDirFullPath,'hw.py')),True) self.assertEqual(pUtils.quickFileRead(os.path.join(testDirFullPath,'hw.py')),string) pUtils.createZipFile(testDirFullPath,['hw.py'],os.path.join(testDirFullPath,'aFile.zip')) self.assertEqual(os.path.exists(os.path.join(testDirFullPath,'aFile.zip')),True) os.remove(os.path.join(testDirFullPath,'hw.py')) self.assertEqual(os.path.exists(os.path.join(testDirFullPath,'hw.py')),False) pUtils.unzipFile(os.path.join(testDirFullPath,'aFile.zip'),testDirFullPath) self.assertEqual(os.path.exists(os.path.join(testDirFullPath,'hw.py')),True) self.assertEqual(pUtils.quickFileRead(os.path.join(testDirFullPath,'hw.py')),string) pUtils.emptyDirectory(testDirFullPath) self.assertEqual(len(os.listdir(testDirFullPath)),0) pUtils.createDirectory(os.path.join(testDirFullPath,'ttt','ttt2')) self.assertEqual(os.path.exists(os.path.join(testDirFullPath,'ttt','ttt2')),True) pUtils.removeDirectory(testDirFullPath) self.assertEqual(os.path.exists(testDirFullPath),False)
def verify(**kwargs): directory = os.path.dirname(kwargs["manifestFileFullPath"]) manifestData = json.loads(pUtils.quickFileRead(kwargs["manifestFileFullPath"])) for fileName, checksum in manifestData["checksumDict"].iteritems(): fileFullPath = os.path.join(directory, fileName) if pUtils.getFileSha1(fileFullPath) != checksum: return {"retCode": 1, "errMsg": "Checksum missmatch: " + fileName} return {"retCode": 0, "errMsg": None}
def createManifest(manifestFileName, inDirectoryFullPath): fileNameList = os.listdir(inDirectoryFullPath) d = {} dc = {} d["checksumDict"] = dc for fileName in fileNameList: if fileName == manifestFileName: continue fileFullPath = os.path.join(inDirectoryFullPath, fileName) t = pUtils.getFileSha1(fileFullPath) dc[fileName] = t d["meta-data"] = {} d["meta-data"]["timeStamp_utc"] = pUtils.getTimeStamp() d["meta-data"]["name"] = os.path.basename(inDirectoryFullPath) fileFullPath = os.path.join(inDirectoryFullPath, manifestFileName) pUtils.quickFileWrite(fileFullPath, json.dumps(d)) return {"retCode": 0, "errMsg": None}