def test_remove_dir_with_link(self): dirname = '/tmp/pykit-ut-fsutil-remove-dir' fsutil.makedirs(dirname) self.assertTrue(os.path.isdir(dirname)) normal_file = 'normal_file' normal_path = os.path.join(dirname, normal_file) fsutil.write_file(normal_path, '') self.assertTrue(os.path.isfile(normal_path)) hard_link = 'hard_link' hard_path = os.path.join(dirname, hard_link) os.link(normal_path, hard_path) self.assertTrue(os.path.isfile(hard_path)) symbolic_link = 'symbolic_link' symbolic_path = os.path.join(dirname, symbolic_link) os.symlink(hard_path, symbolic_path) self.assertTrue(os.path.islink(symbolic_path)) fsutil.remove(dirname) self.assertFalse(os.path.exists(dirname))
def test_remove_dir(self): dirname = '/tmp/pykit-ut-fsutil-remove-dir' fsutil.makedirs(dirname) self.assertTrue(os.path.isdir(dirname)) for is_dir, file_path in ( (False, ('normal_file', )), (True, ('sub_dir', )), (False, ('sub_dir', 'sub_file1')), (False, ('sub_dir', 'sub_file2')), (True, ('sub_empty_dir', )), (True, ('sub_dir', 'sub_sub_dir')), (False, ('sub_dir', 'sub_sub_dir', 'sub_sub_file')), ): path = os.path.join(dirname, *file_path) if is_dir: fsutil.makedirs(path) self.assertTrue(os.path.isdir(path)) else: fsutil.write_file(path, '') self.assertTrue(os.path.isfile(path)) fsutil.remove(dirname) self.assertFalse(os.path.exists(dirname))
def setUpClass(cls): fsutil.makedirs(cls.testArc) # mount testArc to limit it's size cmd = "mount -t tmpfs -o size={size}M tmpfs '{_dir}'".format( _dir=cls.testArc, size=cls.testArcSize) proc.command(cmd, shell=True)
def test_get_sub_dirs(self): fsutil.makedirs('test_dir/sub_dir1') fsutil.makedirs('test_dir/sub_dir2') fsutil.write_file('test_dir/test_file', 'foo') sub_dirs = fsutil.get_sub_dirs('test_dir') self.assertListEqual(['sub_dir1', 'sub_dir2'], sub_dirs) fsutil.remove('test_dir')
def setUp(self): fsutil.makedirs(self.testSrc) for fn in self.src_fns: fsutil.write_file(os.path.join(self.testSrc, fn), fn) for d in self.arc_dirs: fsutil.makedirs(os.path.join(self.testArc, d))
def test_list_fns(self): fsutil.makedirs('test_dir/foo_dir') fsutil.write_file('test_dir/foo1', 'foo1') fsutil.write_file('test_dir/foo2', 'foo2') fsutil.write_file('test_dir/foo21', 'foo21') fsutil.write_file('test_dir/foo_dir/foo', 'foo') fsutil.write_file('test_dir/foo_dir/bar', 'bar') self.assertEqual(['foo1', 'foo2', 'foo21'], fsutil.list_fns('test_dir')) self.assertEqual(['foo2'], fsutil.list_fns('test_dir', pattern='2$')) self.assertEqual(['bar', 'foo'], fsutil.list_fns('test_dir/foo_dir')) self.assertEqual(['bar'], fsutil.list_fns('test_dir/foo_dir', pattern='^b')) fsutil.remove('test_dir')
def make_arc_dir(self): now_ts = timeutil.ts() - time.timezone - self.time_toleration date_str = timeutil.format_ts(now_ts, 'daily') arc_dir = os.path.join(self.arc_base, date_str ) fsutil.makedirs(arc_dir) symlink_path = os.path.join(self.arc_base, 'current') while True: try: os.symlink(arc_dir, symlink_path) break except OSError as e: if e[0] == errno.EEXIST: os.unlink(symlink_path) else: raise return arc_dir
def save_to_file(self, data): try: if not os.path.exists(self.data_file_dir): fsutil.makedirs(self.data_file_dir) except Exception as e: logger.error("mkdir: " + self.data_file_dir + " " + repr(e)) return -1 # data sent error try: content = "\n".join(data) fsutil.write_file(self.data_file, content, self.uid, self.gid) except Exception as e: logger.error("write data to " + self.data_file + " " + repr(e)) return -1 # data sent error
def test_makedirs(self): fn = '/tmp/pykit-ut-fsutil-foo' fn_part = ('/tmp', 'pykit-ut-fsutil-foo') dd('fn_part:', fn_part) force_remove(fn) dd('file is not a dir') with open(fn, 'w') as f: f.write('a') self.assertRaises(OSError, fsutil.makedirs, fn) os.unlink(fn) dd('no error if dir exist') os.mkdir(fn) fsutil.makedirs(fn) os.rmdir(fn) dd('single part path should be created') fsutil.makedirs(fn) self.assertTrue(os.path.isdir(fn)) os.rmdir(fn) dd('multi part path should be created') fsutil.makedirs(*fn_part) self.assertTrue(os.path.isdir(fn), 'multi part path should be created') os.rmdir(fn) dd('default mode') fsutil.makedirs(fn) self.assertEqual(0755, get_mode(fn)) os.rmdir(fn) dd('specify mode') fsutil.makedirs(fn, mode=0700) self.assertEqual(0700, get_mode(fn)) os.rmdir(fn) dd('specify uid/gid, to change uid, you need root privilege') fsutil.makedirs(fn, uid=1, gid=1)
import os import sys from pykit import fsutil fn = sys.argv[1] fsutil.makedirs(fn) stat = os.stat(fn) os.write(1, '{uid},{gid}'.format(uid=stat.st_uid, gid=stat.st_gid))