Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle plot file times under non-english locales #93

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ jobs:
with:
python-version: ${{ matrix.python.action }}

- name: Generate extra locales
run: |
sudo apt-get update
sudo apt-get install --yes tzdata locales
sudo locale-gen en_US.UTF-8 de_DE.UTF-8

- name: Install dependencies
run: |
python -m pip install --upgrade pip setuptools wheel
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ include *.md
include VERSION
include tox.ini
recursive-include src *.py
recursive-include src/plotman/_tests/resources *
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ project_urls =
Changelog = https://github.com/ericaltendorf/plotman/blob/main/CHANGELOG.md

[options]
include_package_data = True
package_dir=
=src
packages=find:
install_requires =
pendulum
psutil ~= 5.8
pyyaml
texttable
Expand Down
56 changes: 56 additions & 0 deletions src/plotman/_tests/job_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import contextlib
import datetime
import locale
import importlib.resources

import pytest
from plotman import job
from plotman._tests import resources


class FauxJobWithLogfile:
# plotman.job.Job does too much in its .__init_() so we have this to let us
# test its .init_from_logfile().

def __init__(self, logfile_path):
self.logfile = logfile_path

def update_from_logfile(self):
pass


@pytest.fixture(name='logfile_path')
def logfile_fixture(tmp_path):
log_name = '2021-04-04-19:00:47.log'
log_contents = importlib.resources.read_binary(resources, log_name)
log_file_path = tmp_path.joinpath(log_name)
log_file_path.write_bytes(log_contents)

return log_file_path


@contextlib.contextmanager
def set_locale(name):
# This is terrible and not thread safe.

original = locale.setlocale(locale.LC_ALL)

try:
yield locale.setlocale(locale.LC_ALL, name)
finally:
locale.setlocale(locale.LC_ALL, original)

with set_locale('C'):
log_file_time = datetime.datetime.strptime('Sun Apr 4 19:00:50 2021', '%a %b %d %H:%M:%S %Y')

@pytest.mark.parametrize(
argnames=['locale_name'],
argvalues=[['C'], ['en_US.utf8'], ['de_DE.utf8']],
)
def test_job_parses_time_with_non_english_locale(logfile_path, locale_name):
faux_job_with_logfile = FauxJobWithLogfile(logfile_path=logfile_path)

with set_locale(locale_name):
job.Job.init_from_logfile(self=faux_job_with_logfile)

assert faux_job_with_logfile.start_time == log_file_time
2,088 changes: 2,088 additions & 0 deletions src/plotman/_tests/resources/2021-04-04-19:00:47.log

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/plotman/_tests/resources/2021-04-04-19:00:47.notes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
This sample log file was created from a real log file. It has had the
following values replaced by naively randomly generated hexadecimal
characters.

pool public key
farmer public key
memo
id
Empty file.
7 changes: 6 additions & 1 deletion src/plotman/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from enum import Enum, auto
from subprocess import call

import pendulum
import psutil


Expand Down Expand Up @@ -47,6 +48,10 @@ def cmdline_argfix(cmdline):
else:
yield i

def parse_chia_plot_time(s):
# This will grow to try ISO8601 as well for when Chia logs that way
return pendulum.from_format(s, 'ddd MMM DD HH:mm:ss YYYY', locale='en', tz=None)

# TODO: be more principled and explicit about what we cache vs. what we look up
# dynamically from the logfile
class Job:
Expand Down Expand Up @@ -173,7 +178,7 @@ def init_from_logfile(self):
m = re.match(r'^Starting phase 1/4:.*\.\.\. (.*)', line)
if m:
# Mon Nov 2 08:39:53 2020
self.start_time = datetime.strptime(m.group(1), '%a %b %d %H:%M:%S %Y')
self.start_time = parse_chia_plot_time(m.group(1))
found_log = True
break # Stop reading lines in file

Expand Down