From de0e66a94a87e148b6a5a19f31e302b1cbdebcbf Mon Sep 17 00:00:00 2001 From: Mahmoud-Emad Date: Mon, 10 Feb 2025 12:08:49 +0000 Subject: [PATCH] feat: Improved the python installer, added an example --- examples/installers/python.vsh | 7 +++ lib/installers/lang/python/python_actions.v | 66 +++++++++----------- lib/installers/lang/python/python_factory_.v | 15 ++--- lib/installers/lang/python/python_model.v | 21 +++++-- 4 files changed, 57 insertions(+), 52 deletions(-) create mode 100755 examples/installers/python.vsh diff --git a/examples/installers/python.vsh b/examples/installers/python.vsh new file mode 100755 index 00000000..42bb041a --- /dev/null +++ b/examples/installers/python.vsh @@ -0,0 +1,7 @@ +#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run + +import freeflowuniverse.herolib.installers.lang.python as python_module + +mut python_installer := python_module.get()! +// python_installer.install()! +python_installer.destroy()! diff --git a/lib/installers/lang/python/python_actions.v b/lib/installers/lang/python/python_actions.v index c246d0dc..9d65f6ec 100644 --- a/lib/installers/lang/python/python_actions.v +++ b/lib/installers/lang/python/python_actions.v @@ -2,27 +2,31 @@ module python import freeflowuniverse.herolib.osal import freeflowuniverse.herolib.ui.console -import freeflowuniverse.herolib.core.texttools import freeflowuniverse.herolib.core -import freeflowuniverse.herolib.installers.ulist import freeflowuniverse.herolib.installers.base +import freeflowuniverse.herolib.core.texttools +import freeflowuniverse.herolib.installers.ulist import os + //////////////////// following actions are not specific to instance of the object // checks if a certain version or above is installed -fn installed_() !bool { +fn installed() !bool { res := os.execute('python3 --version') if res.exit_code != 0 { return false } + r := res.output.split_into_lines().filter(it.trim_space().len > 0) if r.len != 1 { return error("couldn't parse pnpm version.\n${res.output}") } + if texttools.version(r[0].all_after_first('ython')) >= texttools.version(version) { return true } + return false } @@ -32,10 +36,15 @@ fn ulist_get() !ulist.UList { return ulist.UList{} } -fn upload_() ! { +// uploads to S3 server if configured +fn upload() ! { + // installers.upload( + // cmdname: 'python' + // source: '${gitpath}/target/x86_64-unknown-linux-musl/release/python' + // )! } -fn install_() ! { +fn install() ! { console.print_header('install python') base.install()! @@ -44,7 +53,7 @@ fn install_() ! { if pl == .arch { osal.package_install('python-pipx,sqlite')! } else if pl == .ubuntu { - osal.package_install('python-pipx,sqlite')! + osal.package_install('pipx,sqlite')! } else if pl == .osx { osal.package_install('pipx,sqlite')! } else { @@ -53,36 +62,17 @@ fn install_() ! { osal.execute_silent('pipx install uv')! } -fn destroy_() ! { - panic('implement') - - // mut systemdfactory := systemd.new()! - // systemdfactory.destroy("zinit")! - - // osal.process_kill_recursive(name:'zinit')! - // osal.cmd_delete('zinit')! - - // osal.package_remove(' - // podman - // conmon - // buildah - // skopeo - // runc - // ')! - - // //will remove all paths where go/bin is found - // osal.profile_path_add_remove(paths2delete:"go/bin")! - - // osal.rm(" - // podman - // conmon - // buildah - // skopeo - // runc - // /var/lib/containers - // /var/lib/podman - // /var/lib/buildah - // /tmp/podman - // /tmp/conmon - // ")! +fn destroy() ! { + console.print_header('destroy python') + osal.package_remove('python3')! + pl := core.platform()! + if pl == .arch { + osal.package_remove('pipx,sqlite')! + } else if pl == .ubuntu { + osal.package_remove('pipx,sqlite')! + } else if pl == .osx { + osal.package_remove('pipx,sqlite')! + } else { + return error('only support osx, arch & ubuntu.') + } } diff --git a/lib/installers/lang/python/python_factory_.v b/lib/installers/lang/python/python_factory_.v index 32845da9..548f726c 100644 --- a/lib/installers/lang/python/python_factory_.v +++ b/lib/installers/lang/python/python_factory_.v @@ -1,12 +1,9 @@ module python -import freeflowuniverse.herolib.core.base import freeflowuniverse.herolib.core.playbook import freeflowuniverse.herolib.ui.console -import freeflowuniverse.herolib.data.paramsparser import freeflowuniverse.herolib.sysadmin.startupmanager import freeflowuniverse.herolib.osal.zinit -import time __global ( python_global map[string]&Python @@ -45,11 +42,11 @@ pub fn play(args_ PlayArgs) ! { reset := p.get_default_false('reset') if other_action.name == 'destroy' || reset { console.print_debug('install action python.destroy') - // destroy()! + destroy()! } if other_action.name == 'install' { console.print_debug('install action python.install') - // install()! + install()! } } } @@ -89,14 +86,14 @@ pub mut: pub fn (mut self Python) install(args InstallArgs) ! { switch(self.name) - // if args.reset || (!installed()!) { - // install()! - // } + if args.reset || (!installed()!) { + install()! + } } pub fn (mut self Python) destroy() ! { switch(self.name) - // destroy()! + destroy()! } // switch instance to be used for python diff --git a/lib/installers/lang/python/python_model.v b/lib/installers/lang/python/python_model.v index 1054b7ef..3d4ec4d9 100644 --- a/lib/installers/lang/python/python_model.v +++ b/lib/installers/lang/python/python_model.v @@ -1,6 +1,7 @@ module python import freeflowuniverse.herolib.data.paramsparser +import freeflowuniverse.herolib.data.encoderhero import os pub const version = '3.12.0' @@ -14,14 +15,24 @@ pub mut: name string = 'default' } -fn obj_init(obj_ Python) !Python { - // never call get here, only thing we can do here is work on object itself - mut obj := obj_ - panic('implement') - return obj +// your checking & initialization code if needed +fn obj_init(mycfg_ Python) !Python { + mut mycfg := mycfg_ + return mycfg } // called before start if done fn configure() ! { // mut installer := get()! } + +/////////////NORMALLY NO NEED TO TOUCH + +pub fn heroscript_dumps(obj Python) !string { + return encoderhero.encode[Python](obj)! +} + +pub fn heroscript_loads(heroscript string) !Python { + mut obj := encoderhero.decode[Python](heroscript)! + return obj +}