Author: deryck Date: 2004-08-31 04:57:59 +0000 (Tue, 31 Aug 2004) New Revision: 295
WebSVN: http://websvn.samba.org/websvn/changeset.php?rep=samba-web&path=/trunk&rev=295&nolog=1 Log: This is a script I've been working on for handling the updates to news.samba.org. As it's my first bit of coding, I add it here, so jerry, tpot, (and anyone else who feels inclined) can take a look. Once we start using it, I'll remove it from samba-web. tpot, jerry -- I've tested this in several situations and it works fine for me. But does it look okay to you guys? I'm thinking, of course, that it will run as a cron job to build the news web pages. Thanks for looking it over. Cheers, --deryck Added: trunk/scripts/ trunk/scripts/updateNews.py Changeset: Added: trunk/scripts/updateNews.py =================================================================== --- trunk/scripts/updateNews.py 2004-08-30 16:52:53 UTC (rev 294) +++ trunk/scripts/updateNews.py 2004-08-31 04:57:59 UTC (rev 295) @@ -0,0 +1,147 @@ +#! /usr/bin/python + +# +# A script for handling files on news.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. +# +# 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. +# + +import os, time +from stat import * + +top_dir = '/srv/www/htdocs/news' # set to news directory path +not_news = ['.svn', 'images', 'style', 'calendar', 'index.html'] + + +# Get list of news directories. Then, pair up dir name with dir files. + +os.chdir(top_dir) +top_dir_files = os.listdir(os.curdir) + +topics = [] +for x in top_dir_files: + if x in not_news: continue + if os.path.isdir(x): + topics.append(x) +topics.sort() + +topics_files = {} +for x in topics: + topics_files[x] = os.listdir(x) + + +# Loop through each directory, find all stories, and create main index.html + +all_stories = {} + +for x in topics: + topic = x + filelist = os.listdir(topic) + os.chdir(topic) + + topic_stories = {} + + for x in filelist: + if x in not_news: continue + f = open(x, 'r') + f_lines = f.readlines() + story = "".join(f_lines) + '<div class="reference">Link: <a href="/samba/news/' + topic + '/#' + x[:-5] + '">' + topic + '/</a></div>\n\n' + f_stats = os.stat(x) + f_date = time.strftime("%d %B %Y", time.localtime(f_stats[ST_MTIME])) + # group stories on the same date under that one date + if f_date in topic_stories: + topic_stories[f_date] += story + else: + topic_stories[f_date] = story + + for x in topic_stories: + h2date = x + # again, group stories from same date under that date + if h2date in all_stories: + all_stories[h2date] += topic_stories[h2date] + else: + all_stories[h2date] = topic_stories[h2date] + + os.chdir(top_dir) + +index = open('index.html', 'w') +index.write('<!--#include virtual="/samba/news/header.html" -->\n') +index.write('<title>news.samba.org</title>\n') +index.write('<!--#include virtual="/samba/news/header2.html" -->\n\n') +index.close() + +post_dates = all_stories.keys() +post_dates.sort() +post_dates.reverse() + +for x in post_dates: + h2date = x + news_text = all_stories[h2date] + index = open('index.html', 'a') + index.write('<h2>' + h2date + '</h2>\n\n') + index.write(news_text) + index.close() + +index = open('index.html', 'a') +index.write('<!--#include virtual="/samba/news/footer.html" -->\n\n') +index.close + + +# Define function that creates index.html for each directory. + +def archive(dir, files): + topic = dir + os.chdir(topic) + filelist = files + + stories_by_date = {} + + for x in filelist: + if x in not_news: continue + f = open(x, 'r') + f_lines = f.readlines() + f_stats = os.stat(x) + f_date = time.strftime("%d %B %Y", time.localtime(f_stats[ST_MTIME])) + # group stories from same date under that one date + if f_date in stories_by_date: + stories_by_date[f_date].append("".join(f_lines)) + else: + stories_by_date[f_date] = f_lines + + index = open('index.html', 'w') + index.write('<!--#include virtual="/samba/news/header.html" -->\n') + index.write('<title>' + topic + '/' + ' on news.samba.org</title>\n') + index.write('<!--#include virtual="/samba/news/header2.html" -->\n\n') + index.write('<h1>' + topic + ' archive on news.samba.org</h1>\n\n') + index.write('<p>All stories for the ' + topic + ' topic are archived here</p>\n\n') + index.close() + + post_dates = stories_by_date.keys() + post_dates.sort() + post_dates.reverse() + + for x in post_dates: + h2date = x + news_text = "".join(stories_by_date[h2date]) + index = open('index.html', 'a') + index.write('<h2>' + h2date + '</h2>\n\n') + index.write(news_text) + index.close() + + index = open('index.html', 'a') + index.write('<!--#include virtual="/samba/news/footer.html" -->\n\n') + index.close + + os.chdir(top_dir) + + +# Loop through each subdirectory, using function to create each index.html. + +for x in topics_files: + archive(x, topics_files[x]) Property changes on: trunk/scripts/updateNews.py ___________________________________________________________________ Name: svn:executable + *