feat: fixed file management

This commit is contained in:
Didier Slof 2023-07-13 16:50:49 +02:00
parent 7964bb84fd
commit 68bb6822ed
Signed by: didier
GPG key ID: 01E71F18AA4398E5
12 changed files with 148 additions and 43 deletions

View file

@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="CPP_MODULE" version="4">
<component name="FacetManager">
<facet type="Python" name="Python facet">
<configuration sdkName="Python 3.11" />
</facet>
</component>
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/bootpkg/src" isTestSource="false" />
@ -13,5 +18,6 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Python 3.11 interpreter library" level="application" />
</component>
</module>

View file

@ -8,5 +8,8 @@ edition = "2021"
[dependencies]
manifest = { path = "../manifest" }
pkgfile = { path = "../pkgfile" }
toml = { version = "0.7.6", features = [ "parse" ] }
serde = { version = "1.0.171", features = ["derive"] }
regex = "1.9.1"
reqwest = { version = "0.11.18", features = ["blocking"] }
uuid = { version = "1.4.0", features = ["serde", "v4"] }

View file

@ -1,7 +1,7 @@
pub enum Command {
Strap,
Unpack,
None
None,
}
impl From<String> for Command {
@ -9,11 +9,10 @@ impl From<String> for Command {
match value.to_lowercase().as_str() {
"strap" => Command::Strap,
"unpack" => Command::Unpack,
_ => Command::None
_ => Command::None,
}
}
}
pub struct Args {
pub command: Command,
pub args: Vec<String>,
@ -23,7 +22,7 @@ impl From<Vec<String>> for Args {
fn from(value: Vec<String>) -> Self {
Args {
command: Command::from(value[0].to_owned()),
args: value[1..].to_owned()
args: value[1..].to_owned(),
}
}
}

View file

@ -1,10 +1,15 @@
use std::env;
use std::process::exit;
use pkgfile::PKGFile;
use crate::args::{Args, Command};
use pkgfile::PKGFile;
use std::env;
use std::env::temp_dir;
use std::fs::{create_dir, create_dir_all, File};
use std::io::Write;
use std::path::PathBuf;
use std::process::exit;
use uuid::Uuid;
mod prelude;
mod args;
mod prelude;
fn main() {
let args = Args::from(env::args().collect::<Vec<String>>()[1..].to_owned());
@ -33,7 +38,7 @@ fn main() {
}
};
let p = match PKGFile::try_from(data) {
let pkg = match PKGFile::try_from(data) {
Ok(p) => p,
Err(_) => {
println!("!! Could not interpret PKGFile...");
@ -41,8 +46,33 @@ fn main() {
}
};
},
Command::Unpack => todo!(),
let mani = prelude::mani_from_str(&pkg.manifest);
println!("!! Package: {}", &mani.package.name);
if !mani.valid() {
println!("!!! Manifest is not valid.");
exit(1);
}
let mut tmp_dir = temp_dir();
tmp_dir.push(format!("{}", Uuid::new_v4()));
create_dir_all(&tmp_dir)
.expect("Could not create tmp dir.");
{
let mut file = File::create(format!("{}/pkgtar", tmp_dir.to_str().unwrap()))
.expect("Could not create tmp pkgtar file");
file.write(&pkg.data)
.expect("Could not write pkgtar to tmp file");
}
// TODO: untar
// TODO: cd into pkg
// TODO: run bootstrap commands
}
Command::Unpack => {}
_ => {
println!("Unsupported command, allowed commands: strap/unpack.");
exit(1);

View file

@ -1,22 +1,33 @@
use std::collections::HashMap;
use manifest::Manifest;
use manifest::{self, Manifest};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, str::FromStr};
use toml;
#[derive(Serialize, Deserialize)]
pub struct PKGR {
pub bootstrap: Option<Bootstrap>,
}
pub struct Bootstrap {}
pub fn def_manifest() {
Manifest::<Option<PKGR>> {
package: manifest::package::Package::default(),
bin: None,
build: None,
dependencies: HashMap::default(),
fs: manifest::fs::FS {
config: None,
data: None,
},
pkgr: Some(PKGR { bootstrap: None }),
};
#[derive(Serialize, Deserialize)]
pub struct Bootstrap {
check_installed_commands: Vec<String>,
commands: Vec<String>
}
#[derive(Serialize, Deserialize)]
pub struct BadManifest {
pkgr: Option<PKGR>,
}
pub fn mani_from_str(s: &str) -> Manifest<Option<PKGR>> {
let mani = Manifest::from_str(s).unwrap();
let bmani = toml::from_str::<BadManifest>(s).unwrap();
Manifest::<Option<PKGR>> {
package: mani.package,
dependencies: mani.dependencies,
fs: mani.fs,
bin: mani.bin,
build: mani.build,
pkgr: bmani.pkgr
}
}

3
docs/pkgr/flow.md Normal file
View file

@ -0,0 +1,3 @@
# Install flow
pkgr keeps track of installed files and therefore we have a CTREE.

View file

@ -1,7 +1,8 @@
use std::collections::HashMap;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
pub struct Bin {
pub root: String,
pub checksums: HashMap<String, String>
}

View file

@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::{collections::HashMap, str::FromStr};
pub mod package;
pub mod fs;
@ -18,6 +18,22 @@ pub struct Manifest<P = Option<pkgr::PKGR>> {
pub pkgr: P
}
impl<T> Manifest<T> {
pub fn valid(&self) -> bool {
if self.bin.is_none() && self.build.is_none() {
return false
}
true
}
}
impl FromStr for Manifest {
type Err = toml::de::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
toml::from_str(s)
}
}
impl Default for Manifest {
fn default() -> Self {
Manifest {

22
mock-pkg.py Normal file
View file

@ -0,0 +1,22 @@
import tarfile, os
with open("./package.toml", mode='r') as mani:
data = mani.read()
with open("./mock.pkg", mode='wb') as pkg:
print("building header...")
pkg.write(bytes([0x01, (len(data) >> 8) & 0xFF, len(data) & 0xFF]))
print("writing manifest into pkg...")
pkg.write(data.encode("utf-8"))
with tarfile.TarFile("/tmp/pkgtar", 'w') as pkgtar:
for root, dirs, files in os.walk("."):
for file in files:
print(f"\33[2Kadd: {file}", end="\r", flush=True)
pkgtar.add(os.path.join(root, file))
with open("/tmp/pkgtar", 'rb') as pkgtar:
print("appending pkgtar to pkg...")
pkg.write(pkgtar.read())
print("deleting /tmp/pkgtar...")
os.unlink("/tmp/pkgtar")
print("closing write stream")
pkg.close()

View file

@ -18,10 +18,7 @@ version = 1 # this can automatically be incremented when publishing by running `
# previous version will lose this tag
# - oldest
# assigned to the first version.
tags = [
"prod",
"pkgr-spec-1"
]
tags = [ "prod", "pkgr-spec-1" ]
## package.type
# Supported types:
@ -39,7 +36,7 @@ arch = "x86_64" # this is automatically filled by `pkgr publish ...`
# you may use the following syntax
# "<version/tag>" or "<version/tag>,<version/tag>,..."
[dependencies]
example = { arch = "x86_64", tags = [ "stable" ], version = "v1" }
#example = { arch = "x86_64", tags = [ "stable" ], version = "v1" }
exnmbr2 = "v1,stable"
## fs
@ -97,14 +94,12 @@ base = "latest,stable" # selected by default
# This exists so that packager is easy to install on anything!
# and only 1 release channel for pkgr
[pkgr.bootstrap]
# any non-zero = installed
check_installed_commands = [
"/scripts/check_installed"
]
## any non-zero = installed
check_installed_commands = [ "/scripts/check_installed" ]
# any non-zero = fail
commands = [
"/scripts/bootstrap/download_latest @version /tmp/pkgr.pkg",
"/scripts/bootstrap/dirty_install /tmp/pkgr.pkg",
"/scripts/bootstrap/check_install"
"/scripts/bootstrap/download_latest @version /tmp/pkgr.pkg",
"/scripts/bootstrap/dirty_install /tmp/pkgr.pkg",
"/scripts/bootstrap/check_install"
]

View file

@ -0,0 +1,7 @@
#!/bin/sh
fetch_latest_version() {
}
download_file() {
}

View file

@ -20,17 +20,29 @@ impl TryFrom<Vec<u8>> for PKGFile {
Ok(PKGFile {
manifest: match String::from_utf8(
value[
3..(manifest_size as usize)
3..(manifest_size as usize + 3)
].to_vec()
)
{
Ok(s) => s,
_ => return Err(())
},
data: value[(manifest_size as usize)..].to_vec(),
data: value[(manifest_size as usize + 3)..].to_vec(),
})
}
_ => Err(())
}
}
}
impl Into<Vec<u8>> for PKGFile {
fn into(self) -> Vec<u8> {
let mut bytes = vec![0x01];
let manifest_bytes = self.manifest.into_bytes();
bytes.push((manifest_bytes.len() >> 8) as u8);
bytes.push((manifest_bytes.len() | 0xFF) as u8);
bytes.extend(manifest_bytes);
bytes.extend(self.data);
bytes
}
}