PEPify testmodule.py

This commit is contained in:
Teo Mrnjavac 2014-07-29 13:52:55 +02:00
parent b988c3ad72
commit 637242776c

View File

@ -25,61 +25,62 @@ import yaml
try: try:
import libcalamares import libcalamares
except ImportError: except ImportError:
print( "Failed to import libcalamares. Make sure then PYTHONPATH environment variable includes the dir where libcalamares.so is installed." ) print("Failed to import libcalamares. Make sure then PYTHONPATH "
"environment variable includes the dir where libcalamares.so is "
"installed.")
print() print()
raise raise
class Job: class Job:
def __init__( self, workingPath, doc ):
self.module_name = doc[ "name" ]
self.pretty_name = "Testing job " + doc[ "name" ]
self.working_path = workingPath
self.configuration = doc[ "configuration" ]
def setprogress( self, progress ): def __init__(self, working_path, doc):
print( "Job set progress to {}%.".format( progress * 100 ) ) self.module_name = doc["name"]
self.pretty_name = "Testing job " + doc["name"]
self.working_path = working_path
self.configuration = doc["configuration"]
def setprogress(self, progress):
print("Job set progress to {}%.".format(progress * 100))
def main(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument( "moduledir", parser.add_argument("moduledir",
help = "Dir containing the Python module" ) help="Dir containing the Python module")
parser.add_argument( "globalstorage_yaml", nargs = "?", parser.add_argument("globalstorage_yaml", nargs="?",
help = "A yaml file to initialize GlobalStorage" ) help="A yaml file to initialize GlobalStorage")
args = parser.parse_args() args = parser.parse_args()
print( "Testing module in: " + args.moduledir ) print("Testing module in: " + args.moduledir)
confpath = os.path.join( args.moduledir, "module.conf" ) confpath = os.path.join(args.moduledir, "module.conf")
with open( confpath ) as f: with open(confpath) as f:
doc = yaml.load( f ) doc = yaml.load(f)
if doc[ "type" ] != "job" or doc[ "interface" ] != "python": if doc["type"] != "job" or doc["interface"] != "python":
print( "Only Python jobs can be tested." ) print("Only Python jobs can be tested.")
return 1 return 1
libcalamares.job = Job( args.moduledir, doc ) libcalamares.job = Job(args.moduledir, doc)
libcalamares.globalstorage = libcalamares.GlobalStorage() libcalamares.globalstorage = libcalamares.GlobalStorage()
# if a file for simulating globalStorage contents is provided, load it # if a file for simulating globalStorage contents is provided, load it
if args.globalstorage_yaml: if args.globalstorage_yaml:
with open( args.globalstorage_yaml ) as f: with open(args.globalstorage_yaml) as f:
doc = yaml.load( f ) doc = yaml.load(f)
for key, value in doc.items(): for key, value in doc.items():
libcalamares.globalStorage.insert( key, value ) libcalamares.globalStorage.insert(key, value)
scriptpath = os.path.abspath( args.moduledir ) scriptpath = os.path.abspath(args.moduledir)
sys.path.append( scriptpath ) sys.path.append(scriptpath)
import main import main
print( "Output from module:" ) print("Output from module:")
print( main.run() ) print(main.run())
return 0 return 0
if __name__ == "__main__": if __name__ == "__main__":
sys.exit( main() ) sys.exit(main())