def perform(self, targs): if targs.task: taskpath = targs.task elif self.subargv: if "--" in self.subargv: raise UsageError( "'upload' task requires only one sub-task " "to upload, but found multiple sub-tasks: '%s'" % " ".join(self.subargv) ) taskpath = self.subargv[0] else: raise UsageError( "'upload' task requires one sub-task to upload." ) with _Pack(self, taskpath, distname=targs.name) as (taskname, tmpdir): cwd = os.getcwd() os.chdir(os.path.join(tmpdir, taskname)) ret, sout, serr = system(sys.executable + " setup.py sdist") ret, sout, serr = system(sys.executable + " setup.py bdist_wheel --universal") ret = isystem("twine upload --repository-url %s dist/*" % targs.repository) os.chdir(cwd) if ret == 0: print("'%s' task is uploaded successfully." % taskpath) else: print("Uploading of '%s' task is failed." % taskpath)
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)
def perform(self, targs): if os.path.exists(targs.command): raise UsageError("Specified command '%s' already exists." % targs.command) # get OS and select shell script type if OS == "windows": import pdb; pdb.set_trace() else: if len(self.subargv) < 1: raise UsageError("No target task is specified.") if not os.path.exists(self.subargv[0]): raise UsageError("Target task does not exist: %s" % self.subargv[0]) self.subargv[0] = os.path.abspath(os.path.realpath(self.subargv[0])) with open(targs.command, "w") as f: retval, stdout, _ = system("which bash") if retval == 0: f.write("#!" + stdout + "\n") f.write("%s %s $*" % (sys.argv[0], " ".join(self.subargv))) os.chmod(targs.command, 0o744)
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)
def run_shell_command(self, cmd, *vargs, **kwargs): return system(cmd)
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[:]
def __init__(self, task, taskpath, distname=None): from pyloco.task import load_taskclass parent = task.get_proxy() argv = task.subargv[1:] _dirname, _basename = os.path.split(taskpath) _base, _ext = os.path.splitext(_basename) # consumes all subargv del task.subargv[:] task_class, argv, subargv, objs = load_taskclass(taskpath, argv, []) if task_class is None: raise UsageError("ERROR: Task '%s' is not loaded. " " Please check task-path." % taskpath) task = task_class(parent) if task is None: raise UsageError("ERROR: Task '%s' is not created." % taskpath) task._env.update(objs) from pyloco.plxtask import PlXTask if task_class is PlXTask: task._setup(taskpath) self.tmpdir = tempfile.mkdtemp() self.taskname = task._name_ topdir = os.path.join(self.tmpdir, self.taskname) srcdir = os.path.join(topdir, self.taskname) os.makedirs(srcdir) package_data = "" # generate __init__.py if os.path.isfile(taskpath): shutil.copy(taskpath, srcdir) clsname = task.__class__.__name__ init_extra = [] main_extra = [] if task_class is PlXTask: modname = "pyloco.plxtask" init_extra.append('plx = "%s"' % _basename) main_extra.append('plx = "%s"' % _basename) package_data = ('"%s": ["%s"]' % (self.taskname, _basename)) else: modname = "." + _base # TODO: add metadata such as __doc__ with open(os.path.join(srcdir, "__init__.py"), "w") as f: extra = "\n".join(init_extra) f.write(_taskinit_template % (modname, clsname, extra)) with open(os.path.join(srcdir, "__main__.py"), "w") as f: extra = "\n".join(main_extra) f.write(_taskmain_template % (modname, clsname, extra)) elif os.path.isdir(taskpath): import pdb; pdb.set_trace() # noqa: E702 else: raise UsageError( "Task '%s' supports packing of Python module and package." % self.taskname ) setup_kwargs = {} modules = {} mod2dist = {} dists = {} collect_modules(srcdir, modules) ret, sout, serr = system("pip list") sout = sout.replace("(", " ").replace(")", " ") dist_list = [d.split() for d in sout.split("\n")[2:] if d] for d, v in dict(d for d in dist_list if len(d) == 2).items(): ret, sout, serr = system("pip show -f " + d) for line in sout.split("\n"): if line.endswith("__init__.py"): pkgline = line.split(os.sep) if len(pkgline) == 2: mod2dist[pkgline[0].strip()] = (d, v) elif line.strip() == d + ".py": mod2dist[d] = (d, v) site = os.path.dirname(ast.__file__) for m, v in modules.items(): d = mod2dist.get(m, None) if m == "pyloco": from pyloco.manage import PylocoManager dists[m] = PylocoManager._version_ elif m == parent.get_managerattr("name"): dists[m] = parent.get_managerattr("version") elif d is None: d = sys.modules.get(m, None) if m in sys.builtin_module_names: pass else: pass # TODO: check this # moddir = os.path.dirname(d.__file__) # sitem = os.path.join(site, m) # # if moddir != site and moddir != sitem: # import pdb; pdb.set_trace() # raise InternalError( # "Distribution package for '%s' module is not " # "found." % m) else: dists[d[0]] = d[1] # TODO: copy other tasks and data files in srcdir install_requires = [] if hasattr(task, "_install_requires_"): install_requires.extend(['"%s"' % r for r in task._install_requires_]) for dname, dver in dists.items(): if dver is not None: install_requires.append('"{0}>={1}"'.format(dname, dver)) else: install_requires.append('"{}"'.format(dname)) setup_kwargs["install_requires"] = ", ".join(install_requires) # generate setup.py with open(os.path.join(topdir, "setup.py"), "w") as f: setup_kwargs["distname"] = distname if distname else self.taskname setup_kwargs["taskname"] = self.taskname setup_kwargs["version"] = getattr(task, "_version_", "0.1.0") setup_kwargs["package_data"] = package_data setup_kwargs["entry_points"] = ( '"{taskname} = {taskname}:entry_task".format(taskname=__taskname__)' ) f.write(_setup_template.format(**setup_kwargs))