From 3e05408164064d3a51cb368d3c4b0e50cbe08ca7 Mon Sep 17 00:00:00 2001 From: miao_kaibo Date: Wed, 28 Oct 2020 16:38:00 +0800 Subject: [PATCH 1/2] add obs project manager method --- common/parser_config.py | 27 +++++-- core/project_manager.py | 156 ++++++++++++++++++++++++++++++++++++++++ core/runner.py | 5 +- 3 files changed, 182 insertions(+), 6 deletions(-) create mode 100644 core/project_manager.py diff --git a/common/parser_config.py b/common/parser_config.py index 2bb93b4..3f8cd5a 100644 --- a/common/parser_config.py +++ b/common/parser_config.py @@ -7,6 +7,7 @@ date: 2020-10-20 9:55 parser config.ini """ import os +import sys import configparser from common import common #import common @@ -28,17 +29,31 @@ class ParserConfigIni(object): "../config/config.ini") self.config = configparser.ConfigParser() self.config.read(config_path, encoding='utf-8') + self._init_branch_list() self._init_update_enabled_flag() self._init_ignored_repo() self._init_package_info_file() + def _init_branch_list(self): + """ + init branch list from config.ini + return: None + """ + self.branch_list = self.config.options("update_enable") + + def get_branch_list(self): + """ + get current branch list + return: branch list + """ + return self.branch_list + def _init_update_enabled_flag(self): """ init update enable flag for branch from config.ini return: None """ - branch_list = self.config.options("update_enable") - for b in branch_list: + for b in self.branch_list: self.update_enabled_flag[b] = common.str_to_bool(self.config.get("update_enable", b)) def get_update_enabled_flag(self): @@ -70,12 +85,14 @@ class ParserConfigIni(object): def get_package_info_file(self): """ - get file + get package info file which store package that not be updated """ return self.package_info_file if __name__ == "__main__": p = ParserConfigIni() - update_enabled_flag = p.get_update_enabled_flag() - print(update_enabled_flag) + print(p.get_update_enabled_flag()) + print(p.get_branch_list()) + print(p.get_ignored_repo()) + print(p.get_package_info_file()) diff --git a/core/project_manager.py b/core/project_manager.py new file mode 100644 index 0000000..98c45f2 --- /dev/null +++ b/core/project_manager.py @@ -0,0 +1,156 @@ +#!/usr/bin/env python +# -*- encoding=utf-8 -*- +import re +import os +import sys +import time +from datetime import datetime +current_path = os.path.join(os.path.split(os.path.realpath(__file__))[0]) +sys.path.append(os.path.join(current_path, "..")) +from common.log_obs import log + + +class OBSPrjManager(): + """ + obs project manager, include create delete and backup + """ + def __init__(self, obs_meta_path): + """ + init project base info + obs_meta_path: path of obs_meta repository + """ + self.obs_meta_path = obs_meta_path + os.chdir(self.obs_meta_path) + self.commit_msg = [] + + def _set_info(self): + """ + set obs project info, include name branch metadata + """ + cmd = "git diff --name-status HEAD~1 HEAD~0" + log.info(cmd) + ret = os.popen(cmd).read().replace("\t", " ") + log.info(ret) + if "OBS_PRJ_meta" not in ret: + log.info("obs project not be changed") + return 1 + message = ret.split("\n") + log.info(message) + for msg in message: + log.info(msg) + if "OBS_PRJ_meta" not in msg: + continue + action_type = msg.split(" ")[0] + if not action_type: + continue + tmp = {} + tmp["action_type"] = action_type + tmp["branch"] = msg.split(" ")[1].split("/")[1] + if action_type in ["A", "D", "M"]: + tmp["file"] = msg.split(" ")[1] + tmp["name"] = tmp["file"].split("/")[-1] + elif action_type.startswith("R"): + tmp["old_file"] = msg.split(" ")[1] + tmp["new_file"] = msg.split(" ")[2] + tmp["old_name"] = tmp["old_file"].split("/")[-1] + tmp["new_name"] = tmp["new_file"].split("/")[-1] + else: + continue + self.commit_msg.append(tmp) + log.info(self.commit_msg) + return 0 + + def _create(self, branch, name, meta_file): + """ + create project + branch: branch name + name: name of obs project + meta_file: meta of obs project + """ + obs_prj_dir = os.path.join(branch, name) + log.info("obs project dir path: %s" % obs_prj_dir) + if not os.path.exists(obs_prj_dir): + if name.endswith(":Bak"): + cmd = "cp -r %s %s" % (os.path.join(branch, name.replace(":Bak", "")), + obs_prj_dir) + else: + cmd = "mkdir %s && touch %s/README.md" % (obs_prj_dir, obs_prj_dir) + res = os.popen(cmd).read() + log.info(res) + log.info("create new obs project by meta file %s" % meta_file) + if os.path.exists(obs_prj_dir): + cmd = "osc api -X PUT /source/%s/_meta -T %s" % (name, meta_file) + os.system(cmd) + cmd = "git add %s && \ + git commit -m 'add new obs project by meta file' && \ + git push" % obs_prj_dir + os.system(cmd) + + def _change_meta(self, name, meta_file): + """ + change meta data of project + name: name of obs project + meta_file: meta of obs project + """ + cmd = "osc api -X PUT /source/%s/_meta -T %s" % (name, meta_file) + os.system(cmd) + + def _delete(self, branch, name): + """ + delete project + branch: branch name + name: name of obs project + """ + log.info("delete project %s" % name) + cmd = "osc api -X DELETE /source/%s" % name + os.system(cmd) + cmd = "git rm -r %s && git commit -m 'delete project %s' && \ + git push" % (os.path.join(branch, name), name) + os.system(cmd) + + def _rename(self, branch, old_name, new_name, new_meta): + """ + rename project + branch: branch name + name: name of obs project + meta_file: meta of new obs project + """ + cmd = "osc api -X DELETE /source/%s" % old_name + os.system(cmd) + cmd = "osc api -X PUT /source/%s/_meta -T %s" % (new_name, new_meta) + os.system(cmd) + cmd = "cp -r %s %s && git rm -r %s && git add %s && \ + git commit -m 'rename project %s to %s' && \ + git push" % (os.path.join(branch, old_name), \ + os.path.join(branch, new_name), \ + os.path.join(branch, old_name), \ + os.path.join(branch, new_name), \ + os.path.join(branch, old_name), \ + os.path.join(branch, new_name)) + log.info(cmd) + os.system(cmd) + + def manager_action(self): + """ + main function of obs project manager + """ + res = self._set_info() + if res != 0: + return res + for msg in self.commit_msg: + if msg["action_type"] == "A": + self._create(msg["branch"], msg["name"], msg["file"]) + elif msg["action_type"] == "M": + self._change_meta(msg["name"], msg["file"]) + elif msg["action_type"] == "D": + self._delete(msg["branch"], msg["name"]) + elif msg["action_type"].startswith("R"): + self._rename(msg["branch"], msg["old_name"], msg["new_name"], msg["new_file"]) + return 0 + + +if __name__ == "__main__": + obs_meta_path = sys.argv[1] + obs_prj = OBSPrjManager(obs_meta_path) + obs_prj.manager_action() + diff --git a/core/runner.py b/core/runner.py index de4372f..1f375ba 100644 --- a/core/runner.py +++ b/core/runner.py @@ -9,6 +9,7 @@ main script for running from common.log_obs import log from common.parser_config import ParserConfigIni from core.save import SaveInfo +from core.project_manager import OBSPrjManager class Runner(object): @@ -33,7 +34,9 @@ class Runner(object): return: """ log.debug("obs_meta change") - # TODO + obs_pm = OBSPrjManager(self.kwargs["obs_path"]) + obs_pm.manager_action() + # TODO add package service def _save_package_info(self): """ -- Gitee From 34185bba8ba6125099a579a95bb2424c829de6e7 Mon Sep 17 00:00:00 2001 From: miao_kaibo Date: Wed, 28 Oct 2020 16:45:29 +0800 Subject: [PATCH 2/2] fix by checking result --- core/project_manager.py | 6 +++--- core/runner.py | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/core/project_manager.py b/core/project_manager.py index 98c45f2..59635b3 100644 --- a/core/project_manager.py +++ b/core/project_manager.py @@ -14,13 +14,13 @@ class OBSPrjManager(): """ obs project manager, include create delete and backup """ - def __init__(self, obs_meta_path): + def __init__(self, obs_meta): """ init project base info obs_meta_path: path of obs_meta repository """ - self.obs_meta_path = obs_meta_path - os.chdir(self.obs_meta_path) + self.obs_meta = obs_meta + os.chdir(self.obs_meta) self.commit_msg = [] def _set_info(self): diff --git a/core/runner.py b/core/runner.py index 1f375ba..36c1ad8 100644 --- a/core/runner.py +++ b/core/runner.py @@ -36,7 +36,8 @@ class Runner(object): log.debug("obs_meta change") obs_pm = OBSPrjManager(self.kwargs["obs_path"]) obs_pm.manager_action() - # TODO add package service + # TODO + # add package service def _save_package_info(self): """ -- Gitee