From 118a3888db7f5095ce8f9d96644131c5a8caa12b Mon Sep 17 00:00:00 2001 From: Danny Colligan Date: Wed, 30 Mar 2016 08:13:59 -0400 Subject: [PATCH] Fix repo manager choking on traililng path slashes Issue #1032 --- ga4gh/repo_manager.py | 23 ++++++++++++++++------- tests/end_to_end/test_repo_manager.py | 4 +++- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/ga4gh/repo_manager.py b/ga4gh/repo_manager.py index 25dbf2b73..a2b583a7a 100644 --- a/ga4gh/repo_manager.py +++ b/ga4gh/repo_manager.py @@ -37,11 +37,20 @@ def filenameWithoutExtension(filepath, extension): (os.path.splitext(filename)[0] messes up on filenames with more than one period) """ - filename = os.path.basename(filepath) + filename = basename(filepath) index = filename.index(extension) return filename[:index] +def basename(path): + """ + Return the final component of a pathname, after normalizing + the path (os.path.basename returns a empty string when given + a path with a trailing slash) + """ + return os.path.basename(os.path.normpath(path)) + + def runCommandSplits(splits, silent=False): """ Run a shell command given the command's parsed command line @@ -319,7 +328,7 @@ def addReferenceSet(self, filePath, moveMode, metadata): self._repoPath, self.referenceSetsDirName, fileName) self._assertPathEmpty(destPath, inRepo=True) os.mkdir(destPath) - fileDestPath = os.path.join(destPath, os.path.basename(filePath)) + fileDestPath = os.path.join(destPath, basename(filePath)) self._moveFile(filePath, fileDestPath, moveMode) # move the index files if they exist, otherwise do indexing @@ -333,11 +342,11 @@ def addReferenceSet(self, filePath, moveMode, metadata): if os.path.exists(indexPathFai) and os.path.exists(indexPathGzi): self._moveFile( indexPathFai, - os.path.join(destPath, os.path.basename(indexPathFai)), + os.path.join(destPath, basename(indexPathFai)), moveMode) self._moveFile( indexPathGzi, - os.path.join(destPath, os.path.basename(indexPathGzi)), + os.path.join(destPath, basename(indexPathGzi)), moveMode) else: runCommand("samtools faidx {}".format(fileDestPath)) @@ -394,7 +403,7 @@ def addReadGroupSet(self, datasetName, filePath, moveMode): self._check() self._checkDataset(datasetName) self._checkFile(filePath, self.bamExtension) - fileName = os.path.basename(filePath) + fileName = basename(filePath) readGroupSetName = filenameWithoutExtension( fileName, self.bamExtension) destPath = os.path.join( @@ -412,7 +421,7 @@ def addReadGroupSet(self, datasetName, filePath, moveMode): dstDir = os.path.split(destPath)[0] self._moveFile( indexPath, - os.path.join(dstDir, os.path.basename(indexPath)), + os.path.join(dstDir, basename(indexPath)), moveMode) else: pysam.index(destPath.encode('utf-8')) @@ -447,7 +456,7 @@ def addVariantSet(self, datasetName, filePath, moveMode): self._check() self._checkDataset(datasetName) self._checkFolder(filePath, self.vcfExtension) - dirName = os.path.basename(filePath) + dirName = basename(filePath) destPath = os.path.join( self._repoPath, self.datasetsDirName, datasetName, self.variantsDirName, dirName) diff --git a/tests/end_to_end/test_repo_manager.py b/tests/end_to_end/test_repo_manager.py index 9861fc148..478d9315d 100644 --- a/tests/end_to_end/test_repo_manager.py +++ b/tests/end_to_end/test_repo_manager.py @@ -38,7 +38,9 @@ def testEndToEnd(self): self._runCmd("add-referenceset", paths.faPath) self._runCmd("add-dataset", self.datasetName) self._runCmd("add-readgroupset", self.datasetName, paths.bamPath) - self._runCmd("add-variantset", self.datasetName, paths.vcfDirPath) + self._runCmd( + "add-variantset", self.datasetName, + paths.vcfDirPath + '/') # ensure we can handle trailing slashes self._runCmd("check", "--skipConsistencyCheck") self._runCmd("list") self._runCmd(