svn commit: samba-web r339 - in trunk/scripts: .
deryck at samba.org
deryck at samba.org
Mon Sep 20 19:33:53 GMT 2004
Author: deryck
Date: 2004-09-20 19:33:53 +0000 (Mon, 20 Sep 2004)
New Revision: 339
WebSVN: http://websvn.samba.org/websvn/changeset.php?rep=samba-web&path=/trunk/scripts&rev=339&nolog=1
Log:
This is a substative rewrite of the news script (had to
do something while the power was out this weekend :-) ).
Mostly the changes here concern date handling.
Dates aren't formatted until writing to the page; dates
are included in headlines file; created a function for
formatting dates; and generally just cleaned up the script
format.
Also, I added a copyright/GPL statement to the file, not that
it's really source code like the rest of Samba, but it's viewable
since it's housed in a "scripts" directory. In case someone
wandered across it through google, I wanted to be clear that it's
freely available for use, modification, etc.
--deryck
Modified:
trunk/scripts/updateNews.py
Changeset:
Modified: trunk/scripts/updateNews.py
===================================================================
--- trunk/scripts/updateNews.py 2004-09-19 14:19:05 UTC (rev 338)
+++ trunk/scripts/updateNews.py 2004-09-20 19:33:53 UTC (rev 339)
@@ -1,17 +1,24 @@
#! /usr/bin/python
-#
-# A script for handling files on news.samba.org.
+# Copyright (C) 2004 by Deryck Hodge <deryck at samba.org>
#
-# This script reads the news directory and creates an index.html
-# file for news.samba.org. It also creates a permanent archive for
-# each topic by writing an index.html file in each sub directory.
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of the
+# License, or (at your option) any later version.
#
-# Before this can be run, a calendar directory must be created. The
-# calendar updates from the team dir should be moved to the new
-# calendar dir, so calendar updates are not read as news items.
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+# USA
+""" updateNews.py -- a script for handling files on news.samba.org."""
+
import os, time, re
from stat import ST_MTIME
@@ -20,7 +27,6 @@
# Get list of news directories. Then, pair up dir name with dir files.
-
os.chdir(top_dir)
topics = []
@@ -33,12 +39,26 @@
topics_files = {}
for topic in topics:
topics_files[topic] = os.listdir(topic)
+
+# Write list of topics to 'sections.html'
+sections = open('sections.html', 'w')
+sections.write('<ul>')
+for topic in topics:
+ sections.write('<li><a href="/samba/news/' + topic + '/">' + topic + '/</a></li>')
+sections.write('</ul>')
+sections.close()
+
+# Define function for converting date tuple to string
+def date_to_str((year, mn, dy)):
+ mn_name = ('', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')
+
+ return str(dy) + ' ' + mn_name[mn] + ' ' + str(year)
+
+
# Loop through each directory, find all stories, and create main index.html
-
all_stories = {}
-
for topic in topics:
os.chdir(topic)
topic_stories = {}
@@ -47,16 +67,15 @@
if file in not_news: continue
f_lines = open(file, 'r').readlines()
story = "".join(f_lines) + '<div class="reference">Link: <a href="/samba/news/' + topic + '/#' + file[:-5] + '">' + topic + '/</a></div>\n\n'
- f_stats = os.stat(file)
- f_date = time.strftime("%B %d %Y", time.localtime(f_stats[ST_MTIME]))
- # group stories on the same date under that one date
+ f_date = time.gmtime(os.stat(file)[ST_MTIME])[:3]
+ # Temporarily group the topic's stories under date modified
if f_date in topic_stories.keys():
topic_stories[f_date] += story
else:
topic_stories[f_date] = story
for h2date in topic_stories.keys():
- # again, group stories from same date under that date
+ # Now lump topic groups into one dict, again sorted by mdate
if h2date in all_stories.keys():
all_stories[h2date] += topic_stories[h2date]
else:
@@ -77,7 +96,7 @@
for h2date in post_dates:
news_text = all_stories[h2date]
index = open('index.html', 'a')
- index.write('<h2>' + h2date + '</h2>\n\n')
+ index.write('<h2>' + date_to_str(h2date) + '</h2>\n\n')
index.write(news_text)
index.close()
@@ -87,7 +106,6 @@
# Define function that creates index.html for each directory.
-
def archive(dir, files):
topic = dir
os.chdir(topic)
@@ -98,8 +116,7 @@
for file in filelist:
if file in not_news: continue
f_lines = open(file, 'r').readlines()
- f_stats = os.stat(file)
- f_date = time.strftime("%B %d %Y", time.localtime(f_stats[ST_MTIME]))
+ f_date = time.gmtime(os.stat(file)[ST_MTIME])[:3]
# group stories from same date under that one date
if f_date in stories_by_date.keys():
stories_by_date[f_date].append("".join(f_lines))
@@ -121,7 +138,7 @@
for h2date in post_dates:
news_text = "".join(stories_by_date[h2date])
index = open('index.html', 'a')
- index.write('<h2>' + h2date + '</h2>\n\n')
+ index.write('<h2>' + date_to_str(h2date) + '</h2>\n\n')
index.write(news_text)
index.close()
@@ -132,29 +149,50 @@
os.chdir(top_dir)
-# Loop through each subdirectory, using function to create each index.html.
-
+# Loop through each subdirectory, creating an index.html file.
for topic in topics_files.keys():
archive(topic, topics_files[topic])
-
+
# Create headlines for samba.org from last ten news items
headlines = open('headlines.html', 'w')
headlines.write('<ul class="news">\n')
+headlines.close()
-for line in open('index.html', 'r').readlines():
- if line.find('<h3>') > -1 and len(open('headlines.html',
-'r').readlines()) < 10:
+all_news = {}
+for file in topics_files.keys():
+ os.chdir(file)
+ for this_file in topics_files[file]:
+ if this_file in not_news:
+ continue
+ else:
+ all_news[os.stat(this_file)[ST_MTIME]] = open(this_file, 'r').readlines()
+ os.chdir(top_dir)
+
+news_dates = all_news.keys()
+news_dates.sort()
+news_dates.reverse()
+
+news_for_headlines = {}
+x = 10
+for date in news_dates:
+ while x:
+ x = x - 1
+ for line in all_news[date]:
+ if line.find('<h3>') > -1:
# Search for text between quotes
link = re.search('(?<=\")\S+(?=\")', line)
# Search for text between > and </a
title = re.search('(?<=\"\>).+(?=\<\/a)', line)
- headlines = open('headlines.html', 'a')
- headlines.write('<li><a href="/samba/news/#' + link.group(0) + '">'
-+ title.group(0) + '</a></li>\n')
- else:
- continue
-
+ news_for_headlines[date] = (link.group(0), title.group(0))
+headline_dates = news_for_headlines.keys()
+headline_dates.sort()
+headline_dates.reverse()
+
headlines = open('headlines.html', 'a')
+for date in headline_dates:
+ headlines.write('<li>' + date_to_str(time.gmtime(date)[:3]) + ' <a href="/samba/news/#' + news_for_headlines[date][0] + '">' + news_for_headlines[date][1] + '</a></li>\n')
+
headlines.write('</ul>\n')
+headlines.close()
\ No newline at end of file
More information about the samba-cvs
mailing list