3 import os, random, sys, time, urllib
9 dry_run = len(sys.argv) > 1 and "--dry-run" in set(sys.argv[1:])
10 quiet = len(sys.argv) > 1 and "--quiet" in set(sys.argv[1:])
13 # Functions and constants
16 def download_progress_hook(block_count, block_size, total_blocks):
17 if quiet or random.random() > 0.5:
22 def download_url_to_file(url, file, message):
26 dir = os.path.dirname(file)
27 if len(dir) and not os.path.exists(dir):
29 urllib.urlretrieve(url, file, download_progress_hook)
33 # This is mostly just the list of North America http mirrors from http://cygwin.com/mirrors.html,
34 # but a few have been removed that seemed unresponsive from Cupertino.
35 mirror_servers = ["http://cygwin.elite-systems.org/",
36 "http://mirror.mcs.anl.gov/cygwin/",
37 "http://cygwin.osuosl.org/",
38 "http://mirrors.kernel.org/sourceware/cygwin/",
39 "http://cygwin.mirrors.hoobly.com/",
40 "http://cygwin.rtin.bz/",
41 "http://mirrors.wikifusion.info/cygwin/",
42 "http://mirrors.xmission.com/cygwin/",
43 "http://sourceware.mirrors.tds.net/pub/sourceware.org/cygwin/"]
45 package_mirror_url = mirror_servers[random.choice(range(len(mirror_servers)))]
47 def download_package(package, message):
48 download_url_to_file(package_mirror_url + package["path"], package["path"], message)
50 required_packages = frozenset(["apache",
75 print "Using Cygwin mirror server " + package_mirror_url + " to download setup.ini..."
77 urllib.urlretrieve(package_mirror_url + "setup.ini", "setup.ini.orig")
79 downloaded_packages_file_path = "setup.ini.orig"
80 downloaded_packages_file = file(downloaded_packages_file_path, "r")
82 modified_packages_file = file("setup.ini", "w")
86 for line in downloaded_packages_file.readlines():
88 current_package = line[2:-1]
89 packages[current_package] = {"name": current_package, "needs_download": False, "requires": [], "path": ""}
90 elif line[:10] == "category: ":
91 if current_package in required_packages:
92 line = "category: Base\n"
93 if "Base" in set(line[10:-1].split()):
94 packages[current_package]["needs_download"] = True
95 elif line[:10] == "requires: ":
96 packages[current_package]["requires"] = line[10:].split()
97 packages[current_package]["requires"].sort()
98 elif line[:9] == "install: " and not len(packages[current_package]["path"]):
99 end_of_path = line.find(" ", 9)
100 if end_of_path != -1:
101 packages[current_package]["path"] = line[9:end_of_path]
103 modified_packages_file.write(line)
105 downloaded_packages_file.close()
106 os.remove(downloaded_packages_file_path)
108 modified_packages_file.close()
110 names_to_download = set()
111 package_names = packages.keys()
114 def add_package_and_dependencies(name):
115 if name in names_to_download:
117 packages[name]["needs_download"] = True
118 names_to_download.add(name)
119 for dep in packages[name]["requires"]:
120 add_package_and_dependencies(dep)
122 for name in package_names:
123 if packages[name]["needs_download"]:
124 add_package_and_dependencies(name)
126 downloaded_so_far = 0
127 for name in package_names:
128 if packages[name]["needs_download"]:
129 downloaded_so_far += 1
130 download_package(packages[name], "Downloading package %3d of %3d (%s)" % (downloaded_so_far, len(names_to_download), name))
132 download_url_to_file("http://cygwin.com/setup.exe", "setup.exe", "Downloading setup.exe")
134 seconds_to_sleep = 10
137 Finished downloading Cygwin. In %d seconds,
138 I will run setup.exe. Select the "Install
139 from Local Directory" option and browse to
141 when asked for the "Local Package Directory".
142 """ % (seconds_to_sleep, os.getcwd())
145 while seconds_to_sleep > 0:
146 print "%d..." % seconds_to_sleep,
149 seconds_to_sleep -= 1
153 os.execl("setup.exe")