Example #1
0
    def test_get_set_items(self) -> None:
        no_lidvids: Set[LIDVID] = set()

        # Set empty contents
        mv = Multiversioned(MemoryFS())
        empty_lidvid = LIDVID("urn:nasa:pds:empty-bundle::3.14")
        empty_contents = VersionContents.create_from_lidvids(
            no_lidvids, MemoryFS(), set())
        mv[empty_lidvid] = empty_contents
        self.assertTrue(empty_lidvid in mv)
        self.assertEqual(1, len(mv))
        self.assertEqual(empty_contents, mv[empty_lidvid])

        # TODO Is IndexError the right exception to raise?
        with self.assertRaises(IndexError):
            mv[empty_lidvid] = empty_contents

        # Set contents with a single file down a long path
        single_file_lidvid = LIDVID("urn:nasa:pds:single-file::3.14")
        single_file_fs = MemoryFS()
        single_file_path = "/down/a/lot/of/dirs/text.txt"
        single_file_fs.makedirs(fs.path.dirname(single_file_path), None, True)
        single_file_fs.writetext(single_file_path, "Hello, there!")
        single_file_contents = VersionContents.create_from_lidvids(
            no_lidvids, single_file_fs, set([single_file_path]))
        mv[single_file_lidvid] = single_file_contents
        self.assertTrue(empty_lidvid in mv)
        self.assertTrue(single_file_lidvid in mv)
        print("****", set(mv.lidvids()))
        self.assertEqual(2, len(mv))
        self.assertEqual(single_file_contents, mv[single_file_lidvid])

        # Test that LIDVIDs get put correctly into the
        # subdir$version.txts.
        hierarchic = Multiversioned(MemoryFS())
        b_lidvid = LIDVID("urn:nasa:pds:b::1.5")
        c_lidvid = LIDVID("urn:nasa:pds:b:c::2.5")
        p_lidvid = LIDVID("urn:nasa:pds:b:c:p::333.123")

        p_contents = VersionContents.create_from_lidvids(
            no_lidvids, MemoryFS(), set([]))
        hierarchic[p_lidvid] = p_contents
        self.assertEqual(p_contents, hierarchic[p_lidvid])

        c_contents = VersionContents.create_from_lidvids(
            set([p_lidvid]), MemoryFS(), set([]))
        hierarchic[c_lidvid] = c_contents
        self.assertEqual(c_contents, hierarchic[c_lidvid])

        b_contents = VersionContents.create_from_lidvids(
            set([c_lidvid]), MemoryFS(), set([]))
        hierarchic[b_lidvid] = b_contents
        self.assertEqual(b_contents, hierarchic[b_lidvid])

        self.assertEqual(3, len(hierarchic))
        self.assertEqual({p_lidvid, c_lidvid, b_lidvid}, hierarchic.lidvids())
Example #2
0
    def test_categorize_filesystem(self) -> None:
        m = MemoryFS()
        self.assertEqual(EMPTY_FS_TYPE, categorize_filesystem(m))

        m = MemoryFS()
        m.makedir("/hst_12345$")
        self.assertEqual(SINGLE_VERSIONED_FS_TYPE, categorize_filesystem(m))

        m = MemoryFS()
        m.makedirs("/hst_12345/v$1.0")
        self.assertEqual(MULTIVERSIONED_FS_TYPE, categorize_filesystem(m))

        m = MemoryFS()
        m.makedir("/hst_12345")
        self.assertEqual(UNKNOWN_FS_TYPE, categorize_filesystem(m))
Example #3
0
def test__when_copying_dirs_with_glob_patterns__it_copies_matching_dirs_with_content(
):
    mem_fs = MemoryFS()
    sub_fs = mem_fs.makedirs("sub/first")
    sub_fs.create("file.txt")

    sub_fs = mem_fs.makedirs("sub/second")
    sub_fs.create("another.txt")

    sut = _TestFilesystemImpl(mem_fs)

    sut.copy("sub/*", "otherdir/")

    assert mem_fs.exists("otherdir/first/file.txt")
    assert mem_fs.exists("otherdir/second/another.txt")
    def test_garbage_collection(self):

        log_file = self.get_resource('crashplan_backup_files.log')

        new_file = u'foo.txt'
        newer_file = u'/my/crashplan/backups/vms/finn/finn-2018-08-15_00-09-00/finn-5-s004.vmdk'
        older_file = u'/my/crashplan/backups/vms/gabarolas/gabarolas-2018-08-02_16-07-17/gabarolas-s067.vmdk'

        with CrashPlanFS(log_file=log_file.strpath) as fs:
            assert not fs.exists(new_file)
            assert fs.exists(newer_file)
            assert fs.exists(older_file)
            older_file_remote_mtime = fs.getdetails(older_file).modified

        # Create a mock transfer area
        from fs.memoryfs import MemoryFS
        transfer_area = MemoryFS()

        # Populate the transfer area
        transfer_area.appendtext(new_file, u'This file is new')
        transfer_area.makedirs(os.path.split(newer_file)[0])
        transfer_area.appendtext(newer_file,
                                 u'This file has been modified locally')
        transfer_area.makedirs(os.path.split(older_file)[0])
        transfer_area.appendtext(older_file, u'This file is up-to-date')
        transfer_area.settimes(older_file, modified=older_file_remote_mtime)

        assert transfer_area.getdetails(
            older_file).modified <= older_file_remote_mtime

        # Pass the transfer area to crashplanfs
        fs = CrashPlanFS(log_file=log_file.strpath,
                         transfer_area=transfer_area)

        # The new file should not be listed, as it only exists in the transfer area
        assert not fs.exists(new_file)

        # The newer file should be listed, with the remote modification time
        assert fs.exists(newer_file)
        assert transfer_area.getdetails(new_file).modified > fs.getdetails(
            newer_file).modified

        # The older file should be deleted from the transfer area
        assert not transfer_area.exists(older_file)
Example #5
0
def test__when_copying_file_to_other_filesystem__and_parent_dir_exists__should_not_try_to_create_dirs(
):
    target_parent_dir = "another/folder"

    target_fs = MemoryFS()
    target_fs.makedirs(target_parent_dir)
    target_fs_wrapping_mock = MagicMock(spec=MemoryFS, wraps=target_fs)

    origin_fs = MemoryFS()
    origin_fs.create(SOURCE)

    sut = _TestFilesystemImpl(origin_fs)

    complete_path = f"{target_parent_dir}/{TARGET}"
    sut.copy(SOURCE,
             complete_path,
             filesystem=_TestFilesystemImpl(target_fs_wrapping_mock))

    target_fs_wrapping_mock.makedirs.assert_not_called()
Example #6
0
    def test_listdir(self) -> None:
        fs = MemoryFS()
        fs.makedirs("/b$")
        fs.makedirs("/b$/dir1")
        fs.makedirs("/b$/dir2")
        fs.writetext("/b$/file1.txt", "file1")
        fs.writetext("/b$/file2.txt", "file2")
        fs.writetext("/b$/dir1/file1.txt", "file1")
        fs.writetext("/b$/dir1/file2.txt", "file2")
        fs.writetext("/b$/dir2/file1.txt", "file1")
        fs.writetext("/b$/dir2/file2.txt", "file2")

        c = COWFS(fs)
        path = "/b$/dir1/file2.txt"
        c.writetext(path, "xxxx")

        # Now the COW version is different.  But it should still have
        # the old unchanged files.

        self.assertTrue(c.exists("/b$/dir1/file1.txt"))  # Yes, but...
        self.assertEqual({"dir1", "dir2", "file1.txt", "file2.txt"},
                         set(c.listdir("/b$")))
class TestInfo(unittest.TestCase):
    def setUp(self):
        self.fs = MemoryFS()
        self.fs.makedir("foo")
        self.fs.makedir("bar")
        self.fs.makedir("baz")
        self.fs.makedirs("foo/egg1")
        self.fs.makedirs("foo/egg2")
        self.fs.create("/root1")
        self.fs.create("/root2")
        self.fs.create("/foo/test.txt")
        self.fs.create("/foo/test2.txt")
        self.fs.create("/foo/.hidden")
        self.fs.makedirs("/deep/deep1/deep2/deep3/deep4/deep5/deep6")

    def test_tree(self):

        output_file = io.StringIO()

        tree.render(self.fs, file=output_file)

        expected = "|-- bar\n|-- baz\n|-- deep\n|   `-- deep1\n|       `-- deep2\n|           `-- deep3\n|               `-- deep4\n|                   `-- deep5\n|-- foo\n|   |-- egg1\n|   |-- egg2\n|   |-- .hidden\n|   |-- test.txt\n|   `-- test2.txt\n|-- root1\n`-- root2\n"
        self.assertEqual(output_file.getvalue(), expected)

    def test_tree_encoding(self):

        output_file = io.StringIO()

        tree.render(self.fs, file=output_file, with_color=True)

        print(repr(output_file.getvalue()))

        expected = "\x1b[32m\u251c\u2500\u2500\x1b[0m \x1b[1;34mbar\x1b[0m\n\x1b[32m\u251c\u2500\u2500\x1b[0m \x1b[1;34mbaz\x1b[0m\n\x1b[32m\u251c\u2500\u2500\x1b[0m \x1b[1;34mdeep\x1b[0m\n\x1b[32m\u2502   \u2514\u2500\u2500\x1b[0m \x1b[1;34mdeep1\x1b[0m\n\x1b[32m\u2502       \u2514\u2500\u2500\x1b[0m \x1b[1;34mdeep2\x1b[0m\n\x1b[32m\u2502           \u2514\u2500\u2500\x1b[0m \x1b[1;34mdeep3\x1b[0m\n\x1b[32m\u2502               \u2514\u2500\u2500\x1b[0m \x1b[1;34mdeep4\x1b[0m\n\x1b[32m\u2502                   \u2514\u2500\u2500\x1b[0m \x1b[1;34mdeep5\x1b[0m\n\x1b[32m\u251c\u2500\u2500\x1b[0m \x1b[1;34mfoo\x1b[0m\n\x1b[32m\u2502   \u251c\u2500\u2500\x1b[0m \x1b[1;34megg1\x1b[0m\n\x1b[32m\u2502   \u251c\u2500\u2500\x1b[0m \x1b[1;34megg2\x1b[0m\n\x1b[32m\u2502   \u251c\u2500\u2500\x1b[0m \x1b[33m.hidden\x1b[0m\n\x1b[32m\u2502   \u251c\u2500\u2500\x1b[0m test.txt\n\x1b[32m\u2502   \u2514\u2500\u2500\x1b[0m test2.txt\n\x1b[32m\u251c\u2500\u2500\x1b[0m root1\n\x1b[32m\u2514\u2500\u2500\x1b[0m root2\n"
        self.assertEqual(output_file.getvalue(), expected)

    def test_tree_bytes_no_dirs_first(self):

        output_file = io.StringIO()

        tree.render(self.fs, file=output_file, dirs_first=False)

        expected = "|-- bar\n|-- baz\n|-- deep\n|   `-- deep1\n|       `-- deep2\n|           `-- deep3\n|               `-- deep4\n|                   `-- deep5\n|-- foo\n|   |-- .hidden\n|   |-- egg1\n|   |-- egg2\n|   |-- test.txt\n|   `-- test2.txt\n|-- root1\n`-- root2\n"
        self.assertEqual(output_file.getvalue(), expected)

    def test_error(self):
        output_file = io.StringIO()

        filterdir = self.fs.filterdir

        def broken_filterdir(path, **kwargs):
            if path.startswith("/deep/deep1/"):
                # Because error messages differ accross Python versions
                raise Exception("integer division or modulo by zero")
            return filterdir(path, **kwargs)

        self.fs.filterdir = broken_filterdir
        tree.render(self.fs, file=output_file, with_color=True)

        expected = "\x1b[32m\u251c\u2500\u2500\x1b[0m \x1b[1;34mbar\x1b[0m\n\x1b[32m\u251c\u2500\u2500\x1b[0m \x1b[1;34mbaz\x1b[0m\n\x1b[32m\u251c\u2500\u2500\x1b[0m \x1b[1;34mdeep\x1b[0m\n\x1b[32m\u2502   \u2514\u2500\u2500\x1b[0m \x1b[1;34mdeep1\x1b[0m\n\x1b[32m\u2502       \u2514\u2500\u2500\x1b[0m \x1b[1;34mdeep2\x1b[0m\n\x1b[32m\u2502           \u2514\u2500\u2500\x1b[0m \x1b[31merror (integer division or modulo by zero)\x1b[0m\n\x1b[32m\u251c\u2500\u2500\x1b[0m \x1b[1;34mfoo\x1b[0m\n\x1b[32m\u2502   \u251c\u2500\u2500\x1b[0m \x1b[1;34megg1\x1b[0m\n\x1b[32m\u2502   \u251c\u2500\u2500\x1b[0m \x1b[1;34megg2\x1b[0m\n\x1b[32m\u2502   \u251c\u2500\u2500\x1b[0m \x1b[33m.hidden\x1b[0m\n\x1b[32m\u2502   \u251c\u2500\u2500\x1b[0m test.txt\n\x1b[32m\u2502   \u2514\u2500\u2500\x1b[0m test2.txt\n\x1b[32m\u251c\u2500\u2500\x1b[0m root1\n\x1b[32m\u2514\u2500\u2500\x1b[0m root2\n"
        tree_output = output_file.getvalue()
        print(repr(tree_output))

        self.assertEqual(expected, tree_output)

        output_file = io.StringIO()
        tree.render(self.fs, file=output_file, with_color=False)

        expected = "|-- bar\n|-- baz\n|-- deep\n|   `-- deep1\n|       `-- deep2\n|           `-- error (integer division or modulo by zero)\n|-- foo\n|   |-- egg1\n|   |-- egg2\n|   |-- .hidden\n|   |-- test.txt\n|   `-- test2.txt\n|-- root1\n`-- root2\n"
        self.assertEqual(expected, output_file.getvalue())
Example #8
0
from fs.memoryfs import MemoryFS
mem_fs = MemoryFS()

mem_fs.makedirs("HelloMem")
mem_fs.create("./hello.mem")

print(mem_fs.listdir("./"))

with indent(2):
    if "modtemplate.zip" not in cwdfs.listdir("/"):
        puts("Downloading ModTemplate")
        r = requests.get(
            "https://github.com/Monika-After-Story/DDLCModTemplate/releases/download/v1.1.0/DDLCModTemplate_1.1.0.zip",
            stream=True)
        r.raise_for_status()
        with cwdfs.open("modtemplate.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 ModTemplate")
        with ZipFS("./modtemplate.zip") as zipfs:
            fscopy.copy_fs(zipfs, tempfs.makedirs("renpy/My DDLC Mod"))
        cwdfs.remove("modtemplate.zip")

        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: