Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes ftp download when authentication is required #4092

Merged
merged 5 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions conans/client/tools/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ def get(url, md5='', sha1='', sha256='', destination=".", filename="", keep_perm
def ftp_download(ip, filename, login='', password=''):
import ftplib
try:
ftp = ftplib.FTP(ip, login, password)
ftp.login()
ftp = ftplib.FTP(ip)
ftp.login(login, password)
filepath, filename = os.path.split(filename)
if filepath:
ftp.cwd(filepath)
with open(filename, 'wb') as f:
ftp.retrbinary('RETR ' + filename, f.write)
except Exception as e:
try:
os.unlink(filename)
except OSError:
pass
raise ConanException("Error in FTP download from %s\n%s" % (ip, str(e)))
finally:
try:
Expand Down
Empty file.
42 changes: 42 additions & 0 deletions conans/test/client/tools/net_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# coding=utf-8

import os
import shutil
import tempfile
import unittest

from conans.client.tools import chdir
from conans.client.tools import net
from conans.errors import ConanException


class ToolsNetTest(unittest.TestCase):

def run(self, *args, **kwargs):
self.tmp_folder = tempfile.mkdtemp()
try:
with chdir(self.tmp_folder):
super(ToolsNetTest, self).run(*args, **kwargs)
finally:
shutil.rmtree(self.tmp_folder)

def test_ftp_auth(self):
filename = "/pub/example/readme.txt"
net.ftp_download("test.rebex.net", filename, "demo", "password")
self.assertTrue(os.path.exists(os.path.basename(filename)))

def test_ftp_anonymous(self):
filename = "1KB.zip"
net.ftp_download("speedtest.tele2.net", filename)
self.assertTrue(os.path.exists(os.path.basename(filename)))

def test_ftp_invalid_path(self):
with self.assertRaisesRegexp(ConanException,
"550 The system cannot find the file specified."):
net.ftp_download("test.rebex.net", "invalid-file", "demo", "password")
self.assertFalse(os.path.exists("invalid-file"))

def test_ftp_invalid_auth(self):
with self.assertRaisesRegexp(ConanException, "530 User cannot log in."):
net.ftp_download("test.rebex.net", "readme.txt", "demo", "invalid")
self.assertFalse(os.path.exists("readme.txt"))