409f980bc2a37cd7dea17a36db73fd2434c6d2dd
[youtube-dl] / devscripts / transition_helper_exe / youtube-dl.py
1 #!/usr/bin/env python
2
3 import sys, os
4 import urllib2
5
6 sys.stderr.write(u'Hi! We changed distribution method and now youtube-dl needs to update itself one more time.\n')
7 sys.stderr.write(u'This will only happen once. Simply press enter to go on. Sorry for the trouble!\n')
8 sys.stderr.write(u'The new location of the binaries is https://github.com/rg3/youtube-dl/downloads, not the git repository.\n\n')
9
10 raw_input()
11
12 filename = sys.argv[0]
13
14 API_URL = "https://api.github.com/repos/rg3/youtube-dl/downloads"
15 EXE_URL = "https://github.com/downloads/rg3/youtube-dl/youtube-dl.exe"
16
17 if not os.access(filename, os.W_OK):
18     sys.exit('ERROR: no write permissions on %s' % filename)
19
20 exe = os.path.abspath(filename)
21 directory = os.path.dirname(exe)
22 if not os.access(directory, os.W_OK):
23     sys.exit('ERROR: no write permissions on %s' % directory)
24
25 try:
26     urlh = urllib2.urlopen(EXE_URL)
27     newcontent = urlh.read()
28     urlh.close()
29     with open(exe + '.new', 'wb') as outf:
30         outf.write(newcontent)
31 except (IOError, OSError) as err:
32     sys.exit('ERROR: unable to download latest version')
33
34 try:
35     bat = os.path.join(directory, 'youtube-dl-updater.bat')
36     b = open(bat, 'w')
37     b.write("""
38 echo Updating youtube-dl...
39 ping 127.0.0.1 -n 5 -w 1000 > NUL
40 move /Y "%s.new" "%s"
41 del "%s"
42     \n""" %(exe, exe, bat))
43     b.close()
44
45     os.startfile(bat)
46 except (IOError, OSError) as err:
47     sys.exit('ERROR: unable to overwrite current version')
48
49 sys.stderr.write(u'Done! Now you can run youtube-dl.\n')