Example #1
0
def test_logging():

    l = Logging("INFO")
    l.name = "test"
    l.info("test")
    l.debug("test")
    l.warning("test")
    l.error("test")
    l.critical("test")

    for level in ['DEBUG', 'INFO', 'ERROR', 'WARNING', 'CRITICAL']:
        l.level = level
        assert l.level == level
    l.level = True
    l.level = False
    for x in [10, 20, 30, 40, 50]:
        l.level = x

    try:
        l.level = "WARN"
        assert Fales
    except:
        assert True

    # FIXME is this working ??wierd syntax in loggibg_tools.
    import copy
    copy.copy(l)
    copy.deepcopy(l)
Example #2
0
class RPackageManager(object):
    """Implements a R package manager from Python

    So far you can install a package (from source, or CRAN, or biocLite)

    ::

        pm = PackageManager()
        [(x, pm.installed[x][2]) for x in pm.installed.keys()]


    You can access to all information within a dataframe called **packages** where
    indices are the name packages. Some aliases are provided as attributes (e.g., available, 
    installed)


    """
    cran_repos = "http://cran.univ-lyon1.fr/"

    def __init__(self, verbose=True):
        self.session = RSession()
        self.logging = Logging(verbose)
        self.logging.info('Fetching package information')
        self.update()

    def _update(self):
        # local import ?
        import numpy
        import pandas
        # figure out the installed packages first
        code = """rvar_packages = as.data.frame(installed.packages())"""
        self.session.run(code)
        s = self.session.rvar_packages
        # FIXME. these 4 lines are needed as a hack related to pyper.
        try:
            s = s.replace("\n", "")
            df = eval(s)
        except:
            df = s

        df.set_index('Package', inplace=True)
        self._packages = df.copy()

        # Now, fetch was is possible to install from the default cran repo
        code = """rvar_status=packageStatus(repos="%s/src/contrib")"""
        code = code % self.cran_repos

        self.session.run(code)
        s = self.session.rvar_status

        # FIXME.
        try:
            s = s.replace("\n", "")
            res = eval(s)
        except:
            res = s
        res['inst'].set_index('Package', inplace=True)
        res['avail'].set_index('Package', inplace=True)
        self._status = res

    def update(self):
        """If you install/remove packages yourself elsewhere, you may need to 
        call this function to update the package manager"""
        try:
            #self.session.reconnect()          
            self._update()
        except:
            self.logging.warning("Could not update the packages. Call update() again")
        
    def _compat_version(self, version):
        return version.replace("-", "a")

    def _get_installed(self):
        # we do not buffer because packages may be removed manually or from R of
        # using remove_packages method, ....
        #self._package_status()
        return self._status['inst']
    installed = property(_get_installed, "returns list of packages installed as a dataframe")

    def _get_available(self):
        # we do not buffer because packages may be removed manually or from R of
        # using remove_packages method, ....
        #self._package_status()
        return self._status['avail']
    available = property(_get_available, "returns list of packages available as a dataframe")

    def  _get_packages(self):
        # do not buffer since it may change in many places
        return self._packages
    packages = property(_get_packages)

    def get_package_latest_version(self, package):
        """Get latest version available of a package"""
        return self.available['Version'].ix[package]

    def get_package_version(self, package):
        """Get version of an install package"""
        if package not in self.installed.index:
            self.logging.error("package {0} not installed".format(package))
        return self.installed['Version'].ix[package]

    def biocLite(self, package=None, suppressUpdates=True, verbose=False):
        """Installs one or more biocLite packages

        :param package: a package name (string) or list of package names (list of 
            strings) that will be installed from BioConductor. If package is set 
            to None, all packages already installed will be updated.

        """
        if isinstance(package, str):
            if package not in self.installed.index:
                biocLite(package, suppressUpdates, verbose=verbose)
        elif isinstance(package, list):
            for pkg in package:
                self.logging.info("Installing %s" % pkg)
                if self.is_installed(pkg) is False:
                    biocLite(pkg, suppressUpdates, verbose=verbose)
        else: # trying other cases (e.g., None updates biocLite itself). 
            biocLite(package, suppressUpdates, verbose=verbose)
        self.update()

    def _isLocal(self, pkg):
        if os.path.exists(pkg):
            return True
        else:
            return False

    def remove(self, package):
        """Remove a package (or list) from local repository"""
        rcode ="""remove.packages("%s")"""
        if isinstance(package, str):
            package = [package]
        for pkg in package:
            if pkg in self.installed.index:
                self.session(rcode % pkg)
            else:
                self.logging.warning("Package not found. Nothing to remove")
        self.update()

    def require(self, pkg, version):
        "Check if a package with given version is available"

        if pkg not in self.installed.index:
            self.logging.info("Package %s not installed" % pkg)
            return False
        currentVersion = self.packageVersion(pkg)
        if self._get_version(currentVersion) >= self._get_version(version):
            return True
        else:
            return False

    def _install_package(self, packageName, dependencies=True):
        """Installs one or more CRAN packages
        
        .. todo:: check if it is already available to prevent renstallation ?
        """

        repos = self.cran_repos
        # if this is a source file we want to reset the repo
        if isinstance(packageName, str):
            packageName = [packageName]
        for pkg in packageName:
            if self.is_installed(pkg) is False:
                self.logging.info("Package not found. Installing %s..." % pkg)
                install_package(pkg, dependencies=dependencies, 
                        repos=repos)
            else:
                self.logging.info("Package %s found. " % pkg)
                install_package(pkg, dependencies=dependencies, 
                        repos=repos)
        self.update()

    def install(self, pkg, require=None, update=True, reinstall=False):
        """install a package automatically scanning CRAN and biocLite repos

        if require is not set and update is True, when a newest version of a package
        is available, it is installed

        """
        from easydev import to_list
        pkgs = to_list(pkg)
        for pkg in pkgs:
            self._install(pkg, require=require, update=update, reinstall=reinstall)

    def _install(self, pkg, require=None, update=update, reinstall=False):
        # LOCAL file
        if self._isLocal(pkg):
            # if a local file, we do not want to jump to biocLite or CRAN. Let
            # us install it directly. We cannot check version yet so we will
            # overwrite what is already installed
            self.logging.warning("Installing from source")
            self._install_package(pkg)
            return

        # From CRAN
        if self.is_installed(pkg):
            currentVersion = self.get_package_version(pkg)
            # if not provided, require should be the latest version
            if require is None and update is True:
                try:
                    require = self.get_package_latest_version(pkg)
                except:
                    # a non-cran package (bioclite maybe)
                    pass

            if require is None:
                self.logging.info("%s already installed with version %s" % \
                    (pkg, currentVersion))
                return
            
            # if require is not none, is it the required version ?
            if self._get_version(currentVersion) >= self._get_version(require) and reinstall is False:
                self.logging.info("%s already installed with required version %s" \
                    % (pkg, currentVersion))
                # if so, nothing to do
            else:
                # Try updating
                self.logging.info("Updating")
                self._install_package(pkg)
                if require is None:
                    return
                currentVersion = self.get_package_version(pkg)
                if self._get_version(currentVersion) < self._get_version(require):
                    self.logging.warning("%s installed but current version (%s) does not fulfill your requirement" % \
                        (pkg, currentVersion))

        elif pkg in self.available.index:
            self._install_package(pkg)
        else:
            # maybe a biocLite package ?
            # require is ignored. The latest will be installed
            self.logging.info("Trying to find the package on bioconductor")
            self.biocLite(pkg)
            if require is None:
                return
            currentVersion = self.get_package_version(pkg)
            if self._get_version(currentVersion) >= self._get_version(require):
                self.logging.warning("%s installed but version is %s too small (even after update)" % \
                    (pkg, currentVersion, require))

    def _get_version(self, version):
        # some pacakge do not use the correct version convention
        try:
            return StrictVersion(version)
        except:
            try:
                return StrictVersion(version.replace("-", "a"))
            except:
                # snowfall package example was 1.86-6.1
                # This becomes 1.86a61  which is not great but not workaround
                # for now
                left, right = version.split("-")
                version = left + "a" + right.replace('.', '')
                return StrictVersion(version)

    def is_installed(self, pkg_name):
        if pkg_name in self.installed.index:
            return True
        else:
            return False
Example #3
0
class RPackageManager(object):
    """Implements a R package manager from Python

    So far you can install a package (from source, or CRAN, or biocLite)

    ::

        pm = PackageManager()
        [(x, pm.installed[x][2]) for x in pm.installed.keys()]


    You can access to all information within a dataframe called **packages** where
    indices are the name packages. Some aliases are provided as attributes (e.g., available, 
    installed)


    """
    cran_repos = "http://cran.univ-lyon1.fr/"

    def __init__(self, verbose=True):
        self.session = RSession()
        self.logging = Logging(verbose)
        self.logging.info('Fetching package information')
        self.update()

    def _update(self):
        # local import ?
        import numpy
        import pandas
        # figure out the installed packages first
        code = """rvar_packages = as.data.frame(installed.packages())"""
        self.session.run(code)
        s = self.session.rvar_packages
        # FIXME. these 4 lines are needed as a hack related to pyper.
        try:
            s = s.replace("\n", "")
            df = eval(s)
        except:
            df = s

        df.set_index('Package', inplace=True)
        self._packages = df.copy()

        # Now, fetch was is possible to install from the default cran repo
        code = """rvar_status=packageStatus(repos="%s/src/contrib")"""
        code = code % self.cran_repos

        self.session.run(code)
        s = self.session.rvar_status

        # FIXME.
        try:
            s = s.replace("\n", "")
            res = eval(s)
        except:
            res = s
        res['inst'].set_index('Package', inplace=True)
        res['avail'].set_index('Package', inplace=True)
        self._status = res

    def update(self):
        """If you install/remove packages yourself elsewhere, you may need to 
        call this function to update the package manager"""
        try:
            #self.session.reconnect()          
            self._update()
        except:
            self.logging.warning("Could not update the packages. Call update() again")

    def _compat_version(self, version):
        return version.replace("-", "a")

    def _get_installed(self):
        # we do not buffer because packages may be removed manually or from R of
        # using remove_packages method, ....
        #self._package_status()
        return self._status['inst']
    installed = property(_get_installed, "returns list of packages installed as a dataframe")

    def _get_available(self):
        # we do not buffer because packages may be removed manually or from R of
        # using remove_packages method, ....
        #self._package_status()
        return self._status['avail']
    available = property(_get_available, "returns list of packages available as a dataframe")

    def  _get_packages(self):
        # do not buffer since it may change in many places
        return self._packages
    packages = property(_get_packages)

    def get_package_latest_version(self, package):
        """Get latest version available of a package"""
        return self.available['Version'].ix[package]

    def get_package_version(self, package):
        """Get version of an install package"""
        if package not in self.installed.index:
            self.logging.error("package {0} not installed".format(package))
        return self.installed['Version'].ix[package]

    def biocLite(self, package=None, suppressUpdates=True, verbose=False):
        """Installs one or more biocLite packages

        :param package: a package name (string) or list of package names (list of 
            strings) that will be installed from BioConductor. If package is set 
            to None, all packages already installed will be updated.

        """
        if isinstance(package, str):
            if package not in self.installed.index:
                biocLite(package, suppressUpdates, verbose=verbose)
        elif isinstance(package, list):
            for pkg in package:
                self.logging.info("Installing %s" % pkg)
                if self.is_installed(pkg) is False:
                    biocLite(pkg, suppressUpdates, verbose=verbose)
        else: # trying other cases (e.g., None updates biocLite itself). 
            biocLite(package, suppressUpdates, verbose=verbose)
        self.update()

    def _isLocal(self, pkg):
        if os.path.exists(pkg):
            return True
        else:
            return False

    def remove(self, package):
        """Remove a package (or list) from local repository"""
        rcode ="""remove.packages("%s")"""
        if isinstance(package, str):
            package = [package]
        for pkg in package:
            if pkg in self.installed.index:
                self.session(rcode % pkg)
            else:
                self.logging.warning("Package not found. Nothing to remove")
        self.update()

    def require(self, pkg, version):
        "Check if a package with given version is available"

        if pkg not in self.installed.index:
            self.logging.info("Package %s not installed" % pkg)
            return False
        currentVersion = self.packageVersion(pkg)
        if self._get_version(currentVersion) >= self._get_version(version):
            return True
        else:
            return False

    def _install_package(self, packageName, dependencies=True):
        """Installs one or more CRAN packages
        
        .. todo:: check if it is already available to prevent renstallation ?
        """

        repos = self.cran_repos
        # if this is a source file we want to reset the repo
        if isinstance(packageName, str):
            packageName = [packageName]
        for pkg in packageName:
            if self.is_installed(pkg) is False:
                self.logging.info("Package not found. Installing %s..." % pkg)
                install_package(pkg, dependencies=dependencies, 
                        repos=repos)
            else:
                self.logging.info("Package %s found. " % pkg)
                install_package(pkg, dependencies=dependencies, 
                        repos=repos)
        self.update()

    def install(self, pkg, require=None, update=True, reinstall=False):
        """install a package automatically scanning CRAN and biocLite repos

        if require is not set and update is True, when a newest version of a package
        is available, it is installed

        """
        from easydev import to_list
        pkgs = to_list(pkg)
        for pkg in pkgs:
            self._install(pkg, require=require, update=update, reinstall=reinstall)

    def _install(self, pkg, require=None, update=update, reinstall=False):
        # LOCAL file
        if self._isLocal(pkg):
            # if a local file, we do not want to jump to biocLite or CRAN. Let
            # us install it directly. We cannot check version yet so we will
            # overwrite what is already installed
            self.logging.warning("Installing from source")
            self._install_package(pkg)
            return

        # From CRAN
        if self.is_installed(pkg):
            currentVersion = self.get_package_version(pkg)
            # if not provided, require should be the latest version
            if require is None and update is True:
                try:
                    require = self.get_package_latest_version(pkg)
                except:
                    # a non-cran package (bioclite maybe)
                    pass

            if require is None:
                self.logging.info("%s already installed with version %s" % \
                    (pkg, currentVersion))
                return
            
            # if require is not none, is it the required version ?
            if self._get_version(currentVersion) >= self._get_version(require) and reinstall is False:
                self.logging.info("%s already installed with required version %s" \
                    % (pkg, currentVersion))
                # if so, nothing to do
            else:
                # Try updating
                self.logging.info("Updating")
                self._install_package(pkg)
                if require is None:
                    return
                currentVersion = self.get_package_version(pkg)
                if self._get_version(currentVersion) < self._get_version(require):
                    self.logging.warning("%s installed but current version (%s) does not fulfill your requirement" % \
                        (pkg, currentVersion))

        elif pkg in self.available.index:
            self._install_package(pkg)
        else:
            # maybe a biocLite package ?
            # require is ignored. The latest will be installed
            self.logging.info("Trying to find the package on bioconductor")
            self.biocLite(pkg)
            if require is None:
                return
            currentVersion = self.get_package_version(pkg)
            if self._get_version(currentVersion) >= self._get_version(require):
                self.logging.warning("%s installed but version is %s too small (even after update)" % \
                    (pkg, currentVersion, require))

    def _get_version(self, version):
        # some pacakge do not use the correct version convention
        try:
            return StrictVersion(version)
        except:
            try:
                return StrictVersion(version.replace("-", "a"))
            except:
                # snowfall package example was 1.86-6.1
                # This becomes 1.86a61  which is not great but not workaround
                # for now
                left, right = version.split("-")
                version = left + "a" + right.replace('.', '')
                return StrictVersion(version)

    def is_installed(self, pkg_name):
        if pkg_name in self.installed.index:
            return True
        else:
            return False