Skip to content

Instantly share code, notes, and snippets.

@nevack
Last active October 4, 2018 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nevack/3a108e3e58714f70792fddd832896dab to your computer and use it in GitHub Desktop.
Save nevack/3a108e3e58714f70792fddd832896dab to your computer and use it in GitHub Desktop.
Download Chromium latest build from trunk
#!/usr/bin/python3
# Dmitry Nevedomsky, Yandex LLC, 2018
import urllib.request
import zipfile
import sys
import os
import psutil
import time
import subprocess
import argparse
file_name = 'chrome-linux.zip'
dir_name = 'chrome-linux'
url = "https://download-chromium.appspot.com/dl/Linux_x64?type=snapshots"
progressmilestone = 0
parser = argparse.ArgumentParser()
parser.add_argument('--run', '-r', dest='force_run', action='store_true')
parser.parse_args()
args = parser.parse_args()
def check_connectivity(reference):
try:
urllib.request.urlopen(reference, timeout=1)
return True
except:
return False
def reporthook(blocknum, blocksize, totalsize):
global progressmilestone
readsofar = blocknum * blocksize
if totalsize > 0:
percent = readsofar * 1e2 / totalsize
progress = makeprogressbar(percent)
message = "\r[{}] {:6.2f}%".format(progress, percent)
sys.stderr.write(message)
if readsofar >= totalsize: # near the end
sys.stderr.write("\n")
else: # total size is unknown
sys.stderr.write("read %d\n" % (readsofar,))
def makeprogressbar(percent, width=50):
pint = int(percent / (100 / width))
progressbar = '#' * pint
if pint != width:
if (percent / 2 - pint) > 2/3:
progressbar += '='
elif (percent / 2 - pint) > 1/3:
progressbar += '-'
else:
progressbar += ' '
progressbar += ' ' * (width - 1 - pint)
return progressbar
def report(msg):
icon_path = os.getcwd() + '/{}/product_logo_48.png'.format(dir_name)
subprocess.Popen(['notify-send',
'--urgency=low',
'--icon={}'.format(icon_path),
'Chromium', # Title
msg],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
def main():
print('Upgrading chrome v1')
if not check_connectivity(url):
exit('No network connection! Exiting.')
sys.stderr.write("Download progress: \n")
urllib.request.urlretrieve(url, file_name, reporthook)
is_chrome_running = False
chrome_pid = 0
for pid in psutil.pids():
p = psutil.Process(pid)
if p.name().startswith('chrome'):
is_chrome_running = True
current = p
while True:
if current.parent() is None:
break
if current.parent().name().startswith('chrome'):
current = current.parent()
else:
break
chrome_pid = current.pid
break
if is_chrome_running:
print("Chromium is running (PPID: {}). Terminating... ".format(pid))
process = psutil.Process(pid)
process.terminate()
time.sleep(1)
print("Extacting zip... ", end='')
with zipfile.ZipFile(file_name, "r") as zip_ref:
zip_ref.extractall(".")
print('DONE!')
if is_chrome_running or args.force_run:
print("Starting chrome... ".format(pid), end='')
pid = subprocess.Popen(['./{}/chrome-wrapper'.format(dir_name)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
print('DONE!')
report("Download Completed")
def exit(msg, code=0):
sys.stderr.write(msg + "\n")
try:
sys.exit(code)
except SystemExit:
os._exit(code)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
exit('\b\b\n\nInterrupted', 130) # Ctrl+C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment