diff options
author | John Stebbins <[email protected]> | 2018-06-06 16:20:34 -0700 |
---|---|---|
committer | John Stebbins <[email protected]> | 2018-06-06 16:23:03 -0700 |
commit | 93a153eee436a5ca71561dd40c4f128e51e4d47a (patch) | |
tree | b9aa093a59c9c83b2eabebbf69777ac0672c5691 /scripts | |
parent | 73649f092fc8da27f256bbf99f8fea0c4aa243fd (diff) |
scripts: add script for creating flatpak manifests
This script can generate manifests that are suitable for submitting to
flathub for publishing to their repository. It can also be used for
generating manifests suitable for building local flatpak bundles and is
used during 'make pkg.create.flatpak'
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/create_flatpak_manifest.py | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/scripts/create_flatpak_manifest.py b/scripts/create_flatpak_manifest.py new file mode 100755 index 000000000..537191923 --- /dev/null +++ b/scripts/create_flatpak_manifest.py @@ -0,0 +1,154 @@ +#!/usr/bin/env python + +import types +import os +import sys +import json +import getopt +import posixpath +try: + from urlparse import urlsplit + from urllib import unquote +except ImportError: # Python 3 + from urllib.parse import urlsplit, unquote + + +def url2filename(url): + path = urlsplit(url).path + return posixpath.basename(unquote(path)) + +def islocal(url): + result = urlsplit(url) + return result.scheme == "" and result.netloc == "" + +class SourceType: + contrib = 1 + archive = 2 + +class SourceEntry: + def __init__(self, url, entry_type, sha256=None): + self.url = url + self.entry_type = entry_type + self.sha256 = sha256 + +class FlatpakManifest: + def __init__(self, source_list, runtime, template=None): + if template != None: + with open(template, 'r') as fp: + self.manifest = json.load(fp) + + self.finish_args = self.manifest["finish-args"] + self.modules = self.manifest["modules"] + self.hbmodule = self.modules[0] + self.sources = [None] * 2 + + self.hbmodule["sources"] = self.sources + else: + self.manifest = {} + self.finish_args = [] + self.modules = [] + self.hbmodule = {} + self.sources = [None] * 2 + + self.manifest["finish-args"] = self.finish_args + self.manifest["modules"] = self.modules + self.modules[0] = self.hbmodule + self.hbmodule["sources"] = self.sources + + self.manifest["runtime-version"] = runtime + + # create "shell" source to 'mkdir download' + source = {} + source["type"] = "shell" + source["commands"] = [ "mkdir -p download" ] + self.sources[1] = source + + handbrake_found = False + for key, value in source_list.items(): + source = {} + if islocal(value.url): + source["path"] = value.url + else: + if value.sha256 == "" or value.sha256 == None: + continue + source["url"] = value.url + source["sha256"] = value.sha256 + + if value.entry_type == SourceType.archive: + if handbrake_found: + print "Error: only one archive source permitted" + sys.exit(3) + + source["type"] = "archive" + source["strip-components"] = 1 + self.sources[0] = source + handbrake_found = True + + elif value.entry_type == SourceType.contrib: + source["type"] = "file" + source["dest-filename"] = "download/" + url2filename(value.url) + self.sources.append(source) + + +def usage(): + print "create_flatpak_manifest [-a <archive>] [-c <contrib>] [-s <sha265>] [-t <template>] [-r <sdk-runtime-version] [-h] [<dst>]" + print " -a --archive - Main archive (a.k.a. HB sources)" + print " -c --contrib - Contrib download URL (can be repeated)" + print " -s --sha256 - sha256 of previous file on command line" + print " -t --template - Flatpak manifest template" + print " -r --runtime - Flatpak SDK runtime version" + print " -h --help - Show this message" + +if __name__ == "__main__": + try: + opts, args = getopt.getopt(sys.argv[1:], "a:c:s:t:r:h", + ["archive=", "contrib=", "sha265=", + "template=", "runtime=", "help"]) + except getopt.GetoptError: + print "Error: Invalid option" + usage() + sys.exit(2) + + if len(args) > 1: + usage() + exit(2) + + source_list = {} + current_source = None + runtime = "3.28" + for opt, arg in opts: + if opt in ("-h", "--help"): + usage() + sys.exit() + elif opt in ("-a", "--archive"): + if arg != None and arg != "": + current_source = arg + source_list[arg] = SourceEntry(arg, SourceType.archive) + else: + current_source = None + elif opt in ("-c", "--contrib"): + if arg != None and arg != "": + current_source = arg + source_list[arg] = SourceEntry(arg, SourceType.contrib) + else: + current_source = None + elif opt in ("-s", "--sha256"): + if current_source != None: + source_list[current_source].sha256 = arg + elif opt in ("-t", "--template"): + template = arg + elif opt in ("-r", "--runtime"): + runtime = arg + + if len(args) > 0: + dst = args[0] + else: + dst = None + + manifest = FlatpakManifest(source_list, runtime, template) + if dst != None: + with open(dst, 'w') as fp: + json.dump(manifest.manifest, fp, ensure_ascii=False, indent=4) + else: + print json.dumps(manifest.manifest, ensure_ascii=False, indent=4) + |