本地已存在repo代码部署到git服务器上的方法

蓟清野
2023-12-01

部署的原则,参考本地已有的git库部署到git服务器上的方法

本地已经git库,我们将其部署到git服务器上步骤:

第一种:将已经存在的git库添加remote的方式,让已经存在的git库即指向BSP厂家的git库,要指向我们自己git服务器的git库,原厂有更新时,使用git fetch xxx(git fetch autochips(从autochips这个远程指向地址更新)或git fetch origin(从origin这个远程指向地址更新)),然后使用git push origin android-dev-p0.ac8257方式就可以间接更新代码。即可autochips指向BSP厂家git库,origin指向我们服务器git库。

参考命令字:
git服务器端:
git@zhaojingrong-S2600CP:~$ git init --bare atc/android/p/test_project
客户端:
//从BSP厂家下载源码
zhaojr@zhaojr-OptiPlex-7040:~/project/test_project$ git clone ssh://b_flyaudio001@release.autochips.com:29418/atc/android/p/test_project -b android-dev-p0.ac8257
//添加remote_path指向BSP厂家服务器地址
zhaojr@zhaojr-OptiPlex-7040:~/project/test_project$ git remote add remote_path ssh://b_flyaudio001@release.autochips.com:29418/atc/android/p/test_project
//将origin指向BSP服务器的remote名称删除
zhaojr@zhaojr-OptiPlex-7040:~/project/test_project$ git remote remove origin
//添加origin指向我们自己服务上创建的test_project地址
zhaojr@zhaojr-OptiPlex-7040:~/project/test_project$ git remote add origin git@172.168.1.63:/home/git/atc/android/p/test_project
//将代码上传到我们服务器中,保留git commit日志
zhaojr@zhaojr-OptiPlex-7040:~/project/test_project$ git push -u origin  android-dev-p0.ac8257

第二种方法:

使用代码镜像方式更新和迁移代码。

直接在git服务器上运行:
远程镜像获取
git clone --mirror ssh://b_flyaudio001@release.autochips.com:29418/atc/android/p/platform/art.git -b android-dev-p0.ac8257
或者
git clone --mirror http://release.autochips.com:29418/atc/android/p/platform/art.git -b android-dev-p0.ac8257
注意default.xml文件中BSP厂家代码服务器的远端路径
把镜像仓库推送到我们git服务器
在我们的git服务器新建好空项目:git init --bare atc/android/p/platform/art.git
git push --mirror git@172.168.1.63:/home/git/atc/android/p/platform/art.git //这里用绝对路径,直接推送即可,推送的是android-dev-p0.ac8257,并没有master分支。所以后面下载的时候请将上分支名称,否则会报错误
验证:
git clone git@172.168.1.63:/home/git/atc/android/p/platform/art.git -b android-dev-p0.ac8257 
验证OK了之后,在使用当前验证的分支,在远程服务器上创建master分支,如下:
git push origin android-dev-p0.ac8257:master

更新:
1)先执行远程更新命令
git remote update  //从BSP厂家服务器获取(ssh://b_flyaudio001@release.autochips.com:29418/atc/android/p/platform/art.git),因为git remove -v指向BSP厂家服务器
2)再执行推送命令
git push --mirror git@172.168.1.63:/home/git/atc/android/p/platform/art.git //这里用绝对路径,并且推送到我们自己服务器

基于以上原理,目前我们项目使用第一种方法创建的,是基于代码库中的.repo/manifests/default.xml文件编写脚本批量创建git库,同时批量上传代码。而第二种方法更快更迅速(直接编写个脚本全自动话即可),没尝试,下次需要,尝试一下。

1、基于原厂给的BSP下载方式下载代码,将androidP项目的代码拉到本地,如下:

zhaojr@zhaojr-OptiPlex-7040:~/project/mt8257_mirror$ repo init -u ssh://b_flyaudio001@release.autochips.com:29418/atc/android/p/manifest -b android-dev-p0.ac8257 -g all,-notdefault
zhaojr@zhaojr-OptiPlex-7040:~/project/mt8257_mirror$ repo sync –c

2、基于.repo/manifests/default.xml文件在git服务器上批量创建git子库

编写脚本,脚本如下:

//git 服务器上批量创建子库,运行方法:
###getnames_and_create_project.py default.xml
#!/usr/bin/python3

import os
import sys

###该脚本在default.xml的remote地址绝对路径下运行,即/home/git 目录下运行,运行方式
#getnames_and_create_project_autochips.py atc_default.xml即可

if len(sys.argv) == 1:
    print('错误!请传入 xml 文件')
elif len(sys.argv) > 2:
    print('错误!传入参数太多')
else:
    print('传入的文件是 %s' % sys.argv[1])

with open(sys.argv[1], 'r') as fin:
    while True:
        linestr = fin.readline()
        if linestr == '':       #表示文件结束
            break
        print(linestr)
        #下面开始对本行内容分析
        if (('name=' in linestr) or ('name =' in linestr)) and (('project' in linestr) or ('path' in linestr)):   #本行内容含有name信息
            print(linestr)
            #下面分析本行内容,并提取name
            charistr1 = 'name="'
            charistr2 = '"'
            gitprojstr = linestr[linestr.index(charistr1)+len(charistr1) : linestr.index(charistr1)+len(charistr1)+ linestr[linestr.index(charistr1)+len(charistr1):].index(charistr2)]
            ###gitprojstr为default.xml左边的name= xxx/xxx的路径,如name="atc/android/p/device/generic/arm64
            print(gitprojstr)
            path_m=gitprojstr
            if path_m == 'autochips':
                print(path_m)
            else:
                #下面开始创建git工程
                #cmd = 'git init --bare %s.git' % gitprojstr
                syspath = sys.path[0]
                print(syspath)
                #cmd = 'cd %s && mkdir %s.git && cd %s && git init --bare && cd %s' % (sys.path[0], gitprojstr, gitprojstr,  sys.path[0])
                ####在/home/git/路径下批量创建name= xxx/xxx的路径并初始化init,即根据default.xml来批量创建空的git库
                cmd = 'git init --bare %s' % gitprojstr
                print(cmd)
                os.system(cmd)
        #cmd = 'git init --bare %s' % gitprojstr
        #cmd = 'git init --bare %s.git' % gitprojstr
        #cmd = 'mkdir %s.git && cd %s && git init --bare && cd %s' % (localpath, localpath,  sys.path[0])
        #print(cmd)
        #os.system(cmd)

修改default.xml文件,将文件中的

 <remote fetch="../../.." name="autochips" review="release.autochips.com:8080"/>
修改成:
<?xml version="1.0" encoding="UTF-8"?>
<manifest>
  <!-- 注意:这里使用的远程的地址,在运行脚本的时候只能在/home/git/目录运行,后面使用getnames_and_init_push_git_proj_ok.py default.xml脚本的时候,
       上传代码的origin 指向的remote地址必须使用git@172.168.1.63:/home/git/xxxx/xxxx/xxxx/xxx/xxx.git绝对路径,不能使用使用相对路径,否则会存在异常无法上传的情况
       而name=autochips这个不影响在git服务器上批量创建文件,只跟 remote地址有关-->
  <remote fetch="ssh://git@172.168.1.63:/home/git" name="autochips"/>  //主要修改这里,其它不变
  
  <default remote="autochips" revision="android-dev-p0.ac8257" sync-j="4"/>

git 服务器上运行(将修改好的getnames_and_create_project.py文件和default.xml文件拷贝到/home/git/目录中运行):

git服务器端:

注意:以上default.xml文件中我们定义remote是:ssh://git@172.168.1.63:/home/git,所以该脚本在/home/git 目录下运行,否则后面上传代码路径会有差异。
git@zhaojingrong-S2600CP:~$ getnames_and_create_project.py default.xml

即可创建default.xml文件中的左边的name="atc/android/p/xxxxx/xxxx/xxxx的所有子库。

3、将本地从BSP厂家下载的repo代码库批量上传到git 服务器端创建的所有子库上

3.1 先单独创建manifests库,用来管路所有子库,主要是xxx.xml文件上传到这个库中,方法如下:

一)从BSP厂家的repo代码库中单独下载manifests库,如下:
zhaojiawei@zhaojingrong-S2600CP:~/test_manifests$ git clone ssh://b_flyaudio001@release.autochips.com:29418/atc/android/p/manifest -b android-dev-p0.ac8257
二)修改manifest库的远程指向,让origin指向我们的git服务器的manifest库(git@172.168.1.63:atc/android/p/manifest)
zhaojiawei@zhaojingrong-S2600CP:~/test_manifests/manifest$ git remote add remote_path ssh://b_flyaudio001@release.autochips.com:29418/atc/android/p/manifest
zhaojiawei@zhaojingrong-S2600CP:~/test_manifests/manifest$ git remote remove origin
zhaojiawei@zhaojingrong-S2600CP:~/test_manifests/manifest$ git remote add origin git@172.168.1.63:atc/android/p/manifest
2) 修改./manifest/default.xml文件,如下:
<?xml version="1.0" encoding="UTF-8"?>
<manifest>
  <remote fetch="../../.." name="autochips" review="release.autochips.com:8080"/>  
  <default remote="autochips" revision="android-dev-p0.ac8257" sync-j="4"/>
修改成:
<?xml version="1.0" encoding="UTF-8"?>
<manifest>
  <remote fetch="ssh://git@172.168.1.63:/home/git" name="origin"/>  //name为我们远程服务器地址的别名 remote fetch为我们的git服务器的地址,注意这里的地址为第1步中批量创建git库中的default.xml修改时的地址,必须保持一致,否则后面的上传和下载会提示找不到路径
  <default remote="origin" revision="android-dev-p0.ac8257" sync-j="4"/> //revision为创建代码的分支名称
保存,退出。
三、添加修改并上传到我们服务器的manifest库的android-dev-p0.ac8257分支和master分支中
zhaojiawei@zhaojingrong-S2600CP:~/test_manifests/manifest$ git add ./
zhaojiawei@zhaojingrong-S2600CP:~/test_manifests/manifest$ git commit -m "init New xxx Project"
zhaojiawei@zhaojingrong-S2600CP:~/test_manifests/manifest$ git push origin android-dev-p0.ac8257
zhaojiawei@zhaojingrong-S2600CP:~/test_manifests/manifest$ git push origin android-dev-p0.ac8257:master

3.2 编写脚本,基于BSP厂家给的.repo/manifests/default.xml文件批量上传代码

3.2.1)因为在第一步中从BSP原厂拉的代码到本地,是没有指定分支的,如下:

zhaojr@zhaojr-OptiPlex-7040:~/project/mt8257_mirror/art$ git branch -a
* defult
  remotes/autochips/android-dev-p0.ac8257
  remotes/m/android-dev-p0.ac8257 -> autochips/android-dev-p0.ac8257

所以我们需要先切换分支,不要使用repo forall -c "git checkout -b xxxx  autochips/xxxx的方式切换,要跟批量上传脚本保持一致,使用如下脚本(getnames_and_checkout_proj.py):

#!/usr/bin/python3
###running:getnames_and_checkout_proj.py default.xml

import os
import sys

#git@172.168.1.63/home/git/Project/8227L_project
##remote = 'git@172.168.1.63:atc/android/o'
remote = 'git@172.168.1.63:/home/git'

if len(sys.argv) == 1:
    print('错误!请传入 xml 文件')
elif len(sys.argv) > 2:
    print('错误!传入参数太多')
else:
    print('传入的文件是 %s' % sys.argv[1])

with open(sys.argv[1], 'r') as fin:
    while True:
        linestr = fin.readline()
        if linestr == '':       #表示文件结束
            break 
        print(linestr)
        #下面开始对本行内容分析
        if (('name=' in linestr) or ('name =' in linestr)) and (('project' in linestr) or ('path' in linestr)):   #本行内容含有name信息
            #先无条件提取name路径
            charistr1 = 'name="'
            charistr2 = '"'
            namestr = linestr[linestr.index(charistr1)+len(charistr1) : linestr.index(charistr1)+len(charistr1)+ linestr[linestr.index(charistr1)+len(charistr1):].index(charistr2)]
            if 'path=' in linestr:            #如果path存在则用path的路径作为本地路径
                charistr1 = 'path="'
                charistr2 = '"'
                pathstr = linestr[linestr.index(charistr1)+len(charistr1) : linestr.index(charistr1)+len(charistr1)+ linestr[linestr.index(charistr1)+len(charistr1):].index(charistr2)]
            else:                             #如果path不存在,则认为path路径(本地路径)就是name路径
                pathstr = namestr
            print('name="%s", path="%s"' % (namestr, pathstr))
            path_m = pathstr
            #if path_m == 'autochips':  #路径等于origin,那么不创建
            if path_m == 'origin':  #路径等于origin,那么不创建
                print(path_m)
            else:
                #下面开始初始化并提交git工程
                localpath = sys.path[0] + '/' + pathstr        # git工程的本地绝对路径
                remotepath = remote + '/' + namestr            # git工程远程相对路径
                print('localpath="%s"' %localpath)
                print('remotepath="%s"' %remotepath)
                #判断本地目录是否为空,为空的话则新建一个文件,空目录会导致git提交失败
                if os.path.exists(localpath):     #目录存在上传文件
                    #cmd = 'touch %s/._USELESSFILE_' % (localpath)
                    #cmd = 'touch %s/.gitignore' % (localpath)
                    print(localpath)
                    #os.system(cmd)
                    if not os.listdir(localpath):       # 本地目录为空
                        cmd = 'touch %s/.gitignore' % (localpath)
                        print(localpath)
                        #print('localpath NULL="%s"' %localpath)
                        #print('localpath NULL cmd="%s"' %cmd)
                        print(cmd)
                        os.system(cmd)
                    #cmd = 'cd %s && git remote add set-url autochips %s && git add . && git commit -m "init base 8827L project" &&git push -u autochips android-trunk-o1.ac8227L && cd %s' % (localpath, remotepath, sys.path[0])
                    #cmd = 'cd %s && git remote set-url autochips %s &&git push -u autochips android-trunk-o1.ac8227L && cd %s' % (localpath, remotepath, sys.path[0])
                    #print(remotepath)
                    #cmd = 'cd %s && git remote set-url autochips %s && cd %s' % (localpath, remotepath, sys.path[0])
                    #cmd = 'cd %s && git remote add origin %s && cd %s' % (localpath, remotepath, sys.path[0])
                    #cmd = 'cd %s && git remote remove origin  && cd %s' % (localpath, sys.path[0]) ##如果代码库中有origin的远程指向将移除origin
                    #print(cmd)
                    #os.system(cmd)
                    #代码下载之后没切换分支,先切换分支
                    cmd = 'cd %s && git checkout -b android-dev-p0.ac8257 autochips/android-dev-p0.ac8257 && cd %s' % (localpath, sys.path[0]) ##将所有子库分支切出来
                    print(cmd)
                    os.system(cmd)
                    #添加remote为origin 指向我们的git 服务器地址
                    ##cmd = 'cd %s && git remote add origin %s && cd %s' % (localpath, remotepath, sys.path[0]) ##添加origin的远程指向,指向我们的代码服务器
                    ##print(cmd)
                    ##os.system(cmd)
                    ##开始批量上传代码
                    cmd = 'cd %s && git push -u origin android-dev-p0.ac8257 && cd %s' % (localpath, sys.path[0]) ###批量上传代码库                                 
                    ##print(cmd)
                    ##os.system(cmd)
                else:       #目录不存在不上传
                    print(localpath)
                   #cmd = 'cd %s && rm -rf .git && git init && git remote add origin %s && git add . -f && git commit -m "init " &&git push -u origin master && cd %s' % (localpath, remotepath, sys.path[0])
                    #print(cmd)
                    #os.system(cmd)

注意:以上脚本在checkout分支的时候,remote使用autochips,主要原因是我们在下载原厂BSP的时候代码库的remote名称是autochips,指向原厂BSP的gerrite路径,如下:

zhaojr@zhaojr-OptiPlex-7040:~/project/mt8257_mirror/art$ git branch -a
* defult
  remotes/autochips/android-dev-p0.ac8257
  remotes/m/android-dev-p0.ac8257 -> autochips/android-dev-p0.ac8257

主要使用的脚本内容:

#代码下载之后没切换分支,先切换分支
                    cmd = 'cd %s && git checkout -b android-dev-p0.ac8257 autochips/android-dev-p0.ac8257 && cd %s' % (localpath, sys.path[0]) ##将所有子库分支切出来
                    print(cmd)
                    os.system(cmd)

 

修改default.xml文件,内容修改如下:

<?xml version="1.0" encoding="UTF-8"?>
<manifest>
  <remote fetch="../../.." name="autochips" review="release.autochips.com:8080"/>  
  <default remote="autochips" revision="android-dev-p0.ac8257" sync-j="4"/>
  <project name="atc/android/p/ReleaseNote" path="ReleaseNote" />
修改成:
  <?xml version="1.0" encoding="UTF-8"?>
<manifest>
  <remote fetch="ssh://git@172.168.1.63:/home/git" name="origin"/>  
  <default remote="origin" revision="android-dev-p0.ac8257" sync-j="4"/>
  <project name="atc/android/p/ReleaseNote" path="ReleaseNote" />

在本地的test_mt8257/目录中运行(将修改好的getnames_and_checkout_proj.py文件和default.xml文件拷贝到test_mt8257/目录中运行):

zhaojr@zhaojr-OptiPlex-7040:~/project/mt8257_mirror$ getnames_and_checkout_proj.py default.xml

完成后,开始代码库的批量上传

3.2.2)编写脚本进行代码库的批量上传(上传要求保留原厂的修改日志)

1)编写批量上传代码的脚本

#!/usr/bin/python3
#####running: getnames_and_init_push_git_proj.py default.xml

import os
import sys

#remote = 'git@127.0.0.1:rk3229'
#git@172.168.1.63/home/git/Project/8227L_project
##remote = 'git@172.168.1.63:atc/android/o'
remote = 'git@172.168.1.63:/home/git'
#remote = 'ssh://b_flyaudio001@release.autochips.com:29418'
#remote = 'ssh://b_flyaudio001@release.autochips.com:29418'

if len(sys.argv) == 1:
    print('错误!请传入 xml 文件')
elif len(sys.argv) > 2:
    print('错误!传入参数太多')
else:
    print('传入的文件是 %s' % sys.argv[1])

with open(sys.argv[1], 'r') as fin:
    while True:
        linestr = fin.readline()
        if linestr == '':       #表示文件结束
            break 
        #####for project-list.txt       
        #else:
            #print(linestr)			
            #localpath = sys.path[0] + '/' + linestr        # git工程的本地绝对路径
            #remotepath = remote + '/' + linestr            # git工程远程相对路径
            #cmd = 'cd %s && git remote set-url autochips %s &&git push -u autochips android-trunk-o1.ac8227L && cd %s' % (localpath, remotepath, sys.path[0])
            #print(cmd)
            #os.system(cmd)
			#end for  project-list.txt
        print(linestr)
        #下面开始对本行内容分析
        if (('name=' in linestr) or ('name =' in linestr)) and (('project' in linestr) or ('path' in linestr)):   #本行内容含有name信息
            #print(linestr)
            #先无条件提取name路径
            charistr1 = 'name="'
            charistr2 = '"'
            namestr = linestr[linestr.index(charistr1)+len(charistr1) : linestr.index(charistr1)+len(charistr1)+ linestr[linestr.index(charistr1)+len(charistr1):].index(charistr2)]
            if 'path=' in linestr:            #如果path存在则用path的路径作为本地路径
                charistr1 = 'path="'
                charistr2 = '"'
                pathstr = linestr[linestr.index(charistr1)+len(charistr1) : linestr.index(charistr1)+len(charistr1)+ linestr[linestr.index(charistr1)+len(charistr1):].index(charistr2)]
            else:                             #如果path不存在,则认为path路径(本地路径)就是name路径
                pathstr = namestr
            print('name="%s", path="%s"' % (namestr, pathstr))
            path_m = pathstr
            #if path_m == 'autochips':  #路径等于origin,那么不创建
            if path_m == 'origin':  #路径等于origin,那么不创建
                print(path_m)
            else:
                #下面开始初始化并提交git工程
                localpath = sys.path[0] + '/' + pathstr        # git工程的本地绝对路径
                remotepath = remote + '/' + namestr            # git工程远程相对路径
                print('localpath="%s"' %localpath)
                print('remotepath="%s"' %remotepath)
                #判断本地目录是否为空,为空的话则新建一个文件,空目录会导致git提交失败
                if os.path.exists(localpath):     #目录存在上传文件
                    #cmd = 'touch %s/._USELESSFILE_' % (localpath)
                    #cmd = 'touch %s/.gitignore' % (localpath)
                    print(localpath)
                    #os.system(cmd)
                    if not os.listdir(localpath):       # 本地目录为空
                        cmd = 'touch %s/.gitignore' % (localpath)
                        print(localpath)
                        #print('localpath NULL="%s"' %localpath)
                        #print('localpath NULL cmd="%s"' %cmd)
                        print(cmd)
                        os.system(cmd)
                    #cmd = 'cd %s && git remote add set-url autochips %s && git add . && git commit -m "init base 8827L project" &&git push -u autochips android-trunk-o1.ac8227L && cd %s' % (localpath, remotepath, sys.path[0])
                    #cmd = 'cd %s && git remote set-url autochips %s &&git push -u autochips android-trunk-o1.ac8227L && cd %s' % (localpath, remotepath, sys.path[0])
                    #print(remotepath)
                    #cmd = 'cd %s && git remote set-url autochips %s && cd %s' % (localpath, remotepath, sys.path[0])
                    #cmd = 'cd %s && git remote add origin %s && cd %s' % (localpath, remotepath, sys.path[0])
                    #cmd = 'cd %s && git remote remove origin  && cd %s' % (localpath, sys.path[0]) ##如果代码库中有origin的远程指向将移除origin
                    #print(cmd)
                    #os.system(cmd)
                    #代码下载之后没切换分支,先切换分支
                    ##cmd = 'cd %s && git checkout -b android-dev-p0.ac8257 autochips/android-dev-p0.ac8257 && cd %s' % (localpath, sys.path[0]) ##将所有子库分支切出来
                    ##print(cmd)
                    ##os.system(cmd)
                    #添加remote为origin 指向我们的git 服务器地址
                    cmd = 'cd %s && git remote add origin %s && cd %s' % (localpath, remotepath, sys.path[0]) ##添加origin的远程指向,指向我们的代码服务器
                    print(cmd)
                    os.system(cmd)
                    ##开始批量上传代码
                    cmd = 'cd %s && git push -u origin android-dev-p0.ac8257 && cd %s' % (localpath, sys.path[0]) ###批量上传代码库                                 
                    print(cmd)
                    os.system(cmd)
                else:       #目录不存在不上传
                    print(localpath)
                   #cmd = 'cd %s && rm -rf .git && git init && git remote add origin %s && git add . -f && git commit -m "init " &&git push -u origin master && cd %s' % (localpath, remotepath, sys.path[0])
                    #print(cmd)
                    #os.system(cmd)

以上脚本中的关键代码:

            
             if path_m == 'origin':  #路径等于origin,那么不创建,这个很关键,在name=origin时(即default.xml中的<default remote="origin" revision="android-dev-p0.ac8257" sync-j="4"/>这个语句要判断一下,否则会报错误)
                print(path_m)
            else:
            ..................................       
             if not os.listdir(localpath):       # 本地目录为空,这个需要,在上传文件时防止有空目录
                        cmd = 'touch %s/.gitignore' % (localpath)
                        print(localpath)
                        #print('localpath NULL="%s"' %localpath)
                        #print('localpath NULL cmd="%s"' %cmd)
                        print(cmd)
                        os.system(cmd)
                 ............................................
                 ####添加origin的别名的指向,因为BSP厂家给的只有autochips别名,并且指向BSP厂家自己的代码服务器,需要添加我们自己的别名并指向我们自己的代码服务器               
                 cmd = 'cd %s && git remote add origin %s && cd %s' % (localpath, remotepath, sys.path[0]) ##添加origin的远程指向,指向我们的代码服务器
                    print(cmd)
                    os.system(cmd)
                    ##开始批量上传代码,保留BSP厂家的修改
                    cmd = 'cd %s && git push -u origin android-dev-p0.ac8257 && cd %s' % (localpath, sys.path[0]) ###批量上传代码库                                 
                    print(cmd)
                    os.system(cmd)

default.xml文件使用3.2.1中修改好的xml文件,将getnames_and_init_push_git_proj.py文件和default.xml文件拷贝到本地代码镜像目录test_mt8257/目录中执行即可。

zhaojr@zhaojr-OptiPlex-7040:~/project/mt8257_mirror$ getnames_and_init_push_git_proj.py default.xml

上传完成之后进行部署代码库的验证工作。

3.2.3 上传时建立master分支

在以上上传基础上修改如下:

#!/usr/bin/python3
#####running: getnames_and_init_push_master_proj.py default.xml

import os
import sys

#remote = 'git@127.0.0.1:rk3229'
#git@172.168.1.63/home/git/Project/8227L_project
##remote = 'git@172.168.1.63:atc/android/o'
remote = 'git@172.168.1.63:/home/git'
#remote = 'ssh://b_flyaudio001@release.autochips.com:29418'
#remote = 'ssh://b_flyaudio001@release.autochips.com:29418'

if len(sys.argv) == 1:
    print('错误!请传入 xml 文件')
elif len(sys.argv) > 2:
    print('错误!传入参数太多')
else:
    print('传入的文件是 %s' % sys.argv[1])

with open(sys.argv[1], 'r') as fin:
    while True:
        linestr = fin.readline()
        if linestr == '':       #表示文件结束
            break 
        print(linestr)
        #下面开始对本行内容分析
        if (('name=' in linestr) or ('name =' in linestr)) and (('project' in linestr) or ('path' in linestr)):   #本行内容含有name信息
            #print(linestr)
            #先无条件提取name路径
            charistr1 = 'name="'
            charistr2 = '"'
            namestr = linestr[linestr.index(charistr1)+len(charistr1) : linestr.index(charistr1)+len(charistr1)+ linestr[linestr.index(charistr1)+len(charistr1):].index(charistr2)]
            if 'path=' in linestr:            #如果path存在则用path的路径作为本地路径
                charistr1 = 'path="'
                charistr2 = '"'
                pathstr = linestr[linestr.index(charistr1)+len(charistr1) : linestr.index(charistr1)+len(charistr1)+ linestr[linestr.index(charistr1)+len(charistr1):].index(charistr2)]
            else:                             #如果path不存在,则认为path路径(本地路径)就是name路径
                pathstr = namestr
            print('name="%s", path="%s"' % (namestr, pathstr))
            path_m = pathstr
            #if path_m == 'autochips':  #路径等于origin,那么不创建
            if path_m == 'origin':  #路径等于origin,那么不创建
                print(path_m)
            else:
                #下面开始初始化并提交git工程
                localpath = sys.path[0] + '/' + pathstr        # git工程的本地绝对路径
                remotepath = remote + '/' + namestr            # git工程远程相对路径
                print('localpath="%s"' %localpath)
                print('remotepath="%s"' %remotepath)
                #判断本地目录是否为空,为空的话则新建一个文件,空目录会导致git提交失败
                if os.path.exists(localpath):     #目录存在上传文件
                    print(localpath)
                    if not os.listdir(localpath):       # 本地目录为空
                        cmd = 'touch %s/.gitignore' % (localpath)
                        print(localpath)
                        #print('localpath NULL="%s"' %localpath)
                        #print('localpath NULL cmd="%s"' %cmd)
                        print(cmd)
                        os.system(cmd)
                    #cmd = 'cd %s && git remote add set-url autochips %s && git add . && git commit -m "init base 8827L project" &&git push -u autochips android-trunk-o1.ac8227L && cd %s' % (localpath, remotepath, sys.path[0])
                    #cmd = 'cd %s && git remote set-url autochips %s &&git push -u autochips android-trunk-o1.ac8227L && cd %s' % (localpath, remotepath, sys.path[0])
                    #print(remotepath)
                    #cmd = 'cd %s && git remote set-url autochips %s && cd %s' % (localpath, remotepath, sys.path[0])
                    #cmd = 'cd %s && git remote add origin %s && cd %s' % (localpath, remotepath, sys.path[0])
                    #cmd = 'cd %s && git remote remove origin  && cd %s' % (localpath, sys.path[0]) ##如果代码库中有origin的远程指向将移除origin
                    #print(cmd)
                    #os.system(cmd)
                    #代码下载之后没切换分支,先切换分支
                    ##cmd = 'cd %s && git checkout -b android-dev-p0.ac8257 autochips/android-dev-p0.ac8257 && cd %s' % (localpath, sys.path[0]) ##将所有子库分支切出来
                    ##print(cmd)
                    ##os.system(cmd)
                    #添加remote为origin 指向我们的git 服务器地址
                    ##cmd = 'cd %s && git remote add origin %s && cd %s' % (localpath, remotepath, sys.path[0]) ##添加origin的远程指向,指向我们的代码服务器
                    ##print(cmd)
                    ##os.system(cmd)
                    ##开始批量上传代码
                    cmd = 'cd %s && git push -u origin android-dev-p0.ac8257:masetr && cd %s' % (localpath, sys.path[0]) ###批量上传代码库                                 
                    print(cmd)
                    os.system(cmd)
                else:       #目录不存在不上传
                    print(localpath)
                   #cmd = 'cd %s && rm -rf .git && git init && git remote add origin %s && git add . -f && git commit -m "init " &&git push -u origin master && cd %s' % (localpath, remotepath, sys.path[0])
                    #print(cmd)
                    #os.system(cmd)

关键脚本如下:

####3.2.3中在上传完成android-dev-p0.ac8257分支之后,再单独批量上传master分支,master分支和android-dev-p0.ac8257分支指向同一个节点:
                    #代码下载之后没切换分支,先切换分支
                    ##cmd = 'cd %s && git checkout -b android-dev-p0.ac8257 autochips/android-dev-p0.ac8257 && cd %s' % (localpath, sys.path[0]) ##将所有子库分支切出来
                    ##print(cmd)
                    ##os.system(cmd)
                    #添加remote为origin 指向我们的git 服务器地址
                    ##cmd = 'cd %s && git remote add origin %s && cd %s' % (localpath, remotepath, sys.path[0]) ##添加origin的远程指向,指向我们的代码服务器
                    ##print(cmd)
                    ##os.system(cmd)
                    ##开始批量上传代码并创建master分支
                    cmd = 'cd %s && git push -u origin android-dev-p0.ac8257:master && cd %s' % (localpath, sys.path[0]) ###批量上传代码库                                 
                    print(cmd)
                    os.system(cmd)
在3.2.2中可以一次到位,如下脚本:
                     ##cmd = 'cd %s && git checkout -b android-dev-p0.ac8257 autochips/android-dev-p0.ac8257 && cd %s' % (localpath, sys.path[0]) ##将所有子库分支切出来
                    ##print(cmd)
                    ##os.system(cmd)
                    #添加remote为origin 指向我们的git 服务器地址
                    cmd = 'cd %s && git remote add origin %s && cd %s' % (localpath, remotepath, sys.path[0]) ##添加origin的远程指向,指向我们的代码服务器
                    print(cmd)
                    os.system(cmd)
                    ##开始批量上传代码并创建master分支
                    cmd = 'cd %s && git push -u origin android-dev-p0.ac8257 && git push -u origin android-dev-p0.ac8257:master && cd %s' % (localpath, sys.path[0]) ###批量上传代码库                                 
                    print(cmd)
                    os.system(cmd)

将脚本getnames_and_init_push_master_proj文件和py default.xml文件拷贝到~/project/mt8257_mirror/目录中执行:

执行:zhaojr@zhaojr-OptiPlex-7040:~/project/mt8257_mirror$ getnames_and_init_push_master_proj.py default.xml

4、代码验证

   在本地编译环境中下载刚刚我们创建好的代码,本地编译环境预留180G的空间,防止编译过程中硬盘空间不够,如下:

1、单独下载android-dev-p0.ac8257分支
zhaojr@zhaojr-OptiPlex-7040:~/project/test_mt8257$ repo init -u git@172.168.1.63:atc/android/p/manifest -b android-dev-p0.ac8257
zhaojr@zhaojr-OptiPlex-7040:~/project/test_mt8257r$ repo sync -c
2、所有分支全部下载,下载完成后自己切换分支,如下:
zhaojr@zhaojr-OptiPlex-7040:~/project/test_mt8257$ repo init -u git@172.168.1.63:atc/android/p/manifest
zhaojr@zhaojr-OptiPlex-7040:~/project/test_mt8257$ repo sync

我们使用第一种,因为方便同步ATC的代码,我们不使用3.2.3批量上传master分支的方法(这种方法在我们中间代码库(BSP厂家代码服务器拉的库,它同时指向BSP厂家服务器和我们自己的git服务器)),我们将在下载编译OK之后,使用拉我们服务器(git@172.168.1.63:atc/android/p/manifest)ropo代码库到本地,然后使用
repo forall -c "git push origin android-dev-p0.ac8257:master"的方式创建master分支

在代码编译过程中提示以下信息:

FAILED: out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system.img 
/bin/bash -c "(if [ -d out/target/product/k61v1_demo_64_bsp/system/vendor ] && [ ! -h out/target/product/k61v1_demo_64_bsp/system/vendor ]; then echo 'Non-symlink out/target/product/k61v1_demo_64_bsp/system/vendor detected!' 1>&2; echo 'You cannot install files to out/target/product/k61v1_demo_64_bsp/system/vendor while building a separate vendor.img!' 1>&2; exit 1; fi ) && (ln -sf /vendor out/target/product/k61v1_demo_64_bsp/system/vendor ) && (mkdir -p out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/ out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates && rm -rf out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"ext_mkuserimg=mkuserimg_mke2fs.sh\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"fs_type=ext4\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"system_size=3221225472\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"userdata_size=3221225472\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"cache_fs_type=ext4\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"cache_size=452984832\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"vendor_fs_type=ext4\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"vendor_size=838860800\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"extfs_sparse_flag=-s\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"squashfs_sparse_flag=-s\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"selinux_fc=out/target/product/k61v1_demo_64_bsp/obj/ETC/file_contexts.bin_intermediates/file_contexts.bin\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"system_verity_block_device=/dev/block/platform/bootdevice/by-name/system\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"vendor_verity_block_device=/dev/block/platform/bootdevice/by-name/vendor\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"avb_avbtool=avbtool\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"avb_system_hashtree_enable=true\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"avb_system_add_hashtree_footer_args=--rollback_index 0 --setup_as_rootfs_from_kernel\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"avb_system_key_path=device/mediatek/common/system_prvk.pem\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"avb_system_algorithm=SHA256_RSA2048\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"avb_system_rollback_index_location=2\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"avb_vendor_hashtree_enable=true\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"avb_vendor_add_hashtree_footer_args=\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"avb_product_hashtree_enable=true\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"avb_product_add_hashtree_footer_args=\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"system_root_image=true\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt; echo \"ramdisk_dir=out/target/product/k61v1_demo_64_bsp/root\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (echo \"skip_fsck=true\" >>  out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt ) && (mkdir -p out/target/product/k61v1_demo_64_bsp/system/data ) && (python ./build/tools/releasetools/rootcheck.py out/target/product/k61v1_demo_64_bsp/system out/target/product/k61v1_demo_64_bsp/lk.bin out/target/product/k61v1_demo_64_bsp/recovery.img out/target/product/k61v1_demo_64_bsp/boot.img out/target/product/k61v1_demo_64_bsp/logo.bin out/target/product/k61v1_demo_64_bsp/system/data out/target/product/k61v1_demo_64_bsp/trustzone.bin ) && (PATH=out/host/linux-x86/bin/:\$PATH build/make/tools/releasetools/build_image.py out/target/product/k61v1_demo_64_bsp/system out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system_image_info.txt out/target/product/k61v1_demo_64_bsp/obj/PACKAGING/systemimage_intermediates/system.img out/target/product/k61v1_demo_64_bsp/system || ( echo \"Out of space? the tree size of out/target/product/k61v1_demo_64_bsp/system is (MB): \" 1>&2 ; du -sm out/target/product/k61v1_demo_64_bsp/system 1>&2; if [ \"ext4\" == \"ext4\" ]; then maxsize=3221225472; echo \"The max is \$(( maxsize / 1048576 )) MB.\" 1>&2 ; else echo \"The max is \$(( 3221225472 / 1048576 )) MB.\" 1>&2 ; fi; mkdir -p out/dist; cp out/target/product/k61v1_demo_64_bsp/installed-files.txt out/dist/installed-files-rescued.txt; exit 1 ) )"
crc and md5 generate start
out/target/product/k61v1_demo_64_bsp/lk.bin
out/target/product/k61v1_demo_64_bsp/recovery.img
out/target/product/k61v1_demo_64_bsp/boot.img
out/target/product/k61v1_demo_64_bsp/logo.bin
out/target/product/k61v1_demo_64_bsp/system
cal system file crc begin
cal_system_file_crc in_dir:out/target/product/k61v1_demo_64_bsp/system
Traceback (most recent call last):
  File "./build/tools/releasetools/rootcheck.py", line 321, in <module>
    main(sys.argv[1:])
  File "./build/tools/releasetools/rootcheck.py", line 295, in main
    cal_system_file_crc(file_count,in_dir)
  File "./build/tools/releasetools/rootcheck.py", line 215, in cal_system_file_crc
    frb.close()
UnboundLocalError: local variable 'frb' referenced before assignment
ninja: build stopped: subcommand failed.
18:57:02 ninja failed with: exit status 1

请按照以下方法修改:

方法1:修改build\make\core\Makefile中的create-system-vendor-symlink函数,在编译vendor.img存在时,不执行

$(hide) ln -sf /vendor $(TARGET_OUT)/vendor软连接动作

修改:build\make\core\Makefile
# Create symlink /system/vendor to /vendor if necessary.
ifdef BOARD_USES_VENDORIMAGE
define create-system-vendor-symlink
$(hide) if [ -d $(TARGET_OUT)/vendor ] && [ ! -h $(TARGET_OUT)/vendor ]; then \
  echo 'Non-symlink $(TARGET_OUT)/vendor detected!' 1>&2; \
  echo 'You cannot install files to $(TARGET_OUT)/vendor while building a separate vendor.img!' 1>&2; \
  exit 1; \
fi
##$(hide) ln -sf /vendor $(TARGET_OUT)/vendor //注释掉这行,因为在编译成vendor.img的时候就不再需要软连接了,系统已经在out/target/product/k61v1_demo_64_bsp/下面生成vendor,而并不是从system/目录下软连接过来,当然不同的编译环境会有不同,有些环境是不需要修改的,目前使用的环境(gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3))
endef
else
define create-system-vendor-symlink
$(hide) ln -sf /vendor $(TARGET_OUT)/vendor  //在这里添加这行,在有vendor.img时脚本根本跑不到这里
endef
endif

# Create symlink /system/product to /product if necessary.
ifdef BOARD_USES_PRODUCTIMAGE  //这个是没有定义的,即可没有prodect.img编译,所以根本就跑不到以下代码
define create-system-product-symlink
$(hide) if [ -d $(TARGET_OUT)/product ] && [ ! -h $(TARGET_OUT)/product ]; then \
  echo 'Non-symlink $(TARGET_OUT)/product detected!' 1>&2; \
  echo 'You cannot install files to $(TARGET_OUT)/product while building a separate product.img!' 1>&2; \
  exit 1; \
fi
$(hide) ln -sf /product $(TARGET_OUT)/product
endef
else   //没有product.img编译就直接退出,不做任何操作
define create-system-product-symlink
endef
endif

以上方法编译之后,如果系统不能正常启动到主界面,请使用第二种方法.

方法二:修改build\make\tools\releasetools\rootcheck.py

def cal_system_file_crc(file_count,in_dir):
  value=-1
  global ENCRYPT_KEY
  key=ENCRYPT_KEY
  number=0
  crc=0
  fo=open(file_count,"w")
  fo.write(str(value))
  fo.write("\t")
  fo.write("file_number_in_system_dayu")
  fo.write("\t")
  fo.write(str(fileCountIn(in_dir)))
  fo.write("\n")
  for root, dirs, files in os.walk(in_dir):
    for file in files:
      crc32=0
      #mymd5=hashlib.md5()
      if file=="file_count":
        continue
      if os.path.isfile(os.path.join(root,file)) and not os.path.islink(os.path.join(root,file)):
        #crc32 = binascii.crc32((os.path.join(root,file)))+crc32
        mymd5=hashlib.md5()
        frb=open(os.path.join(root,file),'rb')
        while True:
          tmp=frb.read(4*1024)
          if tmp=='':
            break
          crc32 += fileCRC32(os.path.join(root,file),crc32,tmp)
          mymd5.update(tmp)
          #print("%x" %(crc32&0xffffffff))
        #print ("%s:0x%x \n" %(os.path.join(root,file),crc32&0xffffffff))
        crc32=crc32&0xffffffff ###这行开始
        fo.write(str(number))
        fo.write("\t")
        filepath=encrypt(key,os.path.join(root,file))
        fo.write(filepath)
        fo.write("\t")
        filecrc=encrypt(key,crc32)
        fo.write(filecrc)
        fo.write("\t")
        if not os.path.islink(os.path.join(root,file)):
          filemd5=encrypt(key,mymd5.hexdigest())
        else:
          filemd5=encrypt(key,0)
        fo.write(filemd5)
        fo.write("\n")
        number+=1
        frb.close() ##这行结束的代码段tab缩进符必须跟frb=open(os.path.join(root,file),'rb')语句保持一致
      crc=crc+crc32 ##缩进一个tab键
  crc=crc&0xffffffff
  fo.close()

主要原因我有些环境python工具对python脚本语法检查很严格,tab键的缩进符不对导致局部变量在外层引用而导致错误。经过验证,方法二修改是OK的。

编译方式:

zhaojr@zhaojr-OptiPlex-7040:~/project/test_mt8257$ source ./build/envsetup.sh
zhaojr@zhaojr-OptiPlex-7040:~/project/test_mt8257$ lunch full_k61v1_demo_64_bsp-userdebug
zhaojr@zhaojr-OptiPlex-7040:~/project/test_mt8257$ ./allmake.sh -p k61v1_demo_64_bsp

编译完成后使用flashtools下载编译后的代码,在BSP厂家开发板上验证是否OK,BSP厂家一般会给release_image,可以对比这个release_image进行 验证,也可以直接下载BSP包,编译后对比进行验证即可。

 类似资料: