Read about our upcoming Code of Conduct on this issue

Commit 084a117a authored by Pierre Augier's avatar Pierre Augier
Browse files

Fix bug script importing local script + clear-cache

parent 241b2c7654f6
0.0.10 (?)
----------
- Fix bug script importing local script
- Command line option "clear-cache"
0.0.9 (2018-11-20)
------------------
......
......@@ -17,7 +17,12 @@ from glob import glob
from . import __version__
from .transpiler import make_pythran_files
from .log import logger, set_log_level
from .util import compile_pythran_files, ext_suffix_short, has_to_build
from .util import (
compile_pythran_files,
ext_suffix_short,
has_to_build,
clear_cached_extensions,
)
try:
import pythran
......@@ -37,15 +42,20 @@ def run():
See :code:`fluidpythran -h`
"""
args = parse_args()
# print(args)
if args.version:
print(__version__)
return
if not args.path:
if not args.path and not args.clear_cache:
print("No python files given. Nothing to do! ✨ 🍰 ✨.")
return
if args.clear_cache:
clear_cached_extensions(args.clear_cache, force=args.force)
return
if args.verbose is None:
set_log_level("info")
elif args.verbose > 0:
......@@ -125,4 +135,12 @@ def parse_args():
default="",
)
parser.add_argument(
"-cc",
"--clear-cache",
help=("Clear the cached extensions for a module"),
type=str,
# default="",
)
return parser.parse_args()
......@@ -5,6 +5,8 @@ import time
from . import path_data_tests
from .run import run
from . import util
path_dir_out = path_data_tests / "__pythran__"
......@@ -34,6 +36,9 @@ def test_create_pythran_simple():
def test_create_pythran_classic():
util.input = lambda : 'y'
if path_dir_out.exists():
rmtree(path_dir_out)
......@@ -56,6 +61,9 @@ def test_create_pythran_classic():
print("after unlink")
run()
sys.argv = f"fluidpythran -cc {path_file}".split()
run()
sys.argv = f"fluidpythran {path_file}".split()
run()
......
from . import util
from .util import query_yes_no
def test_query_yes_no():
util.input = lambda: "y"
query_yes_no("test", default="y")
query_yes_no("test", default="n")
query_yes_no("test")
util.input = lambda: ""
query_yes_no("test", default="y")
query_yes_no("test", force=True)
......@@ -32,6 +32,7 @@ from token import tok_name
from io import BytesIO
from pathlib import Path
from runpy import run_module, run_path
import sys
from typing import Iterable
......@@ -252,9 +253,10 @@ def make_pythran_code(path_py: Path):
name_mod = ".".join(
path_py.absolute().relative_to(os.getcwd()).with_suffix("").parts
)
sys.path.insert(0, "")
try:
namespace = run_module(name_mod)
except ImportError:
except ImportError as error:
logger.warning(
f"fluidpythran was unable to import module {name_mod}: "
"no Pythran file created. "
......@@ -262,7 +264,11 @@ def make_pythran_code(path_py: Path):
"you could add '# FLUIDPYTHRAN_NO_IMPORT' "
"in the module is needed..."
)
print(error)
raise
return
finally:
del sys.path[0]
fluidpythran.aheadoftime.is_transpiling = False
(
......
......@@ -38,6 +38,9 @@ Internal API
.. autofunction:: import_from_path
.. autofunction:: query_yes_no
.. autofunction:: clear_cached_extensions
"""
import os
......@@ -50,6 +53,8 @@ import ast
import hashlib
import sysconfig
import importlib.util
from distutils.util import strtobool
import shutil
# for SchedulerPopen
import subprocess
......@@ -308,3 +313,78 @@ def import_from_path(path: Path, module_name: str):
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def query_yes_no(question: str, default: str = None, force: bool = False):
"""User yes or no query"""
if force:
return True
if default is None:
end = "(y/n)"
default = ""
elif default == "y":
end = "([y]/n)"
elif default == "n":
end = "(y/[n])"
print(f"{question} {end}")
while True:
answer = input()
if answer == "":
answer = default
try:
return strtobool(answer)
except ValueError:
print('Please respond with "y" or "n".')
def clear_cached_extensions(module_name: str, force: bool = False):
"""Delete the cached extensions related to a module
"""
from .cached_jit import path_cachedjit
if module_name.endswith(".py"):
module_name = module_name[:-3]
if os.path.sep not in module_name:
relative_path = module_name.replace(".", os.path.sep)
else:
relative_path = module_name
path_pythran_dir_jit = path_cachedjit / relative_path
relative_path = Path(relative_path)
path_pythran = relative_path.parent / (
"__pythran__/_" + relative_path.name + ".py"
)
path_ext = path_pythran.with_name(name_ext_from_path_pythran(path_pythran))
if not path_pythran_dir_jit.exists() and not (
path_pythran.exists() or path_ext.exists()
):
print(
f"Not able to find cached extensions corresponding to {module_name}"
)
print("Nothing to do! ✨ 🍰 ✨.")
return
if path_pythran_dir_jit.exists() and query_yes_no(
f"Do you confirm that you want to delete the JIT cache for {module_name}",
default="y",
force=force,
):
shutil.rmtree(path_pythran_dir_jit)
if path_pythran.exists() or path_ext.exists():
if query_yes_no(
f"Do you confirm that you want to delete the AOT cache for {module_name}",
default="y",
force=force,
):
for path in (path_pythran, path_ext):
if path.exists():
path.unlink()
......@@ -33,6 +33,7 @@ exclude_lines =
except IndexError:
except AttributeError:
except KeyError:
except ValueError
if test:
if check:
if __name__ == "__main__":
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment