class StandardLayoutMixin(object): locales = ["en"] def setUp(self): super(StandardLayoutMixin, self).setUp() self.runner = CliRunner() self.locales_fs = MemoryFS() self.output_fs = MemoryFS() def get_locales_fs(path): return self.locales_fs.opendir(path) def get_output_fs(path): return self.output_fs.opendir(path) self.locales_fs_patcher = mock.patch('elm_fluent.cli.get_locales_fs', new=get_locales_fs) self.locales_fs_patcher.start() self.output_fs_patcher = mock.patch('elm_fluent.cli.get_output_fs', new=get_output_fs) self.output_fs_patcher.start() self.setup_fs() def tearDown(self): self.locales_fs_patcher.stop() super(StandardLayoutMixin, self).tearDown() def setup_fs(self): sub = self.locales_fs.makedir("locales") for l in self.locales: sub.makedir(l) def write_ftl_file(self, path, contents): self.locales_fs.writetext(path, dedent_ftl(contents)) def get_all_files(self, fs): return {p: fs.readtext(p) for p in fs.walk.files()} def assertFileSystemEquals(self, fs, files): all_files = self.get_all_files(fs) self.assertEqual({p: c.rstrip() for p, c in all_files.items()}, {p: c.rstrip() for p, c in files.items()}) def run_main(self, args=None): result = self.runner.invoke(cli.main, args=[] if args is None else args) if result.exception is not None and not isinstance( result.exception, SystemExit): exc_info = result.exc_info raise exc_info[0].with_traceback(exc_info[1], exc_info[2]) return result
def test_copydir_indir(self): """Test copydir in a directory""" fs1 = MemoryFS() fs2 = MemoryFS() self._make_fs(fs1) utils.copydir(fs1, (fs2, "copy")) self._check_fs(fs2.opendir("copy")) fs1 = TempFS() fs2 = TempFS() self._make_fs(fs1) utils.copydir(fs1, (fs2, "copy")) self._check_fs(fs2.opendir("copy"))
class DirectoriesFilter(unittest.TestCase): def setUp(self): self.fs = MemoryFS() for i in range(5): self.fs.makedir('sub.{0}'.format(i)) with self.fs.opendir('sub.{0}'.format(i)) as d: d.makedir('sub.0') for i in range(5): self.fs.makedir('child.{0}'.format(i)) def tearDown(self): self.fs.close() def test_basic(self): ff = batch.DirectoriesFilter(include_filters=['sub*']) results = set(ff.lst(self.fs)) expected = {'sub.{}'.format(i) for i in range(5)} self.assertEqual(results, expected) def test_depth_1(self): ff = batch.DirectoriesFilter(include_filters=['sub*'], depth=1) results = set(ff.lst(self.fs)) expected = {'sub.{}'.format(i) for i in range(5) }.union({'sub.{}/sub.0'.format(i) for i in range(5)}) self.assertEqual(results, expected)
def test_movedir_indir(self): """Test movedir in a directory""" fs1 = MemoryFS() fs2 = MemoryFS() fs1sub = fs1.makeopendir("from") self._make_fs(fs1sub) utils.movedir((fs1, "from"), (fs2, "copy")) self.assert_(not fs1.exists("from")) self._check_fs(fs2.opendir("copy")) fs1 = TempFS() fs2 = TempFS() fs1sub = fs1.makeopendir("from") self._make_fs(fs1sub) utils.movedir((fs1, "from"), (fs2, "copy")) self.assert_(not fs1.exists("from")) self._check_fs(fs2.opendir("copy"))
class TestFilesFilter(unittest.TestCase): def setUp(self): self.fs = MemoryFS() for i in range(5): self.fs.makedir('sub.{0}'.format(i)) with self.fs.opendir('sub.{0}'.format(i)) as d: d.touch('result.txt') for i in range(5): self.fs.makedir('child.{0}'.format(i)) with self.fs.opendir('child.{0}'.format(i)) as d: d.touch('result.txt') for i in range(5): self.fs.touch('result.{}.txt'.format(i)) self.fs.touch('errors.{}.txt'.format(i)) def tearDown(self): self.fs.close() def test_basic(self): ff = batch.FilesFilter(include_filters=['result*']) results = set(ff.lst(self.fs)) expected = {'result.{}.txt'.format(i) for i in range(5)} self.assertEqual(results, expected) def test_multi_filters(self): ff = batch.FilesFilter(include_filters=['result*', 'errors*']) results = set(ff.lst(self.fs)) expected = {'result.{}.txt'.format(i) for i in range(5) }.union({'errors.{}.txt'.format(i) for i in range(5)}) self.assertEqual(results, expected) def test_with_directory_filter(self): results = set() (batch.DirectoriesFilter(['sub*']).obv( self.fs).flat_map(lambda p: batch.FilesFilter(['result*']).obv( self.fs, p)).subscribe(results.add)) expected = {'sub.{}/result.txt'.format(i) for i in range(5)} self.assertEqual(results, expected)
def test_mountfile(self): """Test mounting a file""" quote = b"""If you wish to make an apple pie from scratch, you must first invent the universe.""" mem_fs = MemoryFS() mem_fs.makedir('foo') mem_fs.setcontents('foo/bar.txt', quote) foo_dir = mem_fs.opendir('foo') mount_fs = MountFS() mount_fs.mountfile('bar.txt', foo_dir.open, foo_dir.getinfo) self.assert_(mount_fs.isdir('/')) self.assert_(mount_fs.isdir('./')) self.assert_(mount_fs.isdir('')) # Check we can see the mounted file in the dir list self.assertEqual(mount_fs.listdir(), ["bar.txt"]) self.assert_(not mount_fs.exists('nobodyhere.txt')) self.assert_(mount_fs.exists('bar.txt')) self.assert_(mount_fs.isfile('bar.txt')) self.assert_(not mount_fs.isdir('bar.txt')) # Check open and getinfo callables self.assertEqual(mount_fs.getcontents('bar.txt'), quote) self.assertEqual(mount_fs.getsize('bar.txt'), len(quote)) # Check changes are written back mem_fs.setcontents('foo/bar.txt', 'baz') self.assertEqual(mount_fs.getcontents('bar.txt'), b'baz') self.assertEqual(mount_fs.getsize('bar.txt'), len('baz')) # Check changes are written to the original fs self.assertEqual(mem_fs.getcontents('foo/bar.txt'), b'baz') self.assertEqual(mem_fs.getsize('foo/bar.txt'), len('baz')) # Check unmount self.assert_(mount_fs.unmount("bar.txt")) self.assertEqual(mount_fs.listdir(), []) self.assert_(not mount_fs.exists('bar.txt')) # Check unount a second time is a null op, and returns False self.assertFalse(mount_fs.unmount("bar.txt"))
class VirtualFilesystem(AbstractedFS): """Represents a virtual filesystem (currently only memory and s3 are supported) """ def __init__(self, root, cmd_channel): AbstractedFS.__init__(self, root, cmd_channel) self.cwd = root self.type = cmd_channel.type self.s3_bucket = cmd_channel.s3_bucket self.aws_access_key = cmd_channel.aws_access_key self.aws_secret_key = cmd_channel.aws_secret_key self.seperator = cmd_channel.seperator self.thread_synchronize = cmd_channel.thread_synchronize self.key_sync_timeout = cmd_channel.key_sync_timeout if not self.cmd_channel.fs_obj: if self.type == "memory": self.fs_obj = MemoryFS() elif self.type == "s3": self.fs_obj = S3FS(bucket=self.bucket, prefix=self.prefix, aws_access_key=self.aws_access_key, aws_secret_key=self.aws_secret_key, separator=self.seperator, thread_synchronize=self.thread_synchronize, key_sync_timeout=self.key_sync_timeout) self.cmd_channel.fs_obj = self.fs_obj else: self.fs_obj = self.cmd_channel.fs_obj def ftp2fs(self, ftppath): return self.ftpnorm(ftppath) def fs2ftp(self, fspath): return fspath def validpath(self, path): # validpath was used to check symlinks escaping user home # directory; this is no longer necessary. return True def open(self, filename, mode): f = self.fs_obj.open(filename, mode) f.name=filename return f def mkdir(self, path): return self.fs_obj.makedir(path) def chdir(self, path): return self.fs_obj.opendir(path) def listdir(self,path): return self.fs_obj.listdir(path) def rmdir(self, path): return self.fs_obj.removedir(path) def remove(self, path): return self.fs_obj.remove(path) def rename(self, src, dst): return self.fs_obj.rename(src, dst) def chmod(self, path, mode): return True def readlink(self, path): return self.ftp2fs(path) def isfile(self, path): return self.fs_obj.isfile(path) def islink(self, path): return False def getsize(self, path): return self.fs_obj.getsize(path) def getmtime(self, path): return self.fs_obj.getinfo(path)['modified_time'] def realpath(self, path): return path def lexists(self, path): return self.fs_obj.exists(path) def mkstemp(self, suffix='', prefix='', mode='wb'): from tempfile import _RandomNameSequence as RandomName name = RandomName() if suffix != '': suffix = 'tmp' fname = suffix + name.next() return self.fs_obj.open(fname,mode)
puts("Adding game rpas") with indent(2): if "ddlc-win.zip" not in cwdfs.listdir("/"): puts("Downloading DDLC") with indent(2): puts("Getting URL") r = requests.post( "https://teamsalvato.itch.io/ddlc/file/594897") r.raise_for_status() ddlcurl = r.json()["url"] puts("Downloading") r = requests.get(ddlcurl, stream=True) with cwdfs.open("ddlc-win.zip", 'wb') as fd: total_length = int(r.headers.get('content-length')) for chunk in progress.bar( r.iter_content(chunk_size=1024), expected_size=(total_length / 1024) + 1): fd.write(chunk) puts("Extracting game rpas") with ZipFS("./ddlc-win.zip") as zipfs: gamefs = zipfs.opendir(zipfs.listdir("/")[0]).opendir("game") templatefs = tempfs.opendir("renpy/My DDLC Mod/game") for fn in ("images.rpa", "fonts.rpa", "audio.rpa"): puts("Extracting {}".format(fn)) fscopy.copy_file(gamefs, fn, templatefs, fn) puts("Moving to real filesystem...") fscopy.copy_fs(tempfs.opendir("renpy"), cwdfs)