def _install(self, target, source): wrapper = PICKLEY_WRAPPER if self.package_name == system.PICKLEY else GENERIC_WRAPPER contents = wrapper.lstrip().format( hook=self.hook, bg=self.bg, name=runez.quoted(self.package_name), pickley=runez.quoted(system.SETTINGS.base.full_path( system.PICKLEY)), source=runez.quoted(source), ) runez.write(target, contents) runez.make_executable(target)
def add_representation(result, data, indent=""): """ :param list result: Where to add lines representing 'data' :param dict|list|str data: Data to represent :param str indent: Indentation to use """ if not data: return if isinstance(data, list): for item in data: result.append("%s- %s" % (indent, short(item))) return if isinstance(data, dict): for key, value in sorted(data.items()): if isinstance(value, list): brief = runez.quoted(value, delimiter=", ") if len(brief) < REPRESENTATION_WIDTH: result.append("%s%s: [%s]" % (indent, short(key), brief)) continue if isinstance(value, (dict, list)): result.append("%s%s:" % (indent, short(key))) add_representation(result, value, indent=" %s" % indent) else: result.append("%s%s: %s" % (indent, short(key), short(value))) return result.append("%s- %s" % (indent, short(data)))
def __enter__(self): """Acquire lock""" if CFG.base: runez.Anchored.add(CFG.base.path) cutoff = time.time() + self.give_up holder_args = self._locked_by() while holder_args: if time.time() >= cutoff: lock = runez.bold(runez.short(self.lock_path)) holder_args = runez.bold(holder_args) raise SoftLockException( "Can't grab lock %s, giving up\nIt is being held by: pickley %s" % (lock, holder_args)) time.sleep(1) holder_args = self._locked_by() # We got the soft lock if runez.DRYRUN: print("Would acquire %s" % runez.short(self.lock_path)) else: runez.log.trace("Acquired %s" % runez.short(self.lock_path)) runez.write(self.lock_path, "%s\n%s\n" % (os.getpid(), runez.quoted(sys.argv[1:])), logger=False) self.pspec.resolve() return self
def finalize(self): """Run sanity check and/or symlinks, and return a report""" with runez.Anchored(self.folder, CFG.base.path): runez.ensure_folder(CFG.base.path, clean=True, logger=False) dist_folder = runez.resolved_path(self.dist) exes = PACKAGER.package(self.pspec, CFG.base.path, dist_folder, self.requirements, self.compile) if exes: report = PrettyTable( ["Packaged executable", self.sanity_check], border=self.border) report.header.style = "bold" if not self.sanity_check: report.header[1].shown = False for exe in exes: exe_info = self.validate_sanity_check( exe, self.sanity_check) report.add_row(runez.quoted(exe), exe_info) if self.symlink and exe: self.symlink.apply(exe) if not self.compile and not runez.DRYRUN: clean_compiled_artifacts(dist_folder) return report
def check_install_from_pypi(cli, delivery, package, simulate_version=None): cli.run("--debug", "-d%s" % delivery, "install", package) assert cli.succeeded assert cli.match("Installed %s" % package) assert runez.is_executable(package) m = TrackedManifest.from_file(dot_meta("%s/.manifest.json" % package)) assert str(m) assert m.entrypoints[package] assert m.install_info.args == runez.quoted(cli.args) assert m.install_info.timestamp assert m.install_info.vpickley == __version__ assert m.settings.delivery == delivery assert m.settings.python assert m.version r = runez.run(package, "--version") assert r.succeeded cli.expect_success("--debug auto-upgrade %s" % package, "Skipping auto-upgrade, checked recently") cli.expect_success("install %s" % package, "is already installed") cli.expect_success("check", "is installed") cli.expect_success("list", package) cli.expect_success("upgrade", "is already up-to-date") if simulate_version: m.version = simulate_version runez.save_json(m.to_dict(), dot_meta("%s/.manifest.json" % package)) cli.expect_success( "check", "v%s installed, can be upgraded to" % simulate_version)
def package(build, dist, symlink, relocatable, sanity_check, folder): """ Package a project from source checkout """ build = runez.resolved_path(build) root = None target_dist = dist if target_dist.startswith("root/"): # Special case: we're targeting 'root/...' probably for a debian, use target in that case to avoid venv relocation issues target = target_dist[4:] if os.path.isdir(target): root = target_dist[:4] target_dist = target LOG.debug("debian mode: %s -> %s", dist, target) folder = runez.resolved_path(folder) if not os.path.isdir(folder): sys.exit("Folder %s does not exist" % short(folder)) system.SETTINGS.set_base(build) setup_py = os.path.join(folder, "setup.py") if not os.path.exists(setup_py): sys.exit("No setup.py in %s" % short(folder)) with runez.CurrentFolder(folder): # Some setup.py's assume their working folder is the folder where they're in result = system.run_python(setup_py, "--name", fatal=False, dryrun=False) name = result.output if result.failed or not name: sys.exit("Could not determine package name from %s" % short(setup_py)) package_spec = system.PackageSpec(name) runez.Anchored.add(folder) p = PACKAGERS.resolved(package_spec) p.build_folder = build p.dist_folder = runez.resolved_path(target_dist) p.relocatable = relocatable p.source_folder = folder p.package() p.create_symlinks(symlink, root=root) p.sanity_check(sanity_check) if p.executables: overview = "produced: %s" % runez.quoted(p.executables) else: overview = "package has no entry-points" print("Packaged %s successfully, %s" % (short(folder), overview)) runez.Anchored.pop(folder)
def test_quoted(): assert runez.quoted(None) is None assert runez.quoted("") == "" assert runez.quoted(" ") == '" "' assert runez.quoted('"') == '"' assert runez.quoted("a b") == '"a b"' assert runez.quoted('a="b"') == 'a="b"' assert runez.quoted('foo a="b"') == """'foo a="b"'"""
def test_path_resolution(temp_folder): assert runez.resolved_path(None) is None assert runez.resolved_path("some-file") == os.path.join( temp_folder, "some-file") assert runez.resolved_path("some-file", base="bar") == os.path.join( temp_folder, "bar", "some-file") assert runez.quoted( ["ls", os.path.join(temp_folder, "some-file") + " bar", "-a", " foo "]) == 'ls "some-file bar" -a " foo "'
def _install(self, pspec, target, source): pickley = pspec.cfg.base.full_path(PICKLEY) if pspec.dashed == PICKLEY: wrapper = PICKLEY_WRAPPER else: wrapper = GENERIC_WRAPPER if runez.DEV.project_folder and not os.path.exists(pickley): # We're running from development venv pickley = pspec.cfg.program_path contents = wrapper.lstrip().format( hook=self.hook, bg=self.bg, name=runez.quoted(pspec.dashed, adapter=None), pickley=runez.quoted(pickley, adapter=None), source=runez.quoted(source, adapter=None), ) runez.delete(target, logger=False) runez.write(target, contents, logger=False) runez.make_executable(target, logger=False)
def current(cls): return cls(runez.quoted(sys.argv[1:]), datetime.now(), __version__)
def test_quoted(): assert runez.quoted(None) == "None" assert runez.quoted("") == "" assert runez.quoted(" ") == '" "' assert runez.quoted(" ", stringify=runez.short) == "" assert runez.quoted('"') == '"' assert runez.quoted("a b") == '"a b"' assert runez.quoted('a="b"') == 'a="b"' assert runez.quoted('foo a="b"') == """'foo a="b"'""" assert runez.quoted([]) == "" assert runez.quoted([0, 1, 2]) == "0 1 2" assert runez.quoted(["foo", {}, 0, [1, 2], { 3: 4 }, 5]) == 'foo {} 0 1 2 "{3: 4}" 5'