summaryrefslogtreecommitdiff
path: root/archive/bin/rdate.py
blob: 9850dda584474a14db52f374fdc1ae671467db74 (plain)
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
#!/usr/bin/env python
"""
Remembers or restores the original posting date of entry.

Filename may be given in commandline argument, if not so,
program asks for it. 

If dealing with file first time, program reads the modified-time
and stores it as a tag:

#published %Y-%m-%d %H:%M:%S

e.g.

#published 2007-05-15 15:30:28

right after entry title line. The current modified-time of file is
preserved.

If run on the same file again and if the modified-time has been
changed since the time of tag storage, script restores the saved
time (sets it as the current modified-time).

That's all.

Program is rather pyblosxom utility than plugin, so there is no
reason to move it to plugins dir.

There are many ways how to use the script. I personally use CVS as
a primary storage for blog entries. But there is problem - CVS does
not honor mtime. So, I run the rdate.py once on files before adding
them to CVS. Then i update blog from the repository, but, we know, 
cvs does it without correct mtimes. So, after blog update I run the
rdate.py on files once more to restore saved time from #published
tags. Simple bash scripts does this automatically for me, so I care
for blog entry times no more.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Copyright 2007 David Zejda
"""
__author__ = "David Zejda blogger at zejda dot net"
__version__ = "rememberdate.py,v 0.1 2007/05/15 12:00:00 zejdad"
__url__ = "http://www.zejda.net/"
__description__ = "Remembers or restores the original posting date of entry for PyBlosxom."

import os, datetime, time, sys

if len(sys.argv) > 1:
	filename = sys.argv[1]
else:
	filename = raw_input("filename? ")

if not filename:
	print "No filename given.  Quitting. :-|"
	sys.exit(1)
   
try:
	filestats = os.stat(filename)
except:
	print "File not found.  Quitting. :-( "
	sys.exit(1)
	
print "Targetted file:", filename
filestats = os.stat(filename)
atime, mtime = filestats[7:9]

fmt = "#published %Y-%m-%d %H:%M:%S"

fmtime = datetime.datetime.fromtimestamp(mtime)
fmtime = fmtime.strftime(fmt)
print " current mtime:", fmtime

try:
	try:
		f = open(filename, "r")
		lines = f.readlines();
	except Exception, e:
	    print "Failed to open file for reading.", e
	    print "Quitting :-("
	    sys.exit(1)
finally:
	f.close()

# restore
remembered = lines[1].rstrip()
if (len(lines) > 2) and ( remembered.find("#published") > -1):
	print "   saved mtime:", remembered
	if remembered == fmtime:
		print "Times are equal, quitting :-)"
	else:
		print "Trying to restore remembered mtime."
		ttuple = time.strptime(remembered, fmt)
		print "  parsed tuple:", ttuple
		epoch = time.mktime(ttuple)
		print " epoch seconds:", epoch
		
		os.utime(filename, (atime, epoch))
		print "Succesfully restored :-)"
	sys.exit(0)

# remember
try:
	try:
		f = open(filename, "w")
		i = 0
		for line in lines:
			i = i + 1
			if i == 2:
				f.write(fmtime);
				f.write("\n");
			f.write(line);
	except Exception, e:
	    print "Failed to open file for writing.", e
	    print "Quitting :-("
	    sys.exit(1)
finally:
	f.close()

os.utime(filename, (atime, mtime))

print "Successfully remembered :-)"