def versionDocument(self, fileName): # add a version to the new document curVer = self.getLatestVersion() newVer = curVer + 1 # update latest version in json schema and save self.project['latestVersion'] = newVer self.editProject('latestVersion', newVer, permission=True) # split fileName into name and extension fn = os.path.splitext(os.path.basename(fileName))[0] fext = os.path.splitext(os.path.basename(fileName))[1] # new version newName = '{0}_ver_{1}{2}'.format(fn, str(newVer), fext) zf = zipfile.ZipFile(glb.versionsDirectory() + self.project['guid'] + '.zip', mode='a') try: zf.write(fileName, arcname=newName) except zipfile.error: print str(zipfile.error) finally: self.save() print 'Appended successfully' zf.close()
def getFilesInArchive(self): # get list of all files in archive fn = glb.versionsDirectory() + self.project['guid'] + '.zip' zf = zipfile.ZipFile(fn, 'r') nl = zf.namelist() zf.close() return nl
def compareFiles(self, fList): self.currentDiff = [] # open files from archive and compare arc = zipfile.ZipFile( glb.versionsDirectory() + self.project['guid'] + '.zip', 'r') linesF1 = arc.open(fList[0]).readlines() linesF1 = [x.strip() for x in linesF1] linesF2 = arc.open(fList[1]).readlines() linesF2 = [x.strip() for x in linesF2] arc.close() # length to iterate over diffLength = len(linesF1) if len(linesF1) > len(linesF2) else len( linesF2) for f1 in range(len(linesF1)): if len(linesF1[f1]) < 1: linesF1[f1] = '<br />\n' for f2 in range(len(linesF2)): if len(linesF2[f2]) < 1: linesF2[f2] = '<br />\n' # pad file that has less lines if len(linesF1) > len(linesF2): add = len(linesF1) - len(linesF2) for x in range(0, add): linesF2.append('<br />\n') elif len(linesF1) < len(linesF2): add = len(linesF2) - len(linesF1) for x in range(0, add): linesF1.append('<br />\n') # compare for x in range(0, diffLength): difference = False if linesF1[x] == linesF2[x] else True diffType = "" if difference is False else ( "+" if len(linesF1[x]) < len(linesF2[x]) else "-") self.currentDiff.append({ "firstFile": linesF1[x], "secondFile": linesF2[x], "difference": difference, "type": diffType })
def create(self, project, f): # create a project self.project['name'] = project['name'] self.project['description'] = project['description'] self.project['author'] = project['author'] self.project['createDate'] = glb.getTodaysDateAsString() self.project['guid'] = str(uuid.uuid4()) self.project['latestVersion'] = 1 # create a zip directory and place file in it fn = os.path.splitext( os.path.basename(f))[0] + '_ver_1' + os.path.splitext( os.path.basename(f))[1] zf = zipfile.ZipFile(glb.versionsDirectory() + self.project['guid'] + '.zip', mode='w') try: zf.write(f, arcname=fn) except zipfile.error: print str(zipfile.error) finally: print 'Zip file created!' zf.close() self.save()
def deleteProject(self): # first delete project version file, then delete project json file os.remove(glb.versionsDirectory() + self.project['guid'] + '.zip') os.remove(glb.projectDirectory() + self.project['name'] + '.json')