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:
38 print("unable to set {} to {} in {}: {}".format(valuePath, value, self.filename, str(False)))
41 pathToFind = XPath(valuePath)
42 pattern = re.compile('<([^>^\ ^?^!]+(\ \/)?)')
46 with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_file:
47 with open(self.filename) as src_file:
49 if found == False or replaceMultiple:
57 matches = pattern.finditer(line,y)
58 for matchNum, match in enumerate(matches, 1):
63 # start tag detected (not autoclosing xml like <br />)
64 elif not f.endswith("/"):
69 curParent = curPath.last(1)
72 if pathToFind.equals(curPath, False):
73 pre=line[0:line.index('<')]
74 line=pre+'<{x}>{v}</{x}>\n'.format(x=f,v=value)
78 elif pathToFind.parentParamIsNeeded(curPath.subpath(1), f):
79 v = self.tryToGetValue(line, f)
81 curParent.setFilter(f, v)
84 # Overwrite the original file with the munged temporary file in a
85 # manner preserving file attributes (e.g., permissions).
86 shutil.copystat(self.filename, tmp_file.name)
87 shutil.move(tmp_file.name, self.filename)
88 print("set {} to {} in {}: {}".format(valuePath, value, self.filename, str(found)))
91 def tryToGetValue(self, line, xmlTag=None):
92 pattern = re.compile('<([^>^\ ^?^!]+)>([^<]+)<\/([^>^\ ^?^!]+)>' if xmlTag is None else '<('+xmlTag+')>([^<]+)<\/('+xmlTag+')>')
93 matches = pattern.finditer(line)
100 def findAll(folder, excludes=[]):
101 files= glob.glob(folder + "/**/pom.xml", recursive = True)
105 for exclude in excludes: