diff --git a/fcp3/sitemgr.py b/fcp3/sitemgr.py index c130e86..0374a9d 100644 --- a/fcp3/sitemgr.py +++ b/fcp3/sitemgr.py @@ -24,7 +24,17 @@ #@+others #@+node:imports -import sys, os, os.path, io, threading, traceback, pprint, time, stat, json +import fnmatch +import io +import json +import os +import os.path +import pprint +import stat +import sys +import threading +import time +import traceback import fcp3 as fcp from fcp3 import CRITICAL, ERROR, INFO, DETAIL, DEBUG #, NOISY @@ -96,6 +106,7 @@ def __init__(self, *args, **kw): self.index = kw.get('index', 'index.html') self.sitemap = kw.get('index', 'sitemap.html') self.mtype = kw.get('mtype', 'text/html') + self.mimeTypeMatch= kw.get('mimeTypeMatch', []) self.name = "freesitemgr-" + "--".join(args) # To decide whether to upload index and activelink as part of @@ -173,6 +184,7 @@ def load(self): noInsert=self.noInsert, chkCalcNode=self.chkCalcNode, mtype=self.mtype, + mimeTypeMatch=self.mimeTypeMatch, ) self.sites.append(site) @@ -244,6 +256,7 @@ def addSite(self, **kw): index=self.index, sitemap=self.sitemap, mtype=self.mtype, + mimeTypeMatch=self.mimeTypeMatch, **kw) self.sites.append(site) @@ -489,6 +502,7 @@ def __init__(self, **kw): self.index = kw.get('index', 'index.html') self.sitemap = kw.get('sitemap', 'sitemap.html') self.mtype = kw.get('mtype', 'text/html') + self.mimeTypeMatch = kw.get('mimeTypeMatch', []) #print "Verbosity=%s" % self.Verbosity @@ -684,6 +698,7 @@ def writeVars(comment="", tail="", **kw): writeVars(index=self.index) writeVars(sitemap=self.sitemap) writeVars(mtype=self.mtype) + writeVars(mimeTypeMatch=self.mimeTypeMatch) w("\n") # we should not save generated files. @@ -1121,6 +1136,10 @@ def scan(self): rec['path'] = f['fullpath'].decode(enc) rec['name'] = f['relpath'].decode(enc) rec['mimetype'] = f['mimetype'] + for patternType in reversed(self.mimeTypeMatch): + for pattern, mimetype in patternType.items(): + if fnmatch.fnmatch(rec['name'], pattern): + rec['mimetype'] = mimetype rec['hash'] = hashFile(rec['path']) rec['sizebytes'] = getFileSize(rec['path']) rec['uri'] = '' @@ -1165,10 +1184,12 @@ def scan(self): # known file - see if changed knownrec = self.filesDict[name] if (knownrec['state'] in ('changed', 'waiting') - or knownrec['hash'] != rec['hash']): + or knownrec['hash'] != rec['hash'] + or knownrec['mimetype'] != rec['mimetype']): # flag an update log(DETAIL, "scan: file %s has changed" % name) knownrec['hash'] = rec['hash'] + knownrec['mimetype'] = rec['mimetype'] knownrec['sizebytes'] = rec['sizebytes'] knownrec['state'] = 'changed' structureChanged = True diff --git a/freesitemgr b/freesitemgr index cc3cb8c..e8d6e8b 100755 --- a/freesitemgr +++ b/freesitemgr @@ -271,6 +271,10 @@ def help(): print(" - index file (default is index.html)") print(" -m, --mime-type") print(" - mime-type of the index file (default is \"text/html\")") + print(" --mime-type-match=PATTERN=MIME/TYPE") + print(" Set mime-type for files matching PATTERN. Can be given") + print(" multiple times to match distinct patterns.") + print(" This option is only effective when doing add.") print(" -l, --logfile=filename") print(" - location of logfile (default %s)" % logFile) print(" -r, --priority") @@ -357,6 +361,7 @@ def main(): "priority", "cron", "chk-calculation-node=", "max-manifest-size=", "version", "index=", "mime-type=", + "mime-type-match=", ] ) except getopt.GetoptError: @@ -398,6 +403,15 @@ def main(): if o in ("-m", "--mime-type"): opts['mtype'] = a + if o in ("--mime-type-match"): + try: + pattern, mimeType = a.split('=') + except: + usage("--mime-type-match must have PATTERN=MIMETYPE parameter") + if 'mimeTypeMatch' not in opts: + opts['mimeTypeMatch'] = [] + opts['mimeTypeMatch'] += [ { pattern:mimeType, }, ] + if o == '--max-manifest-size': opts['maxManifestSizeBytes'] = int(float(a)*1024)