Chinaunix首页 | 论坛 | 博客
  • 博客访问: 194248
  • 博文数量: 47
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 470
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-18 12:09
个人简介

be a happy lazy and fat cat

文章分类
文章存档

2017年(1)

2016年(5)

2015年(22)

2014年(19)

我的朋友

分类: LINUX

2015-10-29 17:15:30


点击(此处)折叠或打开

  1. import sys
  2. import pprint
  3. from optparse import OptionParser
  4. from xml.etree.ElementTree import ElementTree
  5. from xml.etree.ElementTree import Element
  6. from xml.etree.ElementTree import SubElement as SE
  7. import xml.dom.minidom as minidom

  8. def migrate_aosp_to_snapshot(xml_input, aosp_remote, xml_output):
  9.     tree = ElementTree(file=xml_input)
  10.     root = tree.getroot()
  11.     
  12.     for child in root:
  13.         if (child.tag == "remote"):
  14.             print child.attrib["name"]
  15.             aosp_remote = child.attrib["name"]
  16.             fetch_str = child.attrib["fetch"]
  17.             fetch_str = fetch_str.replace("a/aosp/", "")
  18.             child.set("fetch", fetch_str)
  19.             continue
  20.         
  21.         if (child.tag == "project"):
  22.             print child.attrib["name"]
  23.             real_path = child.attrib["name"]
  24.             if not child.attrib.has_key("path"):
  25.                 child.set("path", real_path)

  26.             if not child.attrib.has_key("remote"):
  27.                 child.set("remote", aosp_remote)

  28.             real_path = "a/aosp/" + real_path
  29.             child.set("name", real_path)
  30.             continue

  31.         if (child.tag == "default"):
  32.             print child.tag
  33.             default_ele = child
  34.             continue

  35.     root.remove(default_ele)
  36.     tree.write(xml_output, 'utf-8')

  37. def migrate_snapshot_to_otc_upstream(xml_input, aosp_remote, xml_output):
  38.     tree = ElementTree(file=xml_input)
  39.     root = tree.getroot()
  40.     
  41.     for child in root:
  42.         if (child.tag == "remote"):
  43.             remote_ele = child
  44.             continue
  45.         
  46.         if (child.tag == "project"):
  47.             print child.attrib["name"]
  48.             real_path = child.attrib["name"]
  49.             if not child.attrib.has_key("path"):
  50.                 child.set("path", real_path)

  51.             if child.attrib.has_key("revision"):
  52.                 del child.attrib["revision"]

  53.             child.set("name", real_path)
  54.             continue

  55.         if (child.tag == "default"):
  56.             print child.tag
  57.             default_ele = child
  58.             continue

  59.     tree.write(xml_output, 'utf-8')


  60. def modify_groups(xml_input, xml_output):
  61.     aosp_tree = ElementTree(file=xml_input)
  62.     aosp_root = aosp_tree.getroot()
  63.     for child in aosp_root:
  64.         if (child.tag == "project"):
  65.                     groups = child.attrib["groups"]
  66.                     print "groups = " + groups
  67.                     real_groups = "aosp," + groups
  68.                     print "real_group= " + real_groups
  69.                     child.set("groups", real_groups)
  70. #write to file
  71.     aosp_tree.write(xml_output, 'utf-8')

  72. def remove_revision(xml_input, xml_output):
  73.     tree = ElementTree(file=xml_input)
  74.     root = tree.getroot()
  75.     
  76.     for child in root:
  77.         if (child.tag == "project"):
  78.             print child.attrib["name"]
  79.             if child.attrib.has_key("revision"):
  80.                 del child.attrib["revision"]
  81.     tree.write(xml_output, 'utf-8')

  82. def remove_remote(xml_input, xml_output):
  83.     tree = ElementTree(file=xml_input)
  84.     root = tree.getroot()
  85.     
  86.     for child in root:
  87.         if (child.tag == "project"):
  88.             print child.attrib["name"]
  89.             if child.attrib.has_key("remote"):
  90.                 del child.attrib["remote"]
  91.     tree.write(xml_output, 'utf-8')

  92. def remove_groups(xml_input, xml_output):
  93.     tree = ElementTree(file=xml_input)
  94.     root = tree.getroot()
  95.     
  96.     for child in root:
  97.         if (child.tag == "project"):
  98.             print child.attrib["name"]
  99.             if child.attrib.has_key("groups"):
  100.                 del child.attrib["groups"]
  101.     tree.write(xml_output, 'utf-8')

  102. def main():
  103.     usage = "usage: %prog [options] arg"
  104.     parser = OptionParser(usage)
  105.     parser.add_option("-i", "--in_file", dest="xml_input",
  106.                         help="xml input file")
  107.     parser.add_option("-j", "--in_file2", dest="xml_input2",
  108.                         help="xml input file")
  109.     parser.add_option("-r", "--remot", dest="aosp_remote",
  110.                         help="aosp xml remote")
  111.     parser.add_option("-o", "--out_file", dest="xml_output",
  112.                         help="xml output file")
  113.     parser.add_option("-t", "--type", dest="mig_type",
  114.                         help="migration type"
  115.                         "'aosp_to_snapshot' means migrate MCG AOSP mirror to snapshot"
  116.                         "'snapshot_to_otc' means migrate snapshot to otc upstream branch"
  117.                         "'del_groups' mean del groups"
  118.                         "'del_remote' mean del remote"
  119.                         "'modify_groups' mean modify groups"
  120.                         "'del_revision' means delete revision")
  121.     
  122.     (options, args) = parser.parse_args()

  123.     if options.mig_type == "aosp_to_snapshot":
  124.         migrate_aosp_to_snapshot(options.xml_input, options.aosp_remote, options.xml_output);
  125.     elif options.mig_type == "snapshot_to_otc":
  126.         migrate_snapshot_to_otc_upstream(options.xml_input, options.aosp_remote, options.xml_output);
  127.     elif options.mig_type == "del_revision":
  128.         remove_revision(options.xml_input, options.xml_output);
  129.     elif options.mig_type == "del_groups":
  130.         remove_groups(options.xml_input, options.xml_output);
  131.     elif options.mig_type == "del_remote":
  132.         remove_remote(options.xml_input, options.xml_output);
  133.     elif options.mig_type == "modify_groups":
  134.         modify_groups(options.xml_input, options.xml_output);
  135. if __name__ == "__main__":
  136.     main()


阅读(1544) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~