]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/update.py
[build] Build Windows x86 version with py3.7
[yt-dlp.git] / yt_dlp / update.py
index 655b26f96779638bcdde46b0c99bd5856b793288..d3681b8323b5947650b392434e5179272cd2fe67 100644 (file)
@@ -1,7 +1,6 @@
 from __future__ import unicode_literals
 
 import hashlib
-import io
 import json
 import os
 import platform
@@ -88,10 +87,15 @@ def calc_sha256sum(path):
                 h.update(mv[:n])
         return h.hexdigest()
 
-    if not isinstance(globals().get('__loader__'), zipimporter) and not hasattr(sys, 'frozen'):
-        return report_error(
-            'It looks like you installed yt-dlp with a package manager, pip, setup.py or a tarball. '
-            'Please use that to update', expected=True)
+    err = None
+    if isinstance(globals().get('__loader__'), zipimporter):
+        pass
+    elif hasattr(sys, 'frozen'):
+        pass
+    else:
+        err = 'It looks like you installed yt-dlp with a package manager, pip, setup.py or a tarball. Please use that to update'
+    if err:
+        return report_error(err, expected=True)
 
     # sys.executable is set to the full pathname of the exe-file for py2exe
     # though symlinks are not followed so that we need to do this manually
@@ -118,7 +122,6 @@ def version_tuple(version_str):
 
     version_labels = {
         'zip_3': '',
-        'zip_2': '',
         'exe_64': '.exe',
         'exe_32': '_x86.exe',
     }
@@ -128,15 +131,19 @@ def get_bin_info(bin_or_exe, version):
         return next((i for i in version_info['assets'] if i['name'] == 'yt-dlp%s' % label), {})
 
     def get_sha256sum(bin_or_exe, version):
-        label = version_labels['%s_%s' % (bin_or_exe, version)]
+        filename = 'yt-dlp%s' % version_labels['%s_%s' % (bin_or_exe, version)]
         urlh = next(
             (i for i in version_info['assets'] if i['name'] in ('SHA2-256SUMS')),
             {}).get('browser_download_url')
         if not urlh:
             return None
         hash_data = ydl._opener.open(urlh).read().decode('utf-8')
-        hashes = list(map(lambda x: x.split(':'), hash_data.splitlines()))
-        return next((i[1] for i in hashes if i[0] == 'yt-dlp%s' % label), None)
+        if hash_data.startswith('version:'):
+            # Old colon-separated hash file
+            return dict(ln.split(':') for ln in hash_data.splitlines()).get(filename)
+        else:
+            # GNU-style hash file
+            return dict(ln.split()[::-1] for ln in hash_data.splitlines()).get(filename)
 
     if not os.access(filename, os.W_OK):
         return report_error('no write permissions on %s' % filename, expected=True)
@@ -147,6 +154,11 @@ def get_sha256sum(bin_or_exe, version):
         directory = os.path.dirname(exe)
         if not os.access(directory, os.W_OK):
             return report_error('no write permissions on %s' % directory, expected=True)
+        try:
+            if os.path.exists(filename + '.old'):
+                os.remove(filename + '.old')
+        except (IOError, OSError):
+            return report_error('unable to remove the old version')
 
         try:
             arch = platform.architecture()[0][:2]
@@ -176,28 +188,29 @@ def get_sha256sum(bin_or_exe, version):
                 return report_error('unable to remove corrupt download')
 
         try:
-            bat = os.path.join(directory, 'yt-dlp-updater.cmd')
-            with io.open(bat, 'w') as batfile:
-                batfile.write('''
-@(
-    echo.Waiting for file handle to be closed ...
-    ping 127.0.0.1 -n 5 -w 1000 > NUL
-    move /Y "%s.new" "%s" > NUL
-    echo.Updated yt-dlp to version %s
-)
-@start /b "" cmd /c del "%%~f0"&exit /b
-                ''' % (exe, exe, version_id))
-
-            subprocess.Popen([bat])  # Continues to run in the background
+            os.rename(exe, exe + '.old')
+        except (IOError, OSError):
+            return report_error('unable to move current version')
+        try:
+            os.rename(exe + '.new', exe)
         except (IOError, OSError):
             report_error('unable to overwrite current version')
-        return True  # Exit app
+            os.rename(exe + '.old', exe)
+            return
+        try:
+            # Continues to run in the background
+            subprocess.Popen(
+                'ping 127.0.0.1 -n 5 -w 1000 & del /F "%s.old"' % exe,
+                shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+            ydl.to_screen('Updated yt-dlp to version %s' % version_id)
+            return True  # Exit app
+        except OSError:
+            report_error('unable to delete old version')
 
     # Zip unix package
     elif isinstance(globals().get('__loader__'), zipimporter):
         try:
-            py_ver = platform.python_version()[0]
-            url = get_bin_info('zip', py_ver).get('browser_download_url')
+            url = get_bin_info('zip', '3').get('browser_download_url')
             if not url:
                 return report_error('unable to fetch updates', True)
             urlh = ydl._opener.open(url)
@@ -206,8 +219,10 @@ def get_sha256sum(bin_or_exe, version):
         except (IOError, OSError, StopIteration):
             return report_error('unable to download latest version', True)
 
-        expected_sum = get_sha256sum('zip', py_ver)
-        if expected_sum and hashlib.sha256(newcontent).hexdigest() != expected_sum:
+        expected_sum = get_sha256sum('zip', '3')
+        if not expected_sum:
+            ydl.report_warning('no hash information found for the release')
+        elif hashlib.sha256(newcontent).hexdigest() != expected_sum:
             return report_error('unable to verify the new zip', True)
 
         try: