Ejemplo n.º 1
0
    def test_filter_replace_module(self):
        """
        Test if it is able to replace whole module by own implemetation
        Test also own implementation of static tempfile module via class
        """

        HANDLE_MODULE_LIST = [
            (
                "^tempfile$",
                {"who_name": SELECTOR},
                {"": [ReplaceType.REPLACE_MODULE, TempFile]},
            )
        ]
        upgrade_import_system(filters=HANDLE_MODULE_LIST)
        import tempfile

        tmpfile = tempfile.mktemp()
        tmpdir = tempfile.mkdtemp()
        self.assertIn(
            f"/tmp/{os.path.basename(PersistentObjectStorage().storage_file)}/static_tmp",
            tmpfile,
        )
        self.assertIn(
            f"/tmp/{os.path.basename(PersistentObjectStorage().storage_file)}/static_tmp",
            tmpdir,
        )
        self.assertFalse(os.path.exists(tmpfile))
        self.assertTrue(os.path.exists(tmpdir))
        self.assertTrue(os.path.isdir(tmpdir))
        os.removedirs(tmpdir)
Ejemplo n.º 2
0
def apply_fn():
    """
    This function is used when installed as  sitecustomize.py script
    to enable replacing system, please set env vars RESPONSE_FILE
    REPLACEMENT_FILE, see doc string of this file
    """
    # file name of storage file
    storage_file = os.getenv(ENV_STORAGE_FILE)
    # file name of replaces for updated import system
    replacement_file = os.getenv(ENV_REPLACEMENT_FILE)
    if_latency = os.getenv(ENV_APPLY_LATENCY)
    replacement_var = os.getenv(ENV_REPLACEMENT_NAME, REPLACE_DEFAULT_KEY)
    debug_print(
        f"You have patched version of your python by requre project "
        f"(python {sys.version_info.major}.{sys.version_info.minor}, {__file__}) "
    )
    if not (storage_file and replacement_file):
        debug_print(f"\tYou have to set {ENV_STORAGE_FILE} and "
                    f"{ENV_REPLACEMENT_FILE} env variables to work properly")
    else:
        if not os.path.exists(replacement_file):
            raise FileExistsError(
                f"{replacement_file} has to exist to work properly "
                f"(python file with replacements definition)")
        if if_latency:
            debug_print("Use latency for function calls")
            DataMiner().use_latency = True
        PersistentObjectStorage().storage_file = storage_file
        spec = importlib.util.spec_from_file_location("replacements",
                                                      replacement_file)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        if hasattr(module, replacement_var):
            replacement = getattr(module, replacement_var)
            debug_print(f"Replaces: {replacement}")
            if isinstance(replacement, UpgradeImportSystem):
                debug_print(
                    f"{replacement_var} is {UpgradeImportSystem.__name__} object"
                )
            elif isinstance(replacement, list):
                debug_print(
                    f"{replacement_var} is list of replacements, apply upgrading"
                )
                upgrade_import_system(filters=replacement)
            else:
                raise_error(
                    126, f"Bad type of {replacement_var}, see documentation")
        else:
            raise_error(
                125,
                f"in {replacement_file} there is not defined '{replacement_var}' variable",
            )
        # register dump command, when python finish
        atexit.register(PersistentObjectStorage().dump)
Ejemplo n.º 3
0
    def test_filter_decorate(self):
        """
        Test patching by decorator_all_keys, not replacing whole function
        """
        HANDLE_MODULE_LIST = [
            (
                "^tempfile$",
                {"who_name": SELECTOR},
                {
                    "mktemp": [
                        ReplaceType.DECORATOR,
                        lambda x: lambda: f"decorated {x()}",
                    ]
                },
            )
        ]
        upgrade_import_system(filters=HANDLE_MODULE_LIST)
        import tempfile

        self.assertIn("decorated", tempfile.mktemp())
        self.assertIn("/tmp", tempfile.mktemp())
Ejemplo n.º 4
0
    def test_filter_replace(self):
        """
        Test improving of import system with import statement
        Check also debug file output if it contains proper debug data
        """
        debug_file = "__modules.log"
        HANDLE_MODULE_LIST = [
            (
                "^tempfile$",
                {"who_name": "test_import_system"},
                {"mktemp": [ReplaceType.REPLACE, lambda: "a"]},
            )
        ]
        upgrade_import_system(filters=HANDLE_MODULE_LIST, debug_file=debug_file)
        import tempfile

        self.assertNotIn("/tmp", tempfile.mktemp())
        self.assertIn("a", tempfile.mktemp())
        with open(debug_file, "r") as fd:
            output = fd.read()
            self.assertIn(SELECTOR, output)
            self.assertIn("replacing mktemp by function", output)
        os.remove(debug_file)
Ejemplo n.º 5
0
from requre.import_system import upgrade_import_system
from tests.replacements import MODULE_LIST

upgrade_import_system(MODULE_LIST, debug_file="modules.out")
Ejemplo n.º 6
0
from requre.helpers.requests_response import RequestResponseHandling
from requre.import_system import upgrade_import_system

ogr_import_system = (upgrade_import_system(
    debug_file="modules.out").log_imports(
        what="^requests$", who_name=["ogr", "gitlab", "github"]).decorate(
            where="^requests$",
            what="Session.send",
            who_name=[
                "ogr.services.pagure",
                "gitlab",
                "github.MainClass",
                "github.Requester",
                "ogr.services.github_tweak",
            ],
            decorator=RequestResponseHandling.decorator(item_list=[]),
        ))
from requre.import_system import upgrade_import_system
from requre.helpers.simple_object import Simple

FILTERS = upgrade_import_system().decorate(where="^time$",
                                           what="sleep",
                                           who_name=[],
                                           decorator=Simple.decorator_plain)
Ejemplo n.º 8
0
import os
from requre.storage import PersistentObjectStorage
from requre.helpers.requests_response import RequestResponseHandling
from requre.import_system import upgrade_import_system

upgrade_import_system().decorate(
    where="^requests$",
    what="Session.send",
    who_name=["github"],
    decorator=RequestResponseHandling.decorator_plain,
)

PersistentObjectStorage().storage_file = "github2.yaml"

import github

g = github.Github(os.getenv("TOKEN", "EMPTY"))
print("Count of your repos: ", len(list(g.get_user().get_repos())))

PersistentObjectStorage().dump()
Ejemplo n.º 9
0
 upgrade_import_system()
 .decorate(
     where="download_helper",
     what="DownloadHelper.request",
     who_name="lookaside_cache_helper",
     decorator=RequestResponseHandling.decorator_plain,
 )
 .decorate(
     where="^requests$",
     who_name=["lookaside_cache_helper", "^copr", "packit.distgit"],
     what="Session.send",
     decorator=RequestResponseHandling.decorator_plain,
 )
 .replace_module(where="^tempfile$", who_name="^packit", replacement=TempFile)
 .decorate(
     where="^packit$",
     who_name="fedpkg",
     what="utils.run_command_remote",
     decorator=Simple.decorator_plain,
 )
 .decorate(
     where="packit.fedpkg",
     what="FedPKG.clone",
     decorator=StoreFiles.arg_references(files_params={"target_path": 2}),
 )
 .decorate(
     where="git",
     who_name="local_project",
     what="remote.Remote.push",
     decorator=PushInfoStorageList.decorator_plain,
 )
 .decorate(
     where="git",
     who_name="local_project",
     what="remote.Remote.fetch",
     decorator=RemoteFetch.decorator_plain,
 )
 .decorate(
     where="git",
     who_name="local_project",
     what="remote.Remote.pull",
     decorator=RemoteFetch.decorator_plain,
 )
 .decorate(  # ogr
     where="^requests$",
     what="Session.send",
     who_name=[
         "ogr.services.pagure",
         "gitlab",
         "github.MainClass",
         "github.Requester",
         "ogr.services.github_tweak",
     ],
     decorator=RequestResponseHandling.decorator_plain,
 )
Ejemplo n.º 10
0
from requre.helpers.requests_response import RequestResponseHandling
from requre.import_system import upgrade_import_system

dashboard_import_system = (
    upgrade_import_system(debug_file="modules.out")
    .log_imports(what="^requests$", who_name=["packit_dashboard"])
    .decorate(
        where="^requests$",
        what="Session.send",
        who_name=["packit_dashboard"],
        decorator=RequestResponseHandling.decorator(item_list=[]),
    )
)


# NOTE:
# To run tests in write mode, delete the test_data folder.
# requre will then run the to-be-tested functions properly
# and store the output from  all 'requests' called within the
# function to a yaml file in test_data
# When you run the tests again, it will run them in read mode.