@ -7,29 +7,43 @@ import os
import sys
import sys
import yaml
import yaml
def get_opts():
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser()
parser.add_argument('--output', required=True)
subparsers = parser.add_subparsers(dest="subcommand")
parser.add_argument('--size', required=True)
parser.add_argument('--squashfs', default='')
parser.add_argument('--translations')
parser.add_argument('--template')
parser.add_argument('--langs', default=None)
opts = parser.parse_args(sys.argv[1:])
parser_source = subparsers.add_parser('source')
parser_source.add_argument('--output', required=True)
parser_source.add_argument('--template', required=True)
parser_source.add_argument('--size', required=True)
parser_source.add_argument('--squashfs', default='')
parser_source.add_argument('--translations')
parser_source.add_argument('--langs')
parser_source.set_defaults(func=on_source)
if os.path.exists(opts.output):
parser_merge = subparsers.add_parser('merge')
with open(opts.output) as fp:
parser_merge.add_argument('--output', required=True)
output = yaml.safe_load(fp)
parser_merge.add_argument('--template', required=True)
else:
parser_merge.set_defaults(func=on_merge)
output = []
return parser.parse_args()
with open(opts.template) as fp:
def yaml_load(filename):
template = yaml.safe_load(fp)
with open(filename) as fp:
return yaml.safe_load(fp)
def yaml_dump(filename, content):
with open(filename, 'w') as fp:
yaml.dump(content, fp)
def on_source(opts, output):
template = yaml_load(opts.template)
id = template['id']
id = template['id']
for entry in output:
for entry in output["sources"] :
# First, look if this source catalogue template id is already present.
# First, look if this source catalogue template id is already present.
# If so, use the template to extend the existing entry with additional
# If so, use the template to extend the existing entry with additional
# variations.
# variations.
@ -73,10 +87,10 @@ else:
if opts.langs is not None:
if opts.langs is not None:
template['preinstalled_langs'] = opts.langs.split(',')
template['preinstalled_langs'] = opts.langs.split(',')
output.append(template)
output["sources"] .append(template)
default_count = 0
default_count = 0
for entry in output:
for entry in output["sources"] :
if entry.get('default', False):
if entry.get('default', False):
default_count += 1
default_count += 1
@ -84,5 +98,22 @@ if default_count > 1:
print("Too many defaults in source catalog!")
print("Too many defaults in source catalog!")
sys.exit(1)
sys.exit(1)
with open(opts.output, 'w') as fp:
yaml.dump(output, fp)
def on_merge(opts, output):
output.update(yaml_load(opts.template))
if __name__ == "__main__":
opts = get_opts()
if os.path.exists(opts.output):
output = yaml_load(opts.output)
else:
output = {
"version": 2,
"sources": [],
}
opts.func(opts, output)
yaml_dump(opts.output, output)