Skip to content

Commit

Permalink
Merge pull request #2426 from craig8/scp-kier
Browse files Browse the repository at this point in the history
Upgraded readme and added arg parse for customizing directories for o…
  • Loading branch information
craig8 authored Aug 6, 2020
2 parents 255be58 + 1cb6a47 commit 44ac2b5
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 79 deletions.
12 changes: 12 additions & 0 deletions examples/SCPAgent/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ the scp transfer.
local_path=local_path,
direction="RECEIVING")).get(timeout=5)
Testing
-------

Within the agent directory there is a trigger_scp.py script. By default the trigger will run through 4 different
tests. The tests will exercise the sending and receiving for both the rpc and pubsub interfaces. The trigger will
require user interaction so run it with a shell that can receive input.

.. code-block::shell
(volttron) (base) osboxes@osboxes:~/repos/volttron$ python examples/SCPAgent/trigger_scp.py
162 changes: 83 additions & 79 deletions examples/SCPAgent/trigger_scp.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,87 +38,91 @@
from pathlib import Path
import os
import shutil
import time

import argparse
import gevent
from volttron.platform.vip.agent.utils import build_agent

agent = build_agent(identity='trigger')

local_root = "/home/osboxes/Desktop/local_files"
remote_root = "/home/osboxes/Desktop/remote_files"


def build_remote_filename(filename):
os.makedirs(remote_root, exist_ok=True)
return str(Path(remote_root).joinpath(filename))


def build_local_filename(filename):
os.makedirs(local_root, exist_ok=True)
return str(Path(local_root).joinpath(filename))


def create_remote_file(filename, content):
full_path = build_remote_filename(filename)
with open(full_path, 'w') as fp:
fp.write(content)
return full_path


def create_local_file(filename, content):
full_path = build_local_filename(filename)
with open(full_path, 'w') as fp:
fp.write(content)
return full_path


def remove_files():
shutil.rmtree(remote_root, ignore_errors=True)
shutil.rmtree(local_root, ignore_errors=True)


remove_files()

remote_path = create_remote_file("t1.txt", "this is f1")
local_path = build_local_filename("t1.after.transfer.txt")

go = input(f"Test 1: rpc: trigger_download\n\tdownload remote_path: {remote_path}\n\tto local_path: {local_path} ")
result = agent.vip.rpc.call("scp.agent", "trigger_download",
remote_path=remote_path,
local_path=local_path).get()
print(f"The result was {result}\n")

print(f"Creating test2 file")
remote_path = build_remote_filename("t2.remote.transfer.txt")
local_path = create_local_file("t2.txt", "This is test 2")
go = input(f"Test 2: rpc: trigger_upload\n\tupload local_path: {local_path}\n\tto remote_path: {remote_path} ")
result = agent.vip.rpc.call("scp.agent", "trigger_upload",
remote_path=remote_path,
local_path=local_path).get()
print(f"The result was {result}\n")

print(f"Creating test3 file")
remote_path = build_remote_filename("t3.sent.pubsub.txt")
local_path = create_local_file("t3.txt", "This is test 3")

go = input(f"Test 3: pubsub: SENDING\n\tlocal_path: {local_path}\n\tto remote_path: {remote_path} ")

agent.vip.pubsub.publish(peer='pubsub', topic="transfer", message=dict(remote_path=remote_path,
local_path=local_path,
direction="SENDING")).get()
gevent.sleep(1)
print(f"The result is {Path(remote_path).exists()}\n")
print(f"Creating test4 file")
remote_path = create_remote_file("t4.receive.pubsub.txt", "This is test 4")
local_path = build_local_filename("t4.receive.txt")

go = input(f"Test 4: pubsub: RECEIVING\n\tlocal_path: {local_path}\n\tfrom remote_path: {remote_path} ")
agent.vip.pubsub.publish(peer='pubsub', topic="transfer", message=dict(remote_path=remote_path,
local_path=local_path,
direction="RECEIVING")).get()
gevent.sleep(1)
print(f"The result is {Path(local_path).exists()}\n")
agent.core.stop()
print("Complete")

def run_tests(local_root="~/local_files", remote_root="~/remote_files"):
agent = build_agent(identity='trigger')

local_root = Path(local_root).expanduser()
remote_root = Path(remote_root).expanduser()

def build_remote_filename(filename):
os.makedirs(remote_root, exist_ok=True)
return str(Path(remote_root).joinpath(filename))

def build_local_filename(filename):
os.makedirs(local_root, exist_ok=True)
return str(Path(local_root).joinpath(filename))

def create_remote_file(filename, content):
full_path = build_remote_filename(filename)
with open(full_path, 'w') as fp:
fp.write(content)
return full_path

def create_local_file(filename, content):
full_path = build_local_filename(filename)
with open(full_path, 'w') as fp:
fp.write(content)
return full_path

def remove_files():
shutil.rmtree(remote_root, ignore_errors=True)
shutil.rmtree(local_root, ignore_errors=True)

remove_files()

remote_path = create_remote_file("t1.txt", "this is f1")
local_path = build_local_filename("t1.after.transfer.txt")

go = input(f"Test 1: rpc: trigger_download\n\tdownload remote_path: {remote_path}\n\tto local_path: {local_path} ")
result = agent.vip.rpc.call("scp.agent", "trigger_download",
remote_path=remote_path,
local_path=local_path).get()
print(f"The result was {result}\n")

print(f"Creating test2 file")
remote_path = build_remote_filename("t2.remote.transfer.txt")
local_path = create_local_file("t2.txt", "This is test 2")
go = input(f"Test 2: rpc: trigger_upload\n\tupload local_path: {local_path}\n\tto remote_path: {remote_path} ")
result = agent.vip.rpc.call("scp.agent", "trigger_upload",
remote_path=remote_path,
local_path=local_path).get()
print(f"The result was {result}\n")

print(f"Creating test3 file")
remote_path = build_remote_filename("t3.sent.pubsub.txt")
local_path = create_local_file("t3.txt", "This is test 3")

go = input(f"Test 3: pubsub: SENDING\n\tlocal_path: {local_path}\n\tto remote_path: {remote_path} ")

agent.vip.pubsub.publish(peer='pubsub', topic="transfer", message=dict(remote_path=remote_path,
local_path=local_path,
direction="SENDING")).get()
gevent.sleep(1)
print(f"The result is {Path(remote_path).exists()}\n")
print(f"Creating test4 file")
remote_path = create_remote_file("t4.receive.pubsub.txt", "This is test 4")
local_path = build_local_filename("t4.receive.txt")

go = input(f"Test 4: pubsub: RECEIVING\n\tlocal_path: {local_path}\n\tfrom remote_path: {remote_path} ")
agent.vip.pubsub.publish(peer='pubsub', topic="transfer", message=dict(remote_path=remote_path,
local_path=local_path,
direction="RECEIVING")).get()
gevent.sleep(1)
print(f"The result is {Path(local_path).exists()}\n")
agent.core.stop()
print("Complete")


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--local_root", help="Local path", default="~/local_root")
parser.add_argument("-r", "--remote_root", help="Remote path", default="~/remote_root")

args = parser.parse_args()
run_tests(args.local_root, args.remote_root)

0 comments on commit 44ac2b5

Please sign in to comment.