[英]python - remove header & footer from mvn dependency tree
I need to take a maven dependency tree & remove the header & footer so that only the dependency info is left. 我需要采取一个Maven依赖树并删除页眉和页脚,以便仅保留依赖项信息。 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). 我必须确保考虑到页眉和页脚不一定总是相同的行数(例如,以“ [WARNING]”开头的行可能并不总是在那里,所以我不能只写这样的程序删除前十行)。 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
import os
mvn_dep_tree_cmd = 'mvn dependency:tree > new.txt'
os.system(mvn_dep_tree_cmd)
file_in = open("new.txt", "r")
file_out = open("bulk.txt", "w")
# flag denoting that current line is inside a tree
in_tree = False
for line in file_in.readlines():
# this condition denotes the start of a tree
if line.startswith("[INFO] +- "):
in_tree = True
if in_tree:
# if we are inside a tree the following condition denotes end of the tree
if line.strip() == '[INFO]':
in_tree = False
file_out.write('\n')
file_out.write('\t<---------------------------------------------->')
file_out.write('\n\n')
print(line)
else:
# writing to the out file all the contents inside the tree
file_out.write(line)
print(line)
file_in.close()
file_out.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.