예제 #1
0
    def _install(self, topdir, taskname, targs):

        dirnames = os.listdir(topdir)

        if "setup.py" not in dirnames:
            raise UsageError("'setup.py' is not found.'")

        if targs.name and targs.name != taskname:
            new_taskname = targs.name
            os.rename(os.path.join(topdir, taskname),
                      os.path.join(topdir, new_taskname))
            taskname = new_taskname
            setup = os.path.join(topdir, "setup.py")
            oldsetup = os.path.join(topdir, "setup.py.old")
            shutil.move(setup, oldsetup)
            prefix = '    __taskname__ = "'
            with open(setup, "w") as fw:
                with open(oldsetup, "r") as fr:
                    for line in fr:
                        if line.startswith(prefix):
                            fw.write(prefix + taskname + '"\n')
                        else:
                            fw.write(line)
            os.remove(oldsetup)

        retval, stdout, stderr = system(sys.executable + " setup.py sdist"
                                        " --formats=tar",
                                        cwd=topdir)

        if retval != 0:
            raise InternalError(stderr)

        distdir = os.path.join(topdir, "dist")
        sdist = None

        for dname in os.listdir(distdir):
            if dname.endswith(".tar"):
                sdist = dname
                break

        if sdist is None:
            raise InternalError("'sdist' file is not found: %s" % taskname)

        if is_venv():
            retval, stdout, stderr = system(get_pip() + " install " + sdist,
                                            cwd=distdir)
            # system("python setup.py install", cwd=topdir)
        else:
            retval, stdout, stderr = system(get_pip() +
                                            " install %s --user" % sdist,
                                            cwd=distdir)
            # system("python setup.py install --user", cwd=topdir)

        if retval == 0:
            # TODO: print installed version with some progress remarks
            print("'%s' task is installed successfully." % taskname)

        else:
            raise InternalError(stderr)
예제 #2
0
    def perform(self, targs):

        if targs.task:
            if len(targs.task) > 1:
                raise UsageError(
                    "'uninstall' task only one task: %s." % targs.task
                )

            taskname = targs.task[0]

        elif self.subargv:
            if "--" in self.subargv:
                raise UsageError(
                    "'uninstall' task requires only one sub-task "
                    "to uninstall, but found multiple sub-tasks: '%s'" %
                    " ".join(self.subargv)
                )

            taskname = self.subargv[0]
            del self.subargv[:]

        else:
            raise UsageError(
                "'uninstall' task requires one sub-task to uninstall."
            )

        for ep in pkg_resources.iter_entry_points(group='pyloco.task'):
            if ep.name == taskname:
                retval, sout, serr = system(
                    get_pip() + " uninstall -y " + ep.dist.project_name
                )

                if retval == 0:
                    print("'%s' task is uninstalled successfully." % taskname)

                else:
                    print("Uninstallation of '%s' task was failed: %s" %
                          (taskname, serr))

                return retval

        raise UsageError("'%s' is not found." % taskname)
예제 #3
0
    def perform(self, targs):

        if targs.task:
            taskpath = targs.task

        elif self.subargv:
            if "--" in self.subargv:
                raise UsageError(
                    "'install' task requires only one sub-task "
                    "to install, but found multiple sub-tasks: '%s'" %
                    " ".join(self.subargv)
                )

            taskpath = self.subargv[0]

        else:
            raise UsageError(
                "'install' task requires one sub-task to install."
            )

        if os.path.exists(taskpath):

            if zipfile.is_zipfile(taskpath):
                if taskpath.endswith(".plz"):

                    with _Unpack(taskpath) as (taskname, tmpdir):
                        topdir = os.path.join(tmpdir, taskname)
                        dirnames = os.listdir(topdir)

                        if taskname not in dirnames:
                            raise UsageError(
                                "'%s' is not a plz file." % taskpath
                            )

                        self._install(topdir, taskname, targs)
                else:
                    raise UsageError("Unknown file format: %s" % taskpath)
            else:
                with _Pack(self, taskpath) as (taskname, tmpdir):
                    topdir = os.path.join(tmpdir, taskname)
                    self._install(topdir, taskname, targs)
        else:

            # TODO?: ask user permission to install

            installed = False

            try:
                distname = taskpath.replace("_", "-")
                url = "%s/%s/json" % (repo_check, distname)

                if not PY3:
                    url = strencode(url)

                if json.load(urlopen(url)):
                    prefix = (" install -i %s --extra-index-url %s " %
                              (repo_install, extra_repo_install))
                    args = []

                    if targs.user or not (is_venv() or "CONDA_DEFAULT_ENV" in os.environ):
                        args.append("--user")

                    if targs.upgrade:
                        args.append("-U")

                    ret, _, _ = system(get_pip() + prefix + " ".join(args) +
                                       " " + distname)

                    if ret == 0:
                        print("'%s' task is installed successfully." % taskpath)
                        installed = True

            except HTTPError:
                pass

            if not installed:
                url = urlparse(taskpath)

                if url.netloc or url.scheme:
                    import pdb; pdb.set_trace()     # noqa: E702

                else:
                    raise UsageError("Task '%s' is not found." % taskpath)

        del self.subargv[:]