Skip to content

Commit

Permalink
Merge pull request #41 from lexara-prime-ai/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
lexara-prime-ai authored Jun 6, 2024
2 parents 086746b + 2247b8c commit 6fa74ba
Show file tree
Hide file tree
Showing 9 changed files with 1,952 additions and 14 deletions.
22 changes: 22 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "wspr_cdk",
"image": "mcr.microsoft.com/devcontainers/rust:bullseye",
"postCreateCommand": "bash .devcontainer/post_create.sh",
"customizations": {
"vscode": {
"extensions": [
"ms-python.black-formatter",
"rust-lang.rust-analyzer",
"charliermarsh.ruff"
],
"settings": {
"editor.formatOnSave": true
}
}
},
"features": {
"ghcr.io/devcontainers/features/sshd:1": {
"version": "latest"
}
}
}
13 changes: 13 additions & 0 deletions .devcontainer/post_create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

set -euxo pipefail
# pipefail

# If set, the return value of a pipeline is the value of the last
# (rightmost) command to exit with a non-zero status, or zero if
# all commands in the pipeline exit successfully.
# This option is disabled by default.

sudo apt-get update
sudo apt-get install -y python3-dev python3-pip python3-venv libclang-dev
sudo python3 -m venv .venv
42 changes: 35 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ RUN cargo install cargo-chef --locked

#----------------------------------------

# Use the chef stage to prepare the build plan.
FROM chef AS planner

COPY . .
Expand All @@ -14,29 +15,56 @@ RUN cargo chef prepare --recipe-path recipe.json

#-------------------------------------------

# Use the chef stage as the base for the builder stage.
FROM chef AS builder

# Install python 3.11 development files.
# To do -> Run scripts to install additional dependencies e.g [mkdocs], [tableauhyperapi] etc.
RUN apt-get update && \
apt-get install -y python3.11-dev && \
apt-get clean
# Install python 3.11 development files and virtual environment tools
RUN apt-get update --no-install-recommends && \
apt-get install libclang-dev -y && \
apt-get install -y python3-dev python3-venv --no-install-recommends && \
apt-get clean --no-install-recommends


# Copy the prepared recipe.json from the [planner] stage.
COPY --from=planner /wspr_cdk/recipe.json recipe.json

# Cook the dependencies according to the recipe.
RUN cargo chef cook --release --recipe-path recipe.json

# Copy [python_deps] script to manage dependencies.
# The script currently installs the following dependencies:
# "mkdocs" "maturin" "tableauhyperapi" "google-api-python-client" "google-auth-httplib2" "google-auth-oauthlib"

COPY scripts/bash/python_deps.sh /scripts/bash/python_deps.sh

# Create and activate a virtual environment.
RUN python3 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
ENV VIRTUAL_ENV=/opt/venv

# Modify script permissions.
RUN chmod +x /scripts/bash/python_deps.sh
RUN /scripts/bash/python_deps.sh

COPY . .

# Set environment variables for Rocket
ENV ROCKET_ADDRESS=0.0.0.0
ENV ROCKET_PORT=8000

# Build the project
RUN cargo build --workspace --release

#------------------------------------

CMD [ "./target/release/wspr_cdk_server"]
# [CURRENT] entry point.
# Run maturin develop during container initialization
ENTRYPOINT ["/bin/bash", "-c", ". /opt/venv/bin/activate && maturin develop -m python_wrapper/Cargo.toml && ./target/release/wspr_cdk_server"]

#------------------------------------

EXPOSE 8000
EXPOSE 8000

# NOTES #
# End users need to provide/generate their own <service_account.json> credentials.
# pip install patchelf
2 changes: 1 addition & 1 deletion hyper/hyper/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async def write_to_csv(self):
"""

try:
output = await python_wrapper.python_wrapper.get_wspr_spots("10000", "JSON")
output = await python_wrapper.python_wrapper.get_wspr_spots("100000", "JSON")
data = output.get_data()

# Display data that's being fetched for [DEBUG] purposes.
Expand Down
2 changes: 1 addition & 1 deletion python_wrapper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ crate-type = ["cdylib"]

[dependencies]
wspr_cdk = { path = "../" }
pyo3 = "0.20.0"
pyo3 = { version = "0.20.0", features = ["extension-module"] }
pyo3-asyncio = { version = "0.20", features = ["tokio-runtime"] }
tokio = "1.9"
7 changes: 4 additions & 3 deletions python_wrapper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ impl ClickHouseStateWrapper {
}

/*
The '__repr__' method will provide a meaningful [string] representaion
The '__repr__' method will provide a meaningful [string] representation
for the ClickHouseState, python <object>.
...........................................................................
The '__str__' method will provide a user-friendly [string] representaion
The '__str__' method will provide a user-friendly [string] representation
for the ClickHouseState, python <object>.
*/
fn __repr__(&self) -> String {
Expand Down Expand Up @@ -99,7 +99,8 @@ fn get_wspr_spots(py: Python, limit: String, result_format: String) -> PyResult<

/// The <wspr_cdk> python module, implemented in Rust.
#[pymodule]
fn python_wrapper(_py: Python, m: &PyModule) -> PyResult<()> {
fn python_wrapper(py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<ClickHouseStateWrapper>()?;
m.add_function(wrap_pyfunction!(get_wspr_spots, m)?)?;
Ok(())
}
4 changes: 4 additions & 0 deletions scripts/bash/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

echo "> [BUILDING] <wspr_cdk>..."
sudo docker build -t test .
6 changes: 4 additions & 2 deletions scripts/bash/python_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ else
echo "pip is already installed."
fi


# Modules that will be installed/upgraded.
modules=("mkdocs" "tableauhyperapi" "google-api-python-client" "google-auth-httplib2" "google-auth-oauthlib")
modules=("mkdocs" "maturin" "patchelf" "tableauhyperapi" "google-api-python-client" "google-auth-httplib2" "google-auth-oauthlib")

echo "Installing dependencies: ${modules[*]}..."
pip install "${modules[@]}" --upgrade
Expand All @@ -29,6 +30,8 @@ verify_installation() {
# The following dictionary contains module to import name mappings.
declare -A module_import_map=(
["mkdocs"]="mkdocs"
["maturin"]="maturin"
["patchelf"]="patchelf"
["tableauhyperapi"]="tableauhyperapi"
["google-api-python-client"]="googleapiclient"
["google-auth-httplib2"]="google_auth_httplib2"
Expand All @@ -40,7 +43,6 @@ for module in "${!module_import_map[@]}"; do
verify_installation "${module}" "${module_import_map[${module}]}"
done


# The [modules] array contains the names of all the modules to be installed.
# The [dictionary] [module_import_map] maps module names to their respective import names.
# The verify_installation function takes a module name and its import name as arguments, checks if the module can be imported, and prints the appropriate message.
Expand Down
Loading

0 comments on commit 6fa74ba

Please sign in to comment.