Skip to content

Commit

Permalink
Revises linux approach to try to get creation date, updates monkeypat…
Browse files Browse the repository at this point in the history
…ching approach, adds dockerfile for ubuntu testing
  • Loading branch information
karloskalcium committed Oct 19, 2024
1 parent 86e0416 commit bf2c775
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
21 changes: 12 additions & 9 deletions md2enex/md2enex.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import os.path
import pathlib
import platform
import subprocess
from enum import Enum
from inspect import getsourcefile
from pathlib import Path
Expand All @@ -32,10 +33,6 @@ class Doctypes(Enum):
ENML_DOCTYPE = '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml3.dtd">'


IMPORT_TAG_WITH_DATETIME = (
Appconfig.APP_NAME.value + "-import" + ":" + datetime.datetime.now().isoformat(timespec="seconds")
)

# taken from here https://dev.evernote.com/doc/articles/enml.php
INVALID_TAGS = [
"applet",
Expand Down Expand Up @@ -97,7 +94,7 @@ class Doctypes(Enum):

# stolen from https://stackoverflow.com/a/39501288/4907881
# returns creation date in seconds since Jan 1 1970 for a file in a platform-agnostic fashion
def creation_date_seconds(path_to_file):
def creation_date_seconds(path_to_file) -> float:
"""
Try to get the date that a file was created, falling back to when it was
last modified if that isn't possible.
Expand All @@ -110,9 +107,12 @@ def creation_date_seconds(path_to_file):
try:
return stat.st_birthtime
except AttributeError:
# We're probably on Linux. No easy way to get creation dates here,
# so we'll settle for when its content was last modified.
return stat.st_mtime
# We're probably on Linux. Try a system call
result = subprocess.run(["stat", "-c", "%W", path_to_file], capture_output=True)
if result.returncode == 0:
return float(result.stdout)
else:
return stat.st_mtime


def create_title(file: str) -> etree.Element:
Expand Down Expand Up @@ -141,7 +141,10 @@ def create_updated_date(file: str) -> etree.Element:

def create_tag() -> etree.Element:
tag_el = etree.Element("tag")
tag_el.text = IMPORT_TAG_WITH_DATETIME
tag_with_datetime = (
Appconfig.APP_NAME.value + "-import" + ":" + datetime.datetime.now().isoformat(timespec="seconds")
)
tag_el.text = tag_with_datetime
return tag_el


Expand Down
15 changes: 7 additions & 8 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,25 @@
from freezegun import freeze_time
from typer.testing import CliRunner

# freeze time before importing b/c of top-level time calls in md2enex
import md2enex
from md2enex.md2enex import app

FIXED_TIME = "2024-10-18 09:00:01"
freezer = freeze_time(FIXED_TIME)
freezer.start()

from md2enex.md2enex import app # noqa: E402

runner = CliRunner(mix_stderr=False)


# Monkeypatch a fixture to fix system file time calls
@pytest.fixture(autouse=True)
def _mock_filetimes(monkeypatch):
def _mock_times(monkeypatch):
"""Patches system calls related to file creation and modification times"""
CREATION_TIME = 1704110400.0 # Jan 1 2024, 12:00 UTC
MODIFICATION_TIME = 1730419199.0 # Oct 31 2024, 23:59:59 UTC
monkeypatch.setattr(os.path, "getctime", lambda _: CREATION_TIME)
monkeypatch.setattr(os.stat_result, "st_birthtime", CREATION_TIME)
# Here we patch our method to just always return the same thing
monkeypatch.setattr(md2enex.md2enex, "creation_date_seconds", lambda _: CREATION_TIME)
# Patch mtime here
monkeypatch.setattr(os.path, "getmtime", lambda _: MODIFICATION_TIME)
monkeypatch.setattr(os.stat_result, "st_mtime", MODIFICATION_TIME)


@freeze_time(FIXED_TIME)
Expand Down
17 changes: 17 additions & 0 deletions ubuntu.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM python:3.12.7-slim AS base
WORKDIR /app
ENV PATH="/app/.venv/bin:$PATH"
RUN apt-get update \
&& apt-get install -y make \
&& rm -rf /var/lib/apt/lists/*

FROM base AS builder
RUN pip install poetry
RUN python -m venv ./.venv
COPY ./pyproject.toml ./poetry.lock ./
RUN poetry install --only=main --no-root

COPY md2enex/ ./md2enex/
COPY Makefile README.md ./
COPY tests ./tests/
RUN poetry install --only-root

0 comments on commit bf2c775

Please sign in to comment.