Exemple #1
0
def copy_file(module, path, src, dst, update=True):
    try:
        import_module(module)
    except ImportError as e:
        print("Import Error %s\n%s" % (module, e))
        exit()

    dst = os.path.normpath("%s/%s" % (path, dst))
    if resource_exists(module, src):
        src_file = resource_stream(module, src).read()
        if not os.path.exists(dst):
            with open(dst, 'wb') as handle:
                handle.write(src_file)
                print("Created %s" % dst)
        else:
            if update is False:
                dst = "%s.default" % (dst, )
                if not os.path.exists(dst):
                    with open(dst, 'wb') as handle:
                        handle.write(src_file)
                        print("Created %s" % dst)
            else:
                src_sig = hashlib.md5(src_file)
                dst_file = open(dst, 'rb').read()
                dst_sig = hashlib.md5(dst_file)
                if src_sig.hexdigest() != dst_sig.hexdigest():
                    with open(dst, 'wb') as handle:
                        handle.write(src_file)
                        print("Updated %s" % dst)
Exemple #2
0
def determine_app_root(name, app_root=None):
    """Determines the root directory of a given application

    Args:
        name (str): application name

    Returns:
        Root directory of app, as a string
    """
    if app_root is None:
        if name == "__main__" or "_mod_wsgi" in name:
            app_mod = imports.import_module(name)
            return abspath(
                os.path.dirname(
                    app_mod.__file__)).rstrip('/')
        else:
            log.error("Unable to determine application root." +
                      " Using current working directory '%s'" % os.getcwd())
            return abspath(os.getcwd()).rstrip('/')
    else:
        if exists(app_root) and not os.path.isfile(app_root):
            return abspath(app_root).rstrip('/')
        else:
            raise FileNotFoundError("Invalid path"
                                    + " for root '%s'"
                                    % app_root) from None
Exemple #3
0
def determine_app_root(name, app_root=None):
    if app_root is None:
        if name == "__main__" or "_mod_wsgi" in name:
            app_mod = imports.import_module(name)
            return os.path.abspath( \
                os.path.dirname( \
                    app_mod.__file__)).rstrip('/')
        else:
            log.error("Unable to determine application root." +
                      " Using current working directory '%s'" % os.getcwd())
            return os.getcwd().rstrip('/')
    else:
        if os.path.exists(app_root) and not os.path.isfile(app_root):
            return app_root.rstrip('/')
        else:
            raise Error("Invalid path for root '%s'" % app_root)
Exemple #4
0
def profile_objects(root=None, include_obj=True):
    usage = []
    modules = sys.modules.keys()
    for module in modules:
        module = module.split('.')
        if ((root is not None and module[0] == root) or
             root is None):
            module = ".".join(module)
            py_mod = import_module(module)
            for obj, py_obj in inspect.getmembers(py_mod):
                if not inspect.ismodule(py_obj):
                    if include_obj is True:
                        usage.append((module + '.' + obj,
                                      sys.getsizeof(py_obj),
                                      str(type(py_obj)),
                                      py_obj))
                    else:
                        usage.append((module + '.' + obj,
                                      sys.getsizeof(py_obj),
                                      str(type(py_obj))))
    return usage
Exemple #5
0
 def __init__(self, module):
     try:
         import_module(module)
         self._module = module
     except ImportError:
         raise ImportError(module) from None