{android,win32}/build.py: move common code to python/build/

This commit is contained in:
Max Kellermann 2015-11-20 22:10:22 +01:00
parent 7dad662d69
commit 556f9ee39c
5 changed files with 66 additions and 104 deletions

2
.gitignore vendored
View File

@ -83,3 +83,5 @@ tags
/*.tar.bz2 /*.tar.bz2
/*.tar.xz /*.tar.xz
/mpd-*/ /mpd-*/
__pycache__/

View File

@ -2,8 +2,6 @@
import os, os.path import os, os.path
import sys, shutil, subprocess import sys, shutil, subprocess
import urllib.request
import hashlib
import re import re
if len(sys.argv) < 3: if len(sys.argv) < 3:
@ -27,6 +25,7 @@ arch = 'arm-linux-androideabi'
# the path to the MPD sources # the path to the MPD sources
mpd_path = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]) or '.', '..')) 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 # output directories
lib_path = os.path.abspath('lib') lib_path = os.path.abspath('lib')
@ -106,43 +105,8 @@ class AndroidNdkToolchain:
# default one on the build host # default one on the build host
self.env['PKG_CONFIG_LIBDIR'] = os.path.join(install_prefix, 'lib/pkgconfig') self.env['PKG_CONFIG_LIBDIR'] = os.path.join(install_prefix, 'lib/pkgconfig')
def file_md5(path): from build.download import download_and_verify
"""Calculate the MD5 checksum of a file and return it in hexadecimal notation.""" from build.tar import untar
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
class Project: class Project:
def __init__(self, url, md5, installed, name=None, version=None, def __init__(self, url, md5, installed, name=None, version=None,
@ -172,7 +136,8 @@ class Project:
self.use_clang = use_clang self.use_clang = use_clang
def download(self): 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): def is_installed(self, toolchain):
tarball = self.download() tarball = self.download()
@ -185,15 +150,7 @@ class Project:
def unpack(self): def unpack(self):
global src_path global src_path
tarball = self.download() return untar(self.download(), src_path, self.base)
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
def make_build_path(self): def make_build_path(self):
path = os.path.join(build_path, self.base) path = os.path.join(build_path, self.base)

40
python/build/download.py Normal file
View File

@ -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

11
python/build/tar.py Normal file
View File

@ -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

View File

@ -2,8 +2,6 @@
import os, os.path import os, os.path
import sys, shutil, subprocess import sys, shutil, subprocess
import urllib.request
import hashlib
import re import re
configure_args = sys.argv[1:] 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 # the path to the MPD sources
mpd_path = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]) or '.', '..')) 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 # output directories
lib_path = os.path.abspath('lib') lib_path = os.path.abspath('lib')
@ -55,43 +54,8 @@ class CrossGccToolchain:
# default one on the build host # default one on the build host
self.env['PKG_CONFIG_LIBDIR'] = os.path.join(install_prefix, 'lib/pkgconfig') self.env['PKG_CONFIG_LIBDIR'] = os.path.join(install_prefix, 'lib/pkgconfig')
def file_md5(path): from build.download import download_and_verify
"""Calculate the MD5 checksum of a file and return it in hexadecimal notation.""" from build.tar import untar
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
class Project: class Project:
def __init__(self, url, md5, installed, name=None, version=None, def __init__(self, url, md5, installed, name=None, version=None,
@ -117,7 +81,7 @@ class Project:
self.installed = installed self.installed = installed
def download(self): 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): def is_installed(self, toolchain):
tarball = self.download() tarball = self.download()
@ -128,21 +92,9 @@ class Project:
except FileNotFoundError: except FileNotFoundError:
return False return False
def unpack(self, out_of_tree=True): def unpack(self):
global src_path, build_path global src_path
tarball = self.download() return untar(self.download(), src_path, self.base)
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 make_build_path(self): def make_build_path(self):
path = os.path.join(build_path, self.base) path = os.path.join(build_path, self.base)