loaih/build.py

134 lines
5.5 KiB
Python

#!/usr/bin/env python3
import urllib.request
from lxml import etree
import tempfile, os, sys, subprocess, shutil
class Build():
LANGSTD = ['ar', 'de', 'en-GB', 'es', 'fr', 'it', 'ja', 'ko', 'pt', 'pt-BR', 'ru', 'zh-CN', 'zh-TW']
def __init__(self, query, arch, url):
"""Build all versions that can be found in the indicated repo."""
self.__query__ = query
self.__arch__ = arch
self.__url__ = url
# 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.chdir(self.__builddir__)
for archive in self.__tarballs__:
# 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, outdir):
"""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
# Let's start with the quickest build: standard languages, no help.
# We start by filtering out tarballs from the list
buildtarballs = [ self.__tarballs__[0] ]
# Let's process standard languages and append results to the
# buildtarball
for lang in LANGSTD:
buildtarballs.extend([ x for x in self.__tarballs__ if ('langpack_' + lang) in x ])
# If it was a build with offline help, another extend would have
# solved it:
#buildtarballs.extend([ x for x in self.__tarballs__ if ('helppack_'+ lang) in x ])
# Creating a subfolder
os.makedirs(os.path.join(self.__builddir__, self.__appname__, self.__appname__ + '.AppImage'), exist_ok = True)
# And then cd to the appname folder.
os.chdir(os.path.join(self.__builddir__, self.__appname__))
# Unpacking the tarballs
for archive in buildtarballs:
subprocess.run("tar xzf ../%s.tar.gz" % archive, shell=True)
# At this point, let's decompress the deb packages
os.chdir(os.path.join(self.__builddir__, self.__appname__, self.__appname__ + '.AppImage'))
subprocess.run("find .. -iname '*.deb' -exec dpkg -x {} . \+", shell=True)
# Changing desktop file
subprocess.run("find . -iname startcenter.desktop -exec cp {} . \+", shell=True)
subprocess.run("sed -i -e 's|Name=.*|Name=%s|g' startcenter.desktop" % self.__appname__, shell=True)
subprocess.run("find . -name startcenter.png -path '*hicolor*48x48*' -exec cp {} \;", shell=True)
binaryname = subprocess.check_output("awk 'BEGIN { FS = \"=\" } /^Exec/ { print $2; exit }' startcenter.desktop | awk '{ print $1 }'", shell=True).strip('\n')
subprocess.run("rm -f usr/bin/%s" % binaryname, shell=True)
os.chdir(os.path.join(self.__builddir__, self.__appname__, self.__appname__ + '.AppImage', 'usr', 'bin'))
subprocess.run("find ../../opt -name soffice -path '*programm*' -exec ln -s {} %si \;" % binaryname, shell=True)
os.chdir(os.path.join(self.__builddir__, self.__appname__, self.__appname__ + '.AppImage'))
# Download AppRun from github
apprunurl = "https://github.com/AppImage/AppImageKit/releases/download/continuous/AppRun-{arch}".format(arch = self.__arch__)
urllib.request.urlretrieve(apprunurl, 'AppRun')
os.chmod('AppRun', 0o755)
os.chdir(os.path.join(self.__builddir__, self.__appname__))
# Download appimagetool from probonopd repo on github
appimagetoolurl = "https://github.com/probonopd/AppImageKit/releases/download/continuous/appimagetool-{arch}.AppImage".format(arch = self.__arch__)
urllib.request.urlretrieve(appimagetoolurl, 'appimagetool')
os.chmod('appimagetool', 0o755)
# Setting app version
appversion = self.__version__ + '.standard'
# Building app
subprocess.run("VERSION={version} ./appimagetool -v ./{appname}.AppDir/".format(version = appversion, appname = self.__appname__), shell=True)
# Copying built image to final directory
subprocess.run("find . -iname '*.AppImage' -exec cp {} %s \;" % outdir, shell = True)
def __del__(self):
"""Destructor for the class."""
# Cleanup
shutil.rmtree(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(sys.argv[4])
del b