一个类似repo的小程序

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# usage :  python EasyRepo.py -u "13051041" -v "v6.0.0"
# by (13051041) 20160909

import os
try:
  import xml.etree.cElementTree as ET
except ImportError:
  import xml.etree.ElementTree as ET
import sys

projectList = []

class Project:
   def __init__(self,n,b):
        self.name = n
        self.branch = b

def downloadManifest(username,version):
    manifest = "git clone ssh://%s@10.27.149.227:29418/manifest"%(username)
    os.system(manifest)

    path = os.path.join(os.getcwd(),"manifest")
    for parent,dirnames,filenames in os.walk(path):
        for filename in filenames:
            if version in str(filename):
                print "the version xml file is:" + os.path.join(parent,filename)
                return os.path.join(parent,filename)

    pass

def analyzerXml(path):
    global projectList
    try:
        tree = ET.parse(path)
        root = tree.getroot()
    except Exception, e:
        print "Error:cannot parse file:country.xml."
        sys.exit(1)
    for project in root.findall('project'):
        name = project.get('name')
        version = project.get('revision')
        p = Project(name,version)
        projectList.append(p)
        print name, version

def syncProject(username):
    global projectList
    for p in projectList:
        cmd = '''
        git clone ssh://%s@10.27.149.227:29418/%s && scp -p -P 29418 %s@10.27.149.227:hooks/commit-msg %s/.git/hooks/
        cd %s
        git pull
        git checkout %s
        '''% (username,p.name,username,p.name,p.name,p.branch)
        os.system(cmd)



if __name__ == '__main__':
    help = '''usage :  python easyRepo.py -u 13051041 -v v6.0.0 '''
    username = ""
    version = ""
    numU = 0
    numV = 0
    numH = 0
    for i in range(1, len(sys.argv)):
        if "-v" in sys.argv[i]:
            numV = i
        if "-u" in sys.argv[i]:
            numU = i
        if "-h" in sys.argv[i]:
            numH = i
        if numU != 0 and i== numU+1:
            username = sys.argv[i]
        if numV != 0 and i== numV+1:
            version = sys.argv[i]

    if numH !=0:
        print help

    if numU == 0:
        print "参数有问题 没有 -u"
        print help
        sys.exit(1)

    if numV == 0:
        print "参数有问题 没有 -v"
        print help
        sys.exit(1)

    print "user name is ",username
    print "version name is ",version

    path = downloadManifest(username,version)
    analyzerXml(path)
    syncProject(username)

  

posted @ 2016-09-09 17:15  身带吴钩  阅读(216)  评论(0编辑  收藏  举报