REGRESSION(AppleWebKit/605.1.15): WebDownloadDelegate delegate methods called on...
[WebKit-https.git] / Tools / CygwinDownloader / cygwin-downloader.py
1 #!/usr/bin/env python
2
3 from __future__ import print_function
4 import os, random, sys, time, urllib
5
6 #
7 # Options
8 #
9
10 dry_run = len(sys.argv) > 1 and "--dry-run" in set(sys.argv[1:])
11 quiet = len(sys.argv) > 1 and "--quiet" in set(sys.argv[1:])
12
13 #
14 # Functions and constants
15 #
16
17 def download_progress_hook(block_count, block_size, total_blocks):
18         if quiet or random.random() > 0.5:
19                 return
20         sys.stdout.write(".")
21         sys.stdout.flush()
22
23 def download_url_to_file(url, file, message):
24         if not quiet:
25                 print(message + " ", end=' ')
26         if not dry_run:
27                 dir = os.path.dirname(file)
28                 if len(dir) and not os.path.exists(dir):
29                     os.makedirs(dir)
30                 urllib.urlretrieve(url, file, download_progress_hook)
31         if not quiet:
32                 print()
33  
34 # This is mostly just the list of North America http mirrors from http://cygwin.com/mirrors.html,
35 # but a few have been removed that seemed unresponsive from Cupertino.
36 mirror_servers = ["http://cygwin.elite-systems.org/",
37                   "http://mirror.mcs.anl.gov/cygwin/",
38                   "http://cygwin.osuosl.org/",
39                   "http://mirrors.kernel.org/sourceware/cygwin/",
40                   "http://mirrors.xmission.com/cygwin/",
41                   "http://sourceware.mirrors.tds.net/pub/sourceware.org/cygwin/"]
42
43 package_mirror_url = mirror_servers[random.choice(range(len(mirror_servers)))]
44
45 def download_package(package, message):
46         download_url_to_file(package_mirror_url + package["path"], package["path"], message)
47
48 required_packages = frozenset(["bc",
49                                "curl",
50                                "diffutils",
51                                "e2fsprogs",
52                                "emacs",
53                                "gcc-g++",
54                                "gperf",
55                                "keychain",
56                                "lighttpd",
57                                "make",
58                                "nano",
59                                "openssh",
60                                "patch",
61                                "perl",
62                                "perl-libwin32",
63                                "python",
64                                "rebase",
65                                "rsync",
66                                "ruby",
67                                "subversion",
68                                "unzip",
69                                "vim",
70                                "zip"])
71
72 required_packages_versions = {"curl": "7.33.0-1",
73                               "libcurl4": "7.33.0-1",
74                               "python": "2.6.8-2",
75                               "subversion": "1.7.14-1"}
76
77 #
78 # Main
79 #
80
81 print("Using Cygwin mirror server " + package_mirror_url + " to download setup.ini...")
82
83 urllib.urlretrieve(package_mirror_url + "x86/setup.ini", "setup.ini.orig")
84
85 downloaded_packages_file_path = "setup.ini.orig"
86 downloaded_packages_file = file(downloaded_packages_file_path, "r")
87 if not dry_run:
88     modified_packages_file = file("setup.ini", "w")
89
90 packages = {}
91 current_package = ''
92 for line in downloaded_packages_file.readlines():
93         if line[0] == "@":
94                 current_package = line[2:-1]
95                 packages[current_package] = {"name": current_package, "needs_download": False, "requires": [], "path": "", "version": "", "found_version": False}
96                 if current_package in required_packages_versions:
97                         packages[current_package]["version"] = required_packages_versions[current_package]
98         elif line[:10] == "category: ":
99                 if current_package in required_packages:
100                         line = "category: Base\n"
101                 if "Base" in set(line[10:-1].split()):
102                         packages[current_package]["needs_download"] = True
103         elif line[:10] == "requires: ":
104                 packages[current_package]["requires"] = line[10:].split()
105                 packages[current_package]["requires"].sort()
106         elif line[:9] == "version: " and not packages[current_package]["found_version"]:
107                 if not len(packages[current_package]["version"]):
108                         packages[current_package]["version"] = line[9:-1]
109                         packages[current_package]["found_version"] = True
110                 else:
111                         packages[current_package]["found_version"] = (packages[current_package]["version"] == line[9:-1])
112         elif line[:9] == "install: " and packages[current_package]["found_version"] and not len(packages[current_package]["path"]):
113                 end_of_path = line.find(" ", 9)
114                 if end_of_path != -1:
115                         packages[current_package]["path"] = line[9:end_of_path]
116         if not dry_run:
117             modified_packages_file.write(line)
118
119 downloaded_packages_file.close()
120 os.remove(downloaded_packages_file_path)
121 if not dry_run:
122     modified_packages_file.close()
123
124 names_to_download = set()
125 package_names = packages.keys()
126 package_names.sort()
127
128 def add_package_and_dependencies(name):
129         if name in names_to_download:
130                 return
131         if not name in packages:
132                 return
133         packages[name]["needs_download"] = True
134         names_to_download.add(name)
135         for dep in packages[name]["requires"]:
136                 add_package_and_dependencies(dep)
137
138 for name in package_names:
139         if packages[name]["needs_download"]:
140                 add_package_and_dependencies(name)
141
142 downloaded_so_far = 0
143 for name in package_names:
144         if packages[name]["needs_download"]:
145                 downloaded_so_far += 1
146                 download_package(packages[name], "Downloading package %3d of %3d (%s)" % (downloaded_so_far, len(names_to_download), name))
147
148 download_url_to_file("http://cygwin.com/setup-x86.exe", "setup.exe", "Downloading setup.exe")
149
150 seconds_to_sleep = 10
151
152 print("""
153 Finished downloading Cygwin. In %d seconds,
154 I will run setup.exe. All the suitable options have
155 been already selected for you.
156 """ % (seconds_to_sleep))
157
158
159 while seconds_to_sleep > 0:
160         print("%d..." % seconds_to_sleep, end=' ')
161         sys.stdout.flush()
162         time.sleep(1)
163         seconds_to_sleep -= 1
164 print()
165
166 if not dry_run:
167         os.execv("setup.exe", list(("-L", "-l", os.getcwd(), "-P", ",".join(required_packages))))