Exemplo n.º 1
0
    def setUp(self):
        # Remote URL, e.g. "ftps://*****:*****@example.com/my/test/folder"

        # TODO: some of those tests are still relevant
        self.skipTest("Not yet implemented.")

        ftp_url = PYFTPSYNC_TEST_FTP_URL
        if not ftp_url:
            self.skipTest("Must configure an FTP target "
                          "(environment variable PYFTPSYNC_TEST_FTP_URL)")
        self.assertTrue("/test" in ftp_url or "/temp" in ftp_url,
                        "FTP target path must include '/test' or '/temp'")

        # Create temp/local folder with files and empty temp/remote folder
        prepare_fixtures_1()

#        print(ftp_url)

        parts = urlparse(ftp_url, allow_fragments=False)
        self.assertIn(parts.scheme.lower(), ["ftp", "ftps"])
#        print(parts)
#        self.creds = parts.username, parts.password
#        self.HOST = parts.netloc.split("@", 1)[1]
        self.PATH = parts.path
#        user, passwd = get_stored_credentials("pyftpsync.pw", self.HOST)

        self.remote = make_target(ftp_url)
        self.remote.open()
        # This check is already performed in the constructor:
#        self.assertEqual(self.remote.pwd(), self.PATH)

        # Delete all files in remote target folder, except for LOCK file
        self.remote._rmdir_impl(".", keep_root_folder=True,
                                predicate=lambda n: n != DirMetadata.LOCK_FILE_NAME)
Exemplo n.º 2
0
def make_target(url, extra_opts=None):
    """Factory that creates `_Target` objects from URLs.

    FTP targets must begin with the scheme ``ftp://`` or ``ftps://`` for TLS.

    Note:
        TLS is only supported on Python 2.7/3.2+.
    Args:
        url (str):
        extra_opts (dict, optional): Passed to Target constructor. Default: None.
    Returns:
        :class:`_Target`
    """
    #    debug = extra_opts.get("debug", 1)
    parts = compat.urlparse(url, allow_fragments=False)
    # scheme is case-insensitive according to http://tools.ietf.org/html/rfc3986
    scheme = parts.scheme.lower()
    if scheme in ["ftp", "ftps"]:
        creds = parts.username, parts.password
        tls = scheme == "ftps"
        from ftpsync import ftp_target
        target = ftp_target.FtpTarget(parts.path,
                                      parts.hostname,
                                      parts.port,
                                      username=creds[0],
                                      password=creds[1],
                                      tls=tls,
                                      timeout=None,
                                      extra_opts=extra_opts)
    else:
        target = FsTarget(url, extra_opts)

    return target
Exemplo n.º 3
0
    def setUp(self):
        # Remote URL, e.g. "ftps://*****:*****@example.com/my/test/folder"
        ftp_url = PYFTPSYNC_TEST_FTP_URL
        if not ftp_url:
            raise SkipTest("Must configure an FTP target "
                           "(environment variable PYFTPSYNC_TEST_FTP_URL)")

        parts = urlparse(ftp_url, allow_fragments=False)
        # self.assertIn(parts.scheme.lower(), ["ftp", "ftps"])
        self.host = parts.netloc.split("@", 1)[1]
        self.path = parts.path
        self.username = parts.username
        self.password = parts.password
        self.remote = None
Exemplo n.º 4
0
    def setUp(self):
        # Remote URL, e.g. "ftps://*****:*****@example.com/my/test/folder"
        ftp_url = PYFTPSYNC_TEST_FTP_URL
        if not ftp_url:
            raise SkipTest(
                "Must configure an FTP target "
                "(environment variable PYFTPSYNC_TEST_FTP_URL)"
            )

        parts = urlparse(ftp_url, allow_fragments=False)
        # self.assertIn(parts.scheme.lower(), ["ftp", "ftps"])
        self.host = parts.netloc.split("@", 1)[1]
        self.path = parts.path
        self.username = parts.username
        self.password = parts.password
        self.remote = None
Exemplo n.º 5
0
    def setUp(self):
        # Remote URL, e.g. "ftps://*****:*****@example.com/my/test/folder"

        # TODO: some of those tests are still relevant
        self.skipTest("Not yet implemented.")

        ftp_url = PYFTPSYNC_TEST_FTP_URL
        if not ftp_url:
            self.skipTest(
                "Must configure an FTP target "
                "(environment variable PYFTPSYNC_TEST_FTP_URL)"
            )
        self.assertTrue(
            "/test" in ftp_url or "/temp" in ftp_url,
            "FTP target path must include '/test' or '/temp'",
        )

        # Create temp/local folder with files and empty temp/remote folder
        prepare_fixtures_1()

        #        print(ftp_url)

        parts = urlparse(ftp_url, allow_fragments=False)
        self.assertIn(parts.scheme.lower(), ["ftp", "ftps"])
        #        print(parts)
        #        self.creds = parts.username, parts.password
        #        self.HOST = parts.netloc.split("@", 1)[1]
        self.PATH = parts.path
        #        user, passwd = get_stored_credentials("pyftpsync.pw", self.HOST)

        self.remote = make_target(ftp_url)
        self.remote.open()
        # This check is already performed in the constructor:
        #        self.assertEqual(self.remote.pwd(), self.PATH)

        # Delete all files in remote target folder, except for LOCK file
        self.remote._rmdir_impl(
            ".",
            keep_root_folder=True,
            predicate=lambda n: n != DirMetadata.LOCK_FILE_NAME,
        )
Exemplo n.º 6
0
    def setUp(self):
        # TODO: some of those tests are still relevant
        self.skipTest("Not yet implemented.")
        # Remote URL, e.g. "ftps://*****:*****@example.com/my/test/folder"
        ftp_url = PYFTPSYNC_TEST_FTP_URL
        if not ftp_url:
            self.skipTest("Must configure an FTP target "
                          "(environment variable PYFTPSYNC_TEST_FTP_URL).")

        parts = urlparse(ftp_url, allow_fragments=False)
        self.assertIn(parts.scheme.lower(), ["ftp", "ftps"])
        print(ftp_url, parts)
        if "@" in parts.netloc:
            host = parts.netloc.split("@", 1)[1]
        else:
            host = parts.netloc
        self.PATH = parts.path
        self.ftp = FTP()
#        self.ftp.debug(1)
        self.ftp.connect(host)
        self.ftp.login(parts.username, parts.password)
Exemplo n.º 7
0
    def setUp(self):
        # TODO: some of those tests are still relevant
        self.skipTest("Not yet implemented.")
        # Remote URL, e.g. "ftps://*****:*****@example.com/my/test/folder"
        ftp_url = PYFTPSYNC_TEST_FTP_URL
        if not ftp_url:
            self.skipTest(
                "Must configure an FTP target "
                "(environment variable PYFTPSYNC_TEST_FTP_URL)."
            )

        parts = urlparse(ftp_url, allow_fragments=False)
        self.assertIn(parts.scheme.lower(), ["ftp", "ftps"])
        print(ftp_url, parts)
        if "@" in parts.netloc:
            host = parts.netloc.split("@", 1)[1]
        else:
            host = parts.netloc
        self.PATH = parts.path
        self.ftp = FTP()
        #        self.ftp.debug(1)
        self.ftp.connect(host)
        self.ftp.login(parts.username, parts.password)
Exemplo n.º 8
0
def check_ftp_test_connection(test_folder, ftp_url, keep_open=False):
    """Check if we have a FTP server for a locally accessible test folder.

    Results are cached after first call.

    Raises:
        SkipTest if the connection fails or is no good.
    """
    # Cache result after first call
    global FTP_PRECONDITIONS_PASSED

    if FTP_PRECONDITIONS_PASSED is False:
        raise SkipTest("Previous check for FTP server configuration failed.")
    elif FTP_PRECONDITIONS_PASSED is True:
        if keep_open:
            # TODO: open and connect
            raise NotImplementedError
        return True

    def _skip(msg):
        msg = "Check for FTP server configuration failed:\n{}\n{}" \
            .format(msg, MSG_FTP_TESTS_NOT_AVAILABLE)
        print(msg, file=sys.stderr)
        # raise RuntimeError(msg)
        raise SkipTest(msg)

    FTP_PRECONDITIONS_PASSED = False
    if not ftp_url:
        _skip("No FTP URL")
    #
    try:
        connected = False
        parts = urlparse(ftp_url, allow_fragments=False)
        assert parts.scheme.lower() in ("ftp", "ftps")
        #         print(ftp_url, "->", parts, ", ", parts.username, ":", parts.password)
        #         if "@" in parts.netloc:
        #             host = parts.netloc.rsplit("@", 1)[1]
        #         else:
        #             host = parts.netloc
        # self.PATH = parts.path
        ftp = FTP()
        # ftp.set_debuglevel(2)
        # Can we connect to host?
        ftp.connect(parts.hostname, parts.port or 0)
        ftp.login(parts.username, parts.password)
        # Change directory
        ftp.cwd(parts.path)
        # Check for MLSD command support
        try:
            ftp.retrlines("MLSD", lambda _line: None)
        except error_perm as e:
            if "500" in str(e.args):
                _skip("The FTP server does not support the 'MLSD' command.")
            raise

        # Check if we have write access
        data = "{}".format(time.time())
        buf = io.BytesIO(to_bytes(data))
        probe_file = "pyftpsync_probe.txt"
        ftp.storbinary("STOR {}".format(probe_file), buf)
        # Check if the FTP target is identical to the FS path
        #         buf = ftp.retrbinary("RETR {}".format(probe_file))
        try:
            data2 = read_test_file("remote/{}".format(probe_file))
        except OSError as e:  # FileNotFoundError is only available in Python 3
            if e.errno == errno.ENOENT:
                _skip(
                    "FTP target path {} does not match `PYFTPSYNC_TEST_FOLDER/remote`"
                    .format(parts.path))
            raise

        if data != data2:
            _skip("Probe file content mismatch")
        # Cleanup
        ftp.delete(probe_file)
        # Convinced: Ok!
        FTP_PRECONDITIONS_PASSED = True

    except Exception as e:
        _skip("{}".format(e))

    finally:
        if connected and not keep_open:
            ftp.quit()

    if keep_open:
        return (ftp, parts.path)
    return True
Exemplo n.º 9
0
def check_ftp_test_connection(test_folder, ftp_url, keep_open=False):
    """Check if we have a FTP server for a locally accessible test folder.

    Results are cached after first call.

    Raises:
        SkipTest if the connection fails or is no good.
    """
    # Cache result after first call
    global FTP_PRECONDITIONS_PASSED

    if FTP_PRECONDITIONS_PASSED is False:
        raise SkipTest("Previous check for FTP server configuration failed.")
    elif FTP_PRECONDITIONS_PASSED is True:
        if keep_open:
            # TODO: open and connect
            raise NotImplementedError
        return True

    def _skip(msg):
        msg = "Check for FTP server configuration failed:\n{}\n{}".format(
            msg, MSG_FTP_TESTS_NOT_AVAILABLE
        )
        print(msg, file=sys.stderr)
        # raise RuntimeError(msg)
        raise SkipTest(msg)

    FTP_PRECONDITIONS_PASSED = False
    if not ftp_url:
        _skip("No FTP URL")
    #
    try:
        connected = False
        parts = urlparse(ftp_url, allow_fragments=False)
        assert parts.scheme.lower() in ("ftp", "ftps")
        #         print(ftp_url, "->", parts, ", ", parts.username, ":", parts.password)
        #         if "@" in parts.netloc:
        #             host = parts.netloc.rsplit("@", 1)[1]
        #         else:
        #             host = parts.netloc
        # self.PATH = parts.path
        ftp = FTP()
        # ftp.set_debuglevel(2)
        # Can we connect to host?
        ftp.connect(parts.hostname, parts.port or 0)
        ftp.login(parts.username, parts.password)
        # Change directory
        ftp.cwd(parts.path)
        # Check for MLSD command support
        try:
            ftp.retrlines("MLSD", lambda _line: None)
        except error_perm as e:
            if "500" in str(e.args):
                _skip("The FTP server does not support the 'MLSD' command.")
            raise

        # Check if we have write access
        data = "{}".format(time.time())
        buf = io.BytesIO(to_bytes(data))
        probe_file = "pyftpsync_probe.txt"
        ftp.storbinary("STOR {}".format(probe_file), buf)
        # Check if the FTP target is identical to the FS path
        #         buf = ftp.retrbinary("RETR {}".format(probe_file))
        try:
            data2 = read_test_file("remote/{}".format(probe_file))
        except OSError as e:  # FileNotFoundError is only available in Python 3
            if e.errno == errno.ENOENT:
                _skip(
                    "FTP target path {} does not match `PYFTPSYNC_TEST_FOLDER/remote`".format(
                        parts.path
                    )
                )
            raise

        if data != data2:
            _skip("Probe file content mismatch")
        # Cleanup
        ftp.delete(probe_file)
        # Convinced: Ok!
        FTP_PRECONDITIONS_PASSED = True

    except Exception as e:
        _skip("{}".format(e))

    finally:
        if connected and not keep_open:
            ftp.quit()

    if keep_open:
        return (ftp, parts.path)
    return True