用python脚本更新NetBeans项目

最近拿到一个NetBeans的示例代码包,是用很老版本的NetBeans创建,在导入NetBeans 8 时出现大量警告,由于项目太多,手动修复太麻烦,于是用python编写了一个简单脚本修复警告,主要修复内容包括以下两个方面:

  • 第一个警告是项目缺少 test-roots 节点,打开项目文件project.xml 可以发现旧版本项目确实没有改节点,只有一个source-roots 节点,修复的方法是在source-roots 节点下面插入如下内容:
<test-roots>
    <root id="src.dir"/>
</test-roots>
  • 第二个警告是在编译运行时产生的,是说没有正确地指定Java源,看来是Java的问题,查看配置文件project.properties 发现有如下两项配置:
javac.source=1.2
javac.target=1.2

显然这是指定了Java编译器的版本,我们只需要把版本号改为自己所用Java版本即可,实际上如果只有一两个项目,那么用文本编辑器直接打开这两个文件按上述方法修改即可,但是如果项目比较多,比如是一个大的项目组,那么就需要用脚本自动化操作了,废话不多说,直接附上python脚本(测试环境 Win10/Python 3.5):

import os
import xml.dom.minidom

'''
This script is used to update the Netbeans project.
Importing the projects created by an older version to a new version NetBeans
will generate some warnings. These warnings can be fixed by running this script
in the root directory of project or project group.

@Date:2018-5-15 10:50:15
@Creator:Jack
@Version:1.0

'''

def detect_walk(dir_path):
    '''
    Traverse all the project files in dir_path,
    and update the Netbeans project
    '''
    for root, dirs, files in os.walk(dir_path):
        for filename in files:        
            if filename == "project.xml":  # update project.xml
                if update_project_xml(root + "\\" + filename):
                    print("Update Project:" + root)
            if filename == "project.properties":  # update project.properties
                update_properties(root + "\\" + filename)
                print("Update Property:" + root)

def update_project_xml(xml_path):
    '''
    Update the file project.xml:
    Add a new xml child node <test-root> for node <data> 
    '''
    DOMTree = xml.dom.minidom.parse(xml_path)
    project = DOMTree.documentElement
    data = project.getElementsByTagName("data")[0]
    if data.getElementsByTagName('test-roots'):   # the node 'test-roots' exists
        return 0
    test_root = DOMTree.createElement("test-roots")
    sub_root = DOMTree.createElement("root")
    sub_root.setAttribute("id", "src.dir")
    test_root.appendChild(sub_root)
    data.appendChild(test_root)

    # Landscaping xml format
    DOMStr = DOMTree.toprettyxml(indent = '', newl = '')
    DOMStr = DOMStr.replace(4*' ', '').replace('\n', '').replace('\t', '')
    DOMTree = xml.dom.minidom.parseString(DOMStr)

    # Update project.xml
    with open(xml_path, 'w') as f:
        f.write(DOMTree.toprettyxml(indent = '\t'))

    return 1

def update_properties(property_path):
    '''
    Update the java properties file project.properties:
    Change the properties javac.source = 1.8, javac.target = 1.8
    Please replace "1.8" with your java version
    '''
    with open(property_path,"r") as f:
        lines = f.readlines()
    with open(property_path,"w") as f:
        for line in lines:
            property = line.split('=',1)
            if property[0] == "javac.source" or property[0] == "javac.target":
                line = property[0] + '=1.8\n' # replace "1.8" with your java version
            f.write(line)



if __name__ == "__main__":
    detect_walk(".")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值