Skip to content

Commit

Permalink
autotest/pymod/test_py_scripts.py - improve run_py_script() to allow …
Browse files Browse the repository at this point in the history
…to run as module
  • Loading branch information
idanmiara committed Apr 28, 2021
1 parent ad23a64 commit b5a2b77
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions autotest/pymod/test_py_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@

import os
import sys
import shlex

import gdaltest
import importlib

###############################################################################
# Return the path in which the Python script is found
Expand Down Expand Up @@ -67,8 +70,19 @@ def get_py_script(script_name):
# Runs a Python script
# Alias of run_py_script_as_external_script()
#
def run_py_script(script_path, script_name, concatenated_argv):
return run_py_script_as_external_script(script_path, script_name, concatenated_argv)
def run_py_script(script_path: str, script_name: str, concatenated_argv: str,
run_as_script: bool = True, run_as_module: bool = False):
result = None
if run_as_module:
try:
module = importlib.import_module('osgeo_utils.' + script_name)
except ImportError:
module = importlib.import_module('osgeo_utils.samples.' + script_name)
argv = [module.__file__] + shlex.split(concatenated_argv)
result = module.main(argv)
if run_as_script:
result = run_py_script_as_external_script(script_path, script_name, concatenated_argv)
return result


###############################################################################
Expand Down

0 comments on commit b5a2b77

Please sign in to comment.