import glob import time import os import ConfigParser import re import sys import win32con import win32ui from pywin.mfc import dialog from stat import * dlgStatic = 130 dlgButton = 128 def exists(file): return os.access(file, os.F_OK) def cutelastmodified(file): filetime = os.stat(file)[ST_MTIME] return time.strftime("Last modified %d %b %Y",time.gmtime(filetime)) def lastmodified(file): filetime = os.stat(file)[ST_MTIME] return time.strftime("%Y%m%d",time.gmtime(filetime)) def readini(file): p = ConfigParser.ConfigParser() p.readfp (open('index.ini')) try: return p.get('files',file) except: return 0 def writeini(file,size): p = ConfigParser.ConfigParser() p.read('index.ini') p.set('files', file, size) fp = open('index.ini','w') p.write(fp) fp.close() #The file needs to be regenerated IF #its size in INI differs from actual OR #the output doesn't exist in /docs (in case you work on two hosts) OR #the input version is more recent than the one in /docs OR #its size is not in INI def NeedUpdate(file): #If run for the first time, no INI yet if not exists('index.ini'): fp = open('index.ini','w') fp.write('[files]') fp.close() if (readini(file) != str(os.stat(file)[ST_SIZE])) or \ (not exists('../docs/' + file)) or \ (lastmodified(file) > lastmodified('../docs/' + file)) or \ (readini(file) == 0): return 1 else: return 0 def displaytext(dlg,text): label=dlg.GetDlgItem(dlgStatic) label.SetWindowText(text) class Mydialog(dialog.Dialog): def OnInitDialog(self): rc = dialog.Dialog.OnInitDialog(self) self.pbar = win32ui.CreateProgressCtrl() self.pbar.CreateWindow (win32con.WS_CHILD | win32con.WS_VISIBLE, (7, 30, 270, 50), self, 1001) return rc def OnOK(self): displaytext(self,"Checking files...") button=self.GetDlgItem(win32con.IDOK) button.EnableWindow(0) t1 = time.clock() if not os.path.isdir("../input"): displaytext(self,"../input not found. Aborting.") #label=self.GetDlgItem(dlgStatic) #label.SetWindowText("../input not found. Aborting.") return if not os.path.isdir("../docs"): os.mkdir("../docs") #glob() mixes forward- and backslashes on the Windows platform... os.chdir('../input') #Check whether template for homepage present if not exists("index.tpl"): #label=self.GetDlgItem(dlgStatic) #label.SetWindowText("Missing ./input/index.tpl") displaytext(self,"Missing ./input/index.tpl") return #Read template.tpl into variable if not exists("template.tpl"): #label=self.GetDlgItem(dlgStatic) #label.SetWindowText("Missing ./input/template.tpl") displaytext(self,"Missing ./input/template.tpl") return else: fp=open("template.tpl") templateorig = fp.read() fp.close template = templateorig #this dictionary will contain all the links belonging to a classification links = {} #Find out how many *.HTM* in /input, and set range of progress bar filecount = 0 for file in glob.glob('*.htm*'): filecount+=1 self.pbar.SetRange(0,filecount) for file in glob.glob('*.htm*'): self.pbar.SetStep(1) self.pbar.StepIt() #Read file into variable f = open(file, "r") inputfile = f.read() f.close() #Add file + title + rest of stuff in header into relevant list, eg. unix[], windows[], etc. title = re.search('(.*?)',inputfile,re.I) if title: title = title.group(1) else: title = "NO TITLE! " + file classification = re.search('',inputfile,re.I) if classification: classification = classification.group(1) else: classification = "NO CLASSIFICATION! " + file #more massaging needed to build a new output in ./docs if NeedUpdate(file): displaytext(self,"Updating " + file) #Write into ../input/index.ini writeini(file,os.stat(file)[ST_SIZE]) description = re.search('',inputfile,re.I) if description: description = description.group(1) else: description = "NO DESC! " + file keywords = re.search('',inputfile,re.I) if keywords: keywords = keywords.group(1) else: keywords = "NO KEYS! " + file #the re module requires escaping any backslash, such as pathnames in Windows inputfile = inputfile.replace('\\', '\\\\') body = re.search('(.*?)',inputfile,re.IGNORECASE | re.DOTALL) if body: body = body.group(1) body = '%s\n\n' % (body) else: body = "NO BODY! " + file #start with fresh version template = templateorig #Mix template and info from file including date to generate output file #string.replace() is much faster than re.sub() template = template.replace('', '\t' + title + '\n') template = template.replace('', '\t\n') template = template.replace('', '\t\n') template = template.replace('', '\t\n') template = template.replace('', body) datefile = cutelastmodified(file) template = template.replace('###date###', datefile) #Save this into ./docs/file fp = open('../docs/' + file,'w') fp.write(template) fp.close() else: displaytext(self, "No need to update " + file) #Here, add keywords to array for later use to build index.html datefile = cutelastmodified(file) if links.has_key(classification): links[classification] = '%s\n\t
  • %s
  • \n' % (links[classification], datefile,datefile,file, title) else: #Capitalize since used as section name in index.html links[classification] = '%s\n\n' % links[i] template = template.replace('###' + i + '###', links[i]) #Save template variable into /index.html index.write(template) index.close() t2 = time.clock() elapsed = "Done in %s seconds." % round(t2-t1, 3) displaytext(self,elapsed) button=self.GetDlgItem(win32con.IDCANCEL) button.SetWindowText("Close") def OnCancel(self): self.Cancel = 1 self._obj_.OnCancel() #------------------- START ------------------------------------------------------------- style = (win32con.DS_MODALFRAME | win32con.WS_POPUP | win32con.WS_VISIBLE | win32con.WS_CAPTION | win32con.WS_SYSMENU | win32con.DS_SETFONT) cs = win32con.WS_CHILD | win32con.WS_VISIBLE s = win32con.WS_TABSTOP | cs w = 184 h = 60 dlg = [["PyWin32",(0, 0, w, h), style, None, (8, "MS Sans Serif")],] dlg.append([dlgStatic, "Rebuild site?", dlgStatic, (5, 3, 270, 10), cs | win32con.SS_LEFT]) dlg.append([dlgButton, "Rebuild", win32con.IDOK, (7, 40, 50, 14), s | win32con.BS_DEFPUSHBUTTON]) s = win32con.BS_PUSHBUTTON | s dlg.append([dlgButton, "Cancel", win32con.IDCANCEL, (130, 40, 50, 14), s]) d = Mydialog(dlg) d.DoModal() #answer = raw_input("Do you want to rebuild the site y/n?") #if answer == "y":