calamares/src/modules/testmodule.py

112 lines
3.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2015-02-18 15:06:10 +01:00
# -*- coding: utf-8 -*-
#
# === This file is part of Calamares - <http://github.com/calamares> ===
#
# Copyright 2014, Teo Mrnjavac <teo@kde.org>
#
# Calamares is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Calamares is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Calamares. If not, see <http://www.gnu.org/licenses/>.
2014-07-24 10:15:16 +02:00
import argparse
import os
import sys
2014-07-24 10:15:16 +02:00
import yaml
2014-07-24 10:15:16 +02:00
try:
import libcalamares
except ImportError:
2014-07-29 13:52:55 +02:00
print("Failed to import libcalamares. Make sure then PYTHONPATH "
"environment variable includes the dir where libcalamares.so is "
"installed.")
2014-07-24 10:15:16 +02:00
print()
raise
class Job:
2015-02-20 20:54:25 +01:00
"""
:param working_path:
:param doc:
:param cfg_doc:
"""
def __init__(self, working_path, doc, cfg_doc):
2014-07-29 13:52:55 +02:00
self.module_name = doc["name"]
self.pretty_name = "Testing job " + doc["name"]
self.working_path = working_path
self.configuration = cfg_doc
2014-07-29 13:52:55 +02:00
def setprogress(self, progress):
2015-02-20 20:54:25 +01:00
"""
:param progress:
"""
2014-07-29 13:52:55 +02:00
print("Job set progress to {}%.".format(progress * 100))
2014-07-23 13:01:30 +02:00
2014-07-24 10:15:16 +02:00
def main():
2015-02-20 20:54:25 +01:00
"""
:return:
"""
2014-07-24 10:15:16 +02:00
parser = argparse.ArgumentParser()
2014-07-29 13:52:55 +02:00
parser.add_argument("moduledir",
help="Dir containing the Python module.")
2014-07-29 13:52:55 +02:00
parser.add_argument("globalstorage_yaml", nargs="?",
help="A yaml file to initialize GlobalStorage.")
parser.add_argument("configuration_yaml", nargs="?",
help="A yaml file to initialize the configuration dict.")
2014-07-24 10:15:16 +02:00
args = parser.parse_args()
2014-07-29 13:52:55 +02:00
print("Testing module in: " + args.moduledir)
confpath = os.path.join(args.moduledir, "module.desc")
2014-07-29 13:52:55 +02:00
with open(confpath) as f:
doc = yaml.load(f)
2014-07-29 13:52:55 +02:00
if doc["type"] != "job" or doc["interface"] != "python":
print("Only Python jobs can be tested.")
return 1
libcalamares.globalstorage = libcalamares.GlobalStorage()
# if a file for simulating globalStorage contents is provided, load it
2014-07-24 10:15:16 +02:00
if args.globalstorage_yaml:
2014-07-29 13:52:55 +02:00
with open(args.globalstorage_yaml) as f:
gs_doc = yaml.load(f)
2014-08-06 12:57:12 +02:00
for key, value in gs_doc.items():
2014-07-30 12:41:34 +02:00
libcalamares.globalstorage.insert(key, value)
cfg_doc = dict()
if args.configuration_yaml:
with open(args.configuration_yaml) as f:
cfg_doc = yaml.load(f)
libcalamares.job = Job(args.moduledir, doc, cfg_doc)
2014-07-29 13:52:55 +02:00
scriptpath = os.path.abspath(args.moduledir)
sys.path.append(scriptpath)
import main
2014-07-29 13:52:55 +02:00
print("Output from module:")
print(main.run())
return 0
if __name__ == "__main__":
2014-07-29 13:52:55 +02:00
sys.exit(main())