diff --git a/core/gitee_to_obs.py b/core/gitee_to_obs.py new file mode 100644 index 0000000000000000000000000000000000000000..cf7930ed9dba94f227cd38da711e5ea1a15c184c --- /dev/null +++ b/core/gitee_to_obs.py @@ -0,0 +1,118 @@ +#!/bin/evn python3 + +""" +created by :yaokai +date: 2020-10-26 +""" +import sys +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 SYNC_latest_to_Obs(object): + """ + 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, 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") + 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_project)) + self.cmd.ssh_cmd("rm -rf %s" % (download_path)) + return obs_project + else: + self.cmd.ssh_cmd("rm -rf %s" % (download_path)) + sys.exit("Failed !!! The rpm is not exist in %s branch !!!" + % self.gitee_branch) + + 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: + 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": + 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 + 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: + rpm_path = source_path + '/' + self.rpm_name + self._git_clone(self.rpm_name, self.gitee_branch, rpm_path) + + def _gitee_pr_to_obs(self, obs_pro): + """ + obs_pro:The obs project that gitee branch corresponds + """ + 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 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._gitee_pr_to_obs(obs_project) + +if __name__ == "__main__": + #Now star + #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()