From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BF9C8C433EF for ; Mon, 10 Jan 2022 03:16:06 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id EE117830DB; Mon, 10 Jan 2022 04:15:31 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="jzxlVbxY"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id AD91681DEC; Mon, 10 Jan 2022 04:15:19 +0100 (CET) Received: from mail-io1-xd29.google.com (mail-io1-xd29.google.com [IPv6:2607:f8b0:4864:20::d29]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 5DEF581B4B for ; Mon, 10 Jan 2022 04:15:12 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-io1-xd29.google.com with SMTP id w22so5247559iov.3 for ; Sun, 09 Jan 2022 19:15:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=moamXpV727mT0OSJEjGjC0Tfz5JRFaRpYq9jhFE/xL4=; b=jzxlVbxYJjBQ9neiDRDvRBd3f72RnaE0mx1dOVvqYaqY2AITwyk/sESDF/Ir6cmvLa CWTvlX7upQP9bpQdqhBIycgUf3B4ge6wMNjAX9k6fiLHNPv/3WarHfDFw4kYXXDdxE9n C1SUoia+ZovnFvR/TsYE/Iat5jbfoXF9iQcMQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=moamXpV727mT0OSJEjGjC0Tfz5JRFaRpYq9jhFE/xL4=; b=Mc4j/bm2X88CZM/peovjJfC2MiPOs5SEZ3iEaZ2SIYj6uvXthHeGtH0aqrZIipixNk jsUDIJ2edTUYi1L85cRMfZ5j1FCUNajeOUkFmCJrKJDtopKcR3MJPw1dZo40AHrch64F ArlxiqBltCLWA4jjibxhfId0gQzGL7SHzTFmseFbi2exUdF380uxAWlsxgF5owNqt53N dbFeEeIIyq6mNMWJomiI0ZAHdNf+pHfWp/8s0X8yVpPUtjSfXg33+U87prUC72Keyxvc 3fSgp3E0R4TZLmAokFNS0MKAS6C61oxOrYn2CnZvqXvgQpMbqmfZiOamIfL6wingUm4a nOtw== X-Gm-Message-State: AOAM5300WHD3jB44l8s1frjEk1QxqFREVV6qMOGLO7fFCTQDPPwE8i+K vT+ig1IWkXC6w/zaD5AIEUXeZUkdrGqEng== X-Google-Smtp-Source: ABdhPJxJLOo35FNOAWmJ6R4/mTDlmuHRFSpscpEgMp5JvTkkP8xU9QR3jwnPJ9VXp8CkdCSdc+FlkQ== X-Received: by 2002:a05:6602:341b:: with SMTP id n27mr7838874ioz.215.1641784510873; Sun, 09 Jan 2022 19:15:10 -0800 (PST) Received: from sjg1.roam.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id d16sm3542182ioy.29.2022.01.09.19.15.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Jan 2022 19:15:10 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Simon Glass Subject: [PATCH v2 06/38] buildman: Move the download function to tools Date: Sun, 9 Jan 2022 20:13:41 -0700 Message-Id: <20220110031413.1970836-7-sjg@chromium.org> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog In-Reply-To: <20220110031413.1970836-1-sjg@chromium.org> References: <20220110031413.1970836-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean This function is handy for binman as well. Move it into the shared 'tools' module. Signed-off-by: Simon Glass --- (no changes since v1) tools/buildman/toolchain.py | 46 +------------------------------------ tools/patman/tools.py | 45 ++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 45 deletions(-) diff --git a/tools/buildman/toolchain.py b/tools/buildman/toolchain.py index 4e2471f3e37..bcae5ef7415 100644 --- a/tools/buildman/toolchain.py +++ b/tools/buildman/toolchain.py @@ -515,50 +515,6 @@ class Toolchains: return arch, links return None - def Download(self, url): - """Download a file to a temporary directory - - Args: - url: URL to download - Returns: - Tuple: - Temporary directory name - Full path to the downloaded archive file in that directory, - or None if there was an error while downloading - """ - print('Downloading: %s' % url) - leaf = url.split('/')[-1] - tmpdir = tempfile.mkdtemp('.buildman') - response = urllib.request.urlopen(url) - fname = os.path.join(tmpdir, leaf) - fd = open(fname, 'wb') - meta = response.info() - size = int(meta.get('Content-Length')) - done = 0 - block_size = 1 << 16 - status = '' - - # Read the file in chunks and show progress as we go - while True: - buffer = response.read(block_size) - if not buffer: - print(chr(8) * (len(status) + 1), '\r', end=' ') - break - - done += len(buffer) - fd.write(buffer) - status = r'%10d MiB [%3d%%]' % (done // 1024 // 1024, - done * 100 // size) - status = status + chr(8) * (len(status) + 1) - print(status, end=' ') - sys.stdout.flush() - fd.close() - if done != size: - print('Error, failed to download') - os.remove(fname) - fname = None - return tmpdir, fname - def Unpack(self, fname, dest): """Unpack a tar file @@ -615,7 +571,7 @@ class Toolchains: os.mkdir(dest) # Download the tar file for this toolchain and unpack it - tmpdir, tarfile = self.Download(url) + tmpdir, tarfile = tools.Download(url) if not tarfile: return 1 print(col.Color(col.GREEN, 'Unpacking to: %s' % dest), end=' ') diff --git a/tools/patman/tools.py b/tools/patman/tools.py index 7af4a52a8fd..2f817f61670 100644 --- a/tools/patman/tools.py +++ b/tools/patman/tools.py @@ -10,6 +10,7 @@ import shutil import struct import sys import tempfile +import urllib.request from patman import command from patman import tout @@ -632,3 +633,47 @@ def PrintFullHelp(fname): if not pager: pager = ['more'] command.Run(*pager, fname) + +def Download(url): + """Download a file to a temporary directory + + Args: + url: URL to download + Returns: + Tuple: + Temporary directory name + Full path to the downloaded archive file in that directory, + or None if there was an error while downloading + """ + print('Downloading: %s' % url) + leaf = url.split('/')[-1] + tmpdir = tempfile.mkdtemp('.buildman') + response = urllib.request.urlopen(url) + fname = os.path.join(tmpdir, leaf) + fd = open(fname, 'wb') + meta = response.info() + size = int(meta.get('Content-Length')) + done = 0 + block_size = 1 << 16 + status = '' + + # Read the file in chunks and show progress as we go + while True: + buffer = response.read(block_size) + if not buffer: + print(chr(8) * (len(status) + 1), '\r', end=' ') + break + + done += len(buffer) + fd.write(buffer) + status = r'%10d MiB [%3d%%]' % (done // 1024 // 1024, + done * 100 // size) + status = status + chr(8) * (len(status) + 1) + print(status, end=' ') + sys.stdout.flush() + fd.close() + if done != size: + print('Error, failed to download') + os.remove(fname) + fname = None + return tmpdir, fname -- 2.34.1.575.g55b058a8bb-goog