]>
jfr.im git - yt-dlp.git/blob - yt_dlp/update.py
1 from __future__
import unicode_literals
10 from zipimport
import zipimporter
12 from .compat
import compat_realpath
13 from .utils
import encode_compat_str
, Popen
, write_string
15 from .version
import __version__
19 if hasattr(sys
, 'frozen'):
20 prefix
= 'mac' if sys
.platform
== 'darwin' else 'win'
21 if getattr(sys
, '_MEIPASS', None):
22 if sys
._MEIPASS
== os
.path
.dirname(sys
.executable
):
23 return f
'{prefix}_dir'
24 return f
'{prefix}_exe'
26 elif isinstance(__loader__
, zipimporter
):
28 elif os
.path
.basename(sys
.argv
[0]) == '__main__.py':
33 _NON_UPDATEABLE_REASONS
= {
38 'win_dir': 'Auto-update is not supported for unpackaged windows executable; Re-download the latest release',
39 'mac_dir': 'Auto-update is not supported for unpackaged MacOS executable; Re-download the latest release',
40 'source': 'You cannot update when running from source code; Use git to pull the latest changes',
41 'unknown': 'It looks like you installed yt-dlp with a package manager, pip or setup.py; Use that to update',
45 def is_non_updateable():
46 return _NON_UPDATEABLE_REASONS
.get(detect_variant(), _NON_UPDATEABLE_REASONS
['unknown'])
51 Update the program file with the latest version from the repository
52 Returns whether the program should terminate
55 JSON_URL
= 'https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest'
57 def report_error(msg
, expected
=False):
58 ydl
.report_error(msg
, tb
='' if expected
else None)
60 def report_unable(action
, expected
=False):
61 report_error(f
'Unable to {action}', expected
)
63 def report_permission_error(file):
64 report_unable(f
'write to {file}; Try running as administrator', True)
66 def report_network_error(action
, delim
=';'):
67 report_unable(f
'{action}{delim} Visit https://github.com/yt-dlp/yt-dlp/releases/latest', True)
69 def calc_sha256sum(path
):
71 b
= bytearray(128 * 1024)
73 with open(os
.path
.realpath(path
), 'rb', buffering
=0) as f
:
74 for n
in iter(lambda: f
.readinto(mv
), 0):
78 # Download and check versions info
80 version_info
= ydl
._opener
.open(JSON_URL
).read().decode('utf-8')
81 version_info
= json
.loads(version_info
)
83 return report_network_error('obtain version info', delim
='; Please try again later or')
85 def version_tuple(version_str
):
86 return tuple(map(int, version_str
.split('.')))
88 version_id
= version_info
['tag_name']
89 ydl
.to_screen(f
'Latest version: {version_id}, Current version: {__version__}')
90 if version_tuple(__version__
) >= version_tuple(version_id
):
91 ydl
.to_screen(f
'yt-dlp is up to date ({__version__})')
94 err
= is_non_updateable()
96 return report_error(err
, True)
98 # sys.executable is set to the full pathname of the exe-file for py2exe
99 # though symlinks are not followed so that we need to do this manually
100 # with help of realpath
101 filename
= compat_realpath(sys
.executable
if hasattr(sys
, 'frozen') else sys
.argv
[0])
102 ydl
.to_screen(f
'Current Build Hash {calc_sha256sum(filename)}')
103 ydl
.to_screen(f
'Updating to version {version_id} ...')
107 'win_exe_64': '.exe',
108 'py2exe_64': '_min.exe',
109 'win_exe_32': '_x86.exe',
110 'mac_exe_64': '_macos',
113 def get_bin_info(bin_or_exe
, version
):
114 label
= version_labels
['%s_%s' % (bin_or_exe
, version
)]
115 return next((i
for i
in version_info
['assets'] if i
['name'] == 'yt-dlp%s' % label
), {})
117 def get_sha256sum(bin_or_exe
, version
):
118 filename
= 'yt-dlp%s' % version_labels
['%s_%s' % (bin_or_exe
, version
)]
120 (i
for i
in version_info
['assets'] if i
['name'] in ('SHA2-256SUMS')),
121 {}).get('browser_download_url')
124 hash_data
= ydl
._opener
.open(urlh
).read().decode('utf-8')
125 return dict(ln
.split()[::-1] for ln
in hash_data
.splitlines()).get(filename
)
127 if not os
.access(filename
, os
.W_OK
):
128 return report_permission_error(filename
)
131 variant
= detect_variant()
132 if variant
in ('win_exe', 'py2exe'):
133 directory
= os
.path
.dirname(filename
)
134 if not os
.access(directory
, os
.W_OK
):
135 return report_permission_error(directory
)
137 if os
.path
.exists(filename
+ '.old'):
138 os
.remove(filename
+ '.old')
139 except (IOError, OSError):
140 return report_unable('remove the old version')
143 arch
= platform
.architecture()[0][:2]
144 url
= get_bin_info(variant
, arch
).get('browser_download_url')
146 return report_network_error('fetch updates')
147 urlh
= ydl
._opener
.open(url
)
148 newcontent
= urlh
.read()
150 except (IOError, OSError):
151 return report_network_error('download latest version')
154 with open(filename
+ '.new', 'wb') as outf
:
155 outf
.write(newcontent
)
156 except (IOError, OSError):
157 return report_permission_error(f
'{filename}.new')
159 expected_sum
= get_sha256sum(variant
, arch
)
161 ydl
.report_warning('no hash information found for the release')
162 elif calc_sha256sum(filename
+ '.new') != expected_sum
:
163 report_network_error('verify the new executable')
165 os
.remove(filename
+ '.new')
167 return report_unable('remove corrupt download')
170 os
.rename(filename
, filename
+ '.old')
171 except (IOError, OSError):
172 return report_unable('move current version')
174 os
.rename(filename
+ '.new', filename
)
175 except (IOError, OSError):
176 report_unable('overwrite current version')
177 os
.rename(filename
+ '.old', filename
)
180 # Continues to run in the background
182 'ping 127.0.0.1 -n 5 -w 1000 & del /F "%s.old"' % filename
,
183 shell
=True, stdout
=subprocess
.DEVNULL
, stderr
=subprocess
.DEVNULL
)
184 ydl
.to_screen('Updated yt-dlp to version %s' % version_id
)
185 return True # Exit app
187 report_unable('delete the old version')
189 elif variant
in ('zip', 'mac_exe'):
190 pack_type
= '3' if variant
== 'zip' else '64'
192 url
= get_bin_info(variant
, pack_type
).get('browser_download_url')
194 return report_network_error('fetch updates')
195 urlh
= ydl
._opener
.open(url
)
196 newcontent
= urlh
.read()
198 except (IOError, OSError):
199 return report_network_error('download the latest version')
201 expected_sum
= get_sha256sum(variant
, pack_type
)
203 ydl
.report_warning('no hash information found for the release')
204 elif hashlib
.sha256(newcontent
).hexdigest() != expected_sum
:
205 return report_network_error('verify the new package')
208 with open(filename
, 'wb') as outf
:
209 outf
.write(newcontent
)
210 except (IOError, OSError):
211 return report_unable('overwrite current version')
213 ydl
.to_screen('Updated yt-dlp to version %s; Restart yt-dlp to use the new version' % version_id
)
216 assert False, f
'Unhandled variant: {variant}'
220 def update_self(to_screen
, verbose
, opener
):
225 'DeprecationWarning: "yt_dlp.update.update_self" is deprecated and may be removed in a future version. '
226 'Use "yt_dlp.update.run_update(ydl)" instead\n')
233 def report_warning(msg
, *args
, **kwargs
):
234 return printfn('WARNING: %s' % msg
, *args
, **kwargs
)
237 def report_error(msg
, tb
=None):
238 printfn('ERROR: %s' % msg
)
242 # Copied from YoutubeDl.trouble
243 if sys
.exc_info()[0]:
245 if hasattr(sys
.exc_info()[1], 'exc_info') and sys
.exc_info()[1].exc_info
[0]:
246 tb
+= ''.join(traceback
.format_exception(*sys
.exc_info()[1].exc_info
))
247 tb
+= encode_compat_str(traceback
.format_exc())
249 tb_data
= traceback
.format_list(traceback
.extract_stack())
250 tb
= ''.join(tb_data
)
254 return run_update(FakeYDL())