#!/usr/bin/python # sd2h - ScriptDirToHtml # Makes HTML listings and a tarball for a directory of scripts. # # The listings are sdindex.html and atoz.html, chronologically and # alphabetically sorted respectively. They depend on # http://www.cv.nrao.edu/~rreid/software/extractcomment and # http://www.cv.nrao.edu/~rreid/software/style.css # # If run in , the tarball is /.tar.gz. # All output gets written over if it is already present! # # Changes: # 2009/12/09: Translated from bash to python, making it 2x faster by only # extracting the comments once. # # The pages now use CSS (get the stylesheet above!) and both they # and the tarball are more careful to exclude unwanted files. # # It is GNU software, and based on DirToHtml. Please send us any # customizations you have made so they can be incorporated in # future releases. # Have fun, Tomas IV., brunclik@mail.com, http://tom4.hyperlink.cz (DirToHtml) # Rob Reid, http://www.cv.nrao.edu/~rreid/ (added script comment extraction) from glob import glob import os import re import shutil import stat import sys import time version = sys.argv[0] + " v2.1e" verblurb = """ """ + version + """ ScriptDirToHTML, (G) Rob Reid 2009, based on DirToHtml, (G) Tomas Brunclik 2001 Use under terms of GNU public licence. """ help = "Use: " + os.path.basename(sys.argv[0]) + """ [-hv | ["]["]] Generates html indices and a tarball of the current directory: sdindex.html: Sorted by last modification time. atoz.html: Alphabetically sorted. $cwd.tar.gz: The tarball. -h, --help Show this help screen -v, --version Version info """ fmask = '' nargs = len(sys.argv) if nargs > 1: if sys.argv[1][0] == '-': for i in xrange(1, min(nargs, 3)): if sys.argv[i].find('v') > 0: print verblurb if sys.argv[i].find('h') > 0: print help sys.exit(0) else: fmask = ' '.join(sys.argv[1:]) owd = os.getcwd() scriptdir = os.path.split(owd)[-1] for f in ('sdindex.html', 'atoz.html', scriptdir + '.tar.gz'): if os.path.isfile(f): os.remove(f) tarball = scriptdir + '.tar.gz' def getscripts(fmask=''): """ Returns the filenames to make entries for, sorted by time. """ lister = os.popen('ls -1t ' + fmask) fmask = [] skippables = ('atoz.html', 'sdindex.html', 'style.css', 'index.html', tarball) for s in lister: s = s.strip() if s[-1] != '~' and s not in skippables: fmask.append(s) lister.close() return fmask fmask = getscripts(fmask) def make_tarball(dirname, files, dest, specialfiles=('COPYING', 'INSTALL', 'LICENSE', 'README')): os.mkdir(dirname) owd = os.getcwd() os.chdir(dirname) files = set(files) files.update(specialfiles) for f in files: absf = os.path.join(owd, f) if os.path.isfile(absf): try: os.symlink(absf, f) except Exception, e: print "Error", e, "symlinking", f, "to", absf #print "files =", ', '.join(files) sys.exit(1) else: print absf, "not found for", dest os.chdir(owd) os.system("tar chfz " + dest + ' ' + dirname) shutil.rmtree(dirname) make_tarball(scriptdir, fmask, tarball) def kilobytes(bytes): """ Given an integer number of bytes, returns it as a string in kilobytes. """ return "%.1fkb" % (bytes / 1024.0) def getentries(fmask): entries = {} for script in fmask: # echo

${i}

>> $page # echo ${i} \ \;\ \;\ \;\ \; "Last modified " `ls -o $i | awk '{printf "%s %s %s, %s bytes\n", $5, $6, $7, $4}'`
>> $page entries[script] = '''
''' fstat = os.stat(script) #blmod=$(ls -o --full-time $i | sed 's/ */ /g' | cut -d ' ' -f 4-9) entries[script] += '''
''' + kilobytes(fstat[stat.ST_SIZE]) + ', last modified ' entries[script] += time.ctime(fstat[stat.ST_MTIME]) + '''
''' sfile = open(script) bangcheck = (sfile.readline()[:2] == '#!') fext = script.split('.')[-1].lower() if fext[:3] == 'htm': sfile.seek(0) for line in sfile: lcline = line.lower() if '' in lcline: titlestart = lcline.index('<title>') + 7 titleend = lcline.index('') entries[script] += line[titlestart:titleend] break elif bangcheck or (fext == "py"): # Allows proportional fonts, but wrecks things that depend on # monospace alignment. # echo

>> $page # extractcomment < $i | sed 's/^$/

/;s/$/
/' >> $page # echo

>> $page # Preserves monospaced arrangement. entries[script] += os.popen('extractcomment -h < ' + script).read() elif ('.tar' in script) or (script[-4:] == '.tgz') or (script[-4:] == '.tbz'): tarcmd = 'tar tf' if script[-2] == 'g': tarcmd += 'z' elif 'bz' in script[-3:]: tarcmd += 'j' listing = os.popen(tarcmd + ' ' + script).read().split("\n") entries[script] += "

    \n" for thing in listing: if not (re.match(r'.*/.*/', thing) or thing == ''): entries[script] += '
  • ' + thing + "\n" entries[script] += "
\n" else: magic = os.popen('file ' + script).read() entries[script] += magic[len(script) + 2:] sfile.close() entries[script] += '''
''' return entries entries = getentries(fmask) def makepage(sortord, fmask, entries): if sortord == "time": page = open('sdindex.html', 'w') else: fmask.sort() page = open('atoz.html', 'w') if scriptdir.lower() != 'scripts': title = scriptdir + ' scripts' else: title = 'Scripts' # HTML header header = ''' ''' + title + ''' ''' body = ''' ''' #

List of Files

body += '

' + title + '''

These scripts are available under the GNU General Public License.

Note that there is no warranty! Although I have put some effort into making them generally useful, they were mainly written to help with a task at hand. So they may not yet work for all cases, and you may have some fixing to do. Please send me any fixes or enhancements.


The whole shebang (''' body += kilobytes(os.stat(tarball)[stat.ST_SIZE]) + ''')


''' if sortord == "time": body += 'Sort alphabetically.' else: body += 'Sort by last modification time.' body += '''


''' # Hyperlink the files for script in fmask: body += entries[script] # Close the HTML. body += '''


Page generated by s2dh, based on DirToHtml, a GNU generator of html directory listings.
''' page.write(header + body) page.close() makepage("time", fmask, entries) makepage("alpha", fmask, entries)