-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetEmailFileTime
More file actions
executable file
·209 lines (167 loc) · 6.24 KB
/
setEmailFileTime
File metadata and controls
executable file
·209 lines (167 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python3
#
# setEmailFileTime.py: Set the file-system timestamps on an email file
# to match the MIME header "Date:" field.
# 2019-03-04: Written by Steven J. DeRose.
#
import argparse
import re
from subprocess import check_output, CalledProcessError
import codecs
from datetime import datetime
import logging
from PowerWalk import PowerWalk
lg = logging.getLogger()
__metadata__ = {
"title" : "setEmailFileTime",
"description" : "Set file-system timestamps on email from their Date field.",
"rightsHolder" : "Steven J. DeRose",
"creator" : "http://viaf.org/viaf/50334488",
"type" : "http://purl.org/dc/dcmitype/Software",
"language" : "Python 3.7",
"created" : "2019-03-04",
"modified" : "2024-05-14",
"publisher" : "http://github.com/sderose",
"license" : "https://creativecommons.org/licenses/by-sa/3.0/"
}
__version__ = __metadata__["modified"]
descr = """
=Description=
Set the file-system timestamps on a (MIME) email file
to match the MIME header "Date:" field.
This can also be done manually with something like:
for x in *.eml; do
touch -mt `grep --max 1 '^Date:' $x | cut -c 7- | date +"%Y%m%d%H%M"` $x
done
But that seems less than ideal to me....
You can also use this to extract and display the key header fields from an email file
(--list).
=Related Commands=
`stat`, `ls -lU`, `date`, `strftime`, etc.
=Known bugs and Limitations=
Doesn't do anything special for files containing ''multiple'' emails.
Could use a MIME library, but it seems like overkill.
=Licensing=
Copyright 2019-03-04 by Steven J. DeRose. This script is licensed under a
Creative Commons Attribution-Share-alike 3.0 unported license.
See http://creativecommons.org/licenses/by-sa/3.0/ for more information.
=History=
* 2019-03-04: Written by Steven J. DeRose.
* 2020-03-04: Lint, new layout.
* 2021-05-07: Cleanup. Add `--list`. Factor out time formats.
* 2021-06-18: Sync to PowerWalk updates.
=Options=
"""
###############################################################################
#
mainFields = [ "Date", "From", "To", "Subject" ]
# MIME "Date" fields should be like
# Fri, 17 Jul 2015 14:12:22 -0500
#
mimeTimeFormat = "%a, %d %b %Y %H:%M:%S %z"
touchTimeFormat = "%Y%m%d%H%M"
def getMainField(rec:str) -> str:
"""Check whether this is one of our main fields. If so,
return the field name and value. Otherwise, None, None.
Also handle escaped UTF in the values.
TODO: Fields also include any following indented lines.
"""
mat = re.match(r"^([-\w]+):\s*(.*)", rec)
if (not mat): return None, None
fdName = mat.group(1)
if (fdName not in mainFields): return None, None
fdVal = mat.group(2)
mat = re.match(r"=?(.*)?=$", fdVal.strip())
if (mat):
fdVal = re.sub(r"=([\da-f][\da-f])", unhex, fdVal, flags=re.I)
fdVal = fdVal.decode(encoding="utf-8")
return fdName, fdVal
def unhex(mat):
return chr(int(mat.group(1),16))
def doOneFile(path:str) -> None:
"""Deal with one individual file.
"""
print("File: %s" % (path))
fh = codecs.open(path, "rb", encoding="ASCII")
dateValue = None
for rec in fh.readlines():
#if (":" in rec): print(rec)
if (rec.strip() == ""): break
k, v = getMainField(rec)
#print("%s ::= %s" % (k or "-", v or "-"))
if (not k): continue
print("%-12s %s" % (k, v))
if (k == "Date"):
dateValue = v
break
fh.close()
if (dateValue is None):
lg.eMsg(0, "No 'Date:' line found in '%s'." % (path))
return
# Should be like 'Fri, 17 Jul 2015 14:12:22 -0500'
try:
dtObject = datetime.strptime(dateValue)
except ValueError as e:
lg.error("Unparseable time '%s':\n %s", dateValue, e)
# Format the time to what `touch` wants
try:
ftime = dtObject.strftime(touchTimeFormat)
except ValueError as e:
lg.warning("strftime could not convert to format '%s':\n %s",
touchTimeFormat, e)
if (not args.dry_run):
try:
check_output([ 'touch', '-mt', '%s' % (ftime) ])
except CalledProcessError as e:
lg.warning("`touch -mt %s` failed:\n %s", ftime, e)
###############################################################################
# Main
#
if __name__ == "__main__":
def processOptions():
try:
from BlockFormatter import BlockFormatter
parser = argparse.ArgumentParser(
description=descr, formatter_class=BlockFormatter)
except ImportError:
parser = argparse.ArgumentParser(description=descr)
parser.add_argument(
"--dry-run", "--test", action="store_true",
help='Do not actually change the filetime.')
parser.add_argument(
"--list", action="store_true",
help='Display the "main" header fields.')
parser.add_argument(
"--quiet", "-q", action="store_true",
help='Suppress most messages.')
parser.add_argument(
"--recursive", action="store_true",
help='Descend into subdirectories.')
parser.add_argument(
"--unicode",action="store_const", dest="iencoding",
const="utf8", help="Assume utf-8 for input files.")
parser.add_argument(
"--verbose", "-v", action="count", default=0,
help="Add more messages (repeatable).")
parser.add_argument(
"--version", action="version", version=__version__,
help="Display version information, then exit.")
parser.add_argument(
"files", type=str, nargs=argparse.REMAINDER,
help="Path(s) to input file(s)")
args0 = parser.parse_args()
if (args0.verbose): lg.setVerbose(args0.verbose)
return(args0)
###########################################################################
#
args = processOptions()
if (len(args.files) == 0):
lg.fatal("No file(s) specified.")
else:
depth = 0
pw = PowerWalk(args.files)
pw.applyOptionsFromArgparse(args)
for path0 in args.files:
fh0 = codecs.open(path0, "rb", encoding=args.iencoding)
doOneFile(path0)
fh0.close()