]> Raphaƫl G. Git Repositories - youtubedl/blob - devscripts/transition_helper_exe/youtube-dl.py
debian/changelog: Finalize release.
[youtubedl] / devscripts / transition_helper_exe / youtube-dl.py
1 #!/usr/bin/env python
2
3 import sys, os
4 import urllib2
5 import json, hashlib
6
7 def rsa_verify(message, signature, key):
8 from struct import pack
9 from hashlib import sha256
10 from sys import version_info
11 def b(x):
12 if version_info[0] == 2: return x
13 else: return x.encode('latin1')
14 assert(type(message) == type(b('')))
15 block_size = 0
16 n = key[0]
17 while n:
18 block_size += 1
19 n >>= 8
20 signature = pow(int(signature, 16), key[1], key[0])
21 raw_bytes = []
22 while signature:
23 raw_bytes.insert(0, pack("B", signature & 0xFF))
24 signature >>= 8
25 signature = (block_size - len(raw_bytes)) * b('\x00') + b('').join(raw_bytes)
26 if signature[0:2] != b('\x00\x01'): return False
27 signature = signature[2:]
28 if not b('\x00') in signature: return False
29 signature = signature[signature.index(b('\x00'))+1:]
30 if not signature.startswith(b('\x30\x31\x30\x0D\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20')): return False
31 signature = signature[19:]
32 if signature != sha256(message).digest(): return False
33 return True
34
35 sys.stderr.write(u'Hi! We changed distribution method and now youtube-dl needs to update itself one more time.\n')
36 sys.stderr.write(u'This will only happen once. Simply press enter to go on. Sorry for the trouble!\n')
37 sys.stderr.write(u'From now on, get the binaries from http://rg3.github.com/youtube-dl/download.html, not from the git repository.\n\n')
38
39 raw_input()
40
41 filename = sys.argv[0]
42
43 UPDATE_URL = "http://rg3.github.io/youtube-dl/update/"
44 VERSION_URL = UPDATE_URL + 'LATEST_VERSION'
45 JSON_URL = UPDATE_URL + 'versions.json'
46 UPDATES_RSA_KEY = (0x9d60ee4d8f805312fdb15a62f87b95bd66177b91df176765d13514a0f1754bcd2057295c5b6f1d35daa6742c3ffc9a82d3e118861c207995a8031e151d863c9927e304576bc80692bc8e094896fcf11b66f3e29e04e3a71e9a11558558acea1840aec37fc396fb6b65dc81a1c4144e03bd1c011de62e3f1357b327d08426fe93, 65537)
47
48 if not os.access(filename, os.W_OK):
49 sys.exit('ERROR: no write permissions on %s' % filename)
50
51 exe = os.path.abspath(filename)
52 directory = os.path.dirname(exe)
53 if not os.access(directory, os.W_OK):
54 sys.exit('ERROR: no write permissions on %s' % directory)
55
56 try:
57 versions_info = urllib2.urlopen(JSON_URL).read().decode('utf-8')
58 versions_info = json.loads(versions_info)
59 except:
60 sys.exit(u'ERROR: can\'t obtain versions info. Please try again later.')
61 if not 'signature' in versions_info:
62 sys.exit(u'ERROR: the versions file is not signed or corrupted. Aborting.')
63 signature = versions_info['signature']
64 del versions_info['signature']
65 if not rsa_verify(json.dumps(versions_info, sort_keys=True), signature, UPDATES_RSA_KEY):
66 sys.exit(u'ERROR: the versions file signature is invalid. Aborting.')
67
68 version = versions_info['versions'][versions_info['latest']]
69
70 try:
71 urlh = urllib2.urlopen(version['exe'][0])
72 newcontent = urlh.read()
73 urlh.close()
74 except (IOError, OSError) as err:
75 sys.exit('ERROR: unable to download latest version')
76
77 newcontent_hash = hashlib.sha256(newcontent).hexdigest()
78 if newcontent_hash != version['exe'][1]:
79 sys.exit(u'ERROR: the downloaded file hash does not match. Aborting.')
80
81 try:
82 with open(exe + '.new', 'wb') as outf:
83 outf.write(newcontent)
84 except (IOError, OSError) as err:
85 sys.exit(u'ERROR: unable to write the new version')
86
87 try:
88 bat = os.path.join(directory, 'youtube-dl-updater.bat')
89 b = open(bat, 'w')
90 b.write("""
91 echo Updating youtube-dl...
92 ping 127.0.0.1 -n 5 -w 1000 > NUL
93 move /Y "%s.new" "%s"
94 del "%s"
95 \n""" %(exe, exe, bat))
96 b.close()
97
98 os.startfile(bat)
99 except (IOError, OSError) as err:
100 sys.exit('ERROR: unable to overwrite current version')
101
102 sys.stderr.write(u'Done! Now you can run youtube-dl.\n')