9bb933d
#!/usr/bin/python3
7fc989c
# Copyright 2021-2023, Than Ngo <than@redhat.com>
9bb933d
# Copyright 2010,2015-2019 Tom Callaway <tcallawa@redhat.com>
f66ad85
# Copyright 2013-2016 Tomas Popela <tpopela@redhat.com>
f66ad85
# Permission is hereby granted, free of charge, to any person obtaining
f66ad85
# a copy of this software and associated documentation files (the
f66ad85
# "Software"), to deal in the Software without restriction, including
f66ad85
# without limitation the rights to use, copy, modify, merge, publish,
f66ad85
# distribute, sublicense, and/or sell copies of the Software, and to
f66ad85
# permit persons to whom the Software is furnished to do so, subject to
f66ad85
# the following conditions:
f66ad85
#
f66ad85
# The above copyright notice and this permission notice shall be included
f66ad85
# in all copies or substantial portions of the Software.
f66ad85
#
f66ad85
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
f66ad85
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
f66ad85
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
f66ad85
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
f66ad85
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
f66ad85
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
f66ad85
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
f66ad85
f66ad85
try:
f66ad85
  import argparse
f66ad85
  optparse = False
f66ad85
except ImportError:
f66ad85
  from optparse import OptionParser
f66ad85
  optparse = True
f66ad85
import csv
f66ad85
import glob
f66ad85
import hashlib
f66ad85
import locale
f66ad85
import os
f66ad85
import shutil
9bb933d
import io
f66ad85
import sys
9bb933d
import urllib.request, urllib.parse, urllib.error
f66ad85
f66ad85
chromium_url = "http://commondatastorage.googleapis.com/chromium-browser-official/"
f66ad85
f66ad85
chromium_root_dir = "."
f66ad85
version_string = "stable"
f66ad85
f66ad85
name = 'Chromium Latest'
9d223e0
script_version = 0.9
f66ad85
my_description = '{0} {1}'.format(name, script_version)
f66ad85
f66ad85
f66ad85
def dlProgress(count, blockSize, totalSize):
f66ad85
f66ad85
  if (totalSize <= blockSize):
f66ad85
    percent = int(count * 100)
f66ad85
  else:
f66ad85
    percent = int(count * blockSize * 100 / totalSize)
f66ad85
  sys.stdout.write("\r" + "Downloading ... %d%%" % percent)
f66ad85
  sys.stdout.flush()
f66ad85
f66ad85
f66ad85
def delete_chromium_dir(ch_dir):
f66ad85
f66ad85
  full_dir = "%s/%s" % (latest_dir, ch_dir)
9bb933d
  print('Deleting %s ' % full_dir)
f66ad85
  if os.path.isdir(full_dir):
f66ad85
    shutil.rmtree(full_dir)
9bb933d
    print('[DONE]')
f66ad85
  else:
9bb933d
    print('[NOT FOUND]')
f66ad85
f66ad85
f66ad85
def delete_chromium_files(files):
f66ad85
f66ad85
  full_path = "%s/%s" % (latest_dir, files)
9bb933d
  print('Deleting ' + full_path + ' ', end=' ')
f66ad85
  for filename in glob.glob(full_path):
9bb933d
    print('Deleting ' + filename + ' ', end=' ')
f66ad85
    os.remove(filename)
9bb933d
    print('[DONE]')
f66ad85
f66ad85
f66ad85
def check_omahaproxy(channel="stable"):
f66ad85
f66ad85
  version = 0
f66ad85
  status_url = "http://omahaproxy.appspot.com/all?os=linux&channel=" + channel
f66ad85
9bb933d
  usock = urllib.request.urlopen(status_url)
9bb933d
  status_dump = usock.read().decode('utf-8')
f66ad85
  usock.close()
9bb933d
  status_list = io.StringIO(status_dump)
f66ad85
  status_reader = list(csv.reader(status_list, delimiter=','))
f66ad85
  linux_channels = [s for s in status_reader if "linux" in s]
f66ad85
  linux_channel = [s for s in linux_channels if channel in s]
f66ad85
  version = linux_channel[0][2]
f66ad85
f66ad85
  if version == 0:
9bb933d
    print('I could not find the latest %s build. Bailing out.' % channel)
f66ad85
    sys.exit(1)
f66ad85
  else:
9bb933d
    print('Latest Chromium Version on %s at %s is %s' % (channel, status_url, version))
f66ad85
    return version
f66ad85
f66ad85
f66ad85
def remove_file_if_exists(filename):
f66ad85
f66ad85
  if os.path.isfile("./%s" % filename):
f66ad85
    try:
f66ad85
      os.remove(filename)
f66ad85
    except Exception:
f66ad85
      pass
f66ad85
f66ad85
f66ad85
def download_file_and_compare_hashes(file_to_download):
f66ad85
f66ad85
  hashes_file = '%s.hashes' % file_to_download
f66ad85
f66ad85
  if (args.clean):
f66ad85
    remove_file_if_exists(file_to_download)
f66ad85
    remove_file_if_exists(hashes_file)
f66ad85
f66ad85
  # Let's make sure we haven't already downloaded it.
f66ad85
  tarball_local_file = "./%s" % file_to_download
f66ad85
  if os.path.isfile(tarball_local_file):
9bb933d
    print("%s already exists!" % file_to_download)
f66ad85
  else:
f66ad85
    path = '%s%s' % (chromium_url, file_to_download)
9bb933d
    print("Downloading %s" % path)
f66ad85
    # Perhaps look at using python-progressbar at some point?
9bb933d
    info=urllib.request.urlretrieve(path, file_to_download, reporthook=dlProgress)[1]
9bb933d
    urllib.request.urlcleanup()
9bb933d
    print("")
f66ad85
    if (info["Content-Type"] != "application/x-tar"):
9bb933d
      print('Chromium tarballs for %s are not on servers.' % file_to_download)
f66ad85
      remove_file_if_exists (file_to_download)
f66ad85
      sys.exit(1)
f66ad85
f66ad85
  hashes_local_file = "./%s" % hashes_file
f66ad85
  if not os.path.isfile(hashes_local_file):
f66ad85
    path = '%s%s' % (chromium_url, hashes_file)
9bb933d
    print("Downloading %s" % path)
f66ad85
    # Perhaps look at using python-progressbar at some point?
9bb933d
    info=urllib.request.urlretrieve(path, hashes_file, reporthook=dlProgress)[1]
9bb933d
    urllib.request.urlcleanup()
9bb933d
    print("")
f66ad85
f66ad85
  if os.path.isfile(hashes_local_file):
f66ad85
    with open(hashes_local_file, "r") as input_file:
f66ad85
      md5sum = input_file.readline().split()[1]
f66ad85
      md5 = hashlib.md5()
f66ad85
      with open(tarball_local_file, "rb") as f:
f66ad85
        for block in iter(lambda: f.read(65536), b""):
f66ad85
          md5.update(block)
f66ad85
        if (md5sum == md5.hexdigest()):
9bb933d
          print("MD5 matches for %s!" % file_to_download)
f66ad85
        else:
9bb933d
          print("MD5 mismatch for %s!" % file_to_download)
f66ad85
          sys.exit(1)
f66ad85
  else:
9bb933d
    print("Cannot compare hashes for %s!" % file_to_download)
f66ad85
f66ad85
f66ad85
def download_version(version):
f66ad85
f66ad85
  download_file_and_compare_hashes ('chromium-%s.tar.xz' % version)
f66ad85
f66ad85
  if (args.tests):
f66ad85
    download_file_and_compare_hashes ('chromium-%s-testdata.tar.xz' % version)
f66ad85
f66ad85
def nacl_versions(version):
07728cd
07728cd
  if sys.version_info[0] == 2 and sys.version_info[1] == 6:
07728cd
    return
07728cd
f66ad85
  myvars = {}
f66ad85
  chrome_dir = './chromium-%s' % version
f66ad85
  with open(chrome_dir + "/native_client/tools/REVISIONS") as myfile:
f66ad85
      for line in myfile:
f66ad85
          name, var = line.partition("=")[::2]
f66ad85
          myvars[name] = var
9bb933d
  print("nacl-binutils commit: %s" % myvars["NACL_BINUTILS_COMMIT"])
9bb933d
  print("nacl-gcc commit: %s" % myvars["NACL_GCC_COMMIT"])
9bb933d
  print("nacl-newlib commit: %s" % myvars["NACL_NEWLIB_COMMIT"])
f66ad85
f66ad85
  # Parse GIT_REVISIONS dict from toolchain_build.py
f66ad85
f66ad85
  sys.path.append(os.path.abspath(chrome_dir + "/native_client/toolchain_build"))
f66ad85
  from toolchain_build import GIT_REVISIONS
9bb933d
  print("nacl-arm-binutils commit: %s" % GIT_REVISIONS['binutils']['rev'])
9bb933d
  print("nacl-arm-gcc commit: %s" % GIT_REVISIONS['gcc']['rev'])
f66ad85
f66ad85
f66ad85
def download_chrome_latest_rpm(arch):
f66ad85
f66ad85
  chrome_rpm = 'google-chrome-%s_current_%s.rpm' % (version_string, arch)
f66ad85
  path = 'https://dl.google.com/linux/direct/%s' % chrome_rpm
f66ad85
f66ad85
  if (args.clean):
f66ad85
    remove_file_if_exists(chrome_rpm)
f66ad85
f66ad85
  # Let's make sure we haven't already downloaded it.
f66ad85
  if os.path.isfile("./%s" % chrome_rpm):
9bb933d
    print("%s already exists!" % chrome_rpm)
f66ad85
  else:
9bb933d
    print("Downloading %s" % path)
f66ad85
    # Perhaps look at using python-progressbar at some point?
9bb933d
    info=urllib.request.urlretrieve(path, chrome_rpm, reporthook=dlProgress)[1]
9bb933d
    urllib.request.urlcleanup()
9bb933d
    print("")
f66ad85
    if (info["Content-Type"] != "binary/octet-stream" and info["Content-Type"] != "application/x-redhat-package-manager"):
9bb933d
      print('Chrome %s rpms are not on servers.' % version_string)
f66ad85
      remove_file_if_exists (chrome_rpm)
f66ad85
      sys.exit(1)
f66ad85
f66ad85
# This is where the magic happens
f66ad85
if __name__ == '__main__':
f66ad85
f66ad85
  # Locale magic
f66ad85
  locale.setlocale(locale.LC_ALL, '')
f66ad85
f66ad85
  # Create the parser object
f66ad85
  if optparse:
f66ad85
    parser = OptionParser(description=my_description)
f66ad85
    parser_add_argument = parser.add_option
f66ad85
  else:
f66ad85
    parser = argparse.ArgumentParser(description=my_description)
f66ad85
    parser_add_argument = parser.add_argument
f66ad85
f66ad85
  parser_add_argument(
f66ad85
      '--ffmpegarm', action='store_true',
f66ad85
      help='Leave arm sources when cleaning ffmpeg')
f66ad85
  parser_add_argument(
f66ad85
      '--beta', action='store_true',
f66ad85
      help='Get the latest beta Chromium source')
f66ad85
  parser_add_argument(
f66ad85
      '--clean', action='store_true',
f66ad85
      help='Re-download all previously downloaded sources')
f66ad85
  parser_add_argument(
f66ad85
      '--cleansources', action='store_true',
f66ad85
      help='Get the latest Chromium release from given channel and clean various directories to from unnecessary or unwanted stuff')
f66ad85
  parser_add_argument(
f66ad85
      '--dev', action='store_true',
f66ad85
      help='Get the latest dev Chromium source')
f66ad85
  parser_add_argument(
f66ad85
      '--ffmpegclean', action='store_true',
f66ad85
      help='Get the latest Chromium release from given channel and cleans ffmpeg sources from proprietary stuff')
f66ad85
  parser_add_argument(
f66ad85
      '--chrome', action='store_true',
f66ad85
      help='Get the latest Chrome rpms for the given channel')
f66ad85
  parser_add_argument(
f66ad85
      '--prep', action='store_true',
f66ad85
      help='Prepare everything, but don\'t compress the result')
f66ad85
  parser_add_argument(
f66ad85
      '--stable', action='store_true',
f66ad85
      help='Get the latest stable Chromium source')
f66ad85
  parser_add_argument(
f66ad85
      '--tests', action='store_true',
f66ad85
      help='Get the additional data for running tests')
f66ad85
  parser_add_argument(
f66ad85
      '--version',
f66ad85
      help='Download a specific version of Chromium')
14101bb
  parser_add_argument(
14101bb
      '--naclvers',
14101bb
      help='Display the commit versions of nacl toolchain components')
f66ad85
f66ad85
  # Parse the args
f66ad85
  if optparse:
f66ad85
    args, options = parser.parse_args()
f66ad85
  else:
f66ad85
    args = parser.parse_args()
f66ad85
f66ad85
  if args.stable:
f66ad85
    version_string = "stable"
f66ad85
  elif args.beta:
f66ad85
    version_string = "beta"
f66ad85
  elif args.dev:
f66ad85
    version_string = "dev"
f66ad85
  elif (not (args.stable or args.beta or args.dev)):
f66ad85
    if (not args.version):
9bb933d
      print('No version specified, downloading STABLE')
f66ad85
    args.stable = True
f66ad85
f66ad85
  chromium_version = args.version if args.version else check_omahaproxy(version_string)
f66ad85
f66ad85
  if args.dev:
f66ad85
    version_string = "unstable"
f66ad85
f66ad85
  if args.chrome:
f66ad85
    if args.version:
9bb933d
      print('You cannot specify a Chrome RPM version!')
f66ad85
      sys.exit(1)
f66ad85
    latest = 'google-chrome-%s_current_i386' % version_string
f66ad85
    download_chrome_latest_rpm("i386")
f66ad85
    latest = 'google-chrome-%s_current_x86_64' % version_string
f66ad85
    download_chrome_latest_rpm("x86_64")
f66ad85
    if (not (args.ffmpegclean or args.tests)):
f66ad85
      sys.exit(0)
f66ad85
f66ad85
  latest = 'chromium-%s.tar.xz' % chromium_version
f66ad85
f66ad85
  download_version(chromium_version)
f66ad85
f66ad85
  # Lets make sure we haven't unpacked it already
f66ad85
  latest_dir = "%s/chromium-%s" % (chromium_root_dir, chromium_version)
f66ad85
  if (args.clean and os.path.isdir(latest_dir)):
f66ad85
    shutil.rmtree(latest_dir)
f66ad85
f66ad85
  if os.path.isdir(latest_dir):
9bb933d
    print("%s already exists, perhaps %s has already been unpacked?" % (latest_dir, latest))
f66ad85
  else:
9bb933d
    print("Unpacking %s into %s, please wait." % (latest, latest_dir))
f66ad85
    if (os.system("tar -xJf %s" % latest) != 0):
9bb933d
      print("%s is possibly corrupted, exiting." % (latest))
f66ad85
      sys.exit(1)
f66ad85
14101bb
  if (args.naclvers):
14101bb
    nacl_versions(chromium_version)
f66ad85
f66ad85
  if (args.cleansources):
f66ad85
    junk_dirs = ['third_party/WebKit/Tools/Scripts/webkitpy/layout_tests',
f66ad85
                 'webkit/data/layout_tests', 'third_party/hunspell/dictionaries',
f66ad85
                 'chrome/test/data', 'native_client/tests',
f66ad85
                 'third_party/WebKit/LayoutTests']
f66ad85
f66ad85
    # First, the dirs:
f66ad85
    for directory in junk_dirs:
f66ad85
      delete_chromium_dir(directory)
f66ad85
f66ad85
  # There has got to be a better, more portable way to do this.
7fc989c
  os.system("find %s -depth -name reference_build -type d -exec rm -rf {} \\;" % latest_dir)
f66ad85
f66ad85
  # I could not find good bindings for xz/lzma support, so we system call here too.
f66ad85
  chromium_clean_xz_file = "chromium-" + chromium_version + "-clean.tar.xz"
f66ad85
f66ad85
  remove_file_if_exists(chromium_clean_xz_file)
f66ad85
f66ad85
  if (args.ffmpegclean):
f66ad85
    print("Cleaning ffmpeg from proprietary things...")
f66ad85
    os.system("./clean_ffmpeg.sh %s %d" % (latest_dir, 0 if args.ffmpegarm else 1))
9bb933d
    print("Done!")
f66ad85
f66ad85
  if (not args.prep):
9bb933d
    print("Compressing cleaned tree, please wait...")
f66ad85
    os.chdir(chromium_root_dir)
7fc989c
    os.system("tar --exclude=\\.svn -cf - chromium-%s | xz -9 -T 0 -f > %s" % (chromium_version, chromium_clean_xz_file))
f66ad85
9bb933d
  print("Finished!")