1
0

Refactor parziale dello script. Refactor della parte getversion.

This commit is contained in:
Emiliano Vavassori 2022-04-30 16:58:00 +02:00
parent 0fae769ec0
commit c30407a244
4 changed files with 104 additions and 77 deletions

View File

@ -4,8 +4,39 @@
import click import click
import yaml import yaml
import loaih import loaih
import re, sys, json
@click.command() @click.group()
def cli():
pass
@cli.command()
@click.option('-j', '--json', 'jsonout', default=False, is_flag=True, help="Output format in json.")
@click.argument('query')
def getversion(query, jsonout):
b = []
queries = []
if ',' in query:
queries.extend(query.split(','))
else:
queries.append(query)
for q in queries:
if '.' in q:
# Numbered version. It is safe to send it to Build.
b.append(loaih.versions.Build(q))
else:
# Named version. For safety, we call a helper method for a collection
b.extend(loaih.versions.Base.collectedbuilds(q))
if len(b) > 0:
if jsonout:
click.echo(json.dumps([x.todict() for x in b]))
else:
for v in b:
click.echo(v)
@cli.command()
@click.option('-a', '--arch', 'arch', type=click.Choice(['x86', 'x86_64', 'all'], case_sensitive=False), default='all', help="Build the AppImage for a specific architecture. If there is no specific options, the process will build for both architectures (if available). Default: all") @click.option('-a', '--arch', 'arch', type=click.Choice(['x86', 'x86_64', 'all'], case_sensitive=False), default='all', help="Build the AppImage for a specific architecture. If there is no specific options, the process will build for both architectures (if available). Default: all")
@click.option('-c/-C', '--check/--no-check', 'check', default=True, help="Check in the final storage if the queried version is existent. Default: check") @click.option('-c/-C', '--check/--no-check', 'check', default=True, help="Check in the final storage if the queried version is existent. Default: check")
@click.option('-d', '--download-path', 'download_path', default = '/var/tmp/downloads', type=str, help="Path to the download folder. Default: /var/tmp/downloads") @click.option('-d', '--download-path', 'download_path', default = '/var/tmp/downloads', type=str, help="Path to the download folder. Default: /var/tmp/downloads")
@ -86,6 +117,3 @@ def build(arch, language, offline, portable, updatable, download_path, repo_path
obj.publish() obj.publish()
obj.generalize_and_link() obj.generalize_and_link()
del obj del obj
if __name__ == '__main__':
build()

View File

@ -36,21 +36,6 @@ class Base(object):
# Class for static methods which might be useful even outside the build # Class for static methods which might be useful even outside the build
# scripts. # scripts.
@staticmethod
def dailyver(date = datetime.datetime.today()):
"""Returns versions present on the latest daily build."""
url = Base.dailyurl(date)['x86_64']
# Since it is possible that a daily build is not yet provided...
if url == '-':
return []
# Rerun the page parsing, this time to find out the versions built
b = etree.HTML(urllib.request.urlopen(Base.dailyurl(date)['x86_64']).read()).xpath("//td/a[contains(text(), '_deb.tar.gz')]/text()")
# This should have returned the main package for a version, but can
# have returned multiple ones, so let's treat it as a list
return [ x.split('_')[1] for x in b ]
@staticmethod @staticmethod
def dailyurl(date = datetime.datetime.today()): def dailyurl(date = datetime.datetime.today()):
"""Returns the URL for the latest valid daily build.""" """Returns the URL for the latest valid daily build."""
@ -68,6 +53,20 @@ class Base(object):
# list and get the latest item # list and get the latest item
return { 'x86': '-', 'x86_64': Definitions.SELECTORS['daily']['URL'] + sorted(a)[-1] } return { 'x86': '-', 'x86_64': Definitions.SELECTORS['daily']['URL'] + sorted(a)[-1] }
@staticmethod
def dailyver(date = datetime.datetime.today()):
"""Returns versions present on the latest daily build."""
url = Base.dailyurl(date)['x86_64']
# If no daily releases has been provided yet, return empty
if url == '-':
return []
# Rerun the page parsing, this time to find out the versions built
b = etree.HTML(urllib.request.urlopen(url).read()).xpath("//td/a[contains(text(), '_deb.tar.gz')]/text()")
# This should have returned the main package for a version, but can
# have returned multiple ones, so let's treat it as a list
return [ x.split('_')[1] for x in b ]
@staticmethod @staticmethod
def namedver(query): def namedver(query):
"""Gets the version for a specific named version.""" """Gets the version for a specific named version."""
@ -87,7 +86,7 @@ class Base(object):
@staticmethod @staticmethod
def fullversion(version): def fullversion(version):
"""Get latest full version from Archive based on partial version.""" """Get latest full version from Archive based on partial version."""
versionlist = etree.HTML(urllib.request.urlopen(Definitions.ARCHIVE).read()).xpath("//td/a[starts-with(text(), '" + version + "')]/text()") versionlist = etree.HTML(urllib.request.urlopen(Definitions.ARCHIVE).read()).xpath(f"//td/a[starts-with(text(), '{version}')]/text()")
cleanlist = sorted([ x.strip('/') for x in versionlist ]) cleanlist = sorted([ x.strip('/') for x in versionlist ])
# Sorting, then returning the last version # Sorting, then returning the last version
@ -112,7 +111,7 @@ class Base(object):
# If the version has only 2 points in it (or splits into three parts by '.'), that's not a full version and we will call the getlatestver() function # If the version has only 2 points in it (or splits into three parts by '.'), that's not a full version and we will call the getlatestver() function
fullversion = version fullversion = version
if len(version.split('.')) == 3: if len(version.split('.')) <= 3:
fullversion = Base.fullversion(version) fullversion = Base.fullversion(version)
# So the final URL is the Archive one, plus the full versions, plus a # So the final URL is the Archive one, plus the full versions, plus a
@ -130,31 +129,67 @@ class Base(object):
return retval return retval
@staticmethod
def collectedbuilds(query):
"""Creates a list of Builds based on each namedver found."""
retval = []
a = Base.namedver(query)
if isinstance(a, list) and len(a) > 1:
retval = [ Build(query, version) for version in Base.namedver(query) ]
else:
retval.append(Build(query))
return retval
class Build(object): class Build(object):
def __init__(self, query): def __init__(self, query, version = None):
"""Should simplify the single builded version.""" """Should simplify the single builded version."""
self.query = query self.query = query
self.version = '' self.version = ''
self.basedirurl = {} self.basedirurl = { 'x86': '-', 'x86_64': '-' }
# For simplification process, let's before see if there's a named query
# (it shouldn't)
if not '.' in self.query: if not '.' in self.query:
# Named version. Let's get the result of the query. # Named version.
a = Base.namedver(self.query) # Let's check if a specific version was requested.
if version:
self.version = version
else:
# In case it was not requested, we will carry on the generic
# namedver() query.
# If the results are more than one, we'll take the latest (since we are requested to provide a single build).
a = Base.namedver(self.query)
if len(a) != 1: if isinstance(a, list) and len(a) == 0:
raise Exception("Queried version for build does not return a single result. Please unfold manually. Exiting.") # No results from the query - let's return default values
return
# So if it is here, the version is one. if len(a) == 1:
self.query = a[0] # version is a single one.
self.version = a[0]
if len(self.query.split('.')) == 4: else:
self.version = self.query # In this case, we will select the latest release.
self.version = sorted(a)[-1]
else: else:
# In case of numbered queries, put it as initial version
self.version = self.query
if len(self.version.split('.')) < 4:
# If not 4 dotted, let's search for the 4 dotted version # If not 4 dotted, let's search for the 4 dotted version
self.version = Base.fullversion(self.query) self.version = Base.fullversion(self.version)
self.basedirurl = Base.urlfromqueryandver(self.query, self.version) self.basedirurl = Base.urlfromqueryandver(self.query, self.version)
def todict(self):
return {
'query': self.query,
'version': self.version,
'basedirurl': self.basedirurl
}
def __str__(self):
return f"""query: {self.query}
version: {self.version}
x86: {self.basedirurl['x86']}
x86_64: {self.basedirurl['x86_64']}"""

View File

@ -1,40 +0,0 @@
#!/usr/bin/env python
# encoding: utf-8
import click
import loaih.versions as versions
import re, sys, json
@click.command()
@click.option('-o', '--output', default = 'rundeck', type=click.Choice(['rundeck', 'json', 'text' ], case_sensitive=False), help="Output format, defaulting to Rundeck Key/Value data format. Options: rundeck,json,text")
@click.argument('query')
def getversion(query, output):
b = []
if '.' in query:
b.append(versions.Build(query))
else:
# In case of names, we might want to loop for versions. So we before
# get the versions available, based on query.
vers = versions.Base.namedver(query)
if len(vers) != 1:
for v in vers:
b.append(versions.Build(v))
else:
b = versions.Build(vers[0])
if output.lower() == 'json':
output = {
'query': query,
'version': b.version,
'basedirurl': b.basedirurl
}
print(json.dumps(output))
else:
print("""query: {query}
version: {version}
x86: {x86_url}
x86_64: {x86_64_url}""".format(query = query, version = b.version, x86_url = b.basedirurl['x86'], x86_64_url = b.basedirurl['x86_64']))
if __name__ == '__main__':
getversion()

View File

@ -11,7 +11,11 @@ setup(
author="Emiliano Vavassori", author="Emiliano Vavassori",
author_email="syntaxerrormmm@libreoffice.org", author_email="syntaxerrormmm@libreoffice.org",
packages=find_packages(exclude=['contrib', 'docs', 'tests']), packages=find_packages(exclude=['contrib', 'docs', 'tests']),
scripts=[ 'scripts/loaih-getversion', 'scripts/loaih-build' ], entry_points={
'console_scripts': [
'loaih = loaih.script:cli',
],
},
install_requires=[ 'click', ], install_requires=[ 'click', ],
license='MIT', license='MIT',
url='https://git.libreitalia.org/LibreItalia/loappimage-helpers/', url='https://git.libreitalia.org/LibreItalia/loappimage-helpers/',