1
0
Fork 0

Prima versione riscritta.

This commit is contained in:
Emiliano Vavassori 2022-03-20 23:57:45 +01:00
parent c79cc2abcf
commit 6aa0b34216
7 changed files with 390 additions and 353 deletions

230
loaih/__init__.py Normal file
View file

@ -0,0 +1,230 @@
#!/usr/bin/env python3
import urllib.request
import loaih.versions as versions
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' ]
LANGBASIC = [ 'en-GB' ]
def __init__(self, query, arch):
"""Build all versions that can be found in the indicated repo."""
self.query = query
self.arch = arch
self.url = ''
self.language = 'basic'
self.offline_help = False
self.portable = False
self.updatable = True
self.storage_path = '/srv/http/appimage.sys42.eu'
self.download_path = '/var/tmp/downloads'
# Getting versions and so on
v = versions.BuildVersion(self.query)
# Building expected AppImageName
languagepart = "."
if ',' in self.language:
languagepart += self.language.replace(',', '-')
self.appimagefilename['x86'] = 'LibreOffice' if not self.query == 'daily' else 'LibreOfficeDev' + '-' + v.version + languagepart + '.help' if self.offline_help else '' + '-x86.AppImage'
self.appimagefilename['x86_64'] = 'LibreOffice' if not self.query == 'daily' else 'LibreOfficeDev' + '-' + v.version + languagepart + '.help' if self.offline_help else '' + '-x86_64.AppImage'
# Creating a tempfile
self.builddir = tempfile.mkdtemp()
self.tarballs = []
self.appname = 'LibreOffice' if not self.query == 'daily' else 'LibreOfficeDev'
self.version = v.version
self.url = v.basedirurl
self.built = False
def check(self, storage_path):
"""Checking if the requested AppImage has been already built."""
self.storage_path = storage_path
if self.portable:
self.storage_path += "/portable"
for arch in self.arch:
res = subprocess.check_output("find {path} -name '{appimage}'".format(
path = self.storage_path,
appimage = self.appimagefilename[arch] + '.zsync' if self.updatable else ''
), shell=True).decode('utf-8').strip('\n')
if len(res) > 1:
self.built = True
if self.built:
print("The requested AppImage already exists on storage. I'll skip downloading, building and moving the results.")
def download(self, download_path):
"""Downloads the contents of the URL as it was a folder."""
if self.built:
return
# Let's start with defining which files are to be downloaded.
# Let's explore the remote folder.
self.download_path = download_path
for a in self.arch:
contents = etree.HTML(urllib.request.urlopen(self.url[a]).read()).xpath("//td/a")
self.tarballs = [ x.text for x in contents if x.text.endswith('tar.gz') ]
maintarball = self.tarballs[0]
mainarr = maintarball.split('')
self.appname = mainarr[0]
self.version = mainarr[1]
os.makedirs(self.downloaddir, existok = 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."""
if self.built:
return
# We have 4 builds to do:
# * standard languages, no help
# * standard languages + offline help
# * all languages, no help
# * all languages + offline help
if self.portable and not 'portable' in self.storage_path:
self.storage_path += "/portable"
# Incompatibilities - if portable and updatable are asked together,
# only portable will be built.
if self.portable and self.updatable:
print("Upgradable and portable options were required together. Building only portable.")
self.updatable = False
for arch in self.arch:
# 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 = arch)
urllib.request.urlretrieve(appimagetoolurl, 'appimagetool')
os.chmod('appimagetool', 0o755)
# Build the requested version.
self.__unpackbuild__()
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 self.language == 'basic':
buildtarballs.extend([ x for x in self.tarballs if 'langpack_en-GB' in x])
if self.offline_help:
buildtarballs.extend([ x for x in self.tarballs if 'helppack_en-GB' in x ])
if self.language == 'standard':
for lang in Build.LANGSTD:
buildtarballs.extend([ x for x in self.tarballs if ('langpack' + lang) in x ])
if self.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 self.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.download_path, archive = archive), shell=True)
os.chdir(self.appnamedir)
os.makedirs(self.appimagedir, exist_ok = True)
# 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 '*program*' -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 = arch)
dest = os.path.join(self.appimagedir, 'AppRun')
urllib.request.urlretrieve(apprunurl, dest)
os.chmod(dest, 0o755)
# Setting app version
appversion = self.version + '.' + self.language if not ',' in self.language else self.language.replace(',', '-')
if self.offline_help:
appversion += '.help'
# Building app
if self.updatable:
zsync = self.appimagefilename[arch] + '.zsync'
subprocess.run("VERSION={version} ./appimagetool -u 'zsync|{zsync}' -v ./{appname}.AppDir/".format(version = appversion, zsync = zsync, appname = self.__appname__), shell=True)
else:
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)
def checksums(self):
"""Create checksums of the built versions."""
if self.built:
return
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."""
if self.built:
return
if not outdir in self.storage_path:
# Let's create an updated storage_path
if self.portable:
self.storage_path = outdir + '/portable'
else:
self.storage_path = outdir
os.chdir(self.appnamedir)
subprocess.run("find . -iname '*.AppImage*' -exec cp {} %s \;" % self.storage_path, shell=True)
def __del__(self):
"""Destructor"""
# Cleaning up build directory
shutil.rmtree(self.builddir)

76
loaih/versions.py Normal file
View file

@ -0,0 +1,76 @@
#!/usr/bin/env python
# encoding: utf-8
import urllib.request
from lxml import etree
from packaging.version import parse as parse_version
class BuildVersion(object):
ARCHIVE = "https://downloadarchive.documentfoundation.org/libreoffice/old/"
RELEASE = "https://download.documentfoundation.org/libreoffice/stable/"
DAILY = "https://dev-builds.libreoffice.org/daily/master/Linux-rpm_deb-x86_64@tb87-TDF/current/"
def __init__(self, query):
self.query = query
self.version = ''
self.basedirurl = {}
# Parsing the query input.
if '.' in self.query:
# Numbered self.version. Let's check it is a 4 dotted release
if len(self.query.split('.')) == 4:
self.version = self.query
else:
# If not 4 dotted, let's search for the 4 dotted version
self.version = self.__getlatestrel(self.query)
self.basedirurl = self.__getbaseurl(self.version)
else:
# String self.versions.
a = self.__getbranchrel(self.query)
self.version = a['version']
self.basedirurl = a['basedirurl']
def __getlatestrel(self, basever):
"""Search in downloadarchive for the latest version matching baseversion."""
versionlist = etree.HTML(urllib.request.urlopen(BuildVersion.ARCHIVE).read()).xpath('//td/a')
# Getting a more polished matching list
cleanlist = list(dict.fromkeys([x.text.strip('/') for x in versionlist if x.text.startswith(basever)]))
# Sorting, then returning the last version
return sorted(cleanlist)[-1]
def __getbranchrel(self, branch):
"""Based on branch names, get the release number."""
basedirurl = {}
version = ''
if branch == 'daily':
basedirurl = { 'x86_64': BuildVersion.DAILY, 'x86': '-' }
version = etree.HTML(urllib.request.urlopen(BuildVersion.DAILY).read()).xpath('//td/a')[1].text.split('_')[1]
return { 'version': version, 'basedirurl': basedirurl }
versions = etree.HTML(urllib.request.urlopen(BuildVersion.RELEASE).read()).xpath('//td/a')
index = 1
if branch == 'still':
index = 2
elif branch == 'fresh':
index = 3
version = self.__getlatestrel(versions[index].text.strip('/'))
return { 'version': version, 'basedirurl': self.__getbaseurl(version) }
def __getbaseurl(self, version):
"""Returns the links based on the numeric version."""
basedirurl = {}
url = BuildVersion.ARCHIVE + '/' + version + '/deb/'
# x86 binaries are not anymore offered after 6.3.0.
if parse_version(version) < parse_version('6.3.0'):
basedirurl['x86'] = url + 'x86/'
else:
basedirurl['x86'] = '-'
basedirurl['x86_64'] = url + 'x86_64/'
return basedirurl