OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Downloads a Firefox Nightly build for the current platform.""" | 6 """Downloads a Firefox Nightly build for the current platform.""" |
7 | 7 |
8 import datetime | 8 import datetime |
9 import glob | 9 import glob |
10 import os | 10 import os |
11 import shutil | 11 import shutil |
12 import sys | 12 import sys |
13 import subprocess | 13 import subprocess |
14 import tarfile | 14 import tarfile |
15 import time | 15 import time |
16 import zipfile | 16 import zipfile |
17 | 17 |
18 from optparse import OptionParser | 18 from optparse import OptionParser |
19 | 19 |
20 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | 20 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
21 THIRD_PARTY_DIR = os.path.abspath(os.path.join(BASE_DIR, 'third_party')) | 21 THIRD_PARTY_DIR = os.path.abspath(os.path.join(BASE_DIR, 'third_party')) |
22 | 22 |
23 sys.path.append(os.path.join(THIRD_PARTY_DIR, 'mozdownload')) | 23 sys.path.append(os.path.join(THIRD_PARTY_DIR, 'mozdownload')) |
24 sys.path.append(os.path.join(THIRD_PARTY_DIR, 'mozinfo')) | 24 sys.path.append(os.path.join(THIRD_PARTY_DIR, 'mozinfo')) |
25 | 25 |
26 from mozdownload import scraper | 26 from mozdownload import scraper |
| 27 import utils |
27 | 28 |
28 | 29 |
29 def _Touch(a_file): | 30 def _Touch(a_file): |
30 with open(a_file, 'a'): | 31 with open(a_file, 'a'): |
31 os.utime(a_file, None) | 32 os.utime(a_file, None) |
32 | 33 |
33 | 34 |
34 def _GetFirefoxArchivesSortedOnModifiedDate(target_dir): | 35 def _GetFirefoxArchivesSortedOnModifiedDate(target_dir): |
35 firefox_archives = glob.glob(os.path.join(target_dir, '*tar.bz2')) | 36 firefox_archives = glob.glob(os.path.join(target_dir, '*tar.bz2')) |
36 if not firefox_archives: | 37 if not firefox_archives: |
(...skipping 14 matching lines...) Expand all Loading... |
51 for old_archive in rest: | 52 for old_archive in rest: |
52 try: | 53 try: |
53 os.remove(old_archive) | 54 os.remove(old_archive) |
54 except OSError: | 55 except OSError: |
55 pass | 56 pass |
56 | 57 |
57 | 58 |
58 def _FindFallbackFirefoxBuild(target_dir): | 59 def _FindFallbackFirefoxBuild(target_dir): |
59 firefox_archives = _GetFirefoxArchivesSortedOnModifiedDate(target_dir) | 60 firefox_archives = _GetFirefoxArchivesSortedOnModifiedDate(target_dir) |
60 if not firefox_archives: | 61 if not firefox_archives: |
61 return None | 62 return None, None |
62 | 63 |
63 newest_build = firefox_archives[0] | 64 newest_build = firefox_archives[0] |
64 build_age_seconds = time.time() - os.path.getmtime(newest_build) | 65 build_age_seconds = time.time() - os.path.getmtime(newest_build) |
65 build_age_days = datetime.timedelta(seconds=build_age_seconds).days | 66 build_age_days = datetime.timedelta(seconds=build_age_seconds).days |
66 | 67 |
67 return newest_build, build_age_days | 68 return newest_build, build_age_days |
68 | 69 |
69 | 70 |
70 def _MaybeDownload(target_dir, force): | 71 def _MaybeDownload(target_dir, force): |
71 try: | 72 try: |
(...skipping 22 matching lines...) Expand all Loading... |
94 if age_days > 3: | 95 if age_days > 3: |
95 raise Exception('We have failed to download firefox builds for more ' | 96 raise Exception('We have failed to download firefox builds for more ' |
96 'than 3 days now: failing so someone looks at it. The ' | 97 'than 3 days now: failing so someone looks at it. The ' |
97 'most recent build we have is %d days old.' % age_days) | 98 'most recent build we have is %d days old.' % age_days) |
98 | 99 |
99 print 'Using %s instead; it is %d days old.' % (fallback_build, age_days) | 100 print 'Using %s instead; it is %d days old.' % (fallback_build, age_days) |
100 return fallback_build | 101 return fallback_build |
101 | 102 |
102 | 103 |
103 def _ExtractArchive(firefox_archive, target_dir): | 104 def _ExtractArchive(firefox_archive, target_dir): |
104 # TODO(phoglund): implement on win/mac. See http://crbug.com/454303. | 105 if utils.GetPlatform() is 'linux': |
105 if sys.platform == 'darwin': | |
106 print "Disabled on mac..." | |
107 return 0 | |
108 volume = '/Volumes/Nightly' | |
109 firefox_executable = '%s/FirefoxNightly.app' % target_dir | |
110 | |
111 # Unmount any previous downloads. | |
112 subprocess.call(['hdiutil', 'detach', volume]) | |
113 | |
114 subprocess.check_call(['hdiutil', 'attach', firefox_archive]) | |
115 shutil.copytree('%s/FirefoxNightly.app' % volume, firefox_executable) | |
116 subprocess.check_call(['hdiutil', 'detach', volume]) | |
117 elif sys.platform == 'linux2': | |
118 tar_archive = tarfile.open(firefox_archive, 'r:bz2') | 106 tar_archive = tarfile.open(firefox_archive, 'r:bz2') |
119 tar_archive.extractall(path=target_dir) | 107 tar_archive.extractall(path=target_dir) |
120 elif sys.platform == 'win32': | |
121 print "Disabled on win..." | |
122 return 0 | |
123 zip_archive = zipfile.ZipFile(firefox_archive) | |
124 zip_archive.extractall(path=target_dir) | |
125 else: | 108 else: |
126 print >> sys.stderr, 'Unsupported platform: %s' % sys.platform | 109 raise Exception('Unsupported platform: %s' % sys.platform) |
127 return 1 | |
128 | 110 |
129 print 'Extracted %s' % firefox_archive | 111 print 'Extracted %s' % firefox_archive |
130 return 0 | |
131 | 112 |
132 | 113 |
133 def main(): | 114 def main(): |
134 usage = 'usage: %prog -t <target_dir>' | 115 usage = 'usage: %prog -t <target_dir>' |
135 parser = OptionParser(usage) | 116 parser = OptionParser(usage) |
136 parser.add_option('-t', '--target-dir', | 117 parser.add_option('-t', '--target-dir', |
137 help=('Target directory to put the downloaded and extracted' | 118 help=('Target directory to put the downloaded and extracted' |
138 ' folder with the Firefox Nightly build in.')) | 119 ' folder with the Firefox Nightly build in.')) |
139 parser.add_option('-f', '--force', action='store_true', | 120 parser.add_option('-f', '--force', action='store_true', |
140 help=('Force download even if the current nightly is ' | 121 help=('Force download even if the current nightly is ' |
141 'already downloaded.')) | 122 'already downloaded.')) |
142 parser.add_option('-c', '--clean-old-firefox-archives', action='store_true', | 123 parser.add_option('-c', '--clean-old-firefox-archives', action='store_true', |
143 help=('Clean old firefox archives; one will always be ' | 124 help=('Clean old firefox archives; one will always be ' |
144 'kept as a fallback.')) | 125 'kept as a fallback.')) |
145 options, _args = parser.parse_args() | 126 options, _args = parser.parse_args() |
146 if not options.target_dir: | 127 if not options.target_dir: |
147 parser.error('You must specify the target directory.') | 128 parser.error('You must specify the target directory.') |
148 | 129 |
| 130 if utils.GetPlatform() is not 'linux': |
| 131 print 'This script is only supported on Linux for now.' |
| 132 return |
| 133 |
149 target_dir = options.target_dir | 134 target_dir = options.target_dir |
150 if not os.path.isdir(target_dir): | 135 if not os.path.isdir(target_dir): |
151 os.mkdir(target_dir) | 136 os.mkdir(target_dir) |
152 | 137 |
153 firefox_archive = _MaybeDownload(target_dir, options.force) | 138 firefox_archive = _MaybeDownload(target_dir, options.force) |
154 if firefox_archive: | 139 if firefox_archive: |
155 return _ExtractArchive(firefox_archive, target_dir) | 140 _ExtractArchive(firefox_archive, target_dir) |
156 | 141 |
157 if options.clean_old_firefox_archives: | 142 if options.clean_old_firefox_archives: |
158 _CleanOldFirefoxArchives(target_dir) | 143 _CleanOldFirefoxArchives(target_dir) |
159 | 144 |
160 if __name__ == '__main__': | 145 if __name__ == '__main__': |
161 sys.exit(main()) | 146 sys.exit(main()) |
OLD | NEW |