def testVisitPath(self): f.makedirs('TestDir/a/b/c/d/e/f') fi = open('TestDir/a/b/c/d/D0','w') fi.write("hi\n") fi.close seen = set() def collector(x): seen.add(x) top = 'TestDir/a' last = 'TestDir/a/b/c/d' absTop = os.path.normpath(top) expected = set([absTop]) for i in [['b'],['b','c'],['b','c','d']]: expected.add(os.path.join(absTop,os.sep.join(i))) f.visitPath(top,last,collector) assert expected == seen, 'but x-s=%s and s-x=%s'%(expected-seen,seen-expected) seen.clear() top = 'TestDir/a/b' last = 'TestDir/a/b/c/d/D0' normTop = os.path.normpath(top) expected = set([normTop]) for i in [['c'],['c','d']]: expected.add(os.path.join(normTop,os.sep.join(i))) f.visitPath(top,last,collector) assert expected == seen, 'but x-s=%s and s-x=%s'%(expected-seen,seen-expected) #Test for non-existent leaf assert_raises(OSError,f.visitPath,'TestDir','TestDir/A/BB',collector) #Test for rootDir not abover fullPath assert_raises(OSError,f.visitPath,'TestDir/A/B','TestDir/A',collector)
def makeNameDir(self, ooid, timestamp=None): """Make sure the name directory exists, and return its path, and list of path components. Raises OSError on failure""" npath, nparts = self.namePath(ooid, timestamp) #self.logger.debug( # "%s - trying makedirs %s", # threading.currentThread().getName(), # npath #) um = self.osModule.umask(0) try: try: socorro_fs.makedirs(npath, self.dirPermissions, self.osModule) except OSError: if not self.osModule.path.isdir(npath): raise finally: self.osModule.umask(um) if self.dumpGID: socorro_fs.visitPath( os.path.join(*nparts[:2]), npath, self.chownGidVisitor ) return npath, nparts
def makeDateDir(self, date, webheadName=None): """Assure existence of date directory for the given date, return path, and list of components""" dpath, dparts = self.datePath(date, webheadName) um = self.osModule.umask(0) try: try: socorro_fs.makedirs(dpath, self.dirPermissions, self.osModule) except OSError, e: if not self.osModule.path.isdir(dpath): #self.logger.debug( # "%s - in makeDateDir, got not isdir(%s): %s", # threading.currentThread().getName(), # dpath, # e #) raise finally: self.osModule.umask(um) if self.dumpGID: socorro_fs.visitPath( os.path.join(*dparts[:2]), dpath, self.chownGidVisitor ) return dpath, dparts
def makeDateDir(self, date, webheadName=None): """Assure existence of date directory for the given date, return path, and list of components""" dpath, dparts = self.datePath(date, webheadName) um = self.osModule.umask(0) try: try: socorro_fs.makedirs(dpath, self.dirPermissions, self.osModule) except OSError: if not self.osModule.path.isdir(dpath): raise finally: self.osModule.umask(um) if self.dumpGID: socorro_fs.visitPath( os.path.join(*dparts[:2]), dpath, self.chownGidVisitor ) return dpath, dparts
def testFailMakedirsOnFileInPath(self): path = 'TestDir/1/2/3/4' tpath = path while True: head,tail = os.path.split(tpath) if tail == 'TestDir': break try: shutil.rmtree('TestDir') except: pass f.makedirs(head) t = open(tpath,'w') t.write('nothing\n') t.close() try: f.makedirs(path) assert False, 'We should have had an OSError, but success for %s a file'%tpath except OSError: pass except Exception,x: assert False, 'We should have had an OSError, got %s: %s'%(type(x),x) tpath = head
def makeDateDir(self, date, webheadName=None): """Assure existence of date directory for the given date, return path, and list of components""" dpath, dparts = self.datePath(date, webheadName) um = self.osModule.umask(0) try: try: socorro_fs.makedirs(dpath, self.dirPermissions, self.osModule) except OSError, e: if not self.osModule.path.isdir(dpath): #self.logger.debug( # "%s - in makeDateDir, got not isdir(%s): %s", # threading.currentThread().getName(), # dpath, # e #) raise finally: self.osModule.umask(um) if self.dumpGID: socorro_fs.visitPath(os.path.join(*dparts[:2]), dpath, self.chownGidVisitor) return dpath, dparts
def testCleanEmptySubdirectories(self): f.makedirs('TestDir/A/B/C/D') f.makedirs('TestDir/AA/BB/C') f.makedirs('TestDir/AA/BB/CC/DD') fi = open('TestDir/A/a', 'w') fi.write('file a\n') fi.close() # Test short-circuit path, full stopper assert os.path.isdir('TestDir/A/B/C/D') f.cleanEmptySubdirectories('TestDir/A/B/C/D', 'TestDir/A/B/C/D') assert os.path.isdir('TestDir/A/B/C/D') # Test short-circuit path, name stopper f.cleanEmptySubdirectories('D', 'TestDir/A/B/C/D') assert os.path.isdir('TestDir/A/B/C/D') # Test some empties, name stopper f.cleanEmptySubdirectories('C', 'TestDir/A/B/C/D') assert not os.path.exists('TestDir/A/B/C/D') assert os.path.isdir('TestDir/A/B/C') # Test some empties, path stopper f.cleanEmptySubdirectories('TestDir/A/B', 'TestDir/A/B/C') assert not os.path.exists('TestDir/A/B/C') assert os.path.isdir('TestDir/A/B') #Test stopping on a file in a subdir f.cleanEmptySubdirectories('TestDir', 'TestDir/A/B') assert not os.path.exists('TestDir/A/B') assert os.path.isdir('TestDir/A') #Test stopping on another subdir f.cleanEmptySubdirectories('TestDir/AA', 'TestDir/AA/BB/CC/DD') assert not os.path.exists('TestDir/AA/BB/CC') assert os.path.isdir('TestDir/AA/BB') #Test for stopper not in path assert_raises(OSError, f.cleanEmptySubdirectories, 'Woo', 'TestDir/AA/BB') #Test for non-existent leaf assert_raises(OSError, f.cleanEmptySubdirectories, 'TestDir', 'TestDir/AA/BB/CC/DD')
def testCleanEmptySubdirectories(self): f.makedirs('TestDir/A/B/C/D') f.makedirs('TestDir/AA/BB/C') f.makedirs('TestDir/AA/BB/CC/DD') fi = open('TestDir/A/a','w') fi.write('file a\n') fi.close() # Test short-circuit path, full stopper assert os.path.isdir('TestDir/A/B/C/D') f.cleanEmptySubdirectories('TestDir/A/B/C/D','TestDir/A/B/C/D') assert os.path.isdir('TestDir/A/B/C/D') # Test short-circuit path, name stopper f.cleanEmptySubdirectories('D','TestDir/A/B/C/D') assert os.path.isdir('TestDir/A/B/C/D') # Test some empties, name stopper f.cleanEmptySubdirectories('C','TestDir/A/B/C/D') assert not os.path.exists('TestDir/A/B/C/D') assert os.path.isdir('TestDir/A/B/C') # Test some empties, path stopper f.cleanEmptySubdirectories('TestDir/A/B','TestDir/A/B/C') assert not os.path.exists('TestDir/A/B/C') assert os.path.isdir('TestDir/A/B') #Test stopping on a file in a subdir f.cleanEmptySubdirectories('TestDir','TestDir/A/B') assert not os.path.exists('TestDir/A/B') assert os.path.isdir('TestDir/A') #Test stopping on another subdir f.cleanEmptySubdirectories('TestDir/AA','TestDir/AA/BB/CC/DD') assert not os.path.exists('TestDir/AA/BB/CC') assert os.path.isdir('TestDir/AA/BB') #Test for stopper not in path assert_raises(OSError,f.cleanEmptySubdirectories,'Woo','TestDir/AA/BB') #Test for non-existent leaf assert_raises(OSError,f.cleanEmptySubdirectories,'TestDir','TestDir/AA/BB/CC/DD')