markdown-site-utils 0.3.0__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {markdown_site_utils-0.3.0 → markdown_site_utils-0.4.0}/PKG-INFO +31 -5
- {markdown_site_utils-0.3.0 → markdown_site_utils-0.4.0}/README.md +30 -4
- markdown_site_utils-0.4.0/mdsite/__init__.py +5 -0
- {markdown_site_utils-0.3.0 → markdown_site_utils-0.4.0}/mdsite/data.py +87 -4
- markdown_site_utils-0.4.0/mdsite/data_test.py +241 -0
- {markdown_site_utils-0.3.0 → markdown_site_utils-0.4.0}/pyproject.toml +1 -1
- {markdown_site_utils-0.3.0 → markdown_site_utils-0.4.0}/pyproject.toml.orig +1 -1
- markdown_site_utils-0.3.0/mdsite/__init__.py +0 -5
- markdown_site_utils-0.3.0/mdsite/data_test.py +0 -104
- {markdown_site_utils-0.3.0 → markdown_site_utils-0.4.0}/LICENSE +0 -0
- {markdown_site_utils-0.3.0 → markdown_site_utils-0.4.0}/mdsite/testdata/config.toml +0 -0
- {markdown_site_utils-0.3.0 → markdown_site_utils-0.4.0}/mdsite/testdata/conflict/index.md +0 -0
- {markdown_site_utils-0.3.0 → markdown_site_utils-0.4.0}/mdsite/testdata/conflict.md +0 -0
- {markdown_site_utils-0.3.0 → markdown_site_utils-0.4.0}/mdsite/testdata/json.md +0 -0
- {markdown_site_utils-0.3.0 → markdown_site_utils-0.4.0}/mdsite/testdata/nohead.md +0 -0
- {markdown_site_utils-0.3.0 → markdown_site_utils-0.4.0}/mdsite/testdata/nougat/config.json +0 -0
- {markdown_site_utils-0.3.0 → markdown_site_utils-0.4.0}/mdsite/testdata/toml.md +0 -0
- {markdown_site_utils-0.3.0 → markdown_site_utils-0.4.0}/mdsite/testdata/yaml.md +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: markdown-site-utils
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: Some utilities for managing Markdown files with metadata.
|
|
5
5
|
Author: Jacob Smullyan
|
|
6
6
|
Author-email: Jacob Smullyan <smulloni@smullyan.org>
|
|
@@ -90,10 +90,11 @@ raised. If nothing exists at the path, an empty dict is returned.
|
|
|
90
90
|
The returned dict contains the file's metadata, plus these keys:
|
|
91
91
|
|
|
92
92
|
- `content`: the Markdown rendered to HTML, as a `markupsafe.Markup` string
|
|
93
|
-
- `last_mod`:
|
|
94
|
-
`datetime` in UTC
|
|
95
|
-
|
|
96
|
-
itself if it
|
|
93
|
+
- `last_mod`: when the Markdown file last changed, as a timezone-aware
|
|
94
|
+
`datetime` in UTC (by default its modification time; see "Dates from git"
|
|
95
|
+
below). For a directory this is the time of its `index.md` (the same value
|
|
96
|
+
`get_data("/node/leaf/index")` returns), or of the directory itself if it
|
|
97
|
+
has no `index.md`
|
|
97
98
|
- `path`: the normalized path, with a leading slash
|
|
98
99
|
- `id`: the last component of the path, or `"index"` for the root
|
|
99
100
|
- `listing`: for a directory, a `(subdirectories, pages)` tuple of sorted
|
|
@@ -104,12 +105,37 @@ directory beneath it, keyed by path relative to the data directory (without a
|
|
|
104
105
|
leading slash; the root is `""`). It raises `mdsite.PathConflict` if any
|
|
105
106
|
directory in the tree conflicts with a file.
|
|
106
107
|
|
|
108
|
+
### Dates from git
|
|
109
|
+
|
|
110
|
+
File modification times depend on the checkout: a fresh clone stamps every
|
|
111
|
+
file with the time of the clone. If the data directory is in a git
|
|
112
|
+
repository, `last_mod` can come from its history instead:
|
|
113
|
+
|
|
114
|
+
mydb = mdsite.DB("/path/to/data/dir", last_mod="git")
|
|
115
|
+
|
|
116
|
+
Then `last_mod` is the committer time of the last commit that changed the
|
|
117
|
+
file, so it is the same in every clone. Uncommitted edits don't change it.
|
|
118
|
+
Files git has no history for (untracked files, or a directory without an
|
|
119
|
+
`index.md`) fall back to their modification time.
|
|
120
|
+
|
|
121
|
+
Git history is read once, on first use, by a single `git log`, and kept for
|
|
122
|
+
the life of the `DB`; create a new `DB` to see later commits. If git isn't
|
|
123
|
+
installed, the directory isn't in a repository, or the repository is a
|
|
124
|
+
shallow clone (whose dates would be wrong), `mdsite.GitHistoryError` is
|
|
125
|
+
raised. CI systems often clone shallowly by default; fetch full history
|
|
126
|
+
there (for GitHub Actions, `fetch-depth: 0` on `actions/checkout`).
|
|
127
|
+
|
|
107
128
|
The library also supports hierarchical configuration, also written in TOML,
|
|
108
129
|
YAML, or JSON, stored in files called `config.{toml,yaml,json}` depending on the
|
|
109
130
|
config language employed. `get_config(path)` merges the configuration found in
|
|
110
131
|
the data directory and each directory along `path`, with deeper files
|
|
111
132
|
overriding shallower ones.
|
|
112
133
|
|
|
134
|
+
## Changes in 0.4.0
|
|
135
|
+
|
|
136
|
+
- `DB(data_dir, last_mod="git")` takes `last_mod` from git history (see
|
|
137
|
+
"Dates from git"). The default, `last_mod="mtime"`, behaves as before.
|
|
138
|
+
|
|
113
139
|
## Upgrading to 0.3.0
|
|
114
140
|
|
|
115
141
|
- `last_mod` is now a timezone-aware UTC `datetime`. It used to be a naive
|
|
@@ -68,10 +68,11 @@ raised. If nothing exists at the path, an empty dict is returned.
|
|
|
68
68
|
The returned dict contains the file's metadata, plus these keys:
|
|
69
69
|
|
|
70
70
|
- `content`: the Markdown rendered to HTML, as a `markupsafe.Markup` string
|
|
71
|
-
- `last_mod`:
|
|
72
|
-
`datetime` in UTC
|
|
73
|
-
|
|
74
|
-
itself if it
|
|
71
|
+
- `last_mod`: when the Markdown file last changed, as a timezone-aware
|
|
72
|
+
`datetime` in UTC (by default its modification time; see "Dates from git"
|
|
73
|
+
below). For a directory this is the time of its `index.md` (the same value
|
|
74
|
+
`get_data("/node/leaf/index")` returns), or of the directory itself if it
|
|
75
|
+
has no `index.md`
|
|
75
76
|
- `path`: the normalized path, with a leading slash
|
|
76
77
|
- `id`: the last component of the path, or `"index"` for the root
|
|
77
78
|
- `listing`: for a directory, a `(subdirectories, pages)` tuple of sorted
|
|
@@ -82,12 +83,37 @@ directory beneath it, keyed by path relative to the data directory (without a
|
|
|
82
83
|
leading slash; the root is `""`). It raises `mdsite.PathConflict` if any
|
|
83
84
|
directory in the tree conflicts with a file.
|
|
84
85
|
|
|
86
|
+
### Dates from git
|
|
87
|
+
|
|
88
|
+
File modification times depend on the checkout: a fresh clone stamps every
|
|
89
|
+
file with the time of the clone. If the data directory is in a git
|
|
90
|
+
repository, `last_mod` can come from its history instead:
|
|
91
|
+
|
|
92
|
+
mydb = mdsite.DB("/path/to/data/dir", last_mod="git")
|
|
93
|
+
|
|
94
|
+
Then `last_mod` is the committer time of the last commit that changed the
|
|
95
|
+
file, so it is the same in every clone. Uncommitted edits don't change it.
|
|
96
|
+
Files git has no history for (untracked files, or a directory without an
|
|
97
|
+
`index.md`) fall back to their modification time.
|
|
98
|
+
|
|
99
|
+
Git history is read once, on first use, by a single `git log`, and kept for
|
|
100
|
+
the life of the `DB`; create a new `DB` to see later commits. If git isn't
|
|
101
|
+
installed, the directory isn't in a repository, or the repository is a
|
|
102
|
+
shallow clone (whose dates would be wrong), `mdsite.GitHistoryError` is
|
|
103
|
+
raised. CI systems often clone shallowly by default; fetch full history
|
|
104
|
+
there (for GitHub Actions, `fetch-depth: 0` on `actions/checkout`).
|
|
105
|
+
|
|
85
106
|
The library also supports hierarchical configuration, also written in TOML,
|
|
86
107
|
YAML, or JSON, stored in files called `config.{toml,yaml,json}` depending on the
|
|
87
108
|
config language employed. `get_config(path)` merges the configuration found in
|
|
88
109
|
the data directory and each directory along `path`, with deeper files
|
|
89
110
|
overriding shallower ones.
|
|
90
111
|
|
|
112
|
+
## Changes in 0.4.0
|
|
113
|
+
|
|
114
|
+
- `DB(data_dir, last_mod="git")` takes `last_mod` from git history (see
|
|
115
|
+
"Dates from git"). The default, `last_mod="mtime"`, behaves as before.
|
|
116
|
+
|
|
91
117
|
## Upgrading to 0.3.0
|
|
92
118
|
|
|
93
119
|
- `last_mod` is now a timezone-aware UTC `datetime`. It used to be a naive
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import datetime
|
|
2
2
|
import json
|
|
3
3
|
import os
|
|
4
|
+
import pathlib
|
|
5
|
+
import subprocess
|
|
4
6
|
import tomllib
|
|
5
7
|
|
|
6
8
|
import markdown2
|
|
@@ -16,12 +18,71 @@ DATA_SEPS = {
|
|
|
16
18
|
"---": ("---", yaml.safe_load),
|
|
17
19
|
}
|
|
18
20
|
|
|
21
|
+
LAST_MOD_SOURCES = ("mtime", "git")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class GitHistoryError(RuntimeError):
|
|
25
|
+
"""Raised when last_mod="git" but no full git history is available."""
|
|
26
|
+
|
|
19
27
|
|
|
20
28
|
def _getmtime(path):
|
|
21
29
|
mtime = os.path.getmtime(path)
|
|
22
30
|
return datetime.datetime.fromtimestamp(mtime, tz=datetime.UTC)
|
|
23
31
|
|
|
24
32
|
|
|
33
|
+
def _git(data_dir, *args):
|
|
34
|
+
try:
|
|
35
|
+
result = subprocess.run(
|
|
36
|
+
["git", "-C", data_dir, *args],
|
|
37
|
+
capture_output=True,
|
|
38
|
+
text=True,
|
|
39
|
+
encoding="utf-8",
|
|
40
|
+
check=True,
|
|
41
|
+
)
|
|
42
|
+
except FileNotFoundError as exc:
|
|
43
|
+
raise GitHistoryError("git is not installed") from exc
|
|
44
|
+
except subprocess.CalledProcessError as exc:
|
|
45
|
+
raise GitHistoryError(
|
|
46
|
+
f"git {args[0]} failed in {data_dir}: {exc.stderr.strip()}"
|
|
47
|
+
) from exc
|
|
48
|
+
return result.stdout
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _git_commit_times(data_dir):
|
|
52
|
+
"""Map each file under data_dir that git has history for (as a POSIX path
|
|
53
|
+
relative to data_dir) to the committer time of the last commit that
|
|
54
|
+
changed it, as an aware UTC datetime."""
|
|
55
|
+
if _git(data_dir, "rev-parse", "--is-shallow-repository").strip() == "true":
|
|
56
|
+
raise GitHistoryError(
|
|
57
|
+
f"{data_dir} is in a shallow clone; its commit dates would be wrong. "
|
|
58
|
+
"Fetch full history (git fetch --unshallow)."
|
|
59
|
+
)
|
|
60
|
+
# %x00 marks each commit's line, so it can't be confused with a path.
|
|
61
|
+
# --relative makes paths relative to data_dir; quotePath=false keeps
|
|
62
|
+
# non-ASCII names unescaped.
|
|
63
|
+
log = _git(
|
|
64
|
+
data_dir,
|
|
65
|
+
"-c",
|
|
66
|
+
"core.quotePath=false",
|
|
67
|
+
"log",
|
|
68
|
+
"--relative",
|
|
69
|
+
"--name-only",
|
|
70
|
+
"--no-renames",
|
|
71
|
+
"--format=%x00%ct",
|
|
72
|
+
"--",
|
|
73
|
+
".",
|
|
74
|
+
)
|
|
75
|
+
times = {}
|
|
76
|
+
when = None
|
|
77
|
+
for line in log.splitlines():
|
|
78
|
+
if line.startswith("\0"):
|
|
79
|
+
when = datetime.datetime.fromtimestamp(int(line[1:]), tz=datetime.UTC)
|
|
80
|
+
elif line:
|
|
81
|
+
# Newest commits come first, so the first sighting wins.
|
|
82
|
+
times.setdefault(line, when)
|
|
83
|
+
return times
|
|
84
|
+
|
|
85
|
+
|
|
25
86
|
def _getlisting(dir):
|
|
26
87
|
_, dirs, files = next(os.walk(dir))
|
|
27
88
|
files = [f[:-3] for f in files if f.endswith(".md")]
|
|
@@ -79,8 +140,28 @@ CONFIG_FILES = (
|
|
|
79
140
|
|
|
80
141
|
|
|
81
142
|
class DB:
|
|
82
|
-
def __init__(self, data_dir):
|
|
143
|
+
def __init__(self, data_dir, last_mod="mtime"):
|
|
144
|
+
"""`last_mod` chooses where `last_mod` values come from: "mtime" (the
|
|
145
|
+
file's modification time) or "git" (the committer time of the last
|
|
146
|
+
commit that changed the file; files git doesn't track fall back to
|
|
147
|
+
their mtime). Git history is read once, on first use, per DB."""
|
|
148
|
+
if last_mod not in LAST_MOD_SOURCES:
|
|
149
|
+
raise ValueError(
|
|
150
|
+
f"last_mod must be one of {LAST_MOD_SOURCES}, not {last_mod!r}"
|
|
151
|
+
)
|
|
83
152
|
self.data_dir = data_dir
|
|
153
|
+
self.last_mod_source = last_mod
|
|
154
|
+
self._commit_times = None
|
|
155
|
+
|
|
156
|
+
def _last_mod(self, fs_path):
|
|
157
|
+
if self.last_mod_source == "git":
|
|
158
|
+
if self._commit_times is None:
|
|
159
|
+
self._commit_times = _git_commit_times(self.data_dir)
|
|
160
|
+
rel = pathlib.Path(os.path.relpath(fs_path, self.data_dir)).as_posix()
|
|
161
|
+
when = self._commit_times.get(rel)
|
|
162
|
+
if when is not None:
|
|
163
|
+
return when
|
|
164
|
+
return _getmtime(fs_path)
|
|
84
165
|
|
|
85
166
|
def get_config(self, path):
|
|
86
167
|
"""Return hierarchically overrideable configuration,
|
|
@@ -120,15 +201,17 @@ class DB:
|
|
|
120
201
|
if listing is None:
|
|
121
202
|
listing = _getlisting(file_path)
|
|
122
203
|
index_path = os.path.join(file_path, "index.md")
|
|
123
|
-
# Use the index file's
|
|
204
|
+
# Use the index file's time, as get_data("<dir>/index") would;
|
|
124
205
|
# fall back to the directory's mtime if there is no index file.
|
|
125
|
-
mtime =
|
|
206
|
+
mtime = self._last_mod(
|
|
207
|
+
index_path if os.path.exists(index_path) else file_path
|
|
208
|
+
)
|
|
126
209
|
file_path = index_path
|
|
127
210
|
else:
|
|
128
211
|
file_path += ".md"
|
|
129
212
|
if not os.path.exists(file_path):
|
|
130
213
|
return {}
|
|
131
|
-
mtime =
|
|
214
|
+
mtime = self._last_mod(file_path)
|
|
132
215
|
if os.path.basename(file_path) == "index.md" and not os.path.exists(file_path):
|
|
133
216
|
data = {"content": ""}
|
|
134
217
|
else:
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import datetime
|
|
2
|
+
import os
|
|
3
|
+
import subprocess
|
|
4
|
+
import tempfile
|
|
5
|
+
import unittest
|
|
6
|
+
|
|
7
|
+
from mdsite.data import DB, GitHistoryError, PathConflict
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class DBTests(unittest.TestCase):
|
|
11
|
+
def setUp(self):
|
|
12
|
+
self.data_dir = os.path.join(
|
|
13
|
+
os.path.dirname(os.path.realpath(__file__)), "testdata"
|
|
14
|
+
)
|
|
15
|
+
self.db = DB(self.data_dir)
|
|
16
|
+
|
|
17
|
+
def testParseTOML(self):
|
|
18
|
+
result = self.db.get_data("toml")
|
|
19
|
+
self.assertEqual(result["owner"]["name"], "Tom Preston-Werner")
|
|
20
|
+
self.assertEqual(
|
|
21
|
+
result["owner"]["dob"].isoformat(),
|
|
22
|
+
datetime.datetime(1979, 5, 27, 7, 32, tzinfo=datetime.UTC).isoformat(),
|
|
23
|
+
)
|
|
24
|
+
self.assertIs(result["owner"]["dob"].tzinfo, datetime.UTC)
|
|
25
|
+
|
|
26
|
+
def testParseYAML(self):
|
|
27
|
+
result = self.db.get_data("yaml")
|
|
28
|
+
self.assertEqual(result["owner"]["name"], "YAML Guy")
|
|
29
|
+
self.assertEqual(result["database"]["enabled"], True)
|
|
30
|
+
self.assertEqual(result["database"]["connection_max"], 5000)
|
|
31
|
+
|
|
32
|
+
def testParseJSON(self):
|
|
33
|
+
result = self.db.get_data("json")
|
|
34
|
+
self.assertEqual(result["title"], "Well, Well!")
|
|
35
|
+
|
|
36
|
+
def testConflict(self):
|
|
37
|
+
with self.assertRaises(PathConflict):
|
|
38
|
+
self.db.get_data("conflict")
|
|
39
|
+
|
|
40
|
+
def testConfig(self):
|
|
41
|
+
config = self.db.get_config("/")
|
|
42
|
+
self.assertEqual(config["template"], "base.tmpl")
|
|
43
|
+
config = self.db.get_config("/nougat")
|
|
44
|
+
self.assertEqual(config["template"], "dessert.tmpl")
|
|
45
|
+
|
|
46
|
+
def testListing(self):
|
|
47
|
+
data = self.db.get_data("/")
|
|
48
|
+
self.assertEqual(
|
|
49
|
+
data["listing"],
|
|
50
|
+
(["conflict", "nougat"], ["conflict", "json", "nohead", "toml", "yaml"]),
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
def testRecursiveListing(self):
|
|
54
|
+
listing = self.db.get_recursive_listing("/")
|
|
55
|
+
self.assertEqual(
|
|
56
|
+
listing,
|
|
57
|
+
{
|
|
58
|
+
"": (
|
|
59
|
+
["conflict", "nougat"],
|
|
60
|
+
["conflict", "json", "nohead", "toml", "yaml"],
|
|
61
|
+
),
|
|
62
|
+
"conflict": ([], ["index"]),
|
|
63
|
+
"nougat": ([], []),
|
|
64
|
+
},
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
def testRecursiveData(self):
|
|
68
|
+
all_data = self.db.get_recursive_data("/nougat")
|
|
69
|
+
self.assertEqual(set(all_data), {"nougat"})
|
|
70
|
+
|
|
71
|
+
def testNoDataHeader(self):
|
|
72
|
+
data = self.db.get_data("nohead")
|
|
73
|
+
self.assertTrue(data.get("content", ""))
|
|
74
|
+
self.assertIn("This markdown file", data.get("content", ""))
|
|
75
|
+
|
|
76
|
+
def testLastModIsAwareUTC(self):
|
|
77
|
+
for path in ("toml", "nougat"):
|
|
78
|
+
last_mod = self.db.get_data(path)["last_mod"]
|
|
79
|
+
self.assertEqual(last_mod.tzinfo, datetime.UTC)
|
|
80
|
+
fs_path = os.path.join(self.data_dir, path)
|
|
81
|
+
if not os.path.isdir(fs_path):
|
|
82
|
+
fs_path += ".md"
|
|
83
|
+
self.assertAlmostEqual(
|
|
84
|
+
last_mod.timestamp(), os.path.getmtime(fs_path), delta=1e-6
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
def testDirectoryLastModComesFromIndexFile(self):
|
|
88
|
+
with tempfile.TemporaryDirectory() as data_dir:
|
|
89
|
+
leaf_dir = os.path.join(data_dir, "a", "b")
|
|
90
|
+
os.makedirs(leaf_dir)
|
|
91
|
+
index_path = os.path.join(leaf_dir, "index.md")
|
|
92
|
+
with open(index_path, "w") as fp:
|
|
93
|
+
fp.write("# Leaf\n")
|
|
94
|
+
# Make the index file and its directory disagree, as they do
|
|
95
|
+
# when index.md is edited in place.
|
|
96
|
+
os.utime(index_path, (1_000_000_000, 1_000_000_000))
|
|
97
|
+
os.utime(leaf_dir, (2_000_000_000, 2_000_000_000))
|
|
98
|
+
db = DB(data_dir)
|
|
99
|
+
expected = datetime.datetime.fromtimestamp(1_000_000_000, tz=datetime.UTC)
|
|
100
|
+
self.assertEqual(db.get_data("/a/b/index")["last_mod"], expected)
|
|
101
|
+
self.assertEqual(db.get_data("/a/b")["last_mod"], expected)
|
|
102
|
+
|
|
103
|
+
def testUnknownLastModSourceIsRejected(self):
|
|
104
|
+
with self.assertRaises(ValueError):
|
|
105
|
+
DB(self.data_dir, last_mod="ctime")
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
T1 = 1_700_000_000
|
|
109
|
+
T2 = 1_710_000_000
|
|
110
|
+
MTIME = 1_000_000_000
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def utc(ts):
|
|
114
|
+
return datetime.datetime.fromtimestamp(ts, tz=datetime.UTC)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class GitLastModTests(unittest.TestCase):
|
|
118
|
+
"""last_mod="git": each repository is built here, with pinned commit
|
|
119
|
+
dates and no dependence on the user's git configuration."""
|
|
120
|
+
|
|
121
|
+
def setUp(self):
|
|
122
|
+
self._tmp = tempfile.TemporaryDirectory()
|
|
123
|
+
self.addCleanup(self._tmp.cleanup)
|
|
124
|
+
self.repo = os.path.join(self._tmp.name, "repo")
|
|
125
|
+
os.makedirs(self.repo)
|
|
126
|
+
self.git("init", "-q")
|
|
127
|
+
|
|
128
|
+
def git(self, *args, when=None, cwd=None):
|
|
129
|
+
env = {
|
|
130
|
+
"PATH": os.environ["PATH"],
|
|
131
|
+
"HOME": self._tmp.name,
|
|
132
|
+
"GIT_CONFIG_NOSYSTEM": "1",
|
|
133
|
+
"GIT_AUTHOR_NAME": "Test",
|
|
134
|
+
"GIT_AUTHOR_EMAIL": "test@example.com",
|
|
135
|
+
"GIT_COMMITTER_NAME": "Test",
|
|
136
|
+
"GIT_COMMITTER_EMAIL": "test@example.com",
|
|
137
|
+
}
|
|
138
|
+
if when is not None:
|
|
139
|
+
env["GIT_AUTHOR_DATE"] = env["GIT_COMMITTER_DATE"] = f"@{when} +0000"
|
|
140
|
+
subprocess.run(
|
|
141
|
+
["git", *args],
|
|
142
|
+
cwd=cwd or self.repo,
|
|
143
|
+
env=env,
|
|
144
|
+
check=True,
|
|
145
|
+
capture_output=True,
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
def write(self, rel, text="+++\ntitle = 'x'\n+++\nbody\n"):
|
|
149
|
+
path = os.path.join(self.repo, rel)
|
|
150
|
+
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
151
|
+
with open(path, "w") as fp:
|
|
152
|
+
fp.write(text)
|
|
153
|
+
# A distinctive mtime, so a fallback to it is visible.
|
|
154
|
+
os.utime(path, (MTIME, MTIME))
|
|
155
|
+
return path
|
|
156
|
+
|
|
157
|
+
def commit(self, when, *paths):
|
|
158
|
+
self.git("add", *paths)
|
|
159
|
+
self.git("commit", "-q", "-m", "c", when=when)
|
|
160
|
+
|
|
161
|
+
def test_last_mod_is_the_last_commit_that_changed_the_file(self):
|
|
162
|
+
self.write("a.md")
|
|
163
|
+
self.write("b.md")
|
|
164
|
+
self.commit(T1, "a.md", "b.md")
|
|
165
|
+
self.write("b.md", "changed\n")
|
|
166
|
+
self.commit(T2, "b.md")
|
|
167
|
+
db = DB(self.repo, last_mod="git")
|
|
168
|
+
self.assertEqual(db.get_data("a")["last_mod"], utc(T1))
|
|
169
|
+
self.assertEqual(db.get_data("b")["last_mod"], utc(T2))
|
|
170
|
+
|
|
171
|
+
def test_uncommitted_edits_keep_the_commit_time(self):
|
|
172
|
+
self.write("a.md")
|
|
173
|
+
self.commit(T1, "a.md")
|
|
174
|
+
self.write("a.md", "edited but not committed\n")
|
|
175
|
+
self.assertEqual(
|
|
176
|
+
DB(self.repo, last_mod="git").get_data("a")["last_mod"], utc(T1)
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
def test_untracked_files_fall_back_to_mtime(self):
|
|
180
|
+
self.write("a.md")
|
|
181
|
+
self.commit(T1, "a.md")
|
|
182
|
+
self.write("new.md")
|
|
183
|
+
db = DB(self.repo, last_mod="git")
|
|
184
|
+
self.assertEqual(db.get_data("new")["last_mod"], utc(MTIME))
|
|
185
|
+
|
|
186
|
+
def test_directory_uses_its_index_commit_time(self):
|
|
187
|
+
self.write("section/index.md")
|
|
188
|
+
self.commit(T1, "section/index.md")
|
|
189
|
+
db = DB(self.repo, last_mod="git")
|
|
190
|
+
self.assertEqual(db.get_data("/section")["last_mod"], utc(T1))
|
|
191
|
+
|
|
192
|
+
def test_data_dir_may_be_a_subdirectory_of_the_repository(self):
|
|
193
|
+
self.write("site/data/a.md")
|
|
194
|
+
self.commit(T1, "site/data/a.md")
|
|
195
|
+
db = DB(os.path.join(self.repo, "site", "data"), last_mod="git")
|
|
196
|
+
self.assertEqual(db.get_data("a")["last_mod"], utc(T1))
|
|
197
|
+
|
|
198
|
+
def test_non_ascii_file_names(self):
|
|
199
|
+
self.write("café.md")
|
|
200
|
+
self.commit(T1, "café.md")
|
|
201
|
+
self.assertEqual(
|
|
202
|
+
DB(self.repo, last_mod="git").get_data("café")["last_mod"], utc(T1)
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
def test_directory_outside_a_repository_is_an_error(self):
|
|
206
|
+
outside = os.path.join(self._tmp.name, "plain")
|
|
207
|
+
os.makedirs(outside)
|
|
208
|
+
with open(os.path.join(outside, "a.md"), "w") as fp:
|
|
209
|
+
fp.write("x\n")
|
|
210
|
+
# Keep git from finding a repository above the temporary directory.
|
|
211
|
+
os.environ["GIT_CEILING_DIRECTORIES"] = self._tmp.name
|
|
212
|
+
self.addCleanup(os.environ.pop, "GIT_CEILING_DIRECTORIES", None)
|
|
213
|
+
with self.assertRaises(GitHistoryError):
|
|
214
|
+
DB(outside, last_mod="git").get_data("a")
|
|
215
|
+
|
|
216
|
+
def test_shallow_clone_is_an_error(self):
|
|
217
|
+
self.write("a.md")
|
|
218
|
+
self.commit(T1, "a.md")
|
|
219
|
+
self.write("a.md", "again\n")
|
|
220
|
+
self.commit(T2, "a.md")
|
|
221
|
+
clone = os.path.join(self._tmp.name, "shallow")
|
|
222
|
+
self.git(
|
|
223
|
+
"clone",
|
|
224
|
+
"-q",
|
|
225
|
+
"--depth",
|
|
226
|
+
"1",
|
|
227
|
+
f"file://{self.repo}",
|
|
228
|
+
clone,
|
|
229
|
+
cwd=self._tmp.name,
|
|
230
|
+
)
|
|
231
|
+
with self.assertRaises(GitHistoryError):
|
|
232
|
+
DB(clone, last_mod="git").get_data("a")
|
|
233
|
+
|
|
234
|
+
def test_mtime_remains_the_default(self):
|
|
235
|
+
self.write("a.md")
|
|
236
|
+
self.commit(T1, "a.md")
|
|
237
|
+
self.assertEqual(DB(self.repo).get_data("a")["last_mod"], utc(MTIME))
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
if __name__ == "__main__":
|
|
241
|
+
unittest.main()
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import datetime
|
|
2
|
-
import os
|
|
3
|
-
import tempfile
|
|
4
|
-
import unittest
|
|
5
|
-
|
|
6
|
-
from mdsite.data import DB, PathConflict
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class DBTests(unittest.TestCase):
|
|
10
|
-
def setUp(self):
|
|
11
|
-
self.data_dir = os.path.join(
|
|
12
|
-
os.path.dirname(os.path.realpath(__file__)), "testdata"
|
|
13
|
-
)
|
|
14
|
-
self.db = DB(self.data_dir)
|
|
15
|
-
|
|
16
|
-
def testParseTOML(self):
|
|
17
|
-
result = self.db.get_data("toml")
|
|
18
|
-
self.assertEqual(result["owner"]["name"], "Tom Preston-Werner")
|
|
19
|
-
self.assertEqual(
|
|
20
|
-
result["owner"]["dob"].isoformat(),
|
|
21
|
-
datetime.datetime(1979, 5, 27, 7, 32, tzinfo=datetime.UTC).isoformat(),
|
|
22
|
-
)
|
|
23
|
-
self.assertIs(result["owner"]["dob"].tzinfo, datetime.UTC)
|
|
24
|
-
|
|
25
|
-
def testParseYAML(self):
|
|
26
|
-
result = self.db.get_data("yaml")
|
|
27
|
-
self.assertEqual(result["owner"]["name"], "YAML Guy")
|
|
28
|
-
self.assertEqual(result["database"]["enabled"], True)
|
|
29
|
-
self.assertEqual(result["database"]["connection_max"], 5000)
|
|
30
|
-
|
|
31
|
-
def testParseJSON(self):
|
|
32
|
-
result = self.db.get_data("json")
|
|
33
|
-
self.assertEqual(result["title"], "Well, Well!")
|
|
34
|
-
|
|
35
|
-
def testConflict(self):
|
|
36
|
-
with self.assertRaises(PathConflict):
|
|
37
|
-
self.db.get_data("conflict")
|
|
38
|
-
|
|
39
|
-
def testConfig(self):
|
|
40
|
-
config = self.db.get_config("/")
|
|
41
|
-
self.assertEqual(config["template"], "base.tmpl")
|
|
42
|
-
config = self.db.get_config("/nougat")
|
|
43
|
-
self.assertEqual(config["template"], "dessert.tmpl")
|
|
44
|
-
|
|
45
|
-
def testListing(self):
|
|
46
|
-
data = self.db.get_data("/")
|
|
47
|
-
self.assertEqual(
|
|
48
|
-
data["listing"],
|
|
49
|
-
(["conflict", "nougat"], ["conflict", "json", "nohead", "toml", "yaml"]),
|
|
50
|
-
)
|
|
51
|
-
|
|
52
|
-
def testRecursiveListing(self):
|
|
53
|
-
listing = self.db.get_recursive_listing("/")
|
|
54
|
-
self.assertEqual(
|
|
55
|
-
listing,
|
|
56
|
-
{
|
|
57
|
-
"": (
|
|
58
|
-
["conflict", "nougat"],
|
|
59
|
-
["conflict", "json", "nohead", "toml", "yaml"],
|
|
60
|
-
),
|
|
61
|
-
"conflict": ([], ["index"]),
|
|
62
|
-
"nougat": ([], []),
|
|
63
|
-
},
|
|
64
|
-
)
|
|
65
|
-
|
|
66
|
-
def testRecursiveData(self):
|
|
67
|
-
all_data = self.db.get_recursive_data("/nougat")
|
|
68
|
-
self.assertEqual(set(all_data), {"nougat"})
|
|
69
|
-
|
|
70
|
-
def testNoDataHeader(self):
|
|
71
|
-
data = self.db.get_data("nohead")
|
|
72
|
-
self.assertTrue(data.get("content", ""))
|
|
73
|
-
self.assertIn("This markdown file", data.get("content", ""))
|
|
74
|
-
|
|
75
|
-
def testLastModIsAwareUTC(self):
|
|
76
|
-
for path in ("toml", "nougat"):
|
|
77
|
-
last_mod = self.db.get_data(path)["last_mod"]
|
|
78
|
-
self.assertEqual(last_mod.tzinfo, datetime.UTC)
|
|
79
|
-
fs_path = os.path.join(self.data_dir, path)
|
|
80
|
-
if not os.path.isdir(fs_path):
|
|
81
|
-
fs_path += ".md"
|
|
82
|
-
self.assertAlmostEqual(
|
|
83
|
-
last_mod.timestamp(), os.path.getmtime(fs_path), delta=1e-6
|
|
84
|
-
)
|
|
85
|
-
|
|
86
|
-
def testDirectoryLastModComesFromIndexFile(self):
|
|
87
|
-
with tempfile.TemporaryDirectory() as data_dir:
|
|
88
|
-
leaf_dir = os.path.join(data_dir, "a", "b")
|
|
89
|
-
os.makedirs(leaf_dir)
|
|
90
|
-
index_path = os.path.join(leaf_dir, "index.md")
|
|
91
|
-
with open(index_path, "w") as fp:
|
|
92
|
-
fp.write("# Leaf\n")
|
|
93
|
-
# Make the index file and its directory disagree, as they do
|
|
94
|
-
# when index.md is edited in place.
|
|
95
|
-
os.utime(index_path, (1_000_000_000, 1_000_000_000))
|
|
96
|
-
os.utime(leaf_dir, (2_000_000_000, 2_000_000_000))
|
|
97
|
-
db = DB(data_dir)
|
|
98
|
-
expected = datetime.datetime.fromtimestamp(1_000_000_000, tz=datetime.UTC)
|
|
99
|
-
self.assertEqual(db.get_data("/a/b/index")["last_mod"], expected)
|
|
100
|
-
self.assertEqual(db.get_data("/a/b")["last_mod"], expected)
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if __name__ == "__main__":
|
|
104
|
-
unittest.main()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|