6 from .xpath import XPath
10 def __init__(self, filename):
11 self.filename=filename
13 def hasParent(self) -> bool:
14 pattern_compiled = re.compile('<project[>\ ]')
16 with open(self.filename,'r') as src_file:
18 m = pattern_compiled.search(line)
23 pattern_compiled = re.compile('<parent[>\ ]')
27 def setDependencyVersion(self, groupId, artifactId, version) -> bool:
28 return self.setXmlValue('/project/dependencies/dependency[groupId={},artifactId={}]/version'.format(groupId,artifactId),version)
29 def setDependencyManagementVersion(self, groupId, artifactId, version) -> bool:
30 return self.setXmlValue('/project/dependencyManagement/dependencies/dependency[groupId={},artifactId={}]/version'.format(groupId,artifactId),version)
31 # set xmlElementValue (just simple values - no objects)
33 # e.g. /project/parent/version
34 # /project/dependencies/dependency[groupId=org.opendaylight.netconf]/version
36 def setXmlValue(self, valuePath, value, replaceMultiple=False) -> bool:
40 pathToFind = XPath(valuePath)
41 pattern = re.compile('<([^>^\ ^?^!]+)')
45 with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_file:
46 with open(self.filename) as src_file:
48 if found == False or replaceMultiple:
56 matches = pattern.finditer(line,y)
57 for matchNum, match in enumerate(matches, 1):
62 # start tag detected (not autoclosing xml like <br />)
63 elif not f.endswith("/"):
68 curParent = curPath.last(1)
71 if pathToFind.equals(curPath, False):
72 pre=line[0:line.index('<')]
73 line=pre+'<{x}>{v}</{x}>\n'.format(x=f,v=value)
77 elif pathToFind.parentParamIsNeeded(curPath.subpath(1), f):
78 v = self.tryToGetValue(line, f)
80 curParent.setFilter(f, v)
83 # Overwrite the original file with the munged temporary file in a
84 # manner preserving file attributes (e.g., permissions).
85 shutil.copystat(self.filename, tmp_file.name)
86 shutil.move(tmp_file.name, self.filename)
87 print("set {} to {} in {}: {}".format(valuePath, value, self.filename, str(found)))
90 def tryToGetValue(self, line, xmlTag=None):
91 pattern = re.compile('<([^>^\ ^?^!]+)>([^<]+)<\/([^>^\ ^?^!]+)>' if xmlTag is None else '<('+xmlTag+')>([^<]+)<\/('+xmlTag+')>')
92 matches = pattern.finditer(line)
99 def findAll(folder, excludes=[]):
100 files= glob.glob(folder + "/**/pom.xml", recursive = True)
104 for exclude in excludes: