diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/.gitignore | 3 | ||||
-rw-r--r-- | examples/mscp-example.ipynb | 226 | ||||
-rwxr-xr-x | examples/mscp-python.py | 63 |
3 files changed, 0 insertions, 292 deletions
diff --git a/examples/.gitignore b/examples/.gitignore deleted file mode 100644 index 9cef686..0000000 --- a/examples/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -simple-copy-dest -*.img -.ipynb_checkpoints diff --git a/examples/mscp-example.ipynb b/examples/mscp-example.ipynb deleted file mode 100644 index 915aeff..0000000 --- a/examples/mscp-example.ipynb +++ /dev/null @@ -1,226 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "ccda9e3a-35de-43fc-9b6e-02475c763f6b", - "metadata": {}, - "source": [ - "# mscp python binding example" - ] - }, - { - "cell_type": "code", - "execution_count": 60, - "id": "df04d655-a082-47eb-9a1e-154ebc2a5655", - "metadata": {}, - "outputs": [], - "source": [ - "import glob\n", - "import time\n", - "import os\n", - "\n", - "import mscp" - ] - }, - { - "cell_type": "code", - "execution_count": 53, - "id": "e9ed4519-c3fd-4639-89a5-1c1cdffd9519", - "metadata": {}, - "outputs": [], - "source": [ - "this_dir = os.getcwd()" - ] - }, - { - "cell_type": "markdown", - "id": "fee75bf8-df40-45f4-81d1-113069c34f13", - "metadata": {}, - "source": [ - "## Simple copy" - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "id": "2b06e6d3-30cc-47be-bd4f-af27eb141c8c", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['../src/ssh.c',\n", - " '../src/mscp.c',\n", - " '../src/platform.c',\n", - " '../src/pymscp.c',\n", - " '../src/main.c',\n", - " '../src/path.c',\n", - " '../src/message.c',\n", - " '../src/fileops.c']" - ] - }, - "execution_count": 54, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# preparing files to be transferred\n", - "c_sources = glob.glob(\"../src/*.c\")\n", - "c_sources" - ] - }, - { - "cell_type": "code", - "execution_count": 55, - "id": "89bb4558-9472-4d26-9af3-24f426b15edc", - "metadata": {}, - "outputs": [], - "source": [ - "# copy files using mscp\n", - "dst_dir = this_dir + \"/simple-copy-dest\"\n", - "m = mscp.mscp(\"localhost\", mscp.LOCAL2REMOTE)\n", - "m.copy(c_sources, dst_dir)" - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "id": "6daf2c98-8905-4039-b82a-a593df3107fe", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['ssh.c',\n", - " 'mscp.c',\n", - " 'platform.c',\n", - " 'pymscp.c',\n", - " 'main.c',\n", - " 'path.c',\n", - " 'message.c',\n", - " 'fileops.c']" - ] - }, - "execution_count": 56, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "os.listdir(\"simple-copy-dest\")" - ] - }, - { - "cell_type": "markdown", - "id": "f4a3869a-878e-43b0-9758-a049eaf8b5bd", - "metadata": {}, - "source": [ - "## Simple Copy with Python Rich ProgressBar" - ] - }, - { - "cell_type": "code", - "execution_count": 64, - "id": "e7cb7cd6-b845-4d26-93ed-aee8ed3983ab", - "metadata": {}, - "outputs": [], - "source": [ - "# make a 256MB file\n", - "src = \"example-256MB-src.img\"\n", - "with open(src, \"wb\") as f:\n", - " f.seek(128 * 1024 * 1024 -1, 0)\n", - " f.write(b'1')" - ] - }, - { - "cell_type": "code", - "execution_count": 69, - "id": "878607ed-5c06-4b15-81ac-9845dad0c9c6", - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "b700e9fc00464969a22a26300404dc35", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Output()" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n" - ], - "text/plain": [] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n", - "</pre>\n" - ], - "text/plain": [ - "\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# copy the 256MB file while ploting progress bar using python rich\n", - "dst = this_dir + \"/example-256MB-dst.img\"\n", - "\n", - "kw = {\"nr_threads\": 1, \"nr_ahead\": 1} # slow mscp to watch the progress bar\n", - "\n", - "m = mscp.mscp(\"localhost\", mscp.LOCAL2REMOTE, **kw)\n", - "m.copy(src, dst, nonblock = True)\n", - "\n", - "# m.stats() returns total bytes to be transferred, bytes transferred (done), and finished (bool).\n", - "total, done, finished = m.stats()\n", - "with Progress() as progress:\n", - "\n", - " task = progress.add_task(f\"[green]Copying {src}\", total = total)\n", - "\n", - " while not progress.finished:\n", - " total, done, finished = m.stats()\n", - " progress.update(task, completed = done)\n", - " time.sleep(0.5)\n", - "\n", - "m.join()\n", - "m.cleanup()" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.4" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/examples/mscp-python.py b/examples/mscp-python.py deleted file mode 100755 index d1cb856..0000000 --- a/examples/mscp-python.py +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env python3 -"""mscp.py - -An example python script running mscp -""" - -import argparse -import time -import sys - -from rich.progress import Progress - -import mscp - -def main(): - parser = argparse.ArgumentParser() - parser.add_argument("-f", "--from", dest = "fr", - metavar = "REMOTE", default = None, - help = "copy a file from this remote host") - parser.add_argument("-t", "--to", metavar = "REMOTE", default = None, - help = "copy a file to this remote host") - parser.add_argument("source", help = "path to source file to be copied") - parser.add_argument("destination", help = "path of copy destination") - - args = parser.parse_args() - - if args.fr and args.to: - print("-f and -t are exclusive", file = sys.stderr) - sys.exit(1) - elif args.fr: - d = mscp.REMOTE2LOCAL - remote = args.fr - elif args.to: - d = mscp.LOCAL2REMOTE - remote = args.to - else: - print("-f or -t must be specified", file = sys.stderr) - sys.exit(1) - - - m = mscp.mscp(remote, d) - m.connect() - m.add_src_path(args.source) - m.set_dst_path(args.destination) - m.scan() - m.start() - - total, done, finished = m.stats() - with Progress() as progress: - - task = progress.add_task("[green]Copying...", total = total) - - while not progress.finished: - total, done, finished = m.stats() - progress.update(task, completed = done) - time.sleep(0.5) - - m.join() - m.cleanup() - - -if __name__ == "__main__": - main() |