From 556f9ee39cf1e675b961614f61e784f996af1e98 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 20 Nov 2015 22:10:22 +0100 Subject: [PATCH] {android,win32}/build.py: move common code to python/build/ --- .gitignore | 2 ++ android/build.py | 55 ++++------------------------------- python/build/download.py | 40 ++++++++++++++++++++++++++ python/build/tar.py | 11 +++++++ win32/build.py | 62 +++++----------------------------------- 5 files changed, 66 insertions(+), 104 deletions(-) create mode 100644 python/build/download.py create mode 100644 python/build/tar.py diff --git a/.gitignore b/.gitignore index ba6d3b30e..612924822 100644 --- a/.gitignore +++ b/.gitignore @@ -83,3 +83,5 @@ tags /*.tar.bz2 /*.tar.xz /mpd-*/ + +__pycache__/ diff --git a/android/build.py b/android/build.py index 04af2a0f4..c961a1f82 100755 --- a/android/build.py +++ b/android/build.py @@ -2,8 +2,6 @@ import os, os.path import sys, shutil, subprocess -import urllib.request -import hashlib import re if len(sys.argv) < 3: @@ -27,6 +25,7 @@ arch = 'arm-linux-androideabi' # the path to the MPD sources mpd_path = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]) or '.', '..')) +sys.path[0] = os.path.join(mpd_path, 'python') # output directories lib_path = os.path.abspath('lib') @@ -106,43 +105,8 @@ class AndroidNdkToolchain: # default one on the build host self.env['PKG_CONFIG_LIBDIR'] = os.path.join(install_prefix, 'lib/pkgconfig') -def file_md5(path): - """Calculate the MD5 checksum of a file and return it in hexadecimal notation.""" - - with open(path, 'rb') as f: - m = hashlib.md5() - while True: - data = f.read(65536) - if len(data) == 0: - # end of file - return m.hexdigest() - m.update(data) - -def download_tarball(url, md5): - """Download a tarball, verify its MD5 checksum and return the local path.""" - - global tarball_path - os.makedirs(tarball_path, exist_ok=True) - path = os.path.join(tarball_path, os.path.basename(url)) - - try: - calculated_md5 = file_md5(path) - if md5 == calculated_md5: return path - os.unlink(path) - except FileNotFoundError: - pass - - tmp_path = path + '.tmp' - - print("download", url) - urllib.request.urlretrieve(url, tmp_path) - calculated_md5 = file_md5(tmp_path) - if calculated_md5 != md5: - os.unlink(tmp_path) - raise "MD5 mismatch" - - os.rename(tmp_path, path) - return path +from build.download import download_and_verify +from build.tar import untar class Project: def __init__(self, url, md5, installed, name=None, version=None, @@ -172,7 +136,8 @@ class Project: self.use_clang = use_clang def download(self): - return download_tarball(self.url, self.md5) + global tarball_path + return download_and_verify(self.url, self.md5, tarball_path) def is_installed(self, toolchain): tarball = self.download() @@ -185,15 +150,7 @@ class Project: def unpack(self): global src_path - tarball = self.download() - path = os.path.join(src_path, self.base) - try: - shutil.rmtree(path) - except FileNotFoundError: - pass - os.makedirs(src_path, exist_ok=True) - subprocess.check_call(['/bin/tar', 'xfC', tarball, src_path]) - return path + return untar(self.download(), src_path, self.base) def make_build_path(self): path = os.path.join(build_path, self.base) diff --git a/python/build/download.py b/python/build/download.py new file mode 100644 index 000000000..62a844435 --- /dev/null +++ b/python/build/download.py @@ -0,0 +1,40 @@ +import os +import hashlib +import urllib.request + +def file_md5(path): + """Calculate the MD5 checksum of a file and return it in hexadecimal notation.""" + + with open(path, 'rb') as f: + m = hashlib.md5() + while True: + data = f.read(65536) + if len(data) == 0: + # end of file + return m.hexdigest() + m.update(data) + +def download_and_verify(url, md5, parent_path): + """Download a file, verify its MD5 checksum and return the local path.""" + + os.makedirs(parent_path, exist_ok=True) + path = os.path.join(parent_path, os.path.basename(url)) + + try: + calculated_md5 = file_md5(path) + if md5 == calculated_md5: return path + os.unlink(path) + except FileNotFoundError: + pass + + tmp_path = path + '.tmp' + + print("download", url) + urllib.request.urlretrieve(url, tmp_path) + calculated_md5 = file_md5(tmp_path) + if calculated_md5 != md5: + os.unlink(tmp_path) + raise "MD5 mismatch" + + os.rename(tmp_path, path) + return path diff --git a/python/build/tar.py b/python/build/tar.py new file mode 100644 index 000000000..15bbfca6b --- /dev/null +++ b/python/build/tar.py @@ -0,0 +1,11 @@ +import os, shutil, subprocess + +def untar(tarball_path, parent_path, base): + path = os.path.join(parent_path, base) + try: + shutil.rmtree(path) + except FileNotFoundError: + pass + os.makedirs(parent_path, exist_ok=True) + subprocess.check_call(['/bin/tar', 'xfC', tarball_path, parent_path]) + return path diff --git a/win32/build.py b/win32/build.py index b15b0b443..f67d23b84 100755 --- a/win32/build.py +++ b/win32/build.py @@ -2,8 +2,6 @@ import os, os.path import sys, shutil, subprocess -import urllib.request -import hashlib import re configure_args = sys.argv[1:] @@ -16,6 +14,7 @@ if len(configure_args) > 0 and configure_args[0] == '--64': # the path to the MPD sources mpd_path = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]) or '.', '..')) +sys.path[0] = os.path.join(mpd_path, 'python') # output directories lib_path = os.path.abspath('lib') @@ -55,43 +54,8 @@ class CrossGccToolchain: # default one on the build host self.env['PKG_CONFIG_LIBDIR'] = os.path.join(install_prefix, 'lib/pkgconfig') -def file_md5(path): - """Calculate the MD5 checksum of a file and return it in hexadecimal notation.""" - - with open(path, 'rb') as f: - m = hashlib.md5() - while True: - data = f.read(65536) - if len(data) == 0: - # end of file - return m.hexdigest() - m.update(data) - -def download_tarball(url, md5): - """Download a tarball, verify its MD5 checksum and return the local path.""" - - global tarball_path - os.makedirs(tarball_path, exist_ok=True) - path = os.path.join(tarball_path, os.path.basename(url)) - - try: - calculated_md5 = file_md5(path) - if md5 == calculated_md5: return path - os.unlink(path) - except FileNotFoundError: - pass - - tmp_path = path + '.tmp' - - print("download", url) - urllib.request.urlretrieve(url, tmp_path) - calculated_md5 = file_md5(tmp_path) - if calculated_md5 != md5: - os.unlink(tmp_path) - raise "MD5 mismatch" - - os.rename(tmp_path, path) - return path +from build.download import download_and_verify +from build.tar import untar class Project: def __init__(self, url, md5, installed, name=None, version=None, @@ -117,7 +81,7 @@ class Project: self.installed = installed def download(self): - return download_tarball(self.url, self.md5) + return download_and_verify(self.url, self.md5, tarball_path) def is_installed(self, toolchain): tarball = self.download() @@ -128,21 +92,9 @@ class Project: except FileNotFoundError: return False - def unpack(self, out_of_tree=True): - global src_path, build_path - tarball = self.download() - if out_of_tree: - parent_path = src_path - else: - parent_path = build_path - path = os.path.join(parent_path, self.base) - try: - shutil.rmtree(path) - except FileNotFoundError: - pass - os.makedirs(parent_path, exist_ok=True) - subprocess.check_call(['/bin/tar', 'xfC', tarball, parent_path]) - return path + def unpack(self): + global src_path + return untar(self.download(), src_path, self.base) def make_build_path(self): path = os.path.join(build_path, self.base)