basic transport by file copying
authorVolker Birk <vb@pep.foundation>
Thu, 04 Jan 2018 22:06:11 +0100
changeset 23463568f9c074bb
parent 2345 c30ed17bf561
child 2347 c45e151bea2b
basic transport by file copying
test/python_tests/test_setup.py
test/python_tests/transport.py
     1.1 --- a/test/python_tests/test_setup.py	Thu Jan 04 19:05:56 2018 +0100
     1.2 +++ b/test/python_tests/test_setup.py	Thu Jan 04 22:06:11 2018 +0100
     1.3 @@ -20,9 +20,9 @@
     1.4  from multiprocessing import Process
     1.5  
     1.6  
     1.7 -
     1.8  realhome = os.path.expanduser("~")
     1.9  mydir = os.path.abspath(os.path.curdir)
    1.10 +files_to_copy = ("transport.py",)
    1.11  
    1.12  
    1.13  def link_if_exists(dirname, arthome):
    1.14 @@ -38,9 +38,12 @@
    1.15      "create an artificial home directory for testing"
    1.16  
    1.17      os.chdir(mydir)
    1.18 +    os.makedirs(arthome, exist_ok=True)
    1.19  
    1.20 -    os.makedirs(arthome, exist_ok=True)
    1.21      os.chdir(arthome)
    1.22 +    for filename in files_to_copy:
    1.23 +        src = os.path.join(os.pardir, filename)
    1.24 +        shutil.copyfile(src, filename)
    1.25  
    1.26      link_if_exists("bin", arthome)
    1.27      link_if_exists("include", arthome)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/python_tests/transport.py	Thu Jan 04 22:06:11 2018 +0100
     2.3 @@ -0,0 +1,46 @@
     2.4 +#!/usr/bin/env python3
     2.5 +# -*- coding: utf-8 -*-
     2.6 +
     2.7 +# this file is under GNU General Public License 3.0
     2.8 +# Copyleft 2017, p≡p foundation
     2.9 +
    2.10 +
    2.11 +"""
    2.12 +This module is implementing a basic message transport. Messages are written
    2.13 +into the artificial home of the receiver and deleted when read.
    2.14 +"""
    2.15 +
    2.16 +
    2.17 +import os
    2.18 +import time
    2.19 +from glob import glob
    2.20 +
    2.21 +
    2.22 +def send_message(to, msg):
    2.23 +    "send message by creating a file in recipient's artificial home"
    2.24 +
    2.25 +    recipient_dir = os.path.join(os.pardir, to)
    2.26 +    filename = '{:024x}'.format(int(time.monotonic() * 10000000000)) + \
    2.27 +            os.extsep + "eml"
    2.28 +    dotpath = os.path.join(recipient_dir, "." + filename)
    2.29 +    path = os.path.join(recipient_dir, filename)
    2.30 +
    2.31 +    with open(dotpath, "w") as file:
    2.32 +        file.write(msg)
    2.33 +    
    2.34 +    os.rename(dotpath, path)
    2.35 +
    2.36 +
    2.37 +def recv_message():
    2.38 +    """receive message by returning the first .eml files content in artificial
    2.39 +    home"""
    2.40 +
    2.41 +    filename = glob("*.eml")[0]
    2.42 +
    2.43 +    with open(filename, "r") as file:
    2.44 +        msg = file.read()
    2.45 +
    2.46 +    os.remove(filename)
    2.47 +
    2.48 +    return msg
    2.49 +