PageRenderTime 75ms CodeModel.GetById 63ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Documentation/ReferenceManualScripts/doxygen/utils/linker/linker.py

https://github.com/aivanov-cern/cmssw
Python | 154 lines | 146 code | 8 blank | 0 comment | 4 complexity | e42f7f50979b5ce8c4b878ed19128302 MD5 | raw file
  1import sys
  2import os
  3import re
  4from BeautifulSoup import BeautifulSoup
  5
  6BASE = "http://cmssdt.cern.ch/SDT/doxygen/"
  7INDEX = {}
  8printOutput = False;
  9
 10def replace(regex,replacement,content):
 11    p = re.compile(regex,re.IGNORECASE);
 12    c = p.sub(replacement,content)
 13    return c 
 14
 15def findMatchingFiles(w, source_htmls):
 16    ret = ""
 17    for srcFile in source_htmls:
 18        if srcFile.split("/")[-1].__str__().find(w) != -1:
 19            ret +=  " " + srcFile
 20            
 21    return ret
 22
 23def filter(s,w,k):
 24    o = s.split()
 25    if len(o) > 1:
 26        betterChoice = ""
 27        for i in range(len(o)):
 28            if re.search("[^a-zA-Z]"+w+"[^a-zA-Z]", o[i]):
 29                if re.search(".*"+k+".*",o[i]):
 30                    return o[i]
 31                else:
 32                    if betterChoice == "" or len(betterChoice) > o[i]:
 33                        betterChoice = o[i]
 34        return betterChoice
 35    else:
 36        if re.search("[^a-zA-Z]"+w+"[^a-zA-Z]", s):
 37            return s
 38        else:
 39            return ""
 40        
 41def getLink(word):
 42        
 43    if word.isdigit() or (len(word) < 5):
 44        return ""
 45    
 46    out = filter(findMatchingFiles(word, py_source_htmls),word,"")
 47    if not out or out == "":
 48        out = filter(findMatchingFiles(word, h_source_htmls),word,"")
 49        if not out or out == "":
 50            return ""
 51    return BASE+out.lstrip()
 52
 53def process(filename):
 54    
 55    if (filename != None) and (len(filename) < 5):
 56        return
 57    
 58    fh = open(filename,'r')
 59    html = fh.read()
 60    fh.close()
 61
 62  
 63    content = ""
 64    # find only code block
 65    soup = BeautifulSoup(html)
 66    pres = soup.findAll("pre", {"class":"fragment"})
 67    
 68    for pre in pres:
 69        if pre.contents != None:
 70            content += pre.renderContents()
 71    # END OF find only code block
 72
 73    # remove links
 74    content = replace(r'<a\b[^>]*>(.*?)</a>','',content)
 75    
 76    content = content.replace("&#39;", "'")    
 77    content = content.replace("&quot;", '"')
 78    
 79    matches = []
 80    tmp = re.findall('[\w,\.]+_cf[i,g,f]',content)
 81    for t in tmp:
 82        matches.extend(t.split("."))
 83        
 84    matches.extend(re.findall('"\w+"',content))
 85    matches.extend(re.findall("'\w+'",content))
 86    
 87    set = {}                                  # 
 88    map(set.__setitem__, matches, [])         # removing duplicate keywords
 89    matches = set.keys()                      # 
 90    
 91    for match in matches:
 92        
 93        match = match.replace("'", "")    
 94        match = match.replace('"', "")
 95        
 96        if (INDEX.has_key(match)):
 97            href = INDEX[match]
 98        else:
 99            href = getLink(match)
100        
101        if (href != ""):
102            INDEX[match] = BASE+href[href.find("CMSSW_"):]
103            
104            link = "<a class=\"configfileLink\" href=\""+href+"\">"+match+"</a>"
105            regex = r"\b"+match+r"\b"
106            html = replace(regex, link, html)
107            
108            ########################
109            if printOutput:
110                print ">>>>>["+match+"]",
111            ########################
112        
113            ########################
114            if printOutput:
115                print href
116            ########################
117        
118    fh = open(filename,'w')
119    fh.write(html)
120    fh.close()
121
122if len(sys.argv) > 1:
123
124    DIR = sys.argv[1] +"/doc/html/"
125              
126    global py_source_htmls
127    global h_source_htmls
128
129    h_source_htmls = []
130    py_source_htmls = []
131    
132    print "ieskau h_source"
133    
134    query = "find "+DIR+" -name '*8h_source.html' -print"
135    output = os.popen(query)
136    h_source_htmls = output.read().split("\n")
137    
138    print "ieskau py_source"
139    
140    query = "find "+DIR+" -name '*8py_source.html' -print"
141    output = os.popen(query)
142    py_source_htmls = output.read().split("\n")
143   
144    query = 'find '+DIR+' \( -name "*cf[i,g,f]*py*html" -or -name "namespace*cf[i,g,f].html" \) -print '
145    output = os.popen(query)
146    files = output.read().split("\n")
147    i = 0
148    for file in files:
149        i = i + 1
150        print i.__str__()+") "+file
151        process(file)
152    print "-----------------------------------------------------------"    
153else:
154    print "not enough parameters"