I need to take a maven dependency tree & remove the header & footer so that only the dependency info is left. An example tree is below.
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------------< brs:libadept >---------------------------
[INFO] Building libadept 0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]--------------------------------
[WARNING] The artifact org.apache.commons:commons-io:jar:1.3.2 has been relocated to commons-io:commons-io:jar:1.3.2
[INFO]
[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ libadept ---
[WARNING] The artifact org.apache.commons:commons-io:jar:1.3.2 has been relocated to commons-io:commons-io:jar:1.3.2
[INFO] brs:libadept:jar:0.1-SNAPSHOT
[INFO] +- org.antlr:antlr4:jar:4.7.1:compile
[INFO] | +- org.antlr:antlr4-runtime:jar:4.7.1:compile
[INFO] | \- com.ibm.icu:icu4j:jar:58.2:compile
[INFO] +- brs:libutil8:jar:1.17.3:compile
[INFO] | \- com.google.code.gson:gson:jar:2.7:compile
[INFO] \- org.slf4j:slf4j-log4j12:jar:1.7.25:compile
[INFO] \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] -----------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] -----------------------------------------------------------------------
[INFO] Total time: 2.760 s
[INFO] Finished at: 2019-07-31T14:26:14-07:00
[INFO] -----------------------------------------------------------------------
I have tried this so far:
import os
myCmd = 'mvn dependency:tree > new.txt'
os.system(myCmd)
fileObj = open("new.txt", "r")
fileOut = open("bulk.txt", "w")
def getBulk(text):
for line in text.readlines():
if line.startswith("[INFO] +- ") or line.startswith("[INFO] | ") or line.startswith("[INFO] \\\- ") or line.startswith("[INFO] \\\- "):
fileOut.write(line)
return fileOut
print(getBulk(fileObj))
I want to be able to just get this info:
[INFO] +- org.antlr:antlr4:jar:4.7.1:compile
[INFO] | +- org.antlr:antlr4-runtime:jar:4.7.1:compile
[INFO] | \- com.ibm.icu:icu4j:jar:58.2:compile
[INFO] +- brs:libutil8:jar:1.17.3:compile
[INFO] | \- com.google.code.gson:gson:jar:2.7:compile
[INFO] \- org.slf4j:slf4j-log4j12:jar:1.7.25:compile
[INFO] \- org.slf4j:slf4j-api:jar:1.7.25:compile
But I am getting
<_io.TextIOWrapper name='bulk.txt' mode='w' encoding='cp1252'>
I have to make sure to take into account that the headers & footers might not always be the same number of lines (eg the lines starting with "[WARNING]" might not always be there, so I can't just write the program such that the first ten lines get deleted). Thanks in advance!
Final answer:
import os
myCmd = 'mvn dependency:tree > new.txt'
os.system(myCmd)
fileObj = open("new.txt", "r").readlines()
fileOut = open("bulk.txt", "w")
def getBulk(text):
for line in text:
if line.startswith("[INFO] +- ") or line.startswith("[INFO] | ") or line.startswith("[INFO] \- ") or line.startswith("[INFO] \- "):
fileOut.write(line)
print(line)
return fileOut