2121from __future__ import absolute_import
2222
2323__all__ = ['split_data' , 'split_and_load' , 'clip_global_norm' ,
24- 'check_sha1' , 'download' ]
24+ 'check_sha1' , 'download' , 'replace_file' ]
2525
2626import os
2727import sys
3535import numpy as np
3636
3737from .. import ndarray
38- from ..util import is_np_shape , is_np_array
38+ from ..util import is_np_shape , is_np_array , makedirs
3939from .. import numpy as _mx_np # pylint: disable=reimported
4040
4141
@@ -209,8 +209,14 @@ def check_sha1(filename, sha1_hash):
209209
210210if not sys .platform .startswith ('win32' ):
211211 # refer to https://github.com/untitaker/python-atomicwrites
212- def _replace_atomic (src , dst ):
213- """Implement atomic os.replace with linux and OSX. Internal use only"""
212+ def replace_file (src , dst ):
213+ """Implement atomic os.replace with linux and OSX.
214+
215+ Parameters
216+ ----------
217+ src : source file path
218+ dst : destination file path
219+ """
214220 try :
215221 os .rename (src , dst )
216222 except OSError :
@@ -252,19 +258,25 @@ def _handle_errors(rv, src):
252258 finally :
253259 raise OSError (msg )
254260
255- def _replace_atomic (src , dst ):
261+ def replace_file (src , dst ):
256262 """Implement atomic os.replace with windows.
263+
257264 refer to https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-movefileexw
258265 The function fails when one of the process(copy, flush, delete) fails.
259- Internal use only"""
266+
267+ Parameters
268+ ----------
269+ src : source file path
270+ dst : destination file path
271+ """
260272 _handle_errors (ctypes .windll .kernel32 .MoveFileExW (
261273 _str_to_unicode (src ), _str_to_unicode (dst ),
262274 _windows_default_flags | _MOVEFILE_REPLACE_EXISTING
263275 ), src )
264276
265277
266278def download (url , path = None , overwrite = False , sha1_hash = None , retries = 5 , verify_ssl = True ):
267- """Download an given URL
279+ """Download a given URL
268280
269281 Parameters
270282 ----------
@@ -310,7 +322,7 @@ def download(url, path=None, overwrite=False, sha1_hash=None, retries=5, verify_
310322 if overwrite or not os .path .exists (fname ) or (sha1_hash and not check_sha1 (fname , sha1_hash )):
311323 dirname = os .path .dirname (os .path .abspath (os .path .expanduser (fname )))
312324 if not os .path .exists (dirname ):
313- os . makedirs (dirname )
325+ makedirs (dirname )
314326 while retries + 1 > 0 :
315327 # Disable pyling too broad Exception
316328 # pylint: disable=W0703
@@ -330,7 +342,7 @@ def download(url, path=None, overwrite=False, sha1_hash=None, retries=5, verify_
330342 # delete the temporary file
331343 if not os .path .exists (fname ) or (sha1_hash and not check_sha1 (fname , sha1_hash )):
332344 # atmoic operation in the same file system
333- _replace_atomic ('{}.{}' .format (fname , random_uuid ), fname )
345+ replace_file ('{}.{}' .format (fname , random_uuid ), fname )
334346 else :
335347 try :
336348 os .remove ('{}.{}' .format (fname , random_uuid ))
0 commit comments