2015-11-21 00:00:42 +01:00
|
|
|
import os, shutil
|
|
|
|
import re
|
|
|
|
|
|
|
|
from build.download import download_and_verify
|
|
|
|
from build.tar import untar
|
2018-02-09 22:57:31 +01:00
|
|
|
from build.quilt import push_all
|
2015-11-21 00:00:42 +01:00
|
|
|
|
|
|
|
class Project:
|
|
|
|
def __init__(self, url, md5, installed, name=None, version=None,
|
|
|
|
base=None,
|
2018-02-09 22:57:31 +01:00
|
|
|
patches=None,
|
2018-01-05 10:04:47 +01:00
|
|
|
edits=None,
|
2016-10-26 11:55:38 +02:00
|
|
|
use_cxx=False):
|
2015-11-21 00:00:42 +01:00
|
|
|
if base is None:
|
|
|
|
basename = os.path.basename(url)
|
|
|
|
m = re.match(r'^(.+)\.(tar(\.(gz|bz2|xz|lzma))?|zip)$', basename)
|
|
|
|
if not m: raise
|
|
|
|
self.base = m.group(1)
|
|
|
|
else:
|
|
|
|
self.base = base
|
|
|
|
|
|
|
|
if name is None or version is None:
|
2018-02-09 22:54:02 +01:00
|
|
|
m = re.match(r'^([-\w]+)-(\d[\d.]*[a-z]?[\d.]*)$', self.base)
|
2015-11-21 00:00:42 +01:00
|
|
|
if name is None: name = m.group(1)
|
|
|
|
if version is None: version = m.group(2)
|
|
|
|
|
|
|
|
self.name = name
|
|
|
|
self.version = version
|
|
|
|
|
|
|
|
self.url = url
|
|
|
|
self.md5 = md5
|
|
|
|
self.installed = installed
|
|
|
|
|
2018-02-09 22:57:31 +01:00
|
|
|
if patches is not None:
|
|
|
|
srcdir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
|
|
|
|
patches = os.path.join(srcdir, patches)
|
|
|
|
self.patches = patches
|
2018-01-05 10:04:47 +01:00
|
|
|
self.edits = edits
|
2015-11-21 00:00:42 +01:00
|
|
|
self.use_cxx = use_cxx
|
|
|
|
|
|
|
|
def download(self, toolchain):
|
|
|
|
return download_and_verify(self.url, self.md5, toolchain.tarball_path)
|
|
|
|
|
|
|
|
def is_installed(self, toolchain):
|
|
|
|
tarball = self.download(toolchain)
|
|
|
|
installed = os.path.join(toolchain.install_prefix, self.installed)
|
|
|
|
tarball_mtime = os.path.getmtime(tarball)
|
|
|
|
try:
|
|
|
|
return os.path.getmtime(installed) >= tarball_mtime
|
|
|
|
except FileNotFoundError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def unpack(self, toolchain, out_of_tree=True):
|
|
|
|
if out_of_tree:
|
|
|
|
parent_path = toolchain.src_path
|
|
|
|
else:
|
|
|
|
parent_path = toolchain.build_path
|
2018-01-05 10:04:47 +01:00
|
|
|
path = untar(self.download(toolchain), parent_path, self.base)
|
|
|
|
|
2018-02-09 22:57:31 +01:00
|
|
|
if self.patches is not None:
|
|
|
|
push_all(toolchain, path, self.patches)
|
|
|
|
|
2018-01-05 10:04:47 +01:00
|
|
|
if self.edits is not None:
|
|
|
|
for filename, function in self.edits.items():
|
|
|
|
with open(os.path.join(path, filename), 'r+t') as f:
|
|
|
|
old_data = f.read()
|
|
|
|
new_data = function(old_data)
|
|
|
|
f.seek(0)
|
|
|
|
f.truncate(0)
|
|
|
|
f.write(new_data)
|
|
|
|
|
|
|
|
return path
|
2015-11-21 00:00:42 +01:00
|
|
|
|
|
|
|
def make_build_path(self, toolchain):
|
|
|
|
path = os.path.join(toolchain.build_path, self.base)
|
|
|
|
try:
|
|
|
|
shutil.rmtree(path)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
os.makedirs(path, exist_ok=True)
|
|
|
|
return path
|