From 2adc6d3f26f13269af308a1f0308f27dba981552 Mon Sep 17 00:00:00 2001 From: yaokai13 Date: Mon, 26 Oct 2020 14:10:36 +0800 Subject: [PATCH 1/7] add update function --- core/gitee_to_obs.py | 111 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 core/gitee_to_obs.py diff --git a/core/gitee_to_obs.py b/core/gitee_to_obs.py new file mode 100644 index 0000000..da1ff33 --- /dev/null +++ b/core/gitee_to_obs.py @@ -0,0 +1,111 @@ +#!/bin/evn python3 + +""" +created by :yaokai +date: 2020-10-26 +""" +import sys +import os +import git +import shutil +from common.log_obs import log + +work_path = "/srv/cache/obs/tar_scm/repo/next/" + +class RPMAction(object): + def __init__(self,rpm_name,gitee_branch): + """ + rpm_name: The package you want sync it to obs + gitee_branch: The branch for you commit in gitee + """ + self.rpm_name = rpm_name + self.gitee_branch= gitee_branch + + def get_obs_project(self): + log.info("Start get the obs_project") + self.git_clone("obs_meta","master") + cmd = "find ./obs_meta/%s -name %s | awk -F '/' '{print $4}'"% ( + self.gitee_branch,self.rpm_name) + obs_pro = os.popen(cmd).readlines() + if obs_pro: + log.info("The %s obs_project for gitee_%s is <<<<%s>>>>"% ( + self.rpm_name,self.gitee_branch,obs_pro[0].replace('\n',''))) + shutil.rmtree("obs_meta") + return obs_pro[0].replace('\n','') + else: + shutil.rmtree("obs_meta") + sys.exit("Failed !!! The rpm is not exist in %s branch !!!" + % self.gitee_branch) + + def git_clone(self,rpm_dir,rpm_branch): + """ + rpm_dir: The name for git repository "it will exist under the current directory" + rpm_branch: The branch you want to checkout for this git repository + """ + log.info("Git clone the %s in %s"% (rpm_dir,rpm_branch)) + if os.path.exists(rpm_dir): + shutil.rmtree(rpm_dir) + repo_url = r'https://gitee.com/src-openEuler/%s'% (rpm_dir) + my_git=git.Repo.clone_from(url=repo_url,to_path=rpm_dir) + cmd = "cd %s && git branch"% rpm_dir + branch_now = os.popen(cmd).read().replace("\n","").replace("* ","") + log.info ("At now %s the branch is in %s"% (rpm_dir,branch_now)) + if branch_now == rpm_branch: + os.popen("cd -").read() + log.info ("Success for Clone %s is already in %s branch"% (rpm_dir,rpm_branch)) + else: + os.popen("cd %s && git checkout remotes/origin/%s -b %s && cd - &>/dev/null" + % (rpm_dir,rpm_branch,rpm_branch)).read() + log.info ("Success for Clone and checkout to %s"% rpm_branch) + return my_git + + def get_latest_gitee_pull(self): + if self.gitee_branch == "master": + source_path = "/srv/cache/obs/tar_scm/repo/next/openEuler" + else: + source_path = "/srv/cache/obs/tar_scm/repo/next/" + self.gitee_branch + if self.rpm_name == "CreateImage": + sys.exit("The rpm packages does not need to be sync!!!") + elif self.rpm_name == "kernel": + os.chdir(source_path+"/kernel") + os.popen("git pull").read() + tag_path = "%s/kernel/SOURCE"% source_path + with open(tag_path,'r') as filereader: + for tags in filereader: + log.info("The self.gitee_branch kernel tag is :"+tags.strip()) + filereader.close() + os.chdir("/srv/cache/obs/tar_scm/repo/next/openEuler-kernel") + if os.path.exists("kernel"): + shutil.rmtree("kernel") + openEuler_kernel_git = "https://gitee.com/openeuler/kernel" + kernel_clone = os.popen("git clone --depth=1 %s -b %s"% ( + openEuler_kernel_git,tags.strip())).readlines() + log.info(kernel_clone) + else: + os.chdir(source_path) + if os.path.exists(self.rpm_name): + os.chdir(source_path +'/'+ self.rpm_name) + cmd = "git branch | awk '{print $2}'" + branch = os.popen(cmd).read().replace('\n','') + if branch == self.gitee_branch: + cmd = "git pull && cd %s" %source_path + pull_info = os.popen(cmd).read().replace('\n','') + log.info(pull_info) + else: + os.chdir(source_path) + self.git_clone(self.rpm_name,self.gitee_branch) + else: + self.git_clone(self.rpm_name,self.gitee_branch) + os.chdir(work_path) + + def sync_pr_to_obs(self,obs_pro): + action = os.system("osc service remoterun %s %s"% (obs_pro,self.rpm_name)) + if action != 0: + sys.exit("Failure!!! The %s faile to sync !!!"% self.rpm_name) + +if __name__ == "__main__": + #Now start + sync_run = RPMAction(sys.argv[1],sys.argv[2]) + obs_project = sync_run.get_obs_project() + sync_run.get_latest_gitee_pull() + sync_run.sync_pr_to_obs(obs_project) -- Gitee From 2fbaa2db605c485aceb677bf2ffe04ffacb450b2 Mon Sep 17 00:00:00 2001 From: yaokai13 Date: Mon, 26 Oct 2020 17:12:04 +0800 Subject: [PATCH 2/7] fix some --- core/gitee_to_obs.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/core/gitee_to_obs.py b/core/gitee_to_obs.py index da1ff33..5f8032b 100644 --- a/core/gitee_to_obs.py +++ b/core/gitee_to_obs.py @@ -13,7 +13,7 @@ from common.log_obs import log work_path = "/srv/cache/obs/tar_scm/repo/next/" class RPMAction(object): - def __init__(self,rpm_name,gitee_branch): + def __init__(self, rpm_name, gitee_branch): """ rpm_name: The package you want sync it to obs gitee_branch: The branch for you commit in gitee @@ -72,14 +72,15 @@ class RPMAction(object): tag_path = "%s/kernel/SOURCE"% source_path with open(tag_path,'r') as filereader: for tags in filereader: - log.info("The self.gitee_branch kernel tag is :"+tags.strip()) + kernel_tags = tags.strip() + log.info("The self.gitee_branch kernel tag is :" + kernel_tags) filereader.close() os.chdir("/srv/cache/obs/tar_scm/repo/next/openEuler-kernel") if os.path.exists("kernel"): shutil.rmtree("kernel") openEuler_kernel_git = "https://gitee.com/openeuler/kernel" kernel_clone = os.popen("git clone --depth=1 %s -b %s"% ( - openEuler_kernel_git,tags.strip())).readlines() + openEuler_kernel_git,kernel_tags)).readlines() log.info(kernel_clone) else: os.chdir(source_path) @@ -99,12 +100,13 @@ class RPMAction(object): os.chdir(work_path) def sync_pr_to_obs(self,obs_pro): + #print("Success for sync %s-%s !!!!"% (obs_pro,self.rpm_name)) action = os.system("osc service remoterun %s %s"% (obs_pro,self.rpm_name)) if action != 0: sys.exit("Failure!!! The %s faile to sync !!!"% self.rpm_name) if __name__ == "__main__": - #Now start + #Now star sync_run = RPMAction(sys.argv[1],sys.argv[2]) obs_project = sync_run.get_obs_project() sync_run.get_latest_gitee_pull() -- Gitee From ae35248367d1ac1af0ed87d6eefc1a31243643cf Mon Sep 17 00:00:00 2001 From: yaokai13 Date: Mon, 26 Oct 2020 17:57:12 +0800 Subject: [PATCH 3/7] fix some --- core/gitee_to_obs.py | 52 ++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/core/gitee_to_obs.py b/core/gitee_to_obs.py index 5f8032b..8f72dfe 100644 --- a/core/gitee_to_obs.py +++ b/core/gitee_to_obs.py @@ -23,40 +23,40 @@ class RPMAction(object): def get_obs_project(self): log.info("Start get the obs_project") - self.git_clone("obs_meta","master") - cmd = "find ./obs_meta/%s -name %s | awk -F '/' '{print $4}'"% ( - self.gitee_branch,self.rpm_name) + self.git_clone("obs_meta", "master") + cmd = "find ./obs_meta/%s -name %s | awk -F '/' '{print $4}'" % ( + self.gitee_branch, self.rpm_name) obs_pro = os.popen(cmd).readlines() if obs_pro: log.info("The %s obs_project for gitee_%s is <<<<%s>>>>"% ( - self.rpm_name,self.gitee_branch,obs_pro[0].replace('\n',''))) + self.rpm_name,self.gitee_branch, obs_pro[0].replace('\n',''))) shutil.rmtree("obs_meta") - return obs_pro[0].replace('\n','') + return obs_pro[0].replace('\n', '') else: shutil.rmtree("obs_meta") sys.exit("Failed !!! The rpm is not exist in %s branch !!!" % self.gitee_branch) - def git_clone(self,rpm_dir,rpm_branch): + def git_clone(self, rpm_dir, rpm_branch): """ rpm_dir: The name for git repository "it will exist under the current directory" rpm_branch: The branch you want to checkout for this git repository """ - log.info("Git clone the %s in %s"% (rpm_dir,rpm_branch)) + log.info("Git clone the %s in %s"% (rpm_dir, rpm_branch)) if os.path.exists(rpm_dir): shutil.rmtree(rpm_dir) - repo_url = r'https://gitee.com/src-openEuler/%s'% (rpm_dir) - my_git=git.Repo.clone_from(url=repo_url,to_path=rpm_dir) + repo_url = r'https://gitee.com/src-openEuler/%s' % (rpm_dir) + my_git=git.Repo.clone_from(url=repo_url, to_path=rpm_dir) cmd = "cd %s && git branch"% rpm_dir - branch_now = os.popen(cmd).read().replace("\n","").replace("* ","") - log.info ("At now %s the branch is in %s"% (rpm_dir,branch_now)) + branch_now = os.popen(cmd).read().replace("\n", "").replace("* ", "") + log.info ("At now %s the branch is in %s" % (rpm_dir, branch_now)) if branch_now == rpm_branch: os.popen("cd -").read() - log.info ("Success for Clone %s is already in %s branch"% (rpm_dir,rpm_branch)) + log.info("Success for Clone %s is already in %s branch" %(rpm_dir, rpm_branch)) else: os.popen("cd %s && git checkout remotes/origin/%s -b %s && cd - &>/dev/null" % (rpm_dir,rpm_branch,rpm_branch)).read() - log.info ("Success for Clone and checkout to %s"% rpm_branch) + log.info("Success for Clone and checkout to %s" % rpm_branch) return my_git def get_latest_gitee_pull(self): @@ -67,10 +67,10 @@ class RPMAction(object): if self.rpm_name == "CreateImage": sys.exit("The rpm packages does not need to be sync!!!") elif self.rpm_name == "kernel": - os.chdir(source_path+"/kernel") + os.chdir(source_path + "/kernel") os.popen("git pull").read() tag_path = "%s/kernel/SOURCE"% source_path - with open(tag_path,'r') as filereader: + with open(tag_path, 'r') as filereader: for tags in filereader: kernel_tags = tags.strip() log.info("The self.gitee_branch kernel tag is :" + kernel_tags) @@ -79,35 +79,35 @@ class RPMAction(object): if os.path.exists("kernel"): shutil.rmtree("kernel") openEuler_kernel_git = "https://gitee.com/openeuler/kernel" - kernel_clone = os.popen("git clone --depth=1 %s -b %s"% ( - openEuler_kernel_git,kernel_tags)).readlines() + kernel_clone = os.popen("git clone --depth = 1 %s -b %s"% ( + openEuler_kernel_git, kernel_tags)).readlines() log.info(kernel_clone) else: os.chdir(source_path) if os.path.exists(self.rpm_name): - os.chdir(source_path +'/'+ self.rpm_name) + os.chdir(source_path + '/' + self.rpm_name) cmd = "git branch | awk '{print $2}'" - branch = os.popen(cmd).read().replace('\n','') + branch = os.popen(cmd).read().replace('\n', '') if branch == self.gitee_branch: cmd = "git pull && cd %s" %source_path - pull_info = os.popen(cmd).read().replace('\n','') + pull_info = os.popen(cmd).read().replace('\n', '') log.info(pull_info) else: os.chdir(source_path) - self.git_clone(self.rpm_name,self.gitee_branch) + self.git_clone(self.rpm_name, self.gitee_branch) else: - self.git_clone(self.rpm_name,self.gitee_branch) + self.git_clone(self.rpm_name, self.gitee_branch) os.chdir(work_path) - def sync_pr_to_obs(self,obs_pro): + def sync_pr_to_obs(self, obs_pro): #print("Success for sync %s-%s !!!!"% (obs_pro,self.rpm_name)) - action = os.system("osc service remoterun %s %s"% (obs_pro,self.rpm_name)) + action = os.system("osc service remoterun %s %s" % (obs_pro, self.rpm_name)) if action != 0: - sys.exit("Failure!!! The %s faile to sync !!!"% self.rpm_name) + sys.exit("Failure!!! The %s faile to sync !!!" % self.rpm_name) if __name__ == "__main__": #Now star - sync_run = RPMAction(sys.argv[1],sys.argv[2]) + sync_run = RPMAction(sys.argv[1], sys.argv[2]) obs_project = sync_run.get_obs_project() sync_run.get_latest_gitee_pull() sync_run.sync_pr_to_obs(obs_project) -- Gitee From 7f8e7666173f6b2d683e4c67f1002e300744dd32 Mon Sep 17 00:00:00 2001 From: yaokai13 Date: Mon, 26 Oct 2020 18:15:19 +0800 Subject: [PATCH 4/7] fix some --- core/gitee_to_obs.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/core/gitee_to_obs.py b/core/gitee_to_obs.py index 8f72dfe..7ed4113 100644 --- a/core/gitee_to_obs.py +++ b/core/gitee_to_obs.py @@ -13,6 +13,10 @@ from common.log_obs import log work_path = "/srv/cache/obs/tar_scm/repo/next/" class RPMAction(object): + """ + if the rpm package has changed in gitee what you should do next + (Synchronize the latest code to OBS) + """ def __init__(self, rpm_name, gitee_branch): """ rpm_name: The package you want sync it to obs @@ -22,14 +26,17 @@ class RPMAction(object): self.gitee_branch= gitee_branch def get_obs_project(self): + """ + Get the obs project from gitee_branch + """ log.info("Start get the obs_project") self.git_clone("obs_meta", "master") cmd = "find ./obs_meta/%s -name %s | awk -F '/' '{print $4}'" % ( self.gitee_branch, self.rpm_name) obs_pro = os.popen(cmd).readlines() if obs_pro: - log.info("The %s obs_project for gitee_%s is <<<<%s>>>>"% ( - self.rpm_name,self.gitee_branch, obs_pro[0].replace('\n',''))) + log.info("The %s obs_project for gitee_%s is <<<<%s>>>>" % ( + self.rpm_name,self.gitee_branch, obs_pro[0].replace('\n', ''))) shutil.rmtree("obs_meta") return obs_pro[0].replace('\n', '') else: @@ -42,24 +49,27 @@ class RPMAction(object): rpm_dir: The name for git repository "it will exist under the current directory" rpm_branch: The branch you want to checkout for this git repository """ - log.info("Git clone the %s in %s"% (rpm_dir, rpm_branch)) + log.info("Git clone the %s in %s" % (rpm_dir, rpm_branch)) if os.path.exists(rpm_dir): shutil.rmtree(rpm_dir) repo_url = r'https://gitee.com/src-openEuler/%s' % (rpm_dir) my_git=git.Repo.clone_from(url=repo_url, to_path=rpm_dir) - cmd = "cd %s && git branch"% rpm_dir + cmd = "cd %s && git branch" % rpm_dir branch_now = os.popen(cmd).read().replace("\n", "").replace("* ", "") - log.info ("At now %s the branch is in %s" % (rpm_dir, branch_now)) + log.info ("At now %s the branch is in %s" %(rpm_dir, branch_now)) if branch_now == rpm_branch: os.popen("cd -").read() log.info("Success for Clone %s is already in %s branch" %(rpm_dir, rpm_branch)) else: os.popen("cd %s && git checkout remotes/origin/%s -b %s && cd - &>/dev/null" - % (rpm_dir,rpm_branch,rpm_branch)).read() + % (rpm_dir, rpm_branch, rpm_branch)).read() log.info("Success for Clone and checkout to %s" % rpm_branch) return my_git def get_latest_gitee_pull(self): + """ + make the obs server code be the latest + """ if self.gitee_branch == "master": source_path = "/srv/cache/obs/tar_scm/repo/next/openEuler" else: @@ -69,7 +79,7 @@ class RPMAction(object): elif self.rpm_name == "kernel": os.chdir(source_path + "/kernel") os.popen("git pull").read() - tag_path = "%s/kernel/SOURCE"% source_path + tag_path = "%s/kernel/SOURCE" % source_path with open(tag_path, 'r') as filereader: for tags in filereader: kernel_tags = tags.strip() @@ -79,7 +89,7 @@ class RPMAction(object): if os.path.exists("kernel"): shutil.rmtree("kernel") openEuler_kernel_git = "https://gitee.com/openeuler/kernel" - kernel_clone = os.popen("git clone --depth = 1 %s -b %s"% ( + kernel_clone = os.popen("git clone --depth = 1 %s -b %s" % ( openEuler_kernel_git, kernel_tags)).readlines() log.info(kernel_clone) else: @@ -89,7 +99,7 @@ class RPMAction(object): cmd = "git branch | awk '{print $2}'" branch = os.popen(cmd).read().replace('\n', '') if branch == self.gitee_branch: - cmd = "git pull && cd %s" %source_path + cmd = "git pull && cd %s" % source_path pull_info = os.popen(cmd).read().replace('\n', '') log.info(pull_info) else: @@ -100,6 +110,9 @@ class RPMAction(object): os.chdir(work_path) def sync_pr_to_obs(self, obs_pro): + """ + make the obs be the latest from gitee + """ #print("Success for sync %s-%s !!!!"% (obs_pro,self.rpm_name)) action = os.system("osc service remoterun %s %s" % (obs_pro, self.rpm_name)) if action != 0: -- Gitee From 276958e41ae0edc7656e1025c050c1cb01d53054 Mon Sep 17 00:00:00 2001 From: yaokai13 Date: Mon, 26 Oct 2020 19:17:27 +0800 Subject: [PATCH 5/7] fix some --- core/gitee_to_obs.py | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/core/gitee_to_obs.py b/core/gitee_to_obs.py index 7ed4113..9605814 100644 --- a/core/gitee_to_obs.py +++ b/core/gitee_to_obs.py @@ -25,18 +25,18 @@ class RPMAction(object): self.rpm_name = rpm_name self.gitee_branch= gitee_branch - def get_obs_project(self): + def _get_obs_project(self): """ Get the obs project from gitee_branch """ log.info("Start get the obs_project") - self.git_clone("obs_meta", "master") + self._git_clone("obs_meta", "master") cmd = "find ./obs_meta/%s -name %s | awk -F '/' '{print $4}'" % ( self.gitee_branch, self.rpm_name) obs_pro = os.popen(cmd).readlines() if obs_pro: log.info("The %s obs_project for gitee_%s is <<<<%s>>>>" % ( - self.rpm_name,self.gitee_branch, obs_pro[0].replace('\n', ''))) + self.rpm_name, self.gitee_branch, obs_pro[0].replace('\n', ''))) shutil.rmtree("obs_meta") return obs_pro[0].replace('\n', '') else: @@ -44,7 +44,7 @@ class RPMAction(object): sys.exit("Failed !!! The rpm is not exist in %s branch !!!" % self.gitee_branch) - def git_clone(self, rpm_dir, rpm_branch): + def _git_clone(self, rpm_dir, rpm_branch): """ rpm_dir: The name for git repository "it will exist under the current directory" rpm_branch: The branch you want to checkout for this git repository @@ -56,17 +56,17 @@ class RPMAction(object): my_git=git.Repo.clone_from(url=repo_url, to_path=rpm_dir) cmd = "cd %s && git branch" % rpm_dir branch_now = os.popen(cmd).read().replace("\n", "").replace("* ", "") - log.info ("At now %s the branch is in %s" %(rpm_dir, branch_now)) + log.info("At now %s the branch is in %s" % (rpm_dir, branch_now)) if branch_now == rpm_branch: os.popen("cd -").read() - log.info("Success for Clone %s is already in %s branch" %(rpm_dir, rpm_branch)) + log.info("Success for Clone %s is already in %s branch" % (rpm_dir, rpm_branch)) else: os.popen("cd %s && git checkout remotes/origin/%s -b %s && cd - &>/dev/null" % (rpm_dir, rpm_branch, rpm_branch)).read() log.info("Success for Clone and checkout to %s" % rpm_branch) return my_git - def get_latest_gitee_pull(self): + def _get_latest_gitee_pull(self): """ make the obs server code be the latest """ @@ -89,7 +89,7 @@ class RPMAction(object): if os.path.exists("kernel"): shutil.rmtree("kernel") openEuler_kernel_git = "https://gitee.com/openeuler/kernel" - kernel_clone = os.popen("git clone --depth = 1 %s -b %s" % ( + kernel_clone = os.popen("git clone --depth=1 %s -b %s" % ( openEuler_kernel_git, kernel_tags)).readlines() log.info(kernel_clone) else: @@ -104,23 +104,29 @@ class RPMAction(object): log.info(pull_info) else: os.chdir(source_path) - self.git_clone(self.rpm_name, self.gitee_branch) + self._git_clone(self.rpm_name, self.gitee_branch) else: - self.git_clone(self.rpm_name, self.gitee_branch) + self._git_clone(self.rpm_name, self.gitee_branch) os.chdir(work_path) - def sync_pr_to_obs(self, obs_pro): + def _sync_pr_to_obs(self, obs_pro): """ - make the obs be the latest from gitee + obs_pro:The obs project that gitee branch corresponds """ - #print("Success for sync %s-%s !!!!"% (obs_pro,self.rpm_name)) - action = os.system("osc service remoterun %s %s" % (obs_pro, self.rpm_name)) - if action != 0: - sys.exit("Failure!!! The %s faile to sync !!!" % self.rpm_name) + pass + #action = os.system("osc service remoterun %s %s" % (obs_pro, self.rpm_name)) + #if action != 0: + # sys.exit("Failure!!! The %s faile to sync !!!" % self.rpm_name) + + def gitee_pr_to_obs(self): + obs_project = self._get_obs_project() + self._get_latest_gitee_pull() + self._sync_pr_to_obs(obs_project) if __name__ == "__main__": #Now star sync_run = RPMAction(sys.argv[1], sys.argv[2]) - obs_project = sync_run.get_obs_project() - sync_run.get_latest_gitee_pull() - sync_run.sync_pr_to_obs(obs_project) + sync_run.gitee_pr_to_obs() +# obs_project = sync_run.get_obs_project() +# sync_run.get_latest_gitee_pull() +# sync_run.sync_pr_to_obs(obs_project) -- Gitee From 1390c9fca5f740e5efab08e096e4efa1cc4b4150 Mon Sep 17 00:00:00 2001 From: yaokai13 Date: Mon, 26 Oct 2020 20:23:39 +0800 Subject: [PATCH 6/7] fix some --- core/gitee_to_obs.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/gitee_to_obs.py b/core/gitee_to_obs.py index 9605814..eb66c8a 100644 --- a/core/gitee_to_obs.py +++ b/core/gitee_to_obs.py @@ -119,6 +119,9 @@ class RPMAction(object): # sys.exit("Failure!!! The %s faile to sync !!!" % self.rpm_name) def gitee_pr_to_obs(self): + """ + The only way to offer that make all fuction runing + """ obs_project = self._get_obs_project() self._get_latest_gitee_pull() self._sync_pr_to_obs(obs_project) -- Gitee From b44bc877774497095418cc28b5bcf22d4487790b Mon Sep 17 00:00:00 2001 From: yaokai13 Date: Wed, 28 Oct 2020 15:08:05 +0800 Subject: [PATCH 7/7] use ssh_cmd --- core/gitee_to_obs.py | 133 +++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 75 deletions(-) diff --git a/core/gitee_to_obs.py b/core/gitee_to_obs.py index eb66c8a..cf7930e 100644 --- a/core/gitee_to_obs.py +++ b/core/gitee_to_obs.py @@ -9,63 +9,61 @@ import os import git import shutil from common.log_obs import log +from common.common import Pexpect work_path = "/srv/cache/obs/tar_scm/repo/next/" -class RPMAction(object): +class SYNC_latest_to_Obs(object): """ - if the rpm package has changed in gitee what you should do next + if the rpm package has changed in gitee what you should donext (Synchronize the latest code to OBS) """ - def __init__(self, rpm_name, gitee_branch): + def __init__(self, rpm_name, gitee_branch, giteeuser, giteeuserpwd, cmd): """ rpm_name: The package you want sync it to obs gitee_branch: The branch for you commit in gitee + giteeuser: The gitee account + giteeuserpwd: The gitee passwd + cmd : The object you create for ssh_cmd """ self.rpm_name = rpm_name self.gitee_branch= gitee_branch + self.cmd = cmd + self.giteeuser = giteeuser + self.giteeuserpwd = giteeuserpwd + + def _git_clone(self, rpm_dir, gitee_branch, path): + """ + rpm_dir: The name for git repository "it will exist under the current directory" + rpm_branch: The branch you want to checkout for this git repository + """ + log.info("Git clone the %s in %s" % (rpm_dir, gitee_branch)) + self.cmd.ssh_cmd("rm -rf %s " % (path)) + repo_url = "https://%s:%s@gitee.com/src-openEuler/%s" % (self.giteeuser, self.giteeuserpwd, rpm_dir) + clone_result = self.cmd.ssh_cmd("git clone --depth=1 %s -b %s %s" + % (repo_url, gitee_branch, path)) + log.info("At now %s the branch is in %s" % (rpm_dir, gitee_branch)) def _get_obs_project(self): """ Get the obs project from gitee_branch """ log.info("Start get the obs_project") - self._git_clone("obs_meta", "master") - cmd = "find ./obs_meta/%s -name %s | awk -F '/' '{print $4}'" % ( - self.gitee_branch, self.rpm_name) - obs_pro = os.popen(cmd).readlines() - if obs_pro: + download_path = "/srv/cache/obs/tar_scm/repo/next/obs_meta" + self._git_clone("obs_meta", "master", download_path) + ss_cmd = "find %s/%s -name %s " % (download_path, self.gitee_branch, + self.rpm_name ) + obs_project = str(self.cmd.ssh_cmd(ss_cmd)[1]).split('/')[9] + if obs_project: log.info("The %s obs_project for gitee_%s is <<<<%s>>>>" % ( - self.rpm_name, self.gitee_branch, obs_pro[0].replace('\n', ''))) - shutil.rmtree("obs_meta") - return obs_pro[0].replace('\n', '') + self.rpm_name, self.gitee_branch, obs_project)) + self.cmd.ssh_cmd("rm -rf %s" % (download_path)) + return obs_project else: - shutil.rmtree("obs_meta") + self.cmd.ssh_cmd("rm -rf %s" % (download_path)) sys.exit("Failed !!! The rpm is not exist in %s branch !!!" % self.gitee_branch) - def _git_clone(self, rpm_dir, rpm_branch): - """ - rpm_dir: The name for git repository "it will exist under the current directory" - rpm_branch: The branch you want to checkout for this git repository - """ - log.info("Git clone the %s in %s" % (rpm_dir, rpm_branch)) - if os.path.exists(rpm_dir): - shutil.rmtree(rpm_dir) - repo_url = r'https://gitee.com/src-openEuler/%s' % (rpm_dir) - my_git=git.Repo.clone_from(url=repo_url, to_path=rpm_dir) - cmd = "cd %s && git branch" % rpm_dir - branch_now = os.popen(cmd).read().replace("\n", "").replace("* ", "") - log.info("At now %s the branch is in %s" % (rpm_dir, branch_now)) - if branch_now == rpm_branch: - os.popen("cd -").read() - log.info("Success for Clone %s is already in %s branch" % (rpm_dir, rpm_branch)) - else: - os.popen("cd %s && git checkout remotes/origin/%s -b %s && cd - &>/dev/null" - % (rpm_dir, rpm_branch, rpm_branch)).read() - log.info("Success for Clone and checkout to %s" % rpm_branch) - return my_git - def _get_latest_gitee_pull(self): """ make the obs server code be the latest @@ -77,59 +75,44 @@ class RPMAction(object): if self.rpm_name == "CreateImage": sys.exit("The rpm packages does not need to be sync!!!") elif self.rpm_name == "kernel": - os.chdir(source_path + "/kernel") - os.popen("git pull").read() + pull_result = str(self.cmd.ssh_cmd("git -C %s/kernel pull" % source_path) + [1].strip()).split("'")[1] + log.info(pull_result) tag_path = "%s/kernel/SOURCE" % source_path - with open(tag_path, 'r') as filereader: - for tags in filereader: - kernel_tags = tags.strip() - log.info("The self.gitee_branch kernel tag is :" + kernel_tags) - filereader.close() - os.chdir("/srv/cache/obs/tar_scm/repo/next/openEuler-kernel") - if os.path.exists("kernel"): - shutil.rmtree("kernel") - openEuler_kernel_git = "https://gitee.com/openeuler/kernel" - kernel_clone = os.popen("git clone --depth=1 %s -b %s" % ( - openEuler_kernel_git, kernel_tags)).readlines() - log.info(kernel_clone) + kernel_tags = str(self.cmd.ssh_cmd("cat %s" % tag_path)[1].strip()).split("'")[1] + log.info(kernel_tags) + open_kernel_path = "/srv/cache/obs/tar_scm/repo/next/openEuler-kernel/kernel" + open_kernel_git = "https://%s:%s@gitee.com/openeuler/kernel" % (self.giteeuser, self.giteeuserpwd) + log.info(open_kernel_git) + self.cmd.ssh_cmd("rm -rf %s" % open_kernel_path) + self.cmd.ssh_cmd("git clone --depth=1 %s -b %s %s/kernel" % (open_kernel_git, + kernel_tags, open_kernel_path)) else: - os.chdir(source_path) - if os.path.exists(self.rpm_name): - os.chdir(source_path + '/' + self.rpm_name) - cmd = "git branch | awk '{print $2}'" - branch = os.popen(cmd).read().replace('\n', '') - if branch == self.gitee_branch: - cmd = "git pull && cd %s" % source_path - pull_info = os.popen(cmd).read().replace('\n', '') - log.info(pull_info) - else: - os.chdir(source_path) - self._git_clone(self.rpm_name, self.gitee_branch) - else: - self._git_clone(self.rpm_name, self.gitee_branch) - os.chdir(work_path) + rpm_path = source_path + '/' + self.rpm_name + self._git_clone(self.rpm_name, self.gitee_branch, rpm_path) - def _sync_pr_to_obs(self, obs_pro): + def _gitee_pr_to_obs(self, obs_pro): """ obs_pro:The obs project that gitee branch corresponds """ - pass - #action = os.system("osc service remoterun %s %s" % (obs_pro, self.rpm_name)) - #if action != 0: - # sys.exit("Failure!!! The %s faile to sync !!!" % self.rpm_name) + osc_service = str(self.cmd.ssh_cmd("osc service remoterun %s %s" % (obs_pro, + self.rpm_name))[1].strip()).split("'")[1] + log.info(osc_service) + if osc_service != "ok": + sys.exit("Failure!!! The %s faile to sync !!!" % self.rpm_name) - def gitee_pr_to_obs(self): + def sync_code_to_obs(self): """ The only way to offer that make all fuction runing """ obs_project = self._get_obs_project() self._get_latest_gitee_pull() - self._sync_pr_to_obs(obs_project) + self._gitee_pr_to_obs(obs_project) if __name__ == "__main__": #Now star - sync_run = RPMAction(sys.argv[1], sys.argv[2]) - sync_run.gitee_pr_to_obs() -# obs_project = sync_run.get_obs_project() -# sync_run.get_latest_gitee_pull() -# sync_run.sync_pr_to_obs(obs_project) + #giteeuser = sys.argv[3] + #giteeuserpwd = sys.argv[4] + sshcmd = Pexpect("root", "124.90.34.227", "654321", port=11243) + sync_run = SYNC_latest_to_Obs(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sshcmd) + sync_run.sync_code_to_obs() -- Gitee