Add python and rsync to cygwin-downloader
[WebKit-https.git] / WebKitTools / CygwinDownloader / cygwin-downloader.py
1 #!/usr/bin/env python
2
3 import os, random, sys, time, urllib
4
5 #
6 # Options
7 #
8
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:])
11
12 #
13 # Functions and constants
14 #
15
16 def download_progress_hook(block_count, block_size, total_blocks):
17         if quiet or random.random() > 0.5:
18                 return
19         sys.stdout.write(".")
20         sys.stdout.flush()
21
22 def download_url_to_file(url, file, message):
23         if not quiet:
24                 print message + " ",
25         if not dry_run:
26                 dir = os.path.dirname(file)
27                 if len(dir) and not os.path.exists(dir):
28                     os.makedirs(dir)
29                 urllib.urlretrieve(url, file, download_progress_hook)
30         if not quiet:
31                 print
32  
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/"]
44
45 package_mirror_url = mirror_servers[random.choice(range(len(mirror_servers)))]
46
47 def download_package(package, message):
48         download_url_to_file(package_mirror_url + package["path"], package["path"], message)
49
50 required_packages = frozenset(["apache",
51                                "bc",
52                                "bison",
53                                "curl",
54                                "diffutils",
55                                "e2fsprogs",
56                                "emacs",
57                                "flex",
58                                "gcc",
59                                "gperf",
60                                "make",
61                                "nano",
62                                "openssh",
63                                "patch",
64                                "perl",
65                                "perl-libwin32",
66                                "python",
67                                "rebase",
68                                "rsync",
69                                "subversion",
70                                "unzip",
71                                "vim",
72                                "zip"])
73
74 #
75 # Main
76 #
77
78 print "Using Cygwin mirror server " + package_mirror_url + " to download setup.ini..."
79
80 urllib.urlretrieve(package_mirror_url + "setup.ini", "setup.ini.orig")
81
82 downloaded_packages_file_path = "setup.ini.orig"
83 downloaded_packages_file = file(downloaded_packages_file_path, "r")
84 if not dry_run:
85     modified_packages_file = file("setup.ini", "w")
86
87 packages = {}
88 current_package = ''
89 for line in downloaded_packages_file.readlines():
90         if line[0] == "@":
91                 current_package = line[2:-1]
92                 packages[current_package] = {"name": current_package, "needs_download": False, "requires": [], "path": ""}
93         elif line[:10] == "category: ":
94                 if current_package in required_packages:
95                         line = "category: Base\n"
96                 if "Base" in set(line[10:-1].split()):
97                         packages[current_package]["needs_download"] = True
98         elif line[:10] == "requires: ":
99                 packages[current_package]["requires"] = line[10:].split()
100                 packages[current_package]["requires"].sort()
101         elif line[:9] == "install: " and not len(packages[current_package]["path"]):
102                 end_of_path = line.find(" ", 9)
103                 if end_of_path != -1:
104                         packages[current_package]["path"] = line[9:end_of_path]
105         if not dry_run:
106             modified_packages_file.write(line)
107
108 downloaded_packages_file.close()
109 os.remove(downloaded_packages_file_path)
110 if not dry_run:
111     modified_packages_file.close()
112
113 names_to_download = set()
114 package_names = packages.keys()
115 package_names.sort()
116
117 def add_package_and_dependencies(name):
118         if name in names_to_download:
119                 return
120         packages[name]["needs_download"] = True
121         names_to_download.add(name)
122         for dep in packages[name]["requires"]:
123                 add_package_and_dependencies(dep)
124
125 for name in package_names:
126         if packages[name]["needs_download"]:
127                 add_package_and_dependencies(name)
128
129 downloaded_so_far = 0
130 for name in package_names:
131         if packages[name]["needs_download"]:
132                 downloaded_so_far += 1
133                 download_package(packages[name], "Downloading package %3d of %3d (%s)" % (downloaded_so_far, len(names_to_download), name))
134
135 download_url_to_file("http://cygwin.com/setup.exe", "setup.exe", "Downloading setup.exe")
136
137 seconds_to_sleep = 10
138
139 print """
140 Finished downloading Cygwin. In %d seconds,
141 I will run setup.exe. Select the "Install
142 from Local Directory" option and browse to
143 "%s"
144 when asked for the "Local Package Directory".
145 """ % (seconds_to_sleep, os.getcwd())
146
147
148 while seconds_to_sleep > 0:
149         print "%d..." % seconds_to_sleep,
150         sys.stdout.flush()
151         time.sleep(1)
152         seconds_to_sleep -= 1
153 print
154
155 if not dry_run:
156         os.execl("setup.exe")