# This is the program I have written to easily update my list of sample code. # It essentially walks a source tree and creates an index html file # Enjoy! # # - Dan Fleck, April 2008 import os, glob, time, re # NOTE: Much of this functions code is from: Daniweb example by vegaseat def walkTreeByDate(outfile, src="./*", pattern="\.py$|\.gif$|\.jpg$"): """Walk the directory tree, any files matching the pattern given, add to the index page ordered by date """ date_file_list = [] for folder in glob.glob(src): # select the type of file, for instance *.jpg or all files *.* for file in glob.glob(folder + '/*.*'): # retrieves the stats for the current file as a tuple # (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) # the tuple element mtime at index 8 is the last-modified-date stats = os.stat(file) # create tuple (year yyyy, month(1-12), day(1-31), hour(0-23), minute(0-59), second(0-59), # weekday(0-6, 0 is monday), Julian day(1-366), daylight flag(-1,0 or 1)) from seconds since epoch # note: this tuple can be sorted properly by date and time lastmod_date = time.localtime(stats[8]) #print image_file, lastmod_date # test # create list of tuples ready for sorting by date date_file_tuple = lastmod_date, file date_file_list.append(date_file_tuple) date_file_list.sort() date_file_list.reverse() # newest mod date now first prevWeek = -1 for file in date_file_list: # extract just the filename folder, file_name = os.path.split(file[1]) # convert date tuple to MM/DD/YYYY HH:MM:SS format file_date = time.strftime("%m/%d/%Y", file[0]) file_week = time.strftime("%W", file[0]) if file_week != prevWeek: outfile.write( "\n

Week %s
" % (file_week)) prevWeek = file_week outfile.write( "\n
%-20s %s" % (file_date, folder +"/"+ file_name, folder +"/"+ file_name )) def walkdirtree(outfile, count, src=".", pattern="\.py$|\.gif$|\.jpg$"): """Walk the directory tree, any files matching the pattern given, add to the index page """ for root, dirs, files in os.walk(src, topdown=True): # Count the file separators in dir seperators = root.count('/') - 1 lastDir = root[root.rfind('/'):] if seperators > 0: outfile.write('
|') else: outfile.write('

') outfile.write(('_'*seperators*4)+lastDir+'\n') for file in files: src = os.path.join(root, file) match = re.search(pattern, src) if match != None: outfile.write('%s ' %(src, file)) def writeHeaderByDate(outfile): outfile.write(""" Sample Code Sample Code for this class sorted by date. (This page automatically generated by make_webpage.py)
Sort by directory
""") def writeHeader(outfile): outfile.write(""" Sample Code Sample Code for this class sorted by directory. (This page automatically generated by make_webpage.py)
Sort by date
""") def writeFooter(outfile): outfile.write("""\n
""") def main(): print 'Making sample webpage ....', # Create the output file outfile = open('sample_index.html', 'w') writeHeader(outfile) walkdirtree(outfile, 0) writeFooter(outfile) outfile.close() outfile = open('sample_by_date.html', 'w') writeHeaderByDate(outfile) walkTreeByDate(outfile) writeFooter(outfile) outfile.close() print 'DONE' main()