From 7d7f59c98147f89bf5c53c34679ef834bb57c2e1 Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Sun, 1 Mar 2020 20:07:50 -0500 Subject: [PATCH 1/3] Fix parse_netrc to work on Windows --- Ska/ftp/ftp.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Ska/ftp/ftp.py b/Ska/ftp/ftp.py index 3c6486f..8633241 100644 --- a/Ska/ftp/ftp.py +++ b/Ska/ftp/ftp.py @@ -27,8 +27,10 @@ def parse_netrc(netrcfile=None): """ out = {} try: - # Use stdlib to parse netrcfile - nrc = netrc.netrc(netrcfile) + # Use stdlib to parse netrcfile. Before Python 3.8 netrc.netrc(None) + # uses $HOME to find ~/.netrc and this fails on Windows (should be + # $HOMEPATH). So just force the issue here if netrcfile is None. + nrc = netrc.netrc(netrcfile or os.path.expanduser('~/.netrc')) for host, vals in nrc.hosts.items(): out[host] = {'login': vals[0], 'account': vals[1], From d32f398d6fdc4bb0c8a0c727ac230e9f424780ac Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Sun, 1 Mar 2020 20:42:32 -0500 Subject: [PATCH 2/3] Fix test to work on Windows --- Ska/ftp/tests/test_basic.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Ska/ftp/tests/test_basic.py b/Ska/ftp/tests/test_basic.py index b70c60e..12ab523 100644 --- a/Ska/ftp/tests/test_basic.py +++ b/Ska/ftp/tests/test_basic.py @@ -3,6 +3,8 @@ import os import sys import re +import random +from pathlib import Path import Ska.ftp import tempfile import pyyaks.logger @@ -20,18 +22,22 @@ def roundtrip(FtpClass, logger=None): lucky.cd(homedir) files_before = lucky.ls() - tmpfile = tempfile.NamedTemporaryFile() + with tempfile.TemporaryDirectory() as tmpdir: + tmpfile_in = Path(tmpdir, 'tmpfile_in') + tmpfile_out = Path(tmpdir, 'tmpfile_out') + text = ''.join(random.choices('abcdefghijklmno', k=100)) + with open(tmpfile_in, 'w') as fh: + fh.write(text) - local_filename = os.path.join(os.environ['HOME'], '.cshrc') - lucky.put(local_filename, '{}/remote_cshrc'.format(homedir)) - lucky.get('remote_cshrc', tmpfile.name) - lucky.delete('remote_cshrc') + lucky.put(tmpfile_in, f'{homedir}/remote_test_file') + lucky.get('remote_test_file', tmpfile_out) + lucky.delete('remote_test_file') - files_after = lucky.ls() - lucky.close() + files_after = lucky.ls() + lucky.close() - orig = open(local_filename).read() - roundtrip = open(tmpfile.name).read() + orig = open(tmpfile_in).read() + roundtrip = open(tmpfile_out).read() assert files_before == files_after assert orig == roundtrip From cbc9d6b9856c670c13e97124a84c22a1ac8e9e5a Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Wed, 4 Mar 2020 07:12:34 -0500 Subject: [PATCH 3/3] Use fully-qualified URL for lucky --- Ska/ftp/tests/test_basic.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Ska/ftp/tests/test_basic.py b/Ska/ftp/tests/test_basic.py index 12ab523..2bfc179 100644 --- a/Ska/ftp/tests/test_basic.py +++ b/Ska/ftp/tests/test_basic.py @@ -14,11 +14,12 @@ NETRC = Ska.ftp.parse_netrc() USER = NETRC['lucky']['login'] +LUCKY = 'lucky.cfa.harvard.edu' def roundtrip(FtpClass, logger=None): homedir = ('/home/' if FtpClass is Ska.ftp.SFTP else '/') + USER - lucky = FtpClass('lucky', logger=logger) + lucky = FtpClass(LUCKY, logger=logger) lucky.cd(homedir) files_before = lucky.ls() @@ -55,7 +56,7 @@ def test_roundtrip_no_logger(): def test_sftp_mkdir_rmdir_rename(): homedir = '/home/{}'.format(USER) - lucky = Ska.ftp.SFTP('lucky', logger=logger) + lucky = Ska.ftp.SFTP(LUCKY, logger=logger) lucky.cd(homedir) tmpdir = str(uuid.uuid4()) # random remote dir name @@ -78,7 +79,7 @@ def test_delete_when_ftp_session_already_gone(capfd): """ Confirm that Ska.ftp does not throw attribute errors when deleted. """ - lucky = Ska.ftp.SFTP('lucky', logger=logger) + lucky = Ska.ftp.SFTP(LUCKY, logger=logger) # Delete the paramiko session (without explicitly closing in this test case) del lucky.ftp del lucky