loaih/build.py

187 lines
7.5 KiB
Python

#!/usr/bin/env python3
import urllib.request
from lxml import etree
import tempfile, os, sys, glob, subprocess, shutil
class Build(object):
LANGSTD = [ 'ar', 'de', 'en-GB', 'es', 'fr', 'it', 'ja', 'ko', 'pt', 'pt-BR', 'ru', 'zh-CN', 'zh-TW' ]
def __init__(self, query, arch, url, downloaddir = '/var/tmp/downloads'):
"""Build all versions that can be found in the indicated repo."""
self.__query__ = query
self.__arch__ = arch
self.__url__ = url
self.__downloaddir__ = downloaddir
# Creating a tempfile
self.__builddir__ = tempfile.mkdtemp()
self.__tarballs__ = []
self.__appname__ = ''
self.__version__ = ''
if self.__url__ == '-':
print("Cannot find this version for arch {arch}.".format(arch = self.__arch__))
return False
def download(self):
"""Downloads the contents of the URL as it was a folder."""
# Let's start with defining which files are to be downloaded.
# Let's explore the remote folder.
contents = etree.HTML(urllib.request.urlopen(self.__url__).read()).xpath("//td/a")
self.__tarballs__ = [ x.text for x in contents if x.text.endswith('tar.gz') ]
maintarball = self.__tarballs__[0]
main_arr = maintarball.split('_')
self.__appname__ = main_arr[0]
self.__version__ = main_arr[1]
os.makedirs(self.__downloaddir__, exist_ok = True)
os.chdir(self.__downloaddir__)
for archive in self.__tarballs__:
# If the archive is already there, do not do anything.
if os.path.exists(os.path.join(self.__downloaddir__, archive)):
print("Archive %s is already there! Sweet" % archive)
continue
# Download the archive
try:
urllib.request.urlretrieve(self.__url__ + archive, archive)
except:
print("Failed to download {archive}.".format(archive = archive))
print("Got %s." % archive)
def build(self):
"""Building all the versions."""
# We have 4 builds to do:
# * standard languages, no help
# * standard languages + offline help
# * all languages, no help
# * all languages + offline help
# Preparation tasks
self.appnamedir = os.path.join(self.__builddir__, self.__appname__)
self.appimagedir = os.path.join(self.__builddir__, self.__appname__, self.__appname__ + '.AppDir')
os.makedirs(self.appimagedir, exist_ok = True)
# And then cd to the appname folder.
os.chdir(self.appnamedir)
# Download appimagetool from github
appimagetoolurl = "https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-{arch}.AppImage".format(arch = self.__arch__)
urllib.request.urlretrieve(appimagetoolurl, 'appimagetool')
os.chmod('appimagetool', 0o755)
# Run to build standard no help
self.__unpackbuild__('standard', False)
# Run to build standard full help
#self.__unpackbuild__('standard', True)
# Build full no help
#self.__unpackbuild__('full', False)
# Full with help
#self.__unpackbuild__('full', True)
def __unpackbuild__(self, languageset = 'full', offlinehelp = False):
# We start by filtering out tarballs from the list
buildtarballs = [ self.__tarballs__[0] ]
# Let's process standard languages and append results to the
# buildtarball
if languageset == 'standard':
for lang in Build.LANGSTD:
buildtarballs.extend([ x for x in self.__tarballs__ if ('langpack_' + lang) in x ])
if offlinehelp:
buildtarballs.extend([ x for x in self.__tarballs__ if ('helppack_' + lang) in x ])
else:
# In any other cases, we build with all languages
if not offlinehelp:
buildtarballs.extend([ x for x in self.__tarballs__ if 'langpack_' in x ])
else:
# We need also all help. Let's replace buildtarball with the
# whole bunch
buildtarballs = self.__tarballs__
# Unpacking the tarballs
for archive in buildtarballs:
subprocess.run("tar xzf {folder}/{archive}".format(folder = self.__downloaddir__, archive = archive), shell=True)
os.chdir(self.appnamedir)
# At this point, let's decompress the deb packages
subprocess.run("find .. -iname '*.deb' -exec dpkg -x {} . \;", shell=True, cwd=self.appimagedir)
# Changing desktop file
subprocess.run("find . -iname startcenter.desktop -exec cp {} . \;", shell=True, cwd=self.appimagedir)
appname = 'LibreOffice' if not self.__query__ == 'daily' else 'LibreOfficeDev'
subprocess.run("sed -i -e 's:^Name=.*$:Name=%s:' startcenter.desktop" % appname, shell=True, cwd=self.appimagedir)
subprocess.run("find . -name '*startcenter.png' -path '*hicolor*48x48*' -exec cp {} . \;", shell=True, cwd=self.appimagedir)
# Find the name of the binary called in the desktop file.
binaryname = subprocess.check_output("awk 'BEGIN { FS = \"=\" } /^Exec/ { print $2; exit }' startcenter.desktop | awk '{ print $1 }'", shell=True, cwd=self.appimagedir).decode('utf-8').strip('\n')
bindir=os.path.join(self.appimagedir, 'usr', 'bin')
os.makedirs(bindir, exist_ok = True)
subprocess.run("find ../../opt -iname soffice -path '*programm*' -exec ln -sf {} ./%s \;" % binaryname, shell=True, cwd=bindir)
# Download AppRun from github
apprunurl = "https://github.com/AppImage/AppImageKit/releases/download/continuous/AppRun-{arch}".format(arch = self.__arch__)
dest = os.path.join(self.appimagedir, 'AppRun')
urllib.request.urlretrieve(apprunurl, dest)
os.chmod(dest, 0o755)
# Setting app version
appversion = self.__version__ + '.' + languageset
if offlinehelp:
appversion += '.help'
# Building app
subprocess.run("VERSION={version} ./appimagetool -v ./{appname}.AppDir/".format(version = appversion, appname = self.__appname__), shell=True)
print("Built AppImage version {version}".format(version = appversion))
# Cleanup phase, before new run.
for deb in glob.glob(self.appnamedir + '/*.deb'):
os.remove(deb)
subprocess.run("find . -type d -maxdepth 1 -exec rm -rf {} \+", shell=True)
os.makedirs(self.appimagedir)
def checksums(self):
"""Create checksums of the built versions."""
os.chdir(self.appnamedir)
for appimage in glob.glob('*.AppImage'):
# See if a checksum already exist
if not os.path.exists(appimage + '.md5'):
subprocess.run("md5sum {appimage} > {appimage}.md5".format(appimage = appimage), shell=True)
def move(self, outdir):
"""Moves built versions to definitive storage."""
os.chdir(self.appnamedir)
subprocess.run("find . -iname '*.AppImage*' -exec cp {} %s \;" % outdir, shell=True)
def __del__(self):
"""Destructor"""
#del self.__builddir__
if __name__ == '__main__':
# Run if it is run as a program.
# 1 -> query
# 2 -> arch
# 3 -> url
# 4 -> outdir
if not len(sys.argv) == 5:
print("Please launch with this parameters: build.py query arch url outputdir")
sys.exit(255)
b = Build(sys.argv[1], sys.argv[2], sys.argv[3])
b.download()
b.build()
b.checksums()
b.move(sys.argv[4])
del b