-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial version of peer processor connection and discovery
- Loading branch information
Showing
14 changed files
with
367 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,41 @@ | ||
from typing import Optional | ||
import asyncio | ||
import os | ||
import random | ||
import string | ||
import tempfile | ||
|
||
from ..Processor import Processor | ||
from .stencilaFiles import get_tempfile, delete_tempfile | ||
from .StreamMultiServer import StreamMultiServer | ||
|
||
class UnixSocketServer(StreamMultiServer): | ||
""" | ||
A Server communicating over UNIX domain sockets | ||
""" | ||
|
||
def __init__(self, processor: Processor, path: str): | ||
_path: Optional[str] | ||
|
||
def __init__(self, processor: Processor): | ||
StreamMultiServer.__init__(self, processor) | ||
self.path = path | ||
self._id = 'unix-py-' + ''.join(random.choices(string.ascii_lowercase + string.digits, k=32)) | ||
self._path = None | ||
|
||
@property | ||
def url(self): | ||
return f'unix://{self.path}' | ||
return f'unix://{self._path}' | ||
|
||
async def open(self) -> None: | ||
""" | ||
Start the UNIX socket server and create an | ||
async connections when a client connects. | ||
""" | ||
await asyncio.start_unix_server(self.on_client_connected, self.path) | ||
self._path = get_tempfile(self._id) | ||
await asyncio.start_unix_server(self.on_client_connected, self._path) | ||
# Change the permissions on the file so that no other user can read/write it | ||
# This needs to be done after the server starts | ||
os.chmod(self._path, 0o600) | ||
|
||
async def close(self) -> None: | ||
await StreamMultiServer.close(self) | ||
delete_tempfile(self._id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from typing import List | ||
import getpass | ||
import os | ||
import stat | ||
import tempfile | ||
|
||
def get_tempdir() -> str: | ||
return os.path.join(tempfile.gettempdir(), 'stencila', getpass.getuser()) | ||
|
||
def create_tempdir() -> str: | ||
tempdir = get_tempdir() | ||
if not os.path.exists(tempdir): | ||
os.makedirs(tempdir, mode=0o700) | ||
return tempdir | ||
|
||
def get_tempfile(name: str) -> str: | ||
return os.path.join(create_tempdir(), name) | ||
|
||
def list_tempfiles() -> List[str]: | ||
return os.listdir(get_tempdir()) | ||
|
||
def create_tempfile(name: str, content: str = None) -> str: | ||
path = get_tempfile(name) | ||
|
||
# Write content to a secure file only readable by current user | ||
# Based on https://stackoverflow.com/a/15015748/4625911 | ||
|
||
# Remove any existing file with potentially elevated mode | ||
if os.path.isfile(path): | ||
os.remove(path) | ||
|
||
# Create a file handle | ||
mode = 0o600 | ||
umask = 0o777 ^ mode # Prevents always downgrading umask to 0. | ||
umask_original = os.umask(umask) | ||
try: | ||
fd = os.open(path, os.O_WRONLY | os.O_CREAT, mode) | ||
finally: | ||
os.umask(umask_original) | ||
|
||
if content: | ||
# Open file fd and write to file | ||
with os.fdopen(fd, 'w') as file: | ||
file.write(content) | ||
|
||
return path | ||
|
||
def delete_tempfile(name: str): | ||
path = get_tempfile(name) | ||
if os.path.exists(path): | ||
os.remove(path) |
Oops, something went wrong.