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

contrib/ftp: Clean up temporary files #2755

Merged
merged 4 commits into from
Aug 19, 2019
Merged
Changes from 1 commit
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
30 changes: 24 additions & 6 deletions luigi/contrib/ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
# limitations under the License.
#
"""
This library is a wrapper of ftplib.
It is convenient to move data from/to FTP.
This library is a wrapper of ftplib or pysftp.
It is convenient to move data from/to (S)FTP servers.

There is an example on how to use it (example/ftp_experiment_outputs.py)

You can also find unittest for each class.

Be aware that normal ftp do not provide secure communication.
Be aware that normal ftp does not provide secure communication.
"""

import datetime
Expand Down Expand Up @@ -368,7 +368,8 @@ class RemoteTarget(luigi.target.FileSystemTarget):
"""
Target used for reading from remote files.

The target is implemented using ssh commands streaming data over the network.
The target is implemented using intermediate files on the local system.
On Python2, these files may not be cleaned up.
"""

def __init__(
Expand Down Expand Up @@ -407,8 +408,25 @@ def open(self, mode):
return self.format.pipe_writer(AtomicFtpFile(self._fs, self.path))

elif mode == 'r':
temp_dir = os.path.join(tempfile.gettempdir(), 'luigi-contrib-ftp')
self.__tmp_path = temp_dir + '/' + self.path.lstrip('/') + '-luigi-tmp-%09d' % random.randrange(0, 1e10)
try:
# store reference to the TemporaryDirectory because it will be removed on GC
self.__temp_dir = tempfile.TemporaryDirectory(
prefix="luigi-contrib-ftp"
)
except AttributeError:
# TemporaryDirectory only available in Python3, use old behaviour in Python2
# this file will not be cleaned up automatically
self.__tmp_path = os.path.join(
tempfile.gettempdir(),
'luigi-contrib-ftp',
self.path + '-luigi-tmp-%09d' % random.randrange(0, 1e10),
)
else:
self.__tmp_path = os.path.join(
self.__temp_dir.name,
self.path + "-luigi-tmp-%09d" % random.randrange(0, 1e10),
jethron marked this conversation as resolved.
Show resolved Hide resolved
)

# download file to local
self._fs.get(self.path, self.__tmp_path)

Expand Down