webengine/osswebengine/WebKitTools/CygwinDownloader/cygwin-downloader.py
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 #!/usr/bin/env python
       
     2 
       
     3 #
       
     4 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     5 # All rights reserved.
       
     6 # This component and the accompanying materials are made available
       
     7 # under the terms of the License "Eclipse Public License v1.0"
       
     8 # which accompanies this distribution, and is available
       
     9 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    10 #
       
    11 # Initial Contributors:
       
    12 # Nokia Corporation - initial contribution.
       
    13 #
       
    14 # Contributors:
       
    15 #
       
    16 # Description:  
       
    17 #
       
    18 # 
       
    19 
       
    20 import os, random, sys, time, urllib
       
    21 
       
    22 #
       
    23 # Options
       
    24 #
       
    25 
       
    26 dry_run = len(sys.argv) > 1 and "--dry-run" in set(sys.argv[1:])
       
    27 quiet = len(sys.argv) > 1 and "--quiet" in set(sys.argv[1:])
       
    28 
       
    29 #
       
    30 # Functions and constants
       
    31 #
       
    32 
       
    33 def download_progress_hook(block_count, block_size, total_blocks):
       
    34         if quiet or random.random() > 0.5:
       
    35                 return
       
    36         sys.stdout.write(".")
       
    37         sys.stdout.flush()
       
    38 
       
    39 def download_url_to_file(url, file, message):
       
    40         if not quiet:
       
    41                 print message + " ",
       
    42         if not dry_run:
       
    43                 dir = os.path.dirname(file)
       
    44                 if len(dir) and not os.path.exists(dir):
       
    45                     os.makedirs(dir)
       
    46                 urllib.urlretrieve(url, file, download_progress_hook)
       
    47         if not quiet:
       
    48                 print
       
    49  
       
    50 # This is mostly just the list of North America http mirrors from http://cygwin.com/mirrors.html,
       
    51 # but a few have been removed that seemed unresponsive from Cupertino.
       
    52 mirror_servers = ["http://cygwin.elite-systems.org/",
       
    53                   "http://mirror.mcs.anl.gov/cygwin/",
       
    54                   "http://cygwin.osuosl.org/",
       
    55                   "http://mirrors.kernel.org/sourceware/cygwin/",
       
    56                   "http://cygwin.mirrors.hoobly.com/",
       
    57                   "http://cygwin.rtin.bz/",
       
    58                   "http://mirrors.wikifusion.info/cygwin/",
       
    59                   "http://mirrors.xmission.com/cygwin/",
       
    60                   "http://sourceware.mirrors.tds.net/pub/sourceware.org/cygwin/"]
       
    61 
       
    62 package_mirror_url = mirror_servers[random.choice(range(len(mirror_servers)))]
       
    63 
       
    64 def download_package(package, message):
       
    65         download_url_to_file(package_mirror_url + package["path"], package["path"], message)
       
    66 
       
    67 required_packages = frozenset(["apache",
       
    68                                "bc",
       
    69                                "bison",
       
    70                                "curl",
       
    71                                "diffutils",
       
    72                                "e2fsprogs",
       
    73                                "emacs",
       
    74                                "flex",
       
    75                                "gcc",
       
    76                                "gperf",
       
    77                                "keychain",
       
    78                                "make",
       
    79                                "nano",
       
    80                                "openssh",
       
    81                                "patch",
       
    82                                "perl",
       
    83                                "perl-libwin32",
       
    84                                "python",
       
    85                                "rebase",
       
    86                                "rsync",
       
    87                                "subversion",
       
    88                                "unzip",
       
    89                                "vim",
       
    90                                "zip"])
       
    91 
       
    92 #
       
    93 # Main
       
    94 #
       
    95 
       
    96 print "Using Cygwin mirror server " + package_mirror_url + " to download setup.ini..."
       
    97 
       
    98 urllib.urlretrieve(package_mirror_url + "setup.ini", "setup.ini.orig")
       
    99 
       
   100 downloaded_packages_file_path = "setup.ini.orig"
       
   101 downloaded_packages_file = file(downloaded_packages_file_path, "r")
       
   102 if not dry_run:
       
   103     modified_packages_file = file("setup.ini", "w")
       
   104 
       
   105 packages = {}
       
   106 current_package = ''
       
   107 for line in downloaded_packages_file.readlines():
       
   108         if line[0] == "@":
       
   109                 current_package = line[2:-1]
       
   110                 packages[current_package] = {"name": current_package, "needs_download": False, "requires": [], "path": ""}
       
   111         elif line[:10] == "category: ":
       
   112                 if current_package in required_packages:
       
   113                         line = "category: Base\n"
       
   114                 if "Base" in set(line[10:-1].split()):
       
   115                         packages[current_package]["needs_download"] = True
       
   116         elif line[:10] == "requires: ":
       
   117                 packages[current_package]["requires"] = line[10:].split()
       
   118                 packages[current_package]["requires"].sort()
       
   119         elif line[:9] == "install: " and not len(packages[current_package]["path"]):
       
   120                 end_of_path = line.find(" ", 9)
       
   121                 if end_of_path != -1:
       
   122                         packages[current_package]["path"] = line[9:end_of_path]
       
   123         if not dry_run:
       
   124             modified_packages_file.write(line)
       
   125 
       
   126 downloaded_packages_file.close()
       
   127 os.remove(downloaded_packages_file_path)
       
   128 if not dry_run:
       
   129     modified_packages_file.close()
       
   130 
       
   131 names_to_download = set()
       
   132 package_names = packages.keys()
       
   133 package_names.sort()
       
   134 
       
   135 def add_package_and_dependencies(name):
       
   136         if name in names_to_download:
       
   137                 return
       
   138         packages[name]["needs_download"] = True
       
   139         names_to_download.add(name)
       
   140         for dep in packages[name]["requires"]:
       
   141                 add_package_and_dependencies(dep)
       
   142 
       
   143 for name in package_names:
       
   144         if packages[name]["needs_download"]:
       
   145                 add_package_and_dependencies(name)
       
   146 
       
   147 downloaded_so_far = 0
       
   148 for name in package_names:
       
   149         if packages[name]["needs_download"]:
       
   150                 downloaded_so_far += 1
       
   151                 download_package(packages[name], "Downloading package %3d of %3d (%s)" % (downloaded_so_far, len(names_to_download), name))
       
   152 
       
   153 download_url_to_file("http://cygwin.com/setup.exe", "setup.exe", "Downloading setup.exe")
       
   154 
       
   155 seconds_to_sleep = 10
       
   156 
       
   157 print """
       
   158 Finished downloading Cygwin. In %d seconds,
       
   159 I will run setup.exe. Select the "Install
       
   160 from Local Directory" option and browse to
       
   161 "%s"
       
   162 when asked for the "Local Package Directory".
       
   163 """ % (seconds_to_sleep, os.getcwd())
       
   164 
       
   165 
       
   166 while seconds_to_sleep > 0:
       
   167         print "%d..." % seconds_to_sleep,
       
   168         sys.stdout.flush()
       
   169         time.sleep(1)
       
   170         seconds_to_sleep -= 1
       
   171 print
       
   172 
       
   173 if not dry_run:
       
   174         os.execl("setup.exe")