Skip to content

Commit

Permalink
Merge pull request #20 from sot/win-netrc
Browse files Browse the repository at this point in the history
Windows compatibility
  • Loading branch information
taldcroft authored Mar 4, 2020
2 parents fc4915b + cbc9d6b commit fd27c58
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
6 changes: 4 additions & 2 deletions Ska/ftp/ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
31 changes: 19 additions & 12 deletions Ska/ftp/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
import sys
import re
import random
from pathlib import Path
import Ska.ftp
import tempfile
import pyyaks.logger
Expand All @@ -12,26 +14,31 @@

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()

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
Expand All @@ -49,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
Expand All @@ -72,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
Expand Down

0 comments on commit fd27c58

Please sign in to comment.