]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/update.py
1 from __future__
import unicode_literals
10 from zipimport
import zipimporter
14 compat_urllib_request
,
16 from .utils
import make_HTTPS_handler
17 from .version
import __version__
20 def rsa_verify(message
, signature
, key
):
21 from struct
import pack
22 from hashlib
import sha256
24 assert isinstance(message
, bytes)
30 signature
= pow(int(signature
, 16), key
[1], key
[0])
33 raw_bytes
.insert(0, pack("B", signature
& 0xFF))
35 signature
= (block_size
- len(raw_bytes
)) * b
'\x00' + b
''.join(raw_bytes
)
36 if signature
[0:2] != b
'\x00\x01':
38 signature
= signature
[2:]
39 if b
'\x00' not in signature
:
41 signature
= signature
[signature
.index(b
'\x00') + 1:]
42 if not signature
.startswith(b
'\x30\x31\x30\x0D\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20'):
44 signature
= signature
[19:]
45 if signature
!= sha256(message
).digest():
50 def update_self(to_screen
, verbose
):
51 """Update the program file with the latest version from the repository"""
53 UPDATE_URL
= "https://rg3.github.io/youtube-dl/update/"
54 VERSION_URL
= UPDATE_URL
+ 'LATEST_VERSION'
55 JSON_URL
= UPDATE_URL
+ 'versions.json'
56 UPDATES_RSA_KEY
= (0x9d60ee4d8f805312fdb15a62f87b95bd66177b91df176765d13514a0f1754bcd2057295c5b6f1d35daa6742c3ffc9a82d3e118861c207995a8031e151d863c9927e304576bc80692bc8e094896fcf11b66f3e29e04e3a71e9a11558558acea1840aec37fc396fb6b65dc81a1c4144e03bd1c011de62e3f1357b327d08426fe93, 65537)
58 if not isinstance(globals().get('__loader__'), zipimporter
) and not hasattr(sys
, "frozen"):
59 to_screen('It looks like you installed youtube-dl with a package manager, pip, setup.py or a tarball. Please use that to update.')
62 https_handler
= make_HTTPS_handler({})
63 opener
= compat_urllib_request
.build_opener(https_handler
)
65 # Check if there is a new version
67 newversion
= opener
.open(VERSION_URL
).read().decode('utf-8').strip()
70 to_screen(compat_str(traceback
.format_exc()))
71 to_screen('ERROR: can\'t find the current version. Please try again later.')
73 if newversion
== __version__
:
74 to_screen('youtube-dl is up-to-date (' + __version__
+ ')')
77 # Download and check versions info
79 versions_info
= opener
.open(JSON_URL
).read().decode('utf-8')
80 versions_info
= json
.loads(versions_info
)
83 to_screen(compat_str(traceback
.format_exc()))
84 to_screen('ERROR: can\'t obtain versions info. Please try again later.')
86 if 'signature' not in versions_info
:
87 to_screen('ERROR: the versions file is not signed or corrupted. Aborting.')
89 signature
= versions_info
['signature']
90 del versions_info
['signature']
91 if not rsa_verify(json
.dumps(versions_info
, sort_keys
=True).encode('utf-8'), signature
, UPDATES_RSA_KEY
):
92 to_screen('ERROR: the versions file signature is invalid. Aborting.')
95 version_id
= versions_info
['latest']
97 def version_tuple(version_str
):
98 return tuple(map(int, version_str
.split('.')))
99 if version_tuple(__version__
) >= version_tuple(version_id
):
100 to_screen('youtube-dl is up to date (%s)' % __version__
)
103 to_screen('Updating to version ' + version_id
+ ' ...')
104 version
= versions_info
['versions'][version_id
]
106 print_notes(to_screen
, versions_info
['versions'])
108 filename
= sys
.argv
[0]
109 # Py2EXE: Filename could be different
110 if hasattr(sys
, "frozen") and not os
.path
.isfile(filename
):
111 if os
.path
.isfile(filename
+ '.exe'):
114 if not os
.access(filename
, os
.W_OK
):
115 to_screen('ERROR: no write permissions on %s' % filename
)
119 if hasattr(sys
, "frozen"):
120 exe
= os
.path
.abspath(filename
)
121 directory
= os
.path
.dirname(exe
)
122 if not os
.access(directory
, os
.W_OK
):
123 to_screen('ERROR: no write permissions on %s' % directory
)
127 urlh
= opener
.open(version
['exe'][0])
128 newcontent
= urlh
.read()
130 except (IOError, OSError):
132 to_screen(compat_str(traceback
.format_exc()))
133 to_screen('ERROR: unable to download latest version')
136 newcontent_hash
= hashlib
.sha256(newcontent
).hexdigest()
137 if newcontent_hash
!= version
['exe'][1]:
138 to_screen('ERROR: the downloaded file hash does not match. Aborting.')
142 with open(exe
+ '.new', 'wb') as outf
:
143 outf
.write(newcontent
)
144 except (IOError, OSError):
146 to_screen(compat_str(traceback
.format_exc()))
147 to_screen('ERROR: unable to write the new version')
151 bat
= os
.path
.join(directory
, 'youtube-dl-updater.bat')
152 with io
.open(bat
, 'w') as batfile
:
155 echo Waiting for file handle to be closed ...
156 ping 127.0.0.1 -n 5 -w 1000 > NUL
157 move /Y "%s.new" "%s" > NUL
158 echo Updated youtube-dl to version %s.
159 start /b "" cmd /c del "%%~f0"&exit /b"
160 \n''' % (exe
, exe
, version_id
))
162 subprocess
.Popen([bat
]) # Continues to run in the background
163 return # Do not show premature success messages
164 except (IOError, OSError):
166 to_screen(compat_str(traceback
.format_exc()))
167 to_screen('ERROR: unable to overwrite current version')
171 elif isinstance(globals().get('__loader__'), zipimporter
):
173 urlh
= opener
.open(version
['bin'][0])
174 newcontent
= urlh
.read()
176 except (IOError, OSError):
178 to_screen(compat_str(traceback
.format_exc()))
179 to_screen('ERROR: unable to download latest version')
182 newcontent_hash
= hashlib
.sha256(newcontent
).hexdigest()
183 if newcontent_hash
!= version
['bin'][1]:
184 to_screen('ERROR: the downloaded file hash does not match. Aborting.')
188 with open(filename
, 'wb') as outf
:
189 outf
.write(newcontent
)
190 except (IOError, OSError):
192 to_screen(compat_str(traceback
.format_exc()))
193 to_screen('ERROR: unable to overwrite current version')
196 to_screen('Updated youtube-dl. Restart youtube-dl to use the new version.')
199 def get_notes(versions
, fromVersion
):
201 for v
, vdata
in sorted(versions
.items()):
203 notes
.extend(vdata
.get('notes', []))
207 def print_notes(to_screen
, versions
, fromVersion
=__version__
):
208 notes
= get_notes(versions
, fromVersion
)
210 to_screen('PLEASE NOTE:')