def _doInstallExternal(self, ball): # Currently we use a temporary directory and extractall() then copy: # this is very slow. The Python documentation warns more sophisticated # approaches have pitfalls without specifying what they are. tf = cautils.open_tarfile(ball, self.__dosXz); members = tf.getmembers(); tempdir = tempfile.mkdtemp(); try: tf.extractall(tempdir); for m in members: if m.isdir(): path = self.__pm.mapPath("/" + m.name); if not os.path.exists(path): os.makedirs(path, m.mode); for m in members: if m.isdir(): path = self.__pm.mapPath("/" + m.name); if not os.path.exists(path): os.makedirs(path, m.mode); else: path = self.__pm.mapPath("/" + m.name); dirname = os.path.dirname(path); if not os.path.exists(dirname): os.makedirs(dirname); if os.path.exists(path): os.chmod(path, 0o777); os.remove(path); # Windows extract() is robust but can't form Cygwin links # (It produces copies instead: bulky and bugbait.) # Convert to links if possible -- depends on coreutils being installed if m.issym() and self.__lnExists: link_target = m.linkname; Process([ self.__dosLn, "-s", link_target, path ]).run(True); elif m.islnk() and self.__lnExists: # Hard link -- expect these to be very rare link_target = m.linkname; mapped_target = self.__pm.mapPath("/" + m.linkname); # Must ensure target exists before forming hard link if not os.path.exists(mapped_target): shutil.move( os.path.join(tempdir, link_target), mapped_target ); Process([ self.__dosLn, mapped_target, path ]).run(True); else: shutil.move(os.path.join(tempdir, m.name), path); finally: tf.close(); cautils.rmtree(tempdir);
def testRmtreeIgnoresNonExistingFiles(self): basePath = self._getTmpDir()+os.path.sep; os.mkdir(basePath+"dir"); utils.rmtree(basePath+"dir"); utils.rmtree(basePath+"file"); self.assertFalse(os.path.isdir(basePath+"dir"));
def testRmtreeCleansFilesAndDirectoriesIteratively(self): basePath = os.path.join(self._getTmpDir(), "directory")+os.path.sep; os.mkdir(basePath); os.mkdir(basePath+"dir"); open(basePath+"file", 'w').close(); utils.rmtree(basePath); self.assertFalse(os.path.isdir(basePath));
def testRmtreeCleansFilesAndDirectories(self): basePath = self._getTmpDir()+os.path.sep; os.mkdir(basePath+"dir"); open(basePath+"file", 'w').close(); utils.rmtree(basePath+"dir"); utils.rmtree(basePath+"file"); self.assertFalse(os.path.isdir(basePath+"dir")); self.assertFalse(os.path.isfile(basePath+"file"));
def testRmtreeCleansInvalidLinks(self): if not hasattr(os, "symlink") : self.skipTest("symlink is not supported"); basePath = self._getTmpDir()+os.path.sep; # create symlink to unexisting file os.symlink(basePath+"file", basePath+"link"); utils.rmtree(basePath+"link"); self.assertFalse(os.path.islink(basePath+"link"));
def testRmtreeCleansValidLinksToDirectory(self): if not hasattr(os, "symlink") : self.skipTest("symlink is not supported"); basePath = self._getTmpDir()+os.path.sep; os.mkdir(basePath+"dir"); os.symlink(basePath+"dir", basePath+"link"); utils.rmtree(basePath+"link"); self.assertTrue(os.path.isdir(basePath+"dir")); self.assertFalse(os.path.islink(basePath+"link"));
def testRmtreeCleansValidLinksToFile(self): if not hasattr(os, "symlink") : self.skipTest("symlink is not supported"); basePath = self._getTmpDir()+os.path.sep; open(basePath+"file", 'w').close(); os.symlink(basePath+"file", basePath+"link"); utils.rmtree(basePath+"link"); self.assertTrue(os.path.isfile(basePath+"file")); self.assertFalse(os.path.islink(basePath+"link"));
def testRmtree(self): # create tree root = os.path.join(self._getTmpDir(), "root~"); subdir = os.path.join(root, "subdir~"); rootfile = os.path.join(root, "file~"); subdirfile = os.path.join(subdir, "file~"); def build_tree(): if not os.path.exists(subdir): os.makedirs(subdir); f = open(rootfile, 'w'); e = open(subdirfile, 'w'); f.close(); e.close(); def rm_tree(): if os.path.exists(rootfile): os.unlink(rootfile); if os.path.exists(rootfile): os.unlink(subdirfile); if os.path.exists(rootfile): os.rmdir(subdir); if os.path.exists(rootfile): os.rmdir(root); build_tree(); utils.rmtree(root); ok = False; if os.path.exists(subdirfile) or \ os.path.exists(subdir) or \ os.path.exists(rootfile) or \ os.path.exists(root): ok = False; else: ok = True; self.assertTrue(ok); rm_tree(); build_tree(); utils.rmtree(rootfile); ok = False; if os.path.exists(subdirfile) and \ os.path.exists(subdir) and \ not os.path.exists(rootfile) and \ os.path.exists(root): ok = True; else: ok = False; self.assertTrue(ok); rm_tree();
def testRmtreeCleansWithoutPermissionIteratively(self): basePath = os.path.join(self._getTmpDir(), "directory")+os.path.sep; os.mkdir(basePath); os.mkdir(basePath+"dir"); open(basePath+"file", 'w').close(); # Removes permissions os.chmod(basePath+"dir", 0o000); os.chmod(basePath+"file", 0o000); utils.rmtree(basePath); self.assertFalse(os.path.isdir(basePath));
def testRmtreeCleansInvalidLinksIteratively(self): if not hasattr(os, "symlink") : self.skipTest("symlink is not supported"); basePath = os.path.join(self._getTmpDir(), "directory")+os.path.sep; os.mkdir(basePath); os.mkdir(basePath+"dir"); # create symlink to unexisting file os.symlink(basePath+"file", basePath+"link"); utils.rmtree(basePath); self.assertFalse(os.path.isdir(basePath));
def _doInstallExternal(self, ball): # Currently we use a temporary directory and extractall() then copy: # this is very slow. The Python documentation warns more sophisticated # approaches have pitfalls without specifying what they are. tf = cautils.open_tarfile(ball, self.__dosXz) members = tf.getmembers() tempdir = tempfile.mkdtemp() try: tf.extractall(tempdir) for m in members: if m.isdir(): path = self.__pm.mapPath("/" + m.name) if not os.path.exists(path): os.makedirs(path, m.mode) for m in members: if m.isdir(): path = self.__pm.mapPath("/" + m.name) if not os.path.exists(path): os.makedirs(path, m.mode) else: path = self.__pm.mapPath("/" + m.name) dirname = os.path.dirname(path) if not os.path.exists(dirname): os.makedirs(dirname) if os.path.exists(path): os.chmod(path, 0o777) os.remove(path) # Windows extract() is robust but can't form Cygwin links # (It produces copies instead: bulky and bugbait.) # Convert to links if possible -- depends on coreutils being installed if m.issym() and self.__lnExists: link_target = m.linkname Process([self.__dosLn, "-s", link_target, path]).run(True) elif m.islnk() and self.__lnExists: # Hard link -- expect these to be very rare link_target = m.linkname mapped_target = self.__pm.mapPath("/" + m.linkname) # Must ensure target exists before forming hard link if not os.path.exists(mapped_target): shutil.move(os.path.join(tempdir, link_target), mapped_target) Process([self.__dosLn, mapped_target, path]).run(True) else: shutil.move(os.path.join(tempdir, m.name), path) finally: tf.close() cautils.rmtree(tempdir)
def testRmtreeKeepsTargetLinkPermissionsToDirectory(self): if not hasattr(os, "symlink") : self.skipTest("symlink is not supported"); basePath = self._getTmpDir()+os.path.sep; os.mkdir(basePath+"dir"); os.symlink(basePath+"dir", basePath+"link"); # Removes permissions os.chmod(basePath+"dir", 0o000); fileMode = os.stat(basePath+"dir")[stat.ST_MODE]; utils.rmtree(basePath+"link"); self.assertTrue(os.path.isdir(basePath+"dir")); self.assertEqual(fileMode, os.stat(basePath+"dir")[stat.ST_MODE]);