def test_rename_file_basename(self): path = self.temp_path("a/b/c.txt") fsutil.create_file(path) fsutil.rename_file_basename(path, "d") self.assertFalse(fsutil.exists(path)) path = self.temp_path("a/b/d.txt") self.assertTrue(fsutil.exists(path))
def test_create_file(self): path = self.temp_path("a/b/c.txt") self.assertFalse(fsutil.exists(path)) fsutil.create_file(path, content="hello world") self.assertTrue(fsutil.exists(path)) self.assertTrue(fsutil.is_file(path)) self.assertEqual(fsutil.read_file(path), "hello world")
def test_rename_file(self): path = self.temp_path('a/b/c.txt') fsutil.create_file(path) fsutil.rename_file(path, 'd.txt.backup') self.assertFalse(fsutil.exists(path)) path = self.temp_path('a/b/d.txt.backup') self.assertTrue(fsutil.exists(path))
def test_rename_file_extension(self): path = self.temp_path("a/b/c.txt") fsutil.create_file(path) fsutil.rename_file_extension(path, "json") self.assertFalse(fsutil.exists(path)) path = self.temp_path("a/b/c.json") self.assertTrue(fsutil.exists(path))
def test_rename_dir(self): path = self.temp_path('a/b/c') fsutil.make_dirs(path) fsutil.rename_dir(path, 'd') self.assertFalse(fsutil.exists(path)) path = self.temp_path('a/b/d') self.assertTrue(fsutil.exists(path))
def test_rename_dir(self): path = self.temp_path("a/b/c") fsutil.make_dirs(path) fsutil.rename_dir(path, "d") self.assertFalse(fsutil.exists(path)) path = self.temp_path("a/b/d") self.assertTrue(fsutil.exists(path))
def test_exists(self): path = self.temp_path("a/b/") self.assertFalse(fsutil.exists(path)) fsutil.create_dir(path) self.assertTrue(fsutil.exists(path)) path = self.temp_path("a/b/c.txt") self.assertFalse(fsutil.exists(path)) fsutil.create_file(path) self.assertTrue(fsutil.exists(path))
def test_download_file(self): url = "https://raw.githubusercontent.com/fabiocaccamo/python-fsutil/master/README.md" path = fsutil.download_file(url, __file__) self.assertTrue(fsutil.exists(path)) lines = fsutil.read_file_lines(path, skip_empty=False) lines_count = len(lines) self.assertTrue(lines_count > 500 and lines_count < 1000) fsutil.remove_file(path) self.assertFalse(fsutil.exists(path))
def test_delete_file(self): path = self.temp_path("a/b/c.txt") fsutil.create_file(self.temp_path("a/b/c.txt")) self.assertTrue(fsutil.exists(path)) deleted = fsutil.delete_file(self.temp_path("a/b/d.txt")) self.assertFalse(deleted) deleted = fsutil.delete_file(path) self.assertTrue(deleted) self.assertFalse(fsutil.exists(path))
def test_remove_file(self): path = self.temp_path("a/b/c.txt") fsutil.create_file(self.temp_path("a/b/c.txt")) self.assertTrue(fsutil.exists(path)) removed = fsutil.remove_file(self.temp_path("a/b/d.txt")) self.assertFalse(removed) removed = fsutil.remove_file(path) self.assertTrue(removed) self.assertFalse(fsutil.exists(path))
def test_delete_dir(self): fsutil.create_file(self.temp_path("a/b/c/d.txt")) fsutil.create_file(self.temp_path("a/b/c/e.txt")) fsutil.create_file(self.temp_path("a/b/c/f.txt")) deleted = fsutil.delete_dir(self.temp_path("a/c/")) self.assertFalse(deleted) deleted = fsutil.delete_dir(self.temp_path("a/b/")) self.assertTrue(deleted) self.assertTrue(fsutil.exists(self.temp_path("a"))) self.assertFalse(fsutil.exists(self.temp_path("a/b")))
def test_remove_dir(self): fsutil.create_file(self.temp_path("a/b/c/d.txt")) fsutil.create_file(self.temp_path("a/b/c/e.txt")) fsutil.create_file(self.temp_path("a/b/c/f.txt")) removed = fsutil.remove_dir(self.temp_path("a/c/")) self.assertFalse(removed) removed = fsutil.remove_dir(self.temp_path("a/b/")) self.assertTrue(removed) self.assertTrue(fsutil.exists(self.temp_path("a"))) self.assertFalse(fsutil.exists(self.temp_path("a/b")))
def test_move_file(self): path = self.temp_path("a/b/c.txt") fsutil.create_file(path, content="Hello World") dest = self.temp_path("a") fsutil.move_file(path, dest) self.assertFalse(fsutil.exists(path)) self.assertTrue(fsutil.is_file(self.temp_path("a/c.txt")))
def __init__(self, relative, absolute, url): self.url = url self.absolute = absolute self.relative = relative self.name = fsutil.get_filename(absolute) self.is_exist = fsutil.exists(absolute) self.is_file = fsutil.is_file(absolute) self.is_dir = fsutil.is_dir(absolute) self.__content = None
def test_clean_dir_only_files(self): path = self.temp_path("a/b/c.txt") fsutil.create_file(self.temp_path("a/b/c/f1.txt"), content="hello world") fsutil.create_file(self.temp_path("a/b/c/f2.txt")) fsutil.create_file(self.temp_path("a/b/c/f3.txt"), content="hello world") fsutil.create_file(self.temp_path("a/b/c/f4.txt")) fsutil.create_file(self.temp_path("a/b/c/f5.txt"), content="hello world") fsutil.clean_dir(self.temp_path("a"), dirs=False, files=False) self.assertTrue(fsutil.exists(self.temp_path("a/b/c/f1.txt"))) self.assertTrue(fsutil.exists(self.temp_path("a/b/c/f2.txt"))) self.assertTrue(fsutil.exists(self.temp_path("a/b/c/f3.txt"))) self.assertTrue(fsutil.exists(self.temp_path("a/b/c/f4.txt"))) self.assertTrue(fsutil.exists(self.temp_path("a/b/c/f5.txt"))) fsutil.clean_dir(self.temp_path("a"), dirs=False, files=True) self.assertTrue(fsutil.exists(self.temp_path("a/b/c/f1.txt"))) self.assertFalse(fsutil.exists(self.temp_path("a/b/c/f2.txt"))) self.assertTrue(fsutil.exists(self.temp_path("a/b/c/f3.txt"))) self.assertFalse(fsutil.exists(self.temp_path("a/b/c/f4.txt"))) self.assertTrue(fsutil.exists(self.temp_path("a/b/c/f5.txt")))
def getAbsolutePath(path, context_dir): context_path = path os.chdir(context_dir) local_path = os.path.join(os.getcwd(), path) if fsutil.exists(local_path): context_path = os.path.abspath(local_path) return context_path
def test_clean_dir_only_dirs(self): path = self.temp_path("a/b/c.txt") fsutil.create_dir(self.temp_path("x/y/z/a")) fsutil.create_dir(self.temp_path("x/y/z/b")) fsutil.create_dir(self.temp_path("x/y/z/c")) fsutil.create_dir(self.temp_path("x/y/z/d")) fsutil.create_dir(self.temp_path("x/y/z/e")) fsutil.create_file(self.temp_path("x/y/z/b/f.txt"), content="hello world") fsutil.create_file(self.temp_path("x/y/z/d/f.txt"), content="hello world") fsutil.clean_dir(self.temp_path("x/y"), dirs=False, files=True) self.assertTrue(fsutil.exists(self.temp_path("x/y/z/a"))) self.assertTrue(fsutil.exists(self.temp_path("x/y/z/b"))) self.assertTrue(fsutil.exists(self.temp_path("x/y/z/c"))) self.assertTrue(fsutil.exists(self.temp_path("x/y/z/d"))) self.assertTrue(fsutil.exists(self.temp_path("x/y/z/e"))) fsutil.clean_dir(self.temp_path("x/y"), dirs=True, files=True) self.assertFalse(fsutil.exists(self.temp_path("x/y/z/a"))) self.assertTrue(fsutil.exists(self.temp_path("x/y/z/b"))) self.assertFalse(fsutil.exists(self.temp_path("x/y/z/c"))) self.assertTrue(fsutil.exists(self.temp_path("x/y/z/d"))) self.assertFalse(fsutil.exists(self.temp_path("x/y/z/e")))
def discoverConfig(path: str = os.getcwd()): # Accepted files autodiscover_configs = [ "arco.yml", "arco.json", ".arco.yml", ".arco.json", ] for config in autodiscover_configs: config_path = os.path.join(path, config) if fsutil.exists(config_path): return config_path
def test_clean_dir_dirs_and_files(self): path = self.temp_path("a/b/c.txt") fsutil.create_file(self.temp_path("a/b/c/f1.txt")) fsutil.create_file(self.temp_path("a/b/c/f2.txt")) fsutil.create_file(self.temp_path("a/b/c/f3.txt")) fsutil.create_file(self.temp_path("a/b/c/d/f4.txt")) fsutil.create_file(self.temp_path("a/b/c/d/f5.txt")) fsutil.clean_dir(self.temp_path("a"), dirs=True, files=True) self.assertFalse(fsutil.exists(self.temp_path("a/b/c/d/f5.txt"))) self.assertFalse(fsutil.exists(self.temp_path("a/b/c/d/f4.txt"))) self.assertFalse(fsutil.exists(self.temp_path("a/b/c/f3.txt"))) self.assertFalse(fsutil.exists(self.temp_path("a/b/c/f2.txt"))) self.assertFalse(fsutil.exists(self.temp_path("a/b/c/f1.txt"))) self.assertFalse(fsutil.exists(self.temp_path("a/b/c"))) self.assertFalse(fsutil.exists(self.temp_path("a/b"))) self.assertTrue(fsutil.exists(self.temp_path("a")))
def test_clean_dir_only_dirs(self): path = self.temp_path('a/b/c.txt') fsutil.create_dir(self.temp_path('x/y/z/a')) fsutil.create_dir(self.temp_path('x/y/z/b')) fsutil.create_dir(self.temp_path('x/y/z/c')) fsutil.create_dir(self.temp_path('x/y/z/d')) fsutil.create_dir(self.temp_path('x/y/z/e')) fsutil.create_file(self.temp_path('x/y/z/b/f.txt'), content='hello world') fsutil.create_file(self.temp_path('x/y/z/d/f.txt'), content='hello world') fsutil.clean_dir(self.temp_path('x/y'), dirs=False, files=True) self.assertTrue(fsutil.exists(self.temp_path('x/y/z/a'))) self.assertTrue(fsutil.exists(self.temp_path('x/y/z/b'))) self.assertTrue(fsutil.exists(self.temp_path('x/y/z/c'))) self.assertTrue(fsutil.exists(self.temp_path('x/y/z/d'))) self.assertTrue(fsutil.exists(self.temp_path('x/y/z/e'))) fsutil.clean_dir(self.temp_path('x/y'), dirs=True, files=True) self.assertFalse(fsutil.exists(self.temp_path('x/y/z/a'))) self.assertTrue(fsutil.exists(self.temp_path('x/y/z/b'))) self.assertFalse(fsutil.exists(self.temp_path('x/y/z/c'))) self.assertTrue(fsutil.exists(self.temp_path('x/y/z/d'))) self.assertFalse(fsutil.exists(self.temp_path('x/y/z/e')))
def test_remove_files(self): path1 = self.temp_path("a/b/c/document.txt") path2 = self.temp_path("a/b/d/document.txt") path3 = self.temp_path("a/b/e/document.txt") path4 = self.temp_path("a/b/f/document.txt") fsutil.create_file(path1) fsutil.create_file(path2) fsutil.create_file(path3) fsutil.create_file(path4) self.assertTrue(fsutil.exists(path1)) self.assertTrue(fsutil.exists(path2)) self.assertTrue(fsutil.exists(path3)) self.assertTrue(fsutil.exists(path4)) fsutil.remove_files(path1, path2, path3, path4) self.assertFalse(fsutil.exists(path1)) self.assertFalse(fsutil.exists(path2)) self.assertFalse(fsutil.exists(path3)) self.assertFalse(fsutil.exists(path4))
def test_remove_dirs(self): fsutil.create_file(self.temp_path('a/b/c/document.txt')) fsutil.create_file(self.temp_path('a/b/d/document.txt')) fsutil.create_file(self.temp_path('a/b/e/document.txt')) fsutil.create_file(self.temp_path('a/b/f/document.txt')) path1 = self.temp_path('a/b/c/') path2 = self.temp_path('a/b/d/') path3 = self.temp_path('a/b/e/') path4 = self.temp_path('a/b/f/') self.assertTrue(fsutil.exists(path1)) self.assertTrue(fsutil.exists(path2)) self.assertTrue(fsutil.exists(path3)) self.assertTrue(fsutil.exists(path4)) fsutil.remove_dirs(path1, path2, path3, path4) self.assertFalse(fsutil.exists(path1)) self.assertFalse(fsutil.exists(path2)) self.assertFalse(fsutil.exists(path3)) self.assertFalse(fsutil.exists(path4))
def test_delete_dirs(self): fsutil.create_file(self.temp_path("a/b/c/document.txt")) fsutil.create_file(self.temp_path("a/b/d/document.txt")) fsutil.create_file(self.temp_path("a/b/e/document.txt")) fsutil.create_file(self.temp_path("a/b/f/document.txt")) path1 = self.temp_path("a/b/c/") path2 = self.temp_path("a/b/d/") path3 = self.temp_path("a/b/e/") path4 = self.temp_path("a/b/f/") self.assertTrue(fsutil.exists(path1)) self.assertTrue(fsutil.exists(path2)) self.assertTrue(fsutil.exists(path3)) self.assertTrue(fsutil.exists(path4)) fsutil.delete_dirs(path1, path2, path3, path4) self.assertFalse(fsutil.exists(path1)) self.assertFalse(fsutil.exists(path2)) self.assertFalse(fsutil.exists(path3)) self.assertFalse(fsutil.exists(path4))
def upload(self, **kwargs): src = kwargs.get('src', None) dst = kwargs.get('dst', None) # if not folder, print error and return if not fsutil.exists(src): StdOut.display(msg=message.get(message.MISSING_FILE, location='local')) return # get meta data meta = self.__find_meta(dst) # if remote does not exist, print and return if meta is None: StdOut.display(msg=message.get(message.MISSING_FILE, location=self.name())) return if meta.get('mimeType') != self.__FOLDER_MIME: StdOut.display(msg=message.get(message.INVALID_DST, location=self.name())) return self.__insert(src, meta)
def create_dir(request): path = request.POST['path'] if fsutil.exists(path): return HttpResponseBadRequest fsutil.create_dir(path) return JsonResponse(True, safe=False)
def valid_state(): # check for whether there are any anomalies with the configurations return fsutil.exists(Conf.HOME)
def exists_home(): return fsutil.exists(Conf.HOME)
def exists_drive(drive_name): return fsutil.exists(Conf.drives.get(drive_name).get(Conf.DRIVE_HOME))
def test_move_dir(self): path = self.temp_path('a/b/c.txt') fsutil.create_file(path, content='Hello World') fsutil.move_dir(self.temp_path('a/b'), self.temp_path('x/y')) self.assertFalse(fsutil.exists(path)) self.assertTrue(fsutil.is_file(self.temp_path('x/y/b/c.txt')))