annotate addDriToIndexMeta.py @ 5:3ebe37d81071

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