snapshot 2025-04-11 16:58:39.696712
This commit is contained in:
parent
4aa14c4914
commit
b5d07f2356
892 changed files with 140027 additions and 36 deletions
|
|
@ -12,4 +12,4 @@ crate-type = ["cdylib", "rlib"]
|
|||
|
||||
[dependencies]
|
||||
byteorder = "^1"
|
||||
arrow = "^54"
|
||||
arrow = {version = "^54", features = ["ffi"]}
|
||||
|
|
|
|||
5
cbindgen.toml_bak
Normal file
5
cbindgen.toml_bak
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
language = "C"
|
||||
|
||||
[parse.expand]
|
||||
# Expand macros
|
||||
crates = ["klamath_rs_ext"]
|
||||
BIN
klamath_rs_ext/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
klamath_rs_ext/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
BIN
klamath_rs_ext/__pycache__/basic.cpython-312.pyc
Normal file
BIN
klamath_rs_ext/__pycache__/basic.cpython-312.pyc
Normal file
Binary file not shown.
1
klamath_rs_ext/libklamath_rs_ext.so
Symbolic link
1
klamath_rs_ext/libklamath_rs_ext.so
Symbolic link
|
|
@ -0,0 +1 @@
|
|||
../target/release/maturin/libklamath_rs_ext.so
|
||||
|
|
@ -20,31 +20,35 @@ use std::collections::HashMap;
|
|||
//use std::io::Write;
|
||||
|
||||
use arrow::array::{
|
||||
StructBuilder, ListBuilder, StringBuilder, ArrayBuilder, Float64Builder, BooleanBuilder,
|
||||
Int32Builder, Int16Builder, UInt32Builder, UInt8Builder,
|
||||
StructBuilder, FixedSizeListBuilder, ListBuilder, StringBuilder, ArrayBuilder, BooleanBuilder,
|
||||
Int32Builder, Int16Builder, UInt32Builder, UInt8Builder, Float64Builder,
|
||||
};
|
||||
|
||||
|
||||
pub type DListBuilder = ListBuilder<Box<dyn ArrayBuilder>>;
|
||||
pub type FListBuilder = FixedSizeListBuilder<Box<dyn ArrayBuilder>>;
|
||||
|
||||
|
||||
pub fn read_elements<'a>(input: &'a [u8], cell_builder: &mut StructBuilder, names: &mut HashMap<String, u32>) -> IResult<'a, ()> {
|
||||
let (mut input, mut header) = RecordHeader::read(input)?;
|
||||
let mut input = input;
|
||||
let (_, mut header) = RecordHeader::read(input)?; // don't consume tag
|
||||
while header.tag != records::RTAG_ENDSTR {
|
||||
match header.tag {
|
||||
records::RTAG_BOUNDARY => {(input, _) = read_boundary(input, cell_builder)?;},
|
||||
records::RTAG_PATH => {read_path(input, cell_builder)?;},
|
||||
records::RTAG_NODE => {read_boxnode(input, cell_builder, header.tag)?;},
|
||||
records::RTAG_BOX => {read_boxnode(input, cell_builder, header.tag)?;},
|
||||
records::RTAG_TEXT => {read_text(input, cell_builder)?;},
|
||||
records::RTAG_SREF => {read_ref(input, cell_builder, header.tag, names)?;},
|
||||
records::RTAG_AREF => {read_ref(input, cell_builder, header.tag, names)?;},
|
||||
(input, _) = match header.tag {
|
||||
records::RTAG_BOUNDARY => read_boundary(input, cell_builder)?,
|
||||
records::RTAG_PATH => read_path(input, cell_builder)?,
|
||||
records::RTAG_NODE => read_boxnode(input, cell_builder, header.tag)?,
|
||||
records::RTAG_BOX => read_boxnode(input, cell_builder, header.tag)?,
|
||||
records::RTAG_TEXT => read_text(input, cell_builder)?,
|
||||
records::RTAG_SREF => read_ref(input, cell_builder, header.tag, names)?,
|
||||
records::RTAG_AREF => read_ref(input, cell_builder, header.tag, names)?,
|
||||
_ => {
|
||||
// don't care, skip
|
||||
(input, _) = take_bytes(input, header.data_size)?;
|
||||
let result = take_bytes(input, header.data_size)?;
|
||||
(result.0, ())
|
||||
}
|
||||
}
|
||||
(input, header) = RecordHeader::read(input)?;
|
||||
};
|
||||
(_, header) = RecordHeader::read(input)?; // don't consume tag
|
||||
println!("{header:?}");
|
||||
}
|
||||
Ok((input, ()))
|
||||
}
|
||||
|
|
@ -299,8 +303,14 @@ pub fn read_text<'a>(input: &'a [u8], cell_builder: &mut StructBuilder) -> IResu
|
|||
|
||||
|
||||
pub fn read_ref<'a>(input: &'a [u8], cell_builder: &mut StructBuilder, tag: u16, names: &mut HashMap<String, u32>) -> IResult<'a, ()> {
|
||||
let (input, _) = match tag {
|
||||
records::RTAG_SREF => records::SREF::read(input)?,
|
||||
records::RTAG_AREF => records::AREF::read(input)?,
|
||||
_ => return fail(input, format!("Unexpected tag {:04x}", tag)),
|
||||
};
|
||||
|
||||
let is_aref = tag == records::RTAG_AREF;
|
||||
let refs_builder = cell_builder.field_builder::<DListBuilder>(7).unwrap();
|
||||
let refs_builder = cell_builder.field_builder::<DListBuilder>(2).unwrap();
|
||||
let ref_builder = refs_builder.values().as_any_mut().downcast_mut::<StructBuilder>().unwrap();
|
||||
|
||||
let mut invert_y = None;
|
||||
|
|
@ -345,6 +355,7 @@ pub fn read_ref<'a>(input: &'a [u8], cell_builder: &mut StructBuilder, tag: u16,
|
|||
return fail(input, format!("Unexpected tag {:04x}", header.tag)),
|
||||
};
|
||||
(input, header) = RecordHeader::read(input)?;
|
||||
println!("hi");
|
||||
}
|
||||
let inv_builder = ref_builder.field_builder::<BooleanBuilder>(1).unwrap();
|
||||
inv_builder.append_option(invert_y);
|
||||
|
|
|
|||
34
src/lib.rs
34
src/lib.rs
|
|
@ -6,9 +6,43 @@ pub mod records;
|
|||
pub mod elements;
|
||||
pub mod library;
|
||||
|
||||
use crate::library::read_library;
|
||||
|
||||
use byteorder::{ByteOrder, BigEndian};
|
||||
use std::mem::size_of;
|
||||
use std::ffi::{CStr, OsStr, c_char};
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
use std::str;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
use arrow::ffi::{to_ffi, FFI_ArrowArray, FFI_ArrowSchema};
|
||||
use arrow::array::Array;
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn read_path(
|
||||
cpath: *const c_char,
|
||||
arr: *mut FFI_ArrowArray,
|
||||
schema: *mut FFI_ArrowSchema,
|
||||
) {
|
||||
let cstr = unsafe { CStr::from_ptr(cpath) };
|
||||
let path: &Path;
|
||||
if cfg!(unix) {
|
||||
let osstr = OsStr::from_bytes(cstr.to_bytes());
|
||||
path = osstr.as_ref();
|
||||
} else if cfg!(windows) {
|
||||
let ustr = str::from_utf8(cstr.to_bytes()).expect("Non-UTF8 paths are not supported");
|
||||
path = ustr.as_ref();
|
||||
} else {
|
||||
panic!("Unsupported OS");
|
||||
}
|
||||
|
||||
println!("{cstr:?}");
|
||||
println!("{path:?}");
|
||||
let input = fs::read(path).expect("File read failed");
|
||||
let (_input, struct_arr) = read_library(&input).expect("Read failed");
|
||||
(*arr, *schema) = to_ffi(&struct_arr.to_data()).unwrap();
|
||||
}
|
||||
|
||||
|
||||
macro_rules! impl_i16be {
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ pub use crate::record;
|
|||
pub use crate::record::{RecordHeader, Record};
|
||||
pub use crate::records;
|
||||
pub use crate::elements;
|
||||
pub use crate::elements::{read_elements, DListBuilder};
|
||||
pub use crate::elements::{read_elements, DListBuilder, FListBuilder};
|
||||
pub use crate::basic::{IResult, OResult, take_bytes, fail};
|
||||
|
||||
use std::string::String;
|
||||
|
|
@ -19,7 +19,7 @@ use std::sync::Arc;
|
|||
use arrow::datatypes::{DataType, Field, Fields};
|
||||
use arrow::array::{
|
||||
StructBuilder, StringBuilder, UInt64Builder, UInt32Builder, Int16Builder, Float64Builder,
|
||||
FixedSizeListBuilder, StructArray,
|
||||
StructArray,
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -134,8 +134,8 @@ pub fn read_library(input: &[u8]) -> IResult<StructArray> {
|
|||
|
||||
|
||||
let text_struct_t = DataType::Struct(Fields::from(vec![
|
||||
Field::new("layer", DataType::UInt16, false),
|
||||
Field::new("dtype", DataType::UInt16, false),
|
||||
Field::new("layer", DataType::Int16, false),
|
||||
Field::new("dtype", DataType::Int16, false),
|
||||
Field::new("presentation_horiz", DataType::UInt8, true),
|
||||
Field::new("presentation_vert", DataType::UInt8, true),
|
||||
Field::new("presentation_font", DataType::UInt8, true),
|
||||
|
|
@ -156,15 +156,15 @@ pub fn read_library(input: &[u8]) -> IResult<StructArray> {
|
|||
));
|
||||
|
||||
let boundary_struct_t = DataType::Struct(Fields::from(vec![
|
||||
Field::new("layer", DataType::UInt16, false),
|
||||
Field::new("dtype", DataType::UInt16, false),
|
||||
Field::new("layer", DataType::Int16, false),
|
||||
Field::new("dtype", DataType::Int16, false),
|
||||
Field::new("xy", coords_t.clone(), false),
|
||||
Field::new("properties", property_list_t.clone(), true),
|
||||
]));
|
||||
|
||||
let path_struct_t = DataType::Struct(Fields::from(vec![
|
||||
Field::new("layer", DataType::UInt16, false),
|
||||
Field::new("dtype", DataType::UInt16, false),
|
||||
Field::new("layer", DataType::Int16, false),
|
||||
Field::new("dtype", DataType::Int16, false),
|
||||
Field::new("path_type", DataType::Int16, false),
|
||||
Field::new("extension_start", DataType::Int32, true),
|
||||
Field::new("extension_end", DataType::Int32, true),
|
||||
|
|
@ -174,8 +174,8 @@ pub fn read_library(input: &[u8]) -> IResult<StructArray> {
|
|||
]));
|
||||
|
||||
let boxnode_struct_t = DataType::Struct(Fields::from(vec![
|
||||
Field::new("layer", DataType::UInt16, false),
|
||||
Field::new("dtype", DataType::UInt16, false),
|
||||
Field::new("layer", DataType::Int16, false),
|
||||
Field::new("dtype", DataType::Int16, false),
|
||||
Field::new("xy", coords_t.clone(), false),
|
||||
Field::new("properties", property_list_t.clone(), true),
|
||||
]));
|
||||
|
|
@ -222,6 +222,10 @@ pub fn read_library(input: &[u8]) -> IResult<StructArray> {
|
|||
Field::new("texts", text_list_t, false),
|
||||
]));
|
||||
|
||||
let cells_list_t = DataType::List(Arc::new(
|
||||
Field::new_list_field(cell_struct_t, false)
|
||||
));
|
||||
|
||||
let mut lib_builder = StructBuilder::from_fields(vec![
|
||||
Field::new("meters_per_db_unit", DataType::Float64, false),
|
||||
Field::new("user_units_per_db_unit", DataType::Float64, false),
|
||||
|
|
@ -229,7 +233,7 @@ pub fn read_library(input: &[u8]) -> IResult<StructArray> {
|
|||
Field::new("mod_time", time_t.clone(), false),
|
||||
Field::new("acc_time", time_t.clone(), false),
|
||||
Field::new("cell_names", name_list_t, false),
|
||||
Field::new("cells", cell_struct_t, false),
|
||||
Field::new("cells", cells_list_t, false),
|
||||
],
|
||||
0,
|
||||
);
|
||||
|
|
@ -241,14 +245,16 @@ pub fn read_library(input: &[u8]) -> IResult<StructArray> {
|
|||
uu_builder.append_value(header.user_units_per_db_unit);
|
||||
let libname_builder = lib_builder.field_builder::<StringBuilder>(2).unwrap();
|
||||
libname_builder.append_value(String::from_utf8(header.name).unwrap());
|
||||
let mt_builder = lib_builder.field_builder::<FixedSizeListBuilder<Int16Builder>>(3).unwrap();
|
||||
mt_builder.values().append_values(&header.mod_time, &[true; 6]);
|
||||
let at_builder = lib_builder.field_builder::<FixedSizeListBuilder<Int16Builder>>(4).unwrap();
|
||||
at_builder.values().append_values(&header.acc_time, &[true; 6]);
|
||||
let mtl_builder = lib_builder.field_builder::<FListBuilder>(3).unwrap();
|
||||
let mt_builder = mtl_builder.values().as_any_mut().downcast_mut::<Int16Builder>().unwrap();
|
||||
mt_builder.append_values(&header.mod_time, &[true; 6]);
|
||||
let atl_builder = lib_builder.field_builder::<FListBuilder>(4).unwrap();
|
||||
let at_builder = atl_builder.values().as_any_mut().downcast_mut::<Int16Builder>().unwrap();
|
||||
at_builder.append_values(&header.acc_time, &[true; 6]);
|
||||
|
||||
|
||||
|
||||
let cells_builder = lib_builder.field_builder::<DListBuilder>(5).unwrap();
|
||||
let cells_builder = lib_builder.field_builder::<DListBuilder>(6).unwrap();
|
||||
|
||||
|
||||
let mut names = HashMap::<String, u32>::new();
|
||||
|
|
@ -260,6 +266,7 @@ pub fn read_library(input: &[u8]) -> IResult<StructArray> {
|
|||
let name_bytes;
|
||||
(input, name_bytes) = records::STRNAME::read(input)?;
|
||||
let name = String::from_utf8(name_bytes).unwrap();
|
||||
println!("{name}");
|
||||
|
||||
let next_id = names.len();
|
||||
let id = names.entry(name).or_insert(next_id.try_into().unwrap());
|
||||
|
|
@ -272,7 +279,9 @@ pub fn read_library(input: &[u8]) -> IResult<StructArray> {
|
|||
let offset_builder = cell_builder.field_builder::<UInt64Builder>(1).unwrap();
|
||||
offset_builder.append_value(position.try_into().unwrap());
|
||||
|
||||
println!("elem");
|
||||
(input, _) = read_elements(input, cell_builder, &mut names)?;
|
||||
println!("elem_done");
|
||||
|
||||
cells_builder.append(true);
|
||||
}
|
||||
|
|
@ -280,7 +289,7 @@ pub fn read_library(input: &[u8]) -> IResult<StructArray> {
|
|||
}
|
||||
|
||||
let mut ids: HashMap<u32, String> = names.into_iter().map(|(kk, vv)| (vv, kk)).collect();
|
||||
let names_builder = lib_builder.field_builder::<StringBuilder>(6).unwrap();
|
||||
let names_builder = lib_builder.field_builder::<StringBuilder>(5).unwrap();
|
||||
for id in 0..ids.len() {
|
||||
names_builder.append_value(ids.remove(&id.try_into().unwrap()).unwrap());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@ https://github.com/apache/arrow/blob/main/python/pyarrow/tests/test_cffi.py
|
|||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import contextlib
|
||||
import ctypes
|
||||
import pyarrow as pa
|
||||
from pyarrow.cffi import ffi
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ use crate::records;
|
|||
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RecordHeader {
|
||||
pub tag: u16,
|
||||
pub data_size: u16,
|
||||
|
|
@ -129,6 +130,7 @@ pub trait Record<RData: RecordData> {
|
|||
|
||||
fn read(input: &[u8]) -> IResult<RData::BareData<'_>> {
|
||||
let (input, size) = Self::expect_header(input)?;
|
||||
println!("{:04x}", Self::tag());
|
||||
Self::check_size(size).unwrap();
|
||||
let (input, data) = Self::read_data(input, size)?;
|
||||
Ok((input, data))
|
||||
|
|
|
|||
|
|
@ -112,7 +112,7 @@ impl Record<Int2> for HEADER {
|
|||
pub struct BGNLIB;
|
||||
impl Record<DateTimePair> for BGNLIB {
|
||||
fn tag() -> u16 { RTAG_BGNLIB }
|
||||
fn expected_size() -> Option<u16> { Some(2 * 6) }
|
||||
fn expected_size() -> Option<u16> { Some(2 * 2 * 6) }
|
||||
}
|
||||
|
||||
pub struct LIBNAME;
|
||||
|
|
@ -143,7 +143,7 @@ impl Record<DateTimePair> for BGNSTR {
|
|||
pub struct STRNAME;
|
||||
impl Record<ASCII> for STRNAME {
|
||||
fn tag() -> u16 { RTAG_STRNAME }
|
||||
fn expected_size() -> Option<u16> { Some(2 * 6) }
|
||||
fn expected_size() -> Option<u16> { None }
|
||||
}
|
||||
|
||||
pub struct ENDSTR;
|
||||
|
|
|
|||
121
src/test_basic_bak.rs
Normal file
121
src/test_basic_bak.rs
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
import struct
|
||||
|
||||
import pytest # type: ignore
|
||||
import numpy # type: ignore
|
||||
from numpy.testing import assert_array_equal # type: ignore
|
||||
|
||||
from .basic import parse_bitarray, parse_int2, parse_int4, parse_real8, parse_ascii
|
||||
from .basic import pack_bitarray, pack_int2, pack_int4, pack_real8, pack_ascii
|
||||
from .basic import decode_real8, encode_real8
|
||||
|
||||
from .basic import KlamathError
|
||||
|
||||
|
||||
def test_parse_bitarray():
|
||||
assert(parse_bitarray(b'59') == 13625)
|
||||
assert(parse_bitarray(b'\0\0') == 0)
|
||||
assert(parse_bitarray(b'\xff\xff') == 65535)
|
||||
|
||||
# 4 bytes (too long)
|
||||
with pytest.raises(KlamathError):
|
||||
parse_bitarray(b'4321')
|
||||
|
||||
# empty data
|
||||
with pytest.raises(KlamathError):
|
||||
parse_bitarray(b'')
|
||||
|
||||
|
||||
def test_parse_int2():
|
||||
assert_array_equal(parse_int2(b'59\xff\xff\0\0'), (13625, -1, 0))
|
||||
|
||||
# odd length
|
||||
with pytest.raises(KlamathError):
|
||||
parse_int2(b'54321')
|
||||
|
||||
# empty data
|
||||
with pytest.raises(KlamathError):
|
||||
parse_int2(b'')
|
||||
|
||||
|
||||
def test_parse_int4():
|
||||
assert_array_equal(parse_int4(b'4321'), (875770417,))
|
||||
|
||||
# length % 4 != 0
|
||||
with pytest.raises(KlamathError):
|
||||
parse_int4(b'654321')
|
||||
|
||||
# empty data
|
||||
with pytest.raises(KlamathError):
|
||||
parse_int4(b'')
|
||||
|
||||
|
||||
def test_decode_real8():
|
||||
# zeroes
|
||||
assert(decode_real8(numpy.array([0x0])) == 0)
|
||||
assert(decode_real8(numpy.array([1<<63])) == 0) # negative
|
||||
assert(decode_real8(numpy.array([0xff << 56])) == 0) # denormalized
|
||||
|
||||
assert(decode_real8(numpy.array([0x4110 << 48])) == 1.0)
|
||||
assert(decode_real8(numpy.array([0xC120 << 48])) == -2.0)
|
||||
|
||||
|
||||
def test_parse_real8():
|
||||
packed = struct.pack('>3Q', 0x0, 0x4110_0000_0000_0000, 0xC120_0000_0000_0000)
|
||||
assert_array_equal(parse_real8(packed), (0.0, 1.0, -2.0))
|
||||
|
||||
# length % 8 != 0
|
||||
with pytest.raises(KlamathError):
|
||||
parse_real8(b'0987654321')
|
||||
|
||||
# empty data
|
||||
with pytest.raises(KlamathError):
|
||||
parse_real8(b'')
|
||||
|
||||
|
||||
def test_parse_ascii():
|
||||
# empty data
|
||||
with pytest.raises(KlamathError):
|
||||
parse_ascii(b'')
|
||||
|
||||
assert(parse_ascii(b'12345') == b'12345')
|
||||
assert(parse_ascii(b'12345\0') == b'12345') # strips trailing null byte
|
||||
|
||||
|
||||
def test_pack_bitarray():
|
||||
packed = pack_bitarray(321)
|
||||
assert(len(packed) == 2)
|
||||
assert(packed == struct.pack('>H', 321))
|
||||
|
||||
|
||||
def test_pack_int2():
|
||||
packed = pack_int2((3, 2, 1))
|
||||
assert(len(packed) == 3*2)
|
||||
assert(packed == struct.pack('>3h', 3, 2, 1))
|
||||
assert(pack_int2([-3, 2, -1]) == struct.pack('>3h', -3, 2, -1))
|
||||
|
||||
|
||||
def test_pack_int4():
|
||||
packed = pack_int4((3, 2, 1))
|
||||
assert(len(packed) == 3*4)
|
||||
assert(packed == struct.pack('>3l', 3, 2, 1))
|
||||
assert(pack_int4([-3, 2, -1]) == struct.pack('>3l', -3, 2, -1))
|
||||
|
||||
|
||||
def test_encode_real8():
|
||||
assert(encode_real8(numpy.array([0.0])) == 0)
|
||||
arr = numpy.array((1.0, -2.0, 1e-9, 1e-3, 1e-12))
|
||||
assert_array_equal(decode_real8(encode_real8(arr)), arr)
|
||||
|
||||
|
||||
def test_pack_real8():
|
||||
reals = (0, 1, -1, 0.5, 1e-9, 1e-3, 1e-12)
|
||||
packed = pack_real8(reals)
|
||||
assert(len(packed) == len(reals) * 8)
|
||||
assert_array_equal(parse_real8(packed), reals)
|
||||
|
||||
|
||||
def test_pack_ascii():
|
||||
assert(pack_ascii(b'4321') == b'4321')
|
||||
assert(pack_ascii(b'321') == b'321\0')
|
||||
*/
|
||||
247
venv/bin/Activate.ps1
Normal file
247
venv/bin/Activate.ps1
Normal file
|
|
@ -0,0 +1,247 @@
|
|||
<#
|
||||
.Synopsis
|
||||
Activate a Python virtual environment for the current PowerShell session.
|
||||
|
||||
.Description
|
||||
Pushes the python executable for a virtual environment to the front of the
|
||||
$Env:PATH environment variable and sets the prompt to signify that you are
|
||||
in a Python virtual environment. Makes use of the command line switches as
|
||||
well as the `pyvenv.cfg` file values present in the virtual environment.
|
||||
|
||||
.Parameter VenvDir
|
||||
Path to the directory that contains the virtual environment to activate. The
|
||||
default value for this is the parent of the directory that the Activate.ps1
|
||||
script is located within.
|
||||
|
||||
.Parameter Prompt
|
||||
The prompt prefix to display when this virtual environment is activated. By
|
||||
default, this prompt is the name of the virtual environment folder (VenvDir)
|
||||
surrounded by parentheses and followed by a single space (ie. '(.venv) ').
|
||||
|
||||
.Example
|
||||
Activate.ps1
|
||||
Activates the Python virtual environment that contains the Activate.ps1 script.
|
||||
|
||||
.Example
|
||||
Activate.ps1 -Verbose
|
||||
Activates the Python virtual environment that contains the Activate.ps1 script,
|
||||
and shows extra information about the activation as it executes.
|
||||
|
||||
.Example
|
||||
Activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv
|
||||
Activates the Python virtual environment located in the specified location.
|
||||
|
||||
.Example
|
||||
Activate.ps1 -Prompt "MyPython"
|
||||
Activates the Python virtual environment that contains the Activate.ps1 script,
|
||||
and prefixes the current prompt with the specified string (surrounded in
|
||||
parentheses) while the virtual environment is active.
|
||||
|
||||
.Notes
|
||||
On Windows, it may be required to enable this Activate.ps1 script by setting the
|
||||
execution policy for the user. You can do this by issuing the following PowerShell
|
||||
command:
|
||||
|
||||
PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
|
||||
|
||||
For more information on Execution Policies:
|
||||
https://go.microsoft.com/fwlink/?LinkID=135170
|
||||
|
||||
#>
|
||||
Param(
|
||||
[Parameter(Mandatory = $false)]
|
||||
[String]
|
||||
$VenvDir,
|
||||
[Parameter(Mandatory = $false)]
|
||||
[String]
|
||||
$Prompt
|
||||
)
|
||||
|
||||
<# Function declarations --------------------------------------------------- #>
|
||||
|
||||
<#
|
||||
.Synopsis
|
||||
Remove all shell session elements added by the Activate script, including the
|
||||
addition of the virtual environment's Python executable from the beginning of
|
||||
the PATH variable.
|
||||
|
||||
.Parameter NonDestructive
|
||||
If present, do not remove this function from the global namespace for the
|
||||
session.
|
||||
|
||||
#>
|
||||
function global:deactivate ([switch]$NonDestructive) {
|
||||
# Revert to original values
|
||||
|
||||
# The prior prompt:
|
||||
if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) {
|
||||
Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt
|
||||
Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT
|
||||
}
|
||||
|
||||
# The prior PYTHONHOME:
|
||||
if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) {
|
||||
Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME
|
||||
Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME
|
||||
}
|
||||
|
||||
# The prior PATH:
|
||||
if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) {
|
||||
Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH
|
||||
Remove-Item -Path Env:_OLD_VIRTUAL_PATH
|
||||
}
|
||||
|
||||
# Just remove the VIRTUAL_ENV altogether:
|
||||
if (Test-Path -Path Env:VIRTUAL_ENV) {
|
||||
Remove-Item -Path env:VIRTUAL_ENV
|
||||
}
|
||||
|
||||
# Just remove VIRTUAL_ENV_PROMPT altogether.
|
||||
if (Test-Path -Path Env:VIRTUAL_ENV_PROMPT) {
|
||||
Remove-Item -Path env:VIRTUAL_ENV_PROMPT
|
||||
}
|
||||
|
||||
# Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether:
|
||||
if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) {
|
||||
Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force
|
||||
}
|
||||
|
||||
# Leave deactivate function in the global namespace if requested:
|
||||
if (-not $NonDestructive) {
|
||||
Remove-Item -Path function:deactivate
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.Description
|
||||
Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the
|
||||
given folder, and returns them in a map.
|
||||
|
||||
For each line in the pyvenv.cfg file, if that line can be parsed into exactly
|
||||
two strings separated by `=` (with any amount of whitespace surrounding the =)
|
||||
then it is considered a `key = value` line. The left hand string is the key,
|
||||
the right hand is the value.
|
||||
|
||||
If the value starts with a `'` or a `"` then the first and last character is
|
||||
stripped from the value before being captured.
|
||||
|
||||
.Parameter ConfigDir
|
||||
Path to the directory that contains the `pyvenv.cfg` file.
|
||||
#>
|
||||
function Get-PyVenvConfig(
|
||||
[String]
|
||||
$ConfigDir
|
||||
) {
|
||||
Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg"
|
||||
|
||||
# Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue).
|
||||
$pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue
|
||||
|
||||
# An empty map will be returned if no config file is found.
|
||||
$pyvenvConfig = @{ }
|
||||
|
||||
if ($pyvenvConfigPath) {
|
||||
|
||||
Write-Verbose "File exists, parse `key = value` lines"
|
||||
$pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath
|
||||
|
||||
$pyvenvConfigContent | ForEach-Object {
|
||||
$keyval = $PSItem -split "\s*=\s*", 2
|
||||
if ($keyval[0] -and $keyval[1]) {
|
||||
$val = $keyval[1]
|
||||
|
||||
# Remove extraneous quotations around a string value.
|
||||
if ("'""".Contains($val.Substring(0, 1))) {
|
||||
$val = $val.Substring(1, $val.Length - 2)
|
||||
}
|
||||
|
||||
$pyvenvConfig[$keyval[0]] = $val
|
||||
Write-Verbose "Adding Key: '$($keyval[0])'='$val'"
|
||||
}
|
||||
}
|
||||
}
|
||||
return $pyvenvConfig
|
||||
}
|
||||
|
||||
|
||||
<# Begin Activate script --------------------------------------------------- #>
|
||||
|
||||
# Determine the containing directory of this script
|
||||
$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
|
||||
$VenvExecDir = Get-Item -Path $VenvExecPath
|
||||
|
||||
Write-Verbose "Activation script is located in path: '$VenvExecPath'"
|
||||
Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)"
|
||||
Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)"
|
||||
|
||||
# Set values required in priority: CmdLine, ConfigFile, Default
|
||||
# First, get the location of the virtual environment, it might not be
|
||||
# VenvExecDir if specified on the command line.
|
||||
if ($VenvDir) {
|
||||
Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values"
|
||||
}
|
||||
else {
|
||||
Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir."
|
||||
$VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/")
|
||||
Write-Verbose "VenvDir=$VenvDir"
|
||||
}
|
||||
|
||||
# Next, read the `pyvenv.cfg` file to determine any required value such
|
||||
# as `prompt`.
|
||||
$pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir
|
||||
|
||||
# Next, set the prompt from the command line, or the config file, or
|
||||
# just use the name of the virtual environment folder.
|
||||
if ($Prompt) {
|
||||
Write-Verbose "Prompt specified as argument, using '$Prompt'"
|
||||
}
|
||||
else {
|
||||
Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value"
|
||||
if ($pyvenvCfg -and $pyvenvCfg['prompt']) {
|
||||
Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'"
|
||||
$Prompt = $pyvenvCfg['prompt'];
|
||||
}
|
||||
else {
|
||||
Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virtual environment)"
|
||||
Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'"
|
||||
$Prompt = Split-Path -Path $venvDir -Leaf
|
||||
}
|
||||
}
|
||||
|
||||
Write-Verbose "Prompt = '$Prompt'"
|
||||
Write-Verbose "VenvDir='$VenvDir'"
|
||||
|
||||
# Deactivate any currently active virtual environment, but leave the
|
||||
# deactivate function in place.
|
||||
deactivate -nondestructive
|
||||
|
||||
# Now set the environment variable VIRTUAL_ENV, used by many tools to determine
|
||||
# that there is an activated venv.
|
||||
$env:VIRTUAL_ENV = $VenvDir
|
||||
|
||||
if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) {
|
||||
|
||||
Write-Verbose "Setting prompt to '$Prompt'"
|
||||
|
||||
# Set the prompt to include the env name
|
||||
# Make sure _OLD_VIRTUAL_PROMPT is global
|
||||
function global:_OLD_VIRTUAL_PROMPT { "" }
|
||||
Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT
|
||||
New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt
|
||||
|
||||
function global:prompt {
|
||||
Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) "
|
||||
_OLD_VIRTUAL_PROMPT
|
||||
}
|
||||
$env:VIRTUAL_ENV_PROMPT = $Prompt
|
||||
}
|
||||
|
||||
# Clear PYTHONHOME
|
||||
if (Test-Path -Path Env:PYTHONHOME) {
|
||||
Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME
|
||||
Remove-Item -Path Env:PYTHONHOME
|
||||
}
|
||||
|
||||
# Add the venv to the PATH
|
||||
Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH
|
||||
$Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH"
|
||||
71
venv/bin/activate
Normal file
71
venv/bin/activate
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
# This file must be used with "source bin/activate" *from bash*
|
||||
# You cannot run it directly
|
||||
|
||||
deactivate () {
|
||||
# reset old environment variables
|
||||
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
|
||||
PATH="${_OLD_VIRTUAL_PATH:-}"
|
||||
export PATH
|
||||
unset _OLD_VIRTUAL_PATH
|
||||
fi
|
||||
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
|
||||
PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}"
|
||||
export PYTHONHOME
|
||||
unset _OLD_VIRTUAL_PYTHONHOME
|
||||
fi
|
||||
|
||||
# Call hash to forget past locations. Without forgetting
|
||||
# past locations the $PATH changes we made may not be respected.
|
||||
# See "man bash" for more details. hash is usually a builtin of your shell
|
||||
hash -r 2> /dev/null
|
||||
|
||||
if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
|
||||
PS1="${_OLD_VIRTUAL_PS1:-}"
|
||||
export PS1
|
||||
unset _OLD_VIRTUAL_PS1
|
||||
fi
|
||||
|
||||
unset VIRTUAL_ENV
|
||||
unset VIRTUAL_ENV_PROMPT
|
||||
if [ ! "${1:-}" = "nondestructive" ] ; then
|
||||
# Self destruct!
|
||||
unset -f deactivate
|
||||
fi
|
||||
}
|
||||
|
||||
# unset irrelevant variables
|
||||
deactivate nondestructive
|
||||
|
||||
# on Windows, a path can contain colons and backslashes and has to be converted:
|
||||
if [ "${OSTYPE:-}" = "cygwin" ] || [ "${OSTYPE:-}" = "msys" ] ; then
|
||||
# transform D:\path\to\venv to /d/path/to/venv on MSYS
|
||||
# and to /cygdrive/d/path/to/venv on Cygwin
|
||||
export VIRTUAL_ENV=$(cygpath /home/jan/projects/klamath-rs/venv)
|
||||
else
|
||||
# use the path as-is
|
||||
export VIRTUAL_ENV=/home/jan/projects/klamath-rs/venv
|
||||
fi
|
||||
|
||||
_OLD_VIRTUAL_PATH="$PATH"
|
||||
PATH="$VIRTUAL_ENV/"bin":$PATH"
|
||||
export PATH
|
||||
|
||||
# unset PYTHONHOME if set
|
||||
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
|
||||
# could use `if (set -u; : $PYTHONHOME) ;` in bash
|
||||
if [ -n "${PYTHONHOME:-}" ] ; then
|
||||
_OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
|
||||
unset PYTHONHOME
|
||||
fi
|
||||
|
||||
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
|
||||
_OLD_VIRTUAL_PS1="${PS1:-}"
|
||||
PS1='(venv) '"${PS1:-}"
|
||||
export PS1
|
||||
VIRTUAL_ENV_PROMPT='(venv) '
|
||||
export VIRTUAL_ENV_PROMPT
|
||||
fi
|
||||
|
||||
# Call hash to forget past commands. Without forgetting
|
||||
# past commands the $PATH changes we made may not be respected
|
||||
hash -r 2> /dev/null
|
||||
27
venv/bin/activate.csh
Normal file
27
venv/bin/activate.csh
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# This file must be used with "source bin/activate.csh" *from csh*.
|
||||
# You cannot run it directly.
|
||||
|
||||
# Created by Davide Di Blasi <davidedb@gmail.com>.
|
||||
# Ported to Python 3.3 venv by Andrew Svetlov <andrew.svetlov@gmail.com>
|
||||
|
||||
alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; unsetenv VIRTUAL_ENV_PROMPT; test "\!:*" != "nondestructive" && unalias deactivate'
|
||||
|
||||
# Unset irrelevant variables.
|
||||
deactivate nondestructive
|
||||
|
||||
setenv VIRTUAL_ENV /home/jan/projects/klamath-rs/venv
|
||||
|
||||
set _OLD_VIRTUAL_PATH="$PATH"
|
||||
setenv PATH "$VIRTUAL_ENV/"bin":$PATH"
|
||||
|
||||
|
||||
set _OLD_VIRTUAL_PROMPT="$prompt"
|
||||
|
||||
if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then
|
||||
set prompt = '(venv) '"$prompt"
|
||||
setenv VIRTUAL_ENV_PROMPT '(venv) '
|
||||
endif
|
||||
|
||||
alias pydoc python -m pydoc
|
||||
|
||||
rehash
|
||||
69
venv/bin/activate.fish
Normal file
69
venv/bin/activate.fish
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
# This file must be used with "source <venv>/bin/activate.fish" *from fish*
|
||||
# (https://fishshell.com/). You cannot run it directly.
|
||||
|
||||
function deactivate -d "Exit virtual environment and return to normal shell environment"
|
||||
# reset old environment variables
|
||||
if test -n "$_OLD_VIRTUAL_PATH"
|
||||
set -gx PATH $_OLD_VIRTUAL_PATH
|
||||
set -e _OLD_VIRTUAL_PATH
|
||||
end
|
||||
if test -n "$_OLD_VIRTUAL_PYTHONHOME"
|
||||
set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME
|
||||
set -e _OLD_VIRTUAL_PYTHONHOME
|
||||
end
|
||||
|
||||
if test -n "$_OLD_FISH_PROMPT_OVERRIDE"
|
||||
set -e _OLD_FISH_PROMPT_OVERRIDE
|
||||
# prevents error when using nested fish instances (Issue #93858)
|
||||
if functions -q _old_fish_prompt
|
||||
functions -e fish_prompt
|
||||
functions -c _old_fish_prompt fish_prompt
|
||||
functions -e _old_fish_prompt
|
||||
end
|
||||
end
|
||||
|
||||
set -e VIRTUAL_ENV
|
||||
set -e VIRTUAL_ENV_PROMPT
|
||||
if test "$argv[1]" != "nondestructive"
|
||||
# Self-destruct!
|
||||
functions -e deactivate
|
||||
end
|
||||
end
|
||||
|
||||
# Unset irrelevant variables.
|
||||
deactivate nondestructive
|
||||
|
||||
set -gx VIRTUAL_ENV /home/jan/projects/klamath-rs/venv
|
||||
|
||||
set -gx _OLD_VIRTUAL_PATH $PATH
|
||||
set -gx PATH "$VIRTUAL_ENV/"bin $PATH
|
||||
|
||||
# Unset PYTHONHOME if set.
|
||||
if set -q PYTHONHOME
|
||||
set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME
|
||||
set -e PYTHONHOME
|
||||
end
|
||||
|
||||
if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
|
||||
# fish uses a function instead of an env var to generate the prompt.
|
||||
|
||||
# Save the current fish_prompt function as the function _old_fish_prompt.
|
||||
functions -c fish_prompt _old_fish_prompt
|
||||
|
||||
# With the original prompt function renamed, we can override with our own.
|
||||
function fish_prompt
|
||||
# Save the return status of the last command.
|
||||
set -l old_status $status
|
||||
|
||||
# Output the venv prompt; color taken from the blue of the Python logo.
|
||||
printf "%s%s%s" (set_color 4B8BBE) '(venv) ' (set_color normal)
|
||||
|
||||
# Restore the return status of the previous command.
|
||||
echo "exit $old_status" | .
|
||||
# Output the original/"old" prompt.
|
||||
_old_fish_prompt
|
||||
end
|
||||
|
||||
set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV"
|
||||
set -gx VIRTUAL_ENV_PROMPT '(venv) '
|
||||
end
|
||||
8
venv/bin/pip
Executable file
8
venv/bin/pip
Executable file
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/jan/projects/klamath-rs/venv/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from pip._internal.cli.main import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
||||
8
venv/bin/pip3
Executable file
8
venv/bin/pip3
Executable file
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/jan/projects/klamath-rs/venv/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from pip._internal.cli.main import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
||||
8
venv/bin/pip3.12
Executable file
8
venv/bin/pip3.12
Executable file
|
|
@ -0,0 +1,8 @@
|
|||
#!/home/jan/projects/klamath-rs/venv/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from pip._internal.cli.main import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
||||
1
venv/bin/python
Symbolic link
1
venv/bin/python
Symbolic link
|
|
@ -0,0 +1 @@
|
|||
python3
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue