Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sublime Update script
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import sys
import urllib2
from subprocess import call
from platform import machine
def Download(url, file_name=""):
if file_name == "":
file_name = url.split('/')[-1]
u = urllib2.urlopen(url.replace(' ', '%20'))
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print("Downloading: {0} Bytes: {1}".format(file_name, file_size))
if os.name == "nt":
print(" 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%")
else:
sys.stdout.write("[ ]")
file_size_dl = 0
block_sz = 8192
last_p = 0
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
p = float(file_size_dl) / file_size
if int(p*50) > last_p:
last_p = int(p*50)
if os.name == "nt":
sys.stdout.write("#")
else:
sys.stdout.write("\r[")
sys.stdout.write("#"*int(p*50))
sys.stdout.flush()
print "\nDone"
f.close()
def PosUpdate(version):
Download(version, "/tmp/sublime.tar.bz2")
try:
os.mkdir("~/.sublime_text")
os.mkdir("~/.bin")
print "Folder created"
except:
print "Folder exists"
call("/bin/tar --strip-components=1 -C ~/.sublime_text/ -xvjf /tmp/sublime.tar.bz2", shell=True)
call("/bin/ln -sf ~/.sublime_text/sublime_text ~/.bin/slime", shell=True)
print "Unpacked new version"
os.remove("/tmp/sublime.tar.bz2")
def WinUpdate(version):
Download(version)
call(version.split('/')[-1])
print "Installing new version"
def GetLatest(version):
conn = urllib2.urlopen("http://www.sublimetext.com/" + version)
data = conn.read()
names = {}
names['windows_x64'] = re.search('href="([^"]+)">Windows 64 bit', data).groups(1)[0]
names['windows_x86'] = re.search('href="([^"]+)">Windows<', data).groups(1)[0]
names['os_x'] = re.search('href="([^"]+)">OS X', data).groups(1)[0]
if version == "3":
names['linux_x64'] = re.search('href="([^"]+)x64.tar.bz2">tarball', data).groups(1)[0] + "x64.tar.bz2"
names['linux_x86'] = re.search('href="([^"]+)x32.tar.bz2">tarball', data).groups(1)[0] + "x32.tar.bz2"
else:
names['linux_x64'] = re.search('href="([^"]+)">Linux 64 bit', data).groups(1)[0]
names['linux_x86'] = re.search('href="([^"]+)">Linux 32 bit', data).groups(1)[0]
return names
def Main():
# if os.name != "nt" and os.geteuid():
# print "You must be root to run this script"
# return
ver = "2"
if len(sys.argv) > 1 and sys.argv[1] == "3":
ver = "3"
elif len(sys.argv) > 1:
print "This script can only update Sublime Text 2 and 3."
return
versions = GetLatest(ver)
version = "x86"
if "64" in machine():
version = "x64"
if os.name == "posix":
print "Updating", version, "Linux version of Sublime Text", ver
PosUpdate(versions["linux_" + version])
elif os.name == "nt":
print "Updating", version, "Windows version of Sublime Text", ver
WinUpdate(versions["windows_" + version])
else:
print "Unknown operating system"
if __name__ == "__main__":
Main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment