From 6365279206759e3051cfb7a6aeec17dd636b2d2c Mon Sep 17 00:00:00 2001 From: RonaldsonBellande Date: Mon, 16 Sep 2024 01:32:40 -0400 Subject: [PATCH 1/4] latest pushes --- .gitignore | 5 +++++ README.md | 26 ++++++++++++++++++++++++++ git_scripts/.gitignore | 3 +++ 3 files changed, 34 insertions(+) create mode 100644 .gitignore create mode 100644 git_scripts/.gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b111f4b --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +publish.sh +tests +setup.py +dist +src/bellande_format.egg-info diff --git a/README.md b/README.md index cd943ab..09b232e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,29 @@ # Bellande Rust Executable ## Bellande Rust Executable is a library that makes rust code into an executable (BRE) library + + +## Website PYPI +- https://pypi.org/project/bellande_rust_executable + +### Installation +- `$ pip install bellande_rust_executable` + +### Upgrade (if not upgraded) +- `$ pip install --upgrade bellande_rust_executable` + +``` +Name: bellande_rust_executable +Version: 0.1.0 +Summary: File type Formats +Home-page: github.com/RonaldsonBellande/bellande_rust_executable +Author: Ronaldson Bellande +Author-email: ronaldsonbellande@gmail.com +License: GNU General Public License v3.0 +Requires: numpy +Required-by: +``` + +## License + +Bellande Rust Executable Packages is distributed under the [GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.en.html), see [LICENSE](https://github.com/Algorithm-Model-Research/bellande_rust_executable/blob/main/LICENSE) and [NOTICE](https://github.com/Algorithm-Model-Research/bellande_rust_executable/blob/main/LICENSE) for more information. diff --git a/git_scripts/.gitignore b/git_scripts/.gitignore new file mode 100644 index 0000000..e5a7a9c --- /dev/null +++ b/git_scripts/.gitignore @@ -0,0 +1,3 @@ +fix_errors.sh +push.sh +repository_recal.sh From 7c954746dca6323eb5d1febd983e55677ce4d262 Mon Sep 17 00:00:00 2001 From: RonaldsonBellande Date: Mon, 16 Sep 2024 01:33:35 -0400 Subject: [PATCH 2/4] latest pushes --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b111f4b..a6827d2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ publish.sh tests setup.py dist -src/bellande_format.egg-info +src/bellande_rust_executable.egg-info From b80853d5c1c90d387d29d54ce61eafbe91badcda Mon Sep 17 00:00:00 2001 From: RonaldsonBellande Date: Sun, 29 Sep 2024 16:38:10 -0400 Subject: [PATCH 3/4] latest pushes --- src/.gitignore | 1 + test/test_import.py | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 src/.gitignore create mode 100644 test/test_import.py diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..f6b8568 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1 @@ +bellande_rust_executable.egg-info diff --git a/test/test_import.py b/test/test_import.py new file mode 100644 index 0000000..09e4651 --- /dev/null +++ b/test/test_import.py @@ -0,0 +1,2 @@ +def test_import(): + from bellande_rust_executable import main From b7190128fe7f0486ec13da218a06f4a8be455b81 Mon Sep 17 00:00:00 2001 From: RonaldsonBellande Date: Wed, 2 Oct 2024 12:46:57 -0400 Subject: [PATCH 4/4] latest pushes --- src/bellande_rust/__init__.py | 0 src/bellande_rust/bellande_rust_executable.py | 163 ++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 src/bellande_rust/__init__.py create mode 100644 src/bellande_rust/bellande_rust_executable.py diff --git a/src/bellande_rust/__init__.py b/src/bellande_rust/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/bellande_rust/bellande_rust_executable.py b/src/bellande_rust/bellande_rust_executable.py new file mode 100644 index 0000000..8c97b65 --- /dev/null +++ b/src/bellande_rust/bellande_rust_executable.py @@ -0,0 +1,163 @@ +# Copyright (C) 2024 Bellande Architecture Mechanism Research Innovation Center, Ronaldson Bellande + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +#!/usr/bin/env python3 + +import subprocess +import os +import shutil +import argparse +import toml + +def ensure_directory(path): + """Ensure a directory exists and create one if it does not""" + os.makedirs(path, exist_ok=True) + +def copy_source_files(src_dir, dest_dir): + """Maintained the structure of the src file; or assigned""" + if not os.path.exists(src_dir): + raise FileNotFoundError(f"Source directory '{src_dir}' not found") + + dest_src_dir = os.path.join(dest_dir, 'src') + ensure_directory(dest_src_dir) + + for root, dirs, files in os.walk(src_dir): + for file in files: + src_path = os.path.join(root, file) + rel_path = os.path.relpath(src_path, src_dir) + dest_path = os.path.join(dest_src_dir, rel_path) + ensure_directory(os.path.dirname(dest_path)) + shutil.copy2(src_path, dest_path) + +def create_cargo_toml(project_dir, main_file, binary_name): + """Create a Cargo.toml file for a binary target.""" + cargo_config = { + 'package': { + 'name': binary_name, + 'version': "0.1.0", + 'edition': "2021" + }, + 'dependencies': {} + } + + # If the main file isn't main.rs, we need to specify the path + if main_file != 'main.rs': + cargo_config['bin'] = [{ + 'name': binary_name, + 'path': os.path.join("src", main_file) + }] + + cargo_toml_path = os.path.join(project_dir, 'Cargo.toml') + with open(cargo_toml_path, 'w') as f: + toml.dump(cargo_config, f) + +def parse_dependencies(dep_file): + """Parse dependencies from the specified dependencies file.""" + dependencies = {} + if os.path.exists(dep_file): + with open(dep_file, 'r') as file: + for line in file: + line = line.strip() + if line and not line.startswith('#'): + try: + name, version = line.split('=') + dependencies[name.strip()] = version.strip().strip('"') + except ValueError: + print(f"Warning: Skipping invalid dependency line: {line}") + return dependencies + +def update_cargo_toml_dependencies(project_dir, dependencies): + """Update the dependencies in Cargo.toml.""" + cargo_toml_path = os.path.join(project_dir, 'Cargo.toml') + with open(cargo_toml_path, 'r') as f: + cargo_config = toml.load(f) + + cargo_config['dependencies'] = dependencies + + with open(cargo_toml_path, 'w') as f: + toml.dump(cargo_config, f) + +def build_project(project_dir, output_path, binary_name): + """Build the Rust project as an executable.""" + cargo_command = ['cargo', 'build', '--release'] + result = subprocess.run(cargo_command, cwd=project_dir, capture_output=True, text=True) + + if result.returncode == 0: + # Determine the correct executable name based on platform + if os.name == 'nt': # Windows + exe_extension = '.exe' + else: # Unix-like systems + exe_extension = '' + + # Copy the built executable to the specified output location + built_exe = os.path.join(project_dir, 'target', 'release', f"{binary_name}{exe_extension}") + ensure_directory(os.path.dirname(output_path)) + shutil.copy2(built_exe, output_path) + + # Make the output file executable on Unix-like systems + if os.name != 'nt': + os.chmod(output_path, 0o755) + + return True + else: + print("Build failed. Cargo output:") + print(result.stdout) + print(result.stderr) + return False + +def main(): + parser = argparse.ArgumentParser(description="Universal Rust Executable Builder") + parser.add_argument("-d", "--dep-file", required=True, help="Path to the dependencies file") + parser.add_argument("-s", "--src-dir", required=True, help="Source directory containing Rust files") + parser.add_argument("-m", "--main-file", required=True, help="Main Rust file name (e.g., main.rs)") + parser.add_argument("-o", "--output", required=True, help="Output path for the compiled executable") + + args = parser.parse_args() + + # Extract binary name from the file name (removing .rs extension) + binary_name = os.path.splitext(args.main_file)[0] + + # Create unique build directory based on binary name + build_dir = f"build_{binary_name}" + ensure_directory(build_dir) + + try: + copy_source_files(args.src_dir, build_dir) + create_cargo_toml(build_dir, args.main_file, binary_name) + + # Parse and update dependencies + dependencies = parse_dependencies(args.dep_file) + update_cargo_toml_dependencies(build_dir, dependencies) + + # Determine the correct output path based on platform + if os.name == 'nt': # Windows + output_path = f"{args.output}.exe" + else: # Unix-like systems + output_path = args.output + + # Build the project + if build_project(build_dir, output_path, binary_name): + print(f"Successfully built and copied to {output_path}") + return 0 + else: + print("Build failed") + return 1 + + finally: + # Clean up build directory + shutil.rmtree(build_dir, ignore_errors=True) + +if __name__ == "__main__": + exit(main())