def remove_group(self, group):
        """
        remove a group

        @param      group       group
        @return                 list of removed files

        see `remove_folder <http://www.xavierdupre.fr/app/pyquickhelper/helpsphinx//pyquickhelper/filehelper/synchelper.html#module-pyquickhelper.filehelper.synchelper>`_
        """
        loc = self.get_group_location(group)
        return remove_folder(loc)
示例#2
0
    def remove_group(self, group):
        """
        remove a group

        @param      group       group
        @return                 list of removed files

        see `remove_folder <http://www.xavierdupre.fr/app/pyquickhelper/helpsphinx//pyquickhelper/filehelper/synchelper.html#module-pyquickhelper.filehelper.synchelper>`_
        """
        loc = self.get_group_location(group)
        return remove_folder(loc)
def last_function(innosetup, folders, verbose=False, fLOG=print):
    """
    Applies last modifications to the setup.

    @param          innosetup   innosetup script which defines the setup
    @param          folders     dictionary with keys *workspace*, *python*, *tools*, *build*, *docs*
    @param          verbose     verbose
    @param          fLOG        logging function
    """
    from pymyinstall.win_installer.win_setup_r import r_run_script

    work = folders["workspace"]
    python = folders["python"]
    tools = folders["tools"]
    build = os.path.join(folders["build"], "custom_ensae_teaching_cs")
    docs = os.path.join(work, "docs")
    logs = folders["logs"]
    this = os.path.abspath(os.path.dirname(__file__))
    doc_annee = [os.path.join(this, "..", "..", "..", "_doc", "notebooks", sub)
                 for sub in ["td1a", "td2a", "td3a", "1a", "2a", "expose"]]

    if not os.path.exists(build):
        os.mkdir(build)

    # folders
    fLOG("folders:", folders)
    fLOG("innosetup:", innosetup)

    # docs
    fLOG("--- cleaning creating folder", docs)
    if os.path.exists(docs):
        remove_folder(docs)
    os.mkdir(docs)

    # RSS
    fLOG("--- update rss.list.xml")
    # flake8 is no happy with: from .. import __blog__
    __blog__ = os.path.abspath(os.path.join(
        os.path.dirname(__file__), "..", "rss_teachings.xml"))

    rssfile = os.path.join(folders["config"], "rss_list.xml")
    shutil.copy(__blog__, rssfile)

    # R_install
    fLOG("--- R install")
    r_script = os.path.join(os.path.dirname(__file__), "R_install.r")
    if os.path.exists(r_script):
        r_run_script(os.path.join(tools, "R"), r_script, os.path.join(
            logs, "r_ensae_teaching_cs.install.log.txt"))

    # documentation
    fLOG("--- documentation, TDs +++")
    for dist in doc_annee:
        end = os.path.split(dist)[-1]
        to = os.path.join(docs, end)
        if not os.path.exists(to):
            os.mkdir(to)
        for ipy in os.listdir(dist):
            if ipy.endswith("ipynb"):
                if verbose:
                    fLOG("copy ", ipy)
                full = os.path.join(dist, ipy)
                shutil.copy(full, to)

    # others packages not from Microsoft
    from pymyinstall import ModuleInstall
    from pymyinstall.win_installer.win_packages import win_install_package_other_python, is_package_installed

    # modules
    modules = [ModuleInstall("pyquickhelper", "pip"),
               ModuleInstall("pyensae", "pip"),
               ModuleInstall("pyrsslocal", "pip"),
               ModuleInstall("code_beatrix", "pip"),
               ModuleInstall("pymmails", "pip"),
               ModuleInstall("pymyinstall", "pip"),
               ModuleInstall("ensae_teaching_cs", "pip"),
               ModuleInstall("actuariat_python", "pip"),
               ]

    # new packages
    fLOG("--- download new packages")
    pack = []
    for mod in modules:
        mname = mod.mname if mod.mname is not None else mod.name
        if not is_package_installed(python, [mod.name, mname]):
            fLOG("download:", mod.name)
            p = mod.download(temp_folder=build)
            pack.append((p, mod))

    # install packages
    fLOG("--- install packages")
    for p, mod in pack:
        fLOG("install", os.path.split(p)[-1])
        win_install_package_other_python(python, p, verbose=verbose, fLOG=fLOG)

    # remove unnecessary folders
    fLOG("--- remove too big folfers")
    to_remove = []
    for rem in to_remove:
        sub = os.path.join(python, rem)
        if os.path.exists(sub):
            fLOG("remove", sub)
            remove_folder(sub)

    # modifies the setup
    fLOG("--- modifies the setup")
    with open(innosetup, "r", encoding="utf8") as f:
        content = f.read()
    with open(innosetup, "w", encoding="utf8") as f:
        f.write(content)
def update_pip(python_path=None, fLOG=print):
    """
    update pip for a specific distribution

    @param      python_path     python path (or sys.executable if None)
    @param      fLOG            logging function
    @return                     output

    The command ``python -m pip install -U pip`` or
    ``pip install --upgrade pip`` might fails on Windows due to very long paths
    (see `Upgrading pip fails on Windows when install path is too long <https://github.com/pypa/pip/issues/3055>`_).
    If that happens,
    assuming the module *pymyinstall* was installed with pip, we can now remove
    *pip* and use *get_pip.py* instead. This part requires *pyquickhelper*.

    We try the url `bootstrap.pypa.io/get-pip.py <https://bootstrap.pypa.io/get-pip.py>`_ first
    then a local copy.
    """
    if python_path is None:
        python_path = sys.executable
    else:
        python_path = os.path.join(python_path, "python")
    cmd = python_path + " -m pip install -U pip"
    out, err = run_cmd(cmd, wait=True, fLOG=fLOG)
    if err and len(err) > 0:
        if ("FileNotFoundError" in err or "No module named pip.__main__" in err) and sys.platform.startswith("win"):
            from pyquickhelper.filehelper import remove_folder

            # we try to remove pip and to install it again
            # it might be due to long path on Windows
            pack = os.path.join(os.path.dirname(python_path), "Lib", "site-packages")
            if not os.path.exists(pack):
                raise FileNotFoundError(pack)
            fpip = os.path.join(pack, "pip")
            if os.path.exists(fpip):
                # remove the folder
                fLOG("  remove folder", fpip)
                remove_folder(fpip)

            pip_ = [_ for _ in os.listdir(pack) if _.startswith("pip-")]
            if len(pip_) > 0:
                for _ in pip_:
                    fp = os.path.join(pack, _)
                    fLOG("  remove folder", fp)
                    remove_folder(fpip)

            url = "https://bootstrap.pypa.io/get-pip.py"
            cmd = python_path + " " + url
            out, err = run_cmd(cmd, wait=True)
            if err and len(err) > 0:
                get_pip = os.path.abspath(os.path.join(os.path.dirname(__file__), "get_pip.py"))
                if not os.path.exists(get_pip):
                    raise FileNotFoundError(get_pip)
                cmd = python_path + " " + get_pip
                out, err = run_cmd(cmd, wait=True)
                if err and len(err) > 0:
                    raise UpdatePipError(
                        "unable to update pip with get_pip.\nCMD:\n{0}\nOUT:\n{1}\nERR:\n{2}".format(cmd, out, err)
                    )
        else:
            lines = err.split("\n")
            keep = []
            for line in lines:
                if not line.startswith(" ") and "RuntimeWarning: Config variable" not in line:
                    keep.append(line)
            if len(keep) > 0:
                raise UpdatePipError("unable to update pip.\nCMD:\n{0}\nOUT:\n{1}\nERR:\n{2}".format(cmd, out, err))
    return out
def last_function(innosetup, folders, verbose=False, fLOG=print):
    """
    applies last modifications to the setup

    @param          innosetup   innosetup script which defines the setup
    @param          folders     dictionary with keys *workspace*, *python*, *tools*, *build*, *docs*
    @param          verbose     verbose
    @param          fLOG        logging function
    """
    from pymyinstall.win_installer.win_setup_r import r_run_script

    work = folders["workspace"]
    python = folders["python"]
    tools = folders["tools"]
    build = os.path.join(folders["build"], "custom_ensae_teaching_cs")
    docs = os.path.join(work, "docs")
    logs = folders["logs"]
    this = os.path.abspath(os.path.dirname(__file__))
    doc_annee = [
        os.path.join(this, "..", "..", "..", "_doc", "notebooks", sub)
        for sub in ["td1a", "td2a", "td3a", "1a", "2a", "expose"]
    ]

    if not os.path.exists(build):
        os.mkdir(build)

    # folders
    fLOG("folders:", folders)
    fLOG("innosetup:", innosetup)

    # docs
    fLOG("--- cleaning creating folder", docs)
    if os.path.exists(docs):
        remove_folder(docs)
    os.mkdir(docs)

    # RSS
    fLOG("--- update rss.list.xml")
    # flake8 is no happy with: from .. import __blog__
    __blog__ = os.path.abspath(
        os.path.join(os.path.dirname(__file__), "..", "rss_teachings.xml"))

    rssfile = os.path.join(folders["config"], "rss_list.xml")
    shutil.copy(__blog__, rssfile)

    # R_install
    fLOG("--- R install")
    r_script = os.path.join(os.path.dirname(__file__), "R_install.r")
    if os.path.exists(r_script):
        r_run_script(os.path.join(tools, "R"), r_script,
                     os.path.join(logs, "r_ensae_teaching_cs.install.log.txt"))

    # documentation
    fLOG("--- documentation, TDs +++")
    for dist in doc_annee:
        end = os.path.split(dist)[-1]
        to = os.path.join(docs, end)
        if not os.path.exists(to):
            os.mkdir(to)
        for ipy in os.listdir(dist):
            if ipy.endswith("ipynb"):
                if verbose:
                    fLOG("copy ", ipy)
                full = os.path.join(dist, ipy)
                shutil.copy(full, to)

    # others packages not from Microsoft
    from pymyinstall import ModuleInstall
    from pymyinstall.win_installer.win_packages import win_install_package_other_python, is_package_installed

    # modules
    modules = [
        ModuleInstall("pyquickhelper", "pip"),
        ModuleInstall("pyensae", "pip"),
        ModuleInstall("pyrsslocal", "pip"),
        ModuleInstall("code_beatrix", "pip"),
        ModuleInstall("pymmails", "pip"),
        ModuleInstall("pymyinstall", "pip"),
        ModuleInstall("ensae_teaching_cs", "pip"),
        ModuleInstall("actuariat_python", "pip"),
    ]

    # new packages
    fLOG("--- download new packages")
    pack = []
    for mod in modules:
        mname = mod.mname if mod.mname is not None else mod.name
        if not is_package_installed(python, [mod.name, mname]):
            fLOG("download:", mod.name)
            p = mod.download(temp_folder=build)
            pack.append((p, mod))

    # install packages
    fLOG("--- install packages")
    for p, mod in pack:
        fLOG("install", os.path.split(p)[-1])
        win_install_package_other_python(python, p, verbose=verbose, fLOG=fLOG)

    # remove unnecessary folders
    fLOG("--- remove too big folfers")
    to_remove = []
    for rem in to_remove:
        sub = os.path.join(python, rem)
        if os.path.exists(sub):
            fLOG("remove", sub)
            remove_folder(sub)

    # modifies the setup
    fLOG("--- modifies the setup")
    with open(innosetup, "r", encoding="utf8") as f:
        content = f.read()
    with open(innosetup, "w", encoding="utf8") as f:
        f.write(content)
def update_pip(python_path=None, fLOG=print):
    """
    update pip for a specific distribution

    @param      python_path     python path (or sys.executable if None)
    @param      fLOG            logging function
    @return                     output

    The command ``python -m pip install -U pip`` or
    ``pip install --upgrade pip`` might fails on Windows due to very long paths
    (see `Upgrading pip fails on Windows when install path is too long <https://github.com/pypa/pip/issues/3055>`_).
    If that happens,
    assuming the module *pymyinstall* was installed with pip, we can now remove
    *pip* and use *get_pip.py* instead. This part requires *pyquickhelper*.

    We try the url `bootstrap.pypa.io/get-pip.py <https://bootstrap.pypa.io/get-pip.py>`_ first
    then a local copy.
    """
    if python_path is None:
        python_path = sys.executable
    else:
        python_path = os.path.join(python_path, "python")
    cmd = python_path + " -m pip install -U pip"
    out, err = run_cmd(cmd, wait=True, fLOG=fLOG)
    if err and len(err) > 0:
        if ("FileNotFoundError" in err or "No module named pip.__main__" in err) \
           and sys.platform.startswith("win"):
            from pyquickhelper.filehelper import remove_folder
            # we try to remove pip and to install it again
            # it might be due to long path on Windows
            pack = os.path.join(os.path.dirname(python_path), "Lib",
                                "site-packages")
            if not os.path.exists(pack):
                raise FileNotFoundError(pack)
            fpip = os.path.join(pack, "pip")
            if os.path.exists(fpip):
                # remove the folder
                fLOG("  remove folder", fpip)
                remove_folder(fpip)

            pip_ = [_ for _ in os.listdir(pack) if _.startswith("pip-")]
            if len(pip_) > 0:
                for _ in pip_:
                    fp = os.path.join(pack, _)
                    fLOG("  remove folder", fp)
                    remove_folder(fpip)

            url = "https://bootstrap.pypa.io/get-pip.py"
            cmd = python_path + " " + url
            out, err = run_cmd(cmd, wait=True)
            if err and len(err) > 0:
                get_pip = os.path.abspath(
                    os.path.join(os.path.dirname(__file__), "get_pip.py"))
                if not os.path.exists(get_pip):
                    raise FileNotFoundError(get_pip)
                cmd = python_path + " " + get_pip
                out, err = run_cmd(cmd, wait=True)
                if err and len(err) > 0:
                    raise UpdatePipError(
                        "unable to update pip with get_pip.\nCMD:\n{0}\nOUT:\n{1}\nERR-E:\n{2}"
                        .format(cmd, out, err))
        else:
            lines = err.split("\n")
            keep = []
            for line in lines:
                if not line.startswith(
                        " ") and "RuntimeWarning: Config variable" not in line:
                    keep.append(line)
            if len(keep) > 0:
                raise UpdatePipError(
                    "unable to update pip.\nCMD:\n{0}\nOUT:\n{1}\nERR-F:\n{2}".
                    format(cmd, out, err))
    return out