def updateFile(options, sink, target): log(options, "update: %s to: %s" % (sink, target)) if not options.dry_run: # Read only and hidden and system files can not be overridden. try: try: if win32file: filemode = win32file.GetFileAttributesW(target) win32file.SetFileAttributesW( target, filemode & ~win32file.FILE_ATTRIBUTE_READONLY & ~win32file.FILE_ATTRIBUTE_HIDDEN & ~win32file.FILE_ATTRIBUTE_SYSTEM) else: os.chmod(target, stat.S_IWUSR) except: pass shutil.copyfile(sink, target) if options.time: try: s = os.stat(sink) os.utime(target, (s.st_atime, s.st_mtime)) except: logError("Fail to copy timestamp of %s" % sink) # The utime issues with unicode except: logError("Fail to override %s" % sink) if win32file: win32file.SetFileAttributesW(target, filemode)
def updateFile(cookie, sink, target): log(cookie, "update: %s to: %s" % (sink, target)) if not cookie.dry_run: # Read only and hidden and system files can not be overridden. try: try: if win32file: filemode = win32file.GetFileAttributesW(target) win32file.SetFileAttributesW(target, filemode & ~win32file.FILE_ATTRIBUTE_READONLY & ~win32file.FILE_ATTRIBUTE_HIDDEN & ~win32file.FILE_ATTRIBUTE_SYSTEM) else: os.chmod(target, stat.S_IWUSR) except: #logError("Fail to allow override of %s" % target) pass shutil.copyfile(sink, target) if cookie.time: try: s = os.stat(sink) os.utime(target, (s.st_atime, s.st_mtime)); except: logError("Fail to copy timestamp of %s" % sink) # The utime api of the 2.3 version of python is not unicode compliant. except: logError("Fail to override %s" % sink) if win32file: win32file.SetFileAttributesW(target, filemode)
def _rmtree_windows(path): """ Windows-specific rmtree that handles path lengths longer than MAX_PATH. Ported from clobberer.py. """ assert _is_windows() path = os.path.realpath(path) full_path = '\\\\?\\' + path if not os.path.exists(full_path): return if not PYWIN32: if not os.path.isdir(path): return run_cmd('del /F /Q "%s"' % path) else: return run_cmd('rmdir /S /Q "%s"' % path) # Make sure directory is writable win32file.SetFileAttributesW('\\\\?\\' + path, win32file.FILE_ATTRIBUTE_NORMAL) # Since we call rmtree() with a file, sometimes if not os.path.isdir('\\\\?\\' + path): return win32file.DeleteFile('\\\\?\\' + path) for ffrec in win32api.FindFiles('\\\\?\\' + path + '\\*.*'): file_attr = ffrec[0] name = ffrec[8] if name == '.' or name == '..': continue full_name = os.path.join(path, name) if file_attr & win32file.FILE_ATTRIBUTE_DIRECTORY: _rmtree_windows(full_name) else: win32file.SetFileAttributesW('\\\\?\\' + full_name, win32file.FILE_ATTRIBUTE_NORMAL) win32file.DeleteFile('\\\\?\\' + full_name) win32file.RemoveDirectory('\\\\?\\' + path)
def test_chmod_rmtree(self): self._create_temp_file() win32file.SetFileAttributesW(self.temp_file, win32file.FILE_ATTRIBUTE_READONLY) self.s = script.BaseScript(initial_config_file='test/test.json') self.s.rmtree('test_dir') self.assertFalse(os.path.exists('test_dir'), msg="rmtree unsuccessful")
def hide_path(path): try: file_flag = win32file.GetFileAttributesW(path) win32file.SetFileAttributesW( path, file_flag | win32con.FILE_ATTRIBUTE_HIDDEN) except: raise
def removeDirs(path): """Remove directories and files recursively. @param path: path of the base directory. """ if not os.path.isdir(path): return for root, dirs, files in os.walk(path, topdown=False): for d in dirs: try: os.removedirs(os.path.join(root, d)) except: pass for f in files: try: if os.name == 'nt': win32file.SetFileAttributesW( os.path.join(root, f), win32con.FILE_ATTRIBUTE_NORMAL) os.remove(os.path.join(root, f)) except: pass if os.path.isdir(path): try: os.removedirs(path) except: pass
def removeDirsFilter(path, filters=['.pyc', '.pyo']): """Remove directories and files recursively. @param path: path of the base directory. """ def checkFilter(name): for filter in filters: if name.lower().find(filter) != -1: return True return False if not os.path.isdir(path): return for root, dirs, files in os.walk(path, topdown=False): for d in dirs: if checkFilter(os.path.join(root, d)): try: os.removedirs(os.path.join(root, d)) except: pass for f in files: if checkFilter(os.path.join(root, f)): try: if os.name == 'nt': win32file.SetFileAttributesW( os.path.join(root, f), win32con.FILE_ATTRIBUTE_NORMAL) os.remove(os.path.join(root, f)) except: pass
def unhide_path(path): try: if not Filer.is_hidden(path): return file_flag = win32file.GetFileAttributesW(path) win32file.SetFileAttributesW( path, file_flag & ~win32con.FILE_ATTRIBUTE_HIDDEN) except: raise
def prepareRemoveFile(path): if win32file: filemode = win32file.GetFileAttributesW(path) win32file.SetFileAttributesW( path, filemode & ~win32file.FILE_ATTRIBUTE_READONLY & ~win32file.FILE_ATTRIBUTE_HIDDEN & ~win32file.FILE_ATTRIBUTE_SYSTEM) else: os.chmod(path, stat.S_IWUSR)
def RMFile(path): """Remove a file. @param path: File path. """ if os.path.isfile(path): try: if os.name == 'nt': win32file.SetFileAttributesW(path, win32con.FILE_ATTRIBUTE_NORMAL) os.remove(path) except: pass
def _check_blacklisted_dir(self): """Create the temporary directory, if it doesn't exist. """ blacklisted_dir = self.absolute_pathname(BLACKLISTED_DIR) if self._check_warebox(): if os.path.exists(blacklisted_dir) and os.path.isdir(blacklisted_dir): if sys.platform.startswith('win'): import win32con import win32file #make the file hidden win32file.SetFileAttributesW( blacklisted_dir, win32con.FILE_ATTRIBUTE_HIDDEN) return True elif os.path.exists(blacklisted_dir) and not os.path.isdir(blacklisted_dir): os.unlink(blacklisted_dir) elif not os.path.exists(blacklisted_dir): self.make_directory(BLACKLISTED_DIR) if sys.platform.startswith('win'): import win32con import win32file #make the file hidden win32file.SetFileAttributesW( blacklisted_dir, win32con.FILE_ATTRIBUTE_HIDDEN)
def delete_file(self, name): if self.job.simulate: self.verbose(_("Must delete %s"), name) self.job.count_delete_file += 1 return self.verbose(_("Deleting %s"), name) if win32file: filemode = win32file.GetFileAttributesW(name) try: win32file.SetFileAttributesW( name, filemode & \ ~win32file.FILE_ATTRIBUTE_READONLY & \ ~win32file.FILE_ATTRIBUTE_HIDDEN & \ ~win32file.FILE_ATTRIBUTE_SYSTEM) except win32file.error, e: self.error(name + " : SetFileAttributesW() failed")
def pm_removeDirTree(self, dirPath, force=False, errorHandler=None): """ Recusrively removes files and folders from a given path @param dirPath: path of the dir @param force: boolean parameter indicating that folders containing hidden files will also be deleted """ if(j.sal.fs.exists(dirPath)): if j.sal.fs.isDir(dirPath): if force: fileMode = win32file.GetFileAttributesW(dirPath) for file in j.sal.fswalk.walk(dirPath, recurse=1): self.logger.info('Changing attributes on %s' % fileMode) win32file.SetFileAttributesW(file, fileMode & ~win32file.FILE_ATTRIBUTE_HIDDEN) if errorHandler is not None: shutil.rmtree(dirPath, onerror=errorHandler) else: shutil.rmtree(dirPath) else: raise ValueError("Specified path: %s is not a Directory in System.removeDirTree" % dirPath)
def removeFile(cookie, target): # Read only files could not be deleted. log(cookie, "remove: %s" % target) if not cookie.dry_run: try: try: if win32file: filemode = win32file.GetFileAttributesW(target) win32file.SetFileAttributesW( target, filemode & \ ~win32file.FILE_ATTRIBUTE_READONLY & \ ~win32file.FILE_ATTRIBUTE_HIDDEN & \ ~win32file.FILE_ATTRIBUTE_SYSTEM) else: os.chmod(target, stat.S_IWUSR) except: #logError("Fail to allow removal of %s" % target) pass os.remove(target) except: logError("Fail to remove %s" % target)
self.job.count_same += 1 else: self.job.done_same += 1 self.debug(_("%s is up-to-date"), target) return if self.job.simulate: self.job.count_update_file += 1 self.verbose("Must update %s", target) return if win32file: filemode = win32file.GetFileAttributesW(target) win32file.SetFileAttributesW( target, filemode & \ ~win32file.FILE_ATTRIBUTE_READONLY & \ ~win32file.FILE_ATTRIBUTE_HIDDEN & \ ~win32file.FILE_ATTRIBUTE_SYSTEM) else: os.chmod(target, stat.S_IWUSR) #self.copy_file(src,target) self.verbose(_("Updating %s"), target) try: shutil.copyfile(src, target) except IOError, e: self.error("copyfile('%s','%s') failed", src, target) return self.utime(src, target)