X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=youtube_dl%2Futils.py;h=5be7cf99200c57639af17b1dc89f9a861658f331;hb=732ea2f09bbcf250e376d6a16dfa0ae8c406ff34;hp=50e515a04a71d76aec89cf313aca9b74c10045ec;hpb=28e614de5ccfd846fe4dafe37d4834ef3488b403;p=youtube-dl diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 50e515a04..5be7cf992 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -71,9 +71,10 @@ def preferredencoding(): def write_json_file(obj, fn): - """ Encode obj as JSON and write it to fn, atomically """ + """ Encode obj as JSON and write it to fn, atomically if possible """ - if sys.version_info < (3, 0): + fn = encodeFilename(fn) + if sys.version_info < (3, 0) and sys.platform != 'win32': encoding = get_filesystem_encoding() # os.path.basename returns a bytes object, but NamedTemporaryFile # will fail if the filename contains non ascii characters unless we @@ -107,6 +108,13 @@ def write_json_file(obj, fn): try: with tf: json.dump(obj, tf) + if sys.platform == 'win32': + # Need to remove existing file on Windows, else os.rename raises + # WindowsError or FileExistsError. + try: + os.unlink(fn) + except OSError: + pass os.rename(tf.name, fn) except: try: @@ -412,6 +420,7 @@ def make_HTTPS_handler(opts_no_check_certificate, **kwargs): pass # Python < 3.4 return compat_urllib_request.HTTPSHandler(context=context, **kwargs) + class ExtractorError(Exception): """Error during info extraction.""" def __init__(self, msg, tb=None, expected=False, cause=None, video_id=None): @@ -426,7 +435,13 @@ class ExtractorError(Exception): if cause: msg += ' (caused by %r)' % cause if not expected: - msg = msg + '; please report this issue on https://yt-dl.org/bug . Be sure to call youtube-dl with the --verbose flag and include its complete output. Make sure you are using the latest version; type youtube-dl -U to update.' + if ytdl_is_updateable(): + update_cmd = 'type youtube-dl -U to update' + else: + update_cmd = 'see https://yt-dl.org/update on how to update' + msg += '; please report this issue on https://yt-dl.org/bug .' + msg += ' Make sure you are using the latest version; %s.' % update_cmd + msg += ' Be sure to call youtube-dl with the --verbose flag and include its complete output.' super(ExtractorError, self).__init__(msg) self.traceback = tb @@ -1411,3 +1426,10 @@ def is_outdated_version(version, limit, assume_new=True): return version_tuple(version) < version_tuple(limit) except ValueError: return not assume_new + + +def ytdl_is_updateable(): + """ Returns if youtube-dl can be updated with -U """ + from zipimport import zipimporter + + return isinstance(globals().get('__loader__'), zipimporter) or hasattr(sys, 'frozen')