def test_create_already_exists_not_dir (self): with self.assertRaises (fs.x_fs): # # If the name is already used by a file, create will raise x_fs # filepath = os.path.join (utils.TEST_ROOT, uuid.uuid1 ().hex) open (filepath, "w").close () self.assertTrue (os.path.isfile (filepath)) fs.dir (filepath).create ()
def test_create_already_exists_not_dir(self): with self.assertRaises(fs.x_fs): # # If the name is already used by a file, create will raise x_fs # filepath = os.path.join(utils.TEST_ROOT, uuid.uuid1().hex) open(filepath, "w").close() self.assertTrue(os.path.isfile(filepath)) fs.dir(filepath).create()
def test_flat_with_dirs(self): filepath = utils.TEST_ROOT filepath2 = os.path.join(filepath, "d") self.assertEquals( set(fs.dir(filepath).flat(includedirs=True)), utils.dirs_in(filepath) | utils.files_in(filepath) | utils.dirs_in(filepath2) | utils.files_in(filepath2))
def test_flat_with_dirs (self): filepath = utils.TEST_ROOT filepath2 = os.path.join (filepath, "d") self.assertEquals ( set (fs.dir (filepath).flat (includedirs=True)), utils.dirs_in (filepath) | utils.files_in (filepath) | utils.dirs_in (filepath2) | utils.files_in (filepath2) )
def test_create_already_exists_dir(self): # # If the dir already exists, create will succeed silently # and will return the normalised path of the dir. # filepath = os.path.join(utils.TEST_ROOT, uuid.uuid1().hex) os.mkdir(filepath) self.assertTrue(os.path.isdir(filepath)) d = fs.dir(filepath) self.assertEquals(fs.normalised(d), fs.normalised(d.create()))
def searcher(root, pattern, text): for dir, dirs, files in fs.dir(root).walk(ignore_access_errors=True): yield dir.filepath for f in files: if f.like(pattern): try: if text in open(f.filepath).read(): files_found.append(f) except fs.exc.x_access_denied: pass
def test_create_already_exists_dir (self): # # If the dir already exists, create will succeed silently # and will return the normalised path of the dir. # filepath = os.path.join (utils.TEST_ROOT, uuid.uuid1 ().hex) os.mkdir (filepath) self.assertTrue (os.path.isdir (filepath)) d = fs.dir (filepath) self.assertEquals (fs.normalised (d), fs.normalised (d.create ()))
def searcher (root, pattern, text): for dir, dirs, files in fs.dir (root).walk (ignore_access_errors=True): yield dir.filepath for f in files: if f.like (pattern): try: if text in open (f.filepath).read (): files_found.append (f) except fs.exc.x_access_denied: pass
def test_zip (self): import zipfile filepath = utils.TEST_ROOT zipped = fs.dir (filepath).zip () unzip_filepath = os.path.join (utils.TEST_ROOT2) os.mkdir (unzip_filepath) zipfile.ZipFile (zipped).extractall (unzip_filepath) self.assertEquals ( set (f.relative_to (filepath) for f in fs.flat (filepath)), set (f.relative_to (unzip_filepath) for f in fs.flat (unzip_filepath)) )
def test_zip(self): import zipfile filepath = utils.TEST_ROOT zipped = fs.dir(filepath).zip() unzip_filepath = os.path.join(utils.TEST_ROOT2) os.mkdir(unzip_filepath) zipfile.ZipFile(zipped).extractall(unzip_filepath) self.assertEquals( set(f.relative_to(filepath) for f in fs.flat(filepath)), set( f.relative_to(unzip_filepath) for f in fs.flat(unzip_filepath)))
def test_walk(self): filepath = utils.TEST_ROOT walker = fs.dir(filepath).walk() dirpath, dirs, files = next(walker) self.assertEquals(dirpath, filepath + "\\") self.assertEquals(set(dirs), utils.dirs_in(filepath)) self.assertEquals(set(files), utils.files_in(filepath)) filepath = os.path.join(filepath, "d") dirpath, dirs, files = next(walker) self.assertEquals(dirpath, filepath + "\\") self.assertEquals(set(dirs), utils.dirs_in(filepath)) self.assertEquals(set(files), utils.files_in(filepath))
def test_walk (self): filepath = utils.TEST_ROOT walker = fs.dir (filepath).walk () dirpath, dirs, files = next (walker) self.assertEquals (dirpath, filepath + "\\") self.assertEquals (set (dirs), utils.dirs_in (filepath)) self.assertEquals (set (files), utils.files_in (filepath)) filepath = os.path.join (filepath, "d") dirpath, dirs, files = next (walker) self.assertEquals (dirpath, filepath + "\\") self.assertEquals (set (dirs), utils.dirs_in (filepath)) self.assertEquals (set (files), utils.files_in (filepath))
def test_watch (self): filepath = utils.TEST_ROOT removed_filename = os.path.join (filepath, "1") added_filename = os.path.join (filepath, uuid.uuid1 ().hex) old_filename = os.path.join (filepath, "2") new_filename = os.path.join (filepath, uuid.uuid1 ().hex) def _change_dir (): os.remove (removed_filename) open (added_filename, "w").close () os.rename (old_filename, new_filename) watcher = fs.dir (filepath).watch () t = threading.Timer (0.5, _change_dir) t.start () self.assertEquals (next (watcher), (fs.FILE_ACTION.REMOVED, removed_filename, None)) self.assertEquals (next (watcher), (fs.FILE_ACTION.ADDED, None, added_filename)) self.assertEquals (next (watcher), (fs.FILE_ACTION.RENAMED_NEW_NAME, old_filename, new_filename)) t.join ()
def get_files (path, size_threshold_mb, results, stop_event): """Intended to run inside a thread: scan the contents of a tree recursively, pushing every file which is at least as big as the size threshold onto a results queue. Stop if the stop_event is set. """ size_threshold = size_threshold_mb * 1024 * 1024 root = fs.dir (path) top_level_folders = sorted (root.dirs (), key=operator.attrgetter ("written_at"), reverse=True) try: for tlf in top_level_folders: for f in tlf.flat (ignore_access_errors=True): if stop_event.isSet (): print "stop event set" raise x_stop_exception try: if f.size > size_threshold: results.put (f) except fs.exc.x_winsys: continue except x_stop_exception: return
def test_watch(self): filepath = utils.TEST_ROOT removed_filename = os.path.join(filepath, "1") added_filename = os.path.join(filepath, uuid.uuid1().hex) old_filename = os.path.join(filepath, "2") new_filename = os.path.join(filepath, uuid.uuid1().hex) def _change_dir(): os.remove(removed_filename) open(added_filename, "w").close() os.rename(old_filename, new_filename) watcher = fs.dir(filepath).watch() t = threading.Timer(0.5, _change_dir) t.start() self.assertEquals(next(watcher), (fs.FILE_ACTION.REMOVED, removed_filename, None)) self.assertEquals(next(watcher), (fs.FILE_ACTION.ADDED, None, added_filename)) self.assertEquals( next(watcher), (fs.FILE_ACTION.RENAMED_NEW_NAME, old_filename, new_filename)) t.join()
def get_files(path, size_threshold_mb, results, stop_event): """Intended to run inside a thread: scan the contents of a tree recursively, pushing every file which is at least as big as the size threshold onto a results queue. Stop if the stop_event is set. """ size_threshold = size_threshold_mb * 1024 * 1024 root = fs.dir(path) top_level_folders = sorted(root.dirs(), key=operator.attrgetter("written_at"), reverse=True) try: for tlf in top_level_folders: for f in tlf.flat(ignore_access_errors=True): if stop_event.isSet(): print("stop event set") raise x_stop_exception try: if f.size > size_threshold: results.put(f) except fs.exc.x_winsys: continue except x_stop_exception: return
from winsys import fs, security full_control = security.ace ((security.me (), "F", "ALLOW")) def take_control (f): f.take_ownership () with f.security () as s: s.dacl.append (full_control) start_from = fs.dir (raw_input ("Start from: ")) take_control (start_from) for f in start_from.flat (includedirs=True): print f take_control (f)
def test_create (self): filepath = os.path.join (utils.TEST_ROOT, uuid.uuid1 ().hex) self.assertFalse (os.path.exists (filepath)) fs.dir (filepath).create () self.assertTrue (os.path.exists (filepath)) self.assertTrue (os.path.isdir (filepath))
def test_DriveRoot(): assert fs.Drive("C:").root() == fs.dir (u"C:\\")
def test_DriveRoot (self): self.assertEquals (fs.drive ("C:").root, fs.dir ("C:\\"))
def test_dirs (self): filepath = utils.TEST_ROOT self.assertEquals ( set (fs.dir (filepath).dirs ()), utils.dirs_in (filepath) )
def test_file (self): filepath = utils.TEST_ROOT self.assertEquals (fs.dir (filepath).file ("1"), os.path.join (filepath, "1"))
def test_dirs(self): filepath = utils.TEST_ROOT self.assertEquals(set(fs.dir(filepath).dirs()), utils.dirs_in(filepath))
def _get_working_directory(self): return fs.dir(self._shell_link.GetWorkingDirectory())
def test_entries(self): filepath = utils.TEST_ROOT self.assertEquals(set(fs.dir(filepath).entries()), utils.files_in(filepath) | utils.dirs_in(filepath))
import operator from winsys import dialogs, fs, utils [root] = dialogs.dialog( "Find top-level sizes", ("Start from", "", dialogs.get_folder) ) sizes = dict( (d, sum(f.size for f in d.flat())) for d in fs.dir(root).dirs() ) for d, size in sorted(sizes.items(), key=operator.itemgetter(1), reverse=True): print d.name, "=>", utils.size_as_mb(size)
def test_create(self): filepath = os.path.join(utils.TEST_ROOT, uuid.uuid1().hex) self.assertFalse(os.path.exists(filepath)) fs.dir(filepath).create() self.assertTrue(os.path.exists(filepath)) self.assertTrue(os.path.isdir(filepath))
def test_entries (self): filepath = utils.TEST_ROOT self.assertEquals ( set (fs.dir (filepath).entries ()), utils.files_in (filepath) | utils.dirs_in (filepath) )
def test_flat (self): filepath = utils.TEST_ROOT self.assertEquals ( set (fs.dir (filepath).flat ()), utils.files_in (filepath) | utils.files_in (os.path.join (filepath, "d")) )
def test_dir (self): filepath = utils.TEST_ROOT self.assertEquals (fs.dir (filepath).dir ("d"), os.path.join (filepath, "d\\"))
def test_DriveRoot(): assert fs.Drive("C:").root() == fs.dir(u"C:\\")
def test_flat(self): filepath = utils.TEST_ROOT self.assertEquals( set(fs.dir(filepath).flat()), utils.files_in(filepath) | utils.files_in(os.path.join(filepath, "d")))
def test_DriveRoot(self): self.assertEquals(fs.drive("C:").root, fs.dir("C:\\"))
def test_file(self): filepath = utils.TEST_ROOT self.assertEquals( fs.dir(filepath).file("1"), os.path.join(filepath, "1"))
from winsys import fs, security full_control = security.ace((security.me(), "F", "ALLOW")) def take_control(f): f.take_ownership() with f.security() as s: s.dacl.append(full_control) start_from = fs.dir(raw_input("Start from: ")) take_control(start_from) for f in start_from.flat(includedirs=True): print f take_control(f)
def test_dir(self): filepath = utils.TEST_ROOT self.assertEquals( fs.dir(filepath).dir("d"), os.path.join(filepath, "d\\"))
import operator from winsys import dialogs, fs, utils [root] = dialogs.dialog("Find top-level sizes", ("Start from", "", dialogs.get_folder)) sizes = dict((d, sum(f.size for f in d.flat())) for d in fs.dir(root).dirs()) for d, size in sorted(sizes.items(), key=operator.itemgetter(1), reverse=True): print d.name, "=>", utils.size_as_mb(size)