5
|
1 '''
|
|
2 Created on 01.11.2012
|
|
3
|
|
4 @author: dwinter
|
|
5 '''
|
|
6 import os
|
|
7 import manageIndexMetaPURLs
|
|
8 import re
|
|
9 from lxml import etree
|
|
10
|
|
11 from os.path import join, getsize
|
|
12
|
|
13
|
|
14 def addPURL(fl,purl,test=False):
|
|
15 tree = etree.parse(fl)
|
|
16
|
|
17 dris = tree.xpath("/resource/meta/dri[@type='mpiwg']")
|
|
18
|
|
19 if len(dris)==0: # erzeuge neu
|
|
20 newDri = etree.Element("dri",type="mpiwg")
|
|
21 newDri.text=purl
|
|
22 metas=tree.xpath("/resource/meta")
|
|
23 if len(metas)==0:
|
|
24 print "no resource/meta in %s"%fl
|
|
25 return False
|
|
26 else:
|
|
27 metas[0].append(newDri)
|
|
28 else:
|
|
29 dris[0].text=purl
|
|
30
|
|
31 print etree.tostring(tree, pretty_print=True)
|
|
32
|
|
33 if not test:
|
|
34 os.rename(fl, fl+"_mpiwg_dri")
|
|
35 out = etree.tostring(tree, encoding="UTF-8",xml_declaration=False)
|
|
36 fo = file(fl,"w")
|
|
37 fo.write(out)
|
|
38 fo.close
|
|
39 return True
|
|
40
|
|
41 def addDriToIndexMeta(path,delpath="",replacepath="", update=False,test=False):
|
|
42
|
|
43
|
|
44
|
|
45 for root, dirs, files in os.walk(path):
|
|
46
|
|
47
|
|
48 for name in files:
|
|
49 if name.endswith(".meta"):
|
|
50 fl=join(root, name)
|
|
51 shortPath=re.sub("^"+delpath,replacepath,fl)
|
|
52 purl=manageIndexMetaPURLs.IndexMetaPURLManager().getPurl(shortPath)
|
|
53
|
|
54 addPURL(fl,purl,test)
|
|
55
|
|
56 if 'pageimg' in dirs:
|
|
57 dirs.remove('pageimg') # don't visit pageimf
|
|
58 for dir in dirs:
|
|
59 if dir== "pageimg":
|
|
60 dirs.remove('pageimg')
|
|
61 if dir.startswith("."):
|
|
62 dirs.remove(dir)
|
|
63
|
|
64 if __name__ == '__main__':
|
|
65 addDriToIndexMeta("/mpiwg/online/permanent/annalen",delpath="/mpiwg/online") |