/[qa]/trunk/pts/www/bin/update_news.py
ViewVC logotype

Contents of /trunk/pts/www/bin/update_news.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2523 - (show annotations) (download) (as text)
Thu May 5 16:18:16 2011 UTC (2 years ago) by hertzog
File MIME type: text/x-python
File size: 4033 byte(s)
Many fixes to cope with UTF-8 and to be able to regenerate all news.xml
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Make sure tabs expand to 8 spaces in vim
5 # vim: expandtab
6
7 # Copyright 2002-2011 Raphaƫl Hertzog
8 # Copyright 2006 Jeroen van Wolffelaar
9 # Copyright 2011 Jan Dittberner
10 # This file is distributed under the terms of the General Public License
11 # version 2 or (at your option) any later version.
12
13 import os, rfc822, sys, string, re, email, time, common, subprocess
14 import xml.dom
15
16 from config import dir, odir, root
17 from common import hash_name
18
19 os.environ["PATH"] = "/bin:/sbin:/usr/bin:/usr/sbin"
20
21 def read_msg(file):
22 f = open(file, "r")
23 msg = email.message_from_file(f)
24 f.close()
25 return msg
26
27 def create_html_file(pkg, source, htmlfile):
28 # Fixme: old code had some mechanism to clean attachments, apparantly
29 # produced by mhonarc
30 dir = os.path.dirname(htmlfile)
31 if not os.path.exists(dir): os.makedirs(dir)
32
33 mhproc = subprocess.Popen("""cd %s; \
34 mhonarc -rcfile /dev/stdin \
35 -rcfile %s/etc/mhonarc.rc \
36 -single %s 2> /dev/null""" % (dir, root, source), shell=True,
37 stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
38 close_fds=True)
39 (stdin, stdout, stderr) = (mhproc.stdin, mhproc.stdout, mhproc.stderr)
40 stdin.write("""
41 <MSGHEAD>
42 <h3><a href="../../%s.html">Back to %s PTS page</a></h3>
43 </MSGHEAD>
44 """ % (pkg, pkg))
45 stdin.close()
46 html = open(htmlfile+".new", "w")
47 html.writelines(stdout.readlines())
48 for f in html, stdout, stderr: f.close()
49 os.chmod(htmlfile+".new", 0664)
50 os.rename(htmlfile+".new", htmlfile)
51
52 def timestamp_to_rfc822date(timestamp):
53 """Format a timestamp in format YYYYMMDDThhmmssZ to a RFC 822 String.
54
55 >>> timestamp_to_rfc822date('20090718T163915Z')
56 'Sat, 18 Jul 2009 16:39:15 GMT'
57 """
58 return time.strftime("%a, %d %b %Y %H:%M:%S GMT",
59 time.strptime(timestamp, '%Y%m%dT%H%M%SZ'))
60
61 def add_resume_for_msg(pkg, file, elt, doc, kind):
62 if not os.path.isfile(file):
63 pass
64
65 msg = read_msg(file)
66 info = common.extract_info(msg)
67 sub_elt = doc.createElement("item")
68 sub_elt.appendChild(doc.createTextNode(info["subject"]))
69 if info.has_key("url"):
70 sub_elt.setAttribute("url", info["url"])
71 else:
72 htmlfile = pkg+"/"+kind+"/"+info["timestamp"]+".html"
73 hash = hash_name(pkg)
74 htmlpath = "%s/web/%s/%s" % (root, hash, htmlfile)
75 if not os.path.exists(htmlpath):
76 create_html_file(pkg, file, htmlpath)
77 sub_elt.setAttribute("url", htmlfile)
78 if info.has_key("date"):
79 sub_elt.setAttribute("date", info["date"])
80 sub_elt.setAttribute("rfc822date",
81 timestamp_to_rfc822date(info["timestamp"]))
82 if info.has_key("from_name"):
83 sub_elt.setAttribute("from", info["from_name"])
84 elt.appendChild(sub_elt)
85
86
87 if __name__ == '__main__':
88 # Create the XML documents
89 while 1:
90 line = sys.stdin.readline()
91 if not line: break #eof
92 pkg = line.strip()
93
94 doc = xml.dom.getDOMImplementation('minidom').createDocument(None, "news", None)
95 root_elt = doc.documentElement
96 hash = hash_name(pkg)
97
98 # Get news information : static/news/auto
99 for type, number in [("static", 5), ("news", 30)]:
100 elt = doc.createElement(type)
101 dir = "%s/base/%s/%s/%s" % (root, hash, pkg, type)
102 if not os.path.exists(dir): os.makedirs(dir)
103 entries = os.listdir(dir)
104 entries.sort()
105 entries.reverse()
106 for entry in entries[:number]:
107 add_resume_for_msg(pkg, dir+"/"+entry, elt, doc, type)
108 root_elt.appendChild(elt)
109
110 # Output the data to the XML file
111 try:
112 f = open("%s/%s/%s/news.xml" % (odir, hash, pkg), "w")
113 f.write(doc.toxml(encoding='UTF-8'))
114 f.close()
115 except Exception, msg:
116 sys.stderr.write("Can't update news.xml for %s\n%s\n" % (pkg, msg))

Properties

Name Value
svn:eol-style native
svn:executable *
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.5