Skip to content

Instantly share code, notes, and snippets.

@uchida
Created August 31, 2012 22:22
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save uchida/3559987 to your computer and use it in GitHub Desktop.
Save uchida/3559987 to your computer and use it in GitHub Desktop.
CUI Downloader of Command Line Tools for Xcode
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# CUI Downloader of "Command Line Tools for Xcode"
# by Akihiro Uchida, CC0 dedicated to the public domain
# see http://creativecommons.org/publicdomain/zero/1.0/
import sys, os
import urllib, urllib2, cookielib
from getpass import getpass
from HTMLParser import HTMLParser
class LoginPageParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.action = ""
self.params = {}
def handle_starttag(self, tag, attrs):
if tag == "form":
attrs = dict(attrs)
if "action" in attrs:
self.action = attrs["action"]
def read_with_progress(response, chunk_size=8192):
total_size = int(response.info().getheader("Content-Length").strip())
read = ""
bytes_so_far = 0
while True:
chunk = response.read(chunk_size)
bytes_so_far += len(chunk)
if not chunk:
break
show_progress(bytes_so_far, total_size)
read += chunk
return read
def show_progress(bytes_so_far, total_size, bar_width=40):
percent = 100 * bytes_so_far / total_size
progress = bar_width * percent / 100
fill = '#' * progress
blank = '.' * (bar_width - progress)
sys.stdout.write("[{}{}]{:3d}%\r".format(fill, blank, percent))
if bytes_so_far >= total_size:
sys.stdout.write("\n")
def login(opener):
# get login action
login_url = "https://developer.apple.com/membercenter/login.action"
res = opener.open(login_url)
login_page = res.read()
parser = LoginPageParser()
parser.feed(login_page)
action, params = parser.action, parser.params
parser.close()
# input Apple ID and password
params["appleId"] = raw_input("Apple ID: ")
params["accountPassword"] = getpass("Password: ")
# log in Apple Developpers Member Center and get cookie
url = "https://idmsa.apple.com/IDMSWebAuth/{}?{}".format(action, urllib.urlencode(params))
res = opener.open(url)
if res.geturl() != 'https://developer.apple.com/membercenter/index.action':
sys.stderr.write("Login failed!\n")
sys.exit(1)
sys.stdout.write("Login succeed!\n")
def download(version):
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
# log in Apple Developpers Member Center and get cookie
login(opener)
# download Command Line Tools for Xcode
base_url = "https://developer.apple.com/downloads/download.action?path=Developer_Tools/"
if version == "10.9":
path = "command_line_tools_os_x_mavericks_for_xcode__april_2014/command_line_tools_for_osx_mavericks_april_2014.dmg"
elif version == "10.8":
path = "command_line_tools_os_x_mountain_lion_for_xcode__april_2014/command_line_tools_for_osx_mountain_lion_april_2014.dmg"
elif version == "10.7":
path = "command_line_tools_os_x_lion_for_xcode__april_2013/xcode462_cltools_10_76938260a.dmg"
else:
err_msg = "Command Line Tools for OS X {} is not available!\n"
sys.stderr.write(err_msg.format(version))
sys.exit(1)
res = opener.open(base_url + path)
# write Command Line Tools for Xcode
with open(os.path.basename(path), "w") as f:
f.write(read_with_progress(res))
if __name__ == "__main__":
import platform
ver = platform.mac_ver()[0]
# version >= 10.7.3
if "10.7" in ver:
if int(ver.split('.')[-1]) < 3:
sys.stderr.write("Mac OS X version 10.7.3 or later is required.")
sys.exit(1)
download("10.7")
elif "10.8" in ver:
download("10.8")
elif "10.9" in ver:
download("10.9")
else:
sys.stderr.write("unknown Mac OS X version".format(version))
sys.exit(1)
# for Marvericks 10.9
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_mavericks_for_xcode__late_october_2013/command_line_tools_os_x_mavericks_for_xcode__late_october_2013.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_mavericks_for_xcode__march_2014/commandline_tools_os_x_mavericks_for_xcode__march_2014.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_mavericks_for_xcode__april_2014/command_line_tools_for_osx_mavericks_april_2014.dmg
# for Mountain Lion 10.8
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_mountain_lion_for_xcode__july_2012/xcode44cltools_10_86938106a.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_mountain_lion_for_xcode__august_2012/command_line_tools_for_xcode_os_x_mountain_lion_aug_2012.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_for_xcode_4.5_os_x_mountain_lion__september_2012/command_line_tools_for_xcode_4.5_os_x_mountain_lion.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_mountain_lion_for_xcode__october_2012/xcode451cltools_10_86938200a.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_mountain_lion_for_xcode__november_2012/xcode452cltools10_86938211a.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_mountain_lion_for_xcode__january_2013/xcode46cltools_10_86938131a.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_mountain_lion_for_xcode__march_2013/xcode461_cltools_10_86938245a.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_mountain_lion_for_xcode__april_2013/xcode462_cltools_10_86938259a.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_mountain_lion_for_xcode__september_2013/command_line_tools_os_x_mountain_lion_for_xcode__september_2013.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_mountain_lion_for_xcode__october_2013/command_line_tools_os_x_mountain_lion_for_xcode__october_2013.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_mountain_lion_for_xcode__march_2014/commandline_tools_os_x_mountain_lion_for_xcode__march_2014.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_mountain_lion_for_xcode__april_2014/command_line_tools_for_osx_mountain_lion_april_2014.dmg
# for Lion 10.7
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_for_xcode__february_2012_21258/cltools_lion_february12.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_for_xcode__march_2012_21259/cltools_lion_march12.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_for_xcode_4.4__late_march_2012/cltools_lion_latemarch12.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_for_xcode__june_2012/command_line_tools_for_xcode_june_2012.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_lion_for_xcode__july_2012/xcode44cltools_10_76938107a.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_lion_for_xcode__august_2012/command_line_tools_for_xcode_os_x_lion_aug_2012.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_for_xcode_4.5_os_x_lion__september_2012/command_line_tools_for_xcode_4.5_os_x_lion.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/cltools_lion_from_xcode_4.5.1/xcode451cltools_10_76938201a.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_lion_for_xcode__november_2012/xcode452cltools10_76938212a.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_lion_for_xcode__january_2013/xcode46cltools_10_76938132a.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_lion_for_xcode__march_2013/xcode461_cltools_10_76938246a.dmg
https://developer.apple.com/downloads/download.action?path=Developer_Tools/command_line_tools_os_x_lion_for_xcode__april_2013/xcode462_cltools_10_76938260a.dmg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment