Skip to content

Commit

Permalink
Add Windows support (#43)
Browse files Browse the repository at this point in the history
* Add Windows support for file read and interrupt.
* Add install/start scripts for Windows and Linux
  • Loading branch information
skrustev authored Apr 26, 2021
1 parent 3f4fac5 commit 991243c
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ config.yaml
# dev files
.idea
venv
.*/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
1 change: 1 addition & 0 deletions config-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ log_level: INFO

# Only one consumer can be enabled at a time, you can
# delete the section for the consumer which you aren't using
# For Windows file path needs to be absolute.
chia_logs:
file_log_consumer:
enable: true
Expand Down
24 changes: 24 additions & 0 deletions install.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
$command = "py"
if (Get-Command python -errorAction SilentlyContinue) {
$command = "python"
}
elseif (Get-Command python3 -errorAction SilentlyContinue) {
$command = "python3"
}

# Create virtual environment
Invoke-expression "$($command) -m venv .venv"

# Activate the virtual environment
./.venv/Scripts/activate.ps1

# Update pip to latest version
Invoke-expression "$($command) -m pip install --upgrade pip"

# Install dependencies
pip install wheel

pip install -r requirements.txt

# Deactivate virtual env
deactivate
14 changes: 14 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Create virtual environment
python3 -m venv .venv

# Activate virtual environment
. ./.venv/bin/activate

# Update pip3 to latest version
python3 -m pip install --upgrade pip

# Install dependencies
pip3 install wheel && pip3 install -r requirements.txt

# Deactivate virtual environment
deactivate
13 changes: 11 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
import argparse
import logging
import signal
import time
from pathlib import Path

# project
from src.chia_log.handlers.daily_stats.stats_manager import StatsManager
from src.chia_log.log_consumer import create_log_consumer_from_config
from src.chia_log.log_handler import LogHandler
from src.config import Config
from src.config import Config, is_win_platform
from src.notifier.keep_alive_monitor import KeepAliveMonitor
from src.notifier.notify_manager import NotifyManager

Expand Down Expand Up @@ -77,4 +78,12 @@ def interrupt(signal_number, frame):
exit(0)

signal.signal(signal.SIGINT, interrupt)
signal.pause()

if is_win_platform:
while True:
try:
time.sleep(5)
except IOError:
pass
else:
signal.pause()
10 changes: 8 additions & 2 deletions src/chia_log/log_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from typing import List, Optional

# project
from src.config import check_keys
from src.config import check_keys, is_win_platform

# lib
import paramiko
Expand Down Expand Up @@ -66,7 +66,13 @@ def stop(self):
def _consume_loop(self):
expanded_user_log_path = self._log_path.expanduser()
logging.info(f"Consuming log file from {expanded_user_log_path}")
f = subprocess.Popen(["tail", "-F", expanded_user_log_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

if is_win_platform:
consume_command_args = ["powershell.exe", "get-content", expanded_user_log_path, "-tail", "1", "-wait"]
else:
consume_command_args = ["tail", "-F", expanded_user_log_path]

f = subprocess.Popen(consume_command_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while self._is_running:
log_line = f.stdout.readline().decode(encoding="utf-8")
self._notify_subscribers(log_line)
Expand Down
4 changes: 4 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# std
import logging
import sys
from pathlib import Path
from typing import Optional

Expand Down Expand Up @@ -49,3 +50,6 @@ def check_keys(required_keys, config) -> bool:
logging.error(f"Incompatible configuration. Missing {key} in {config}.")
return False
return True

def is_win_platform() -> bool:
return sys.platform.startswith("win")
13 changes: 13 additions & 0 deletions start.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
$command = "py"
if (Get-Command python -errorAction SilentlyContinue) {
$command = "python"
}
elseif (Get-Command python3 -errorAction SilentlyContinue) {
$command = "python3"
}

# Activate the virtual environment
./.venv/Scripts/activate.ps1

# Start the ChiaDog
Invoke-expression "$($command) main.py --config config.yaml"
5 changes: 5 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Activate the virtual environment
. ./.venv/bin/activate

# Start the ChiaDog
python3 main.py --config config.yaml

0 comments on commit 991243c

Please sign in to comment.