diff --git a/common/parser_config.py b/common/parser_config.py index 2bb93b48af4feac5d3d12d1643f4d37d0f72604e..3f8cd5aceed7d9f94059875e7ef1a2604fd63068 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 0000000000000000000000000000000000000000..59635b34ab4f7d9724c72874ec15a6a84a8b413c --- /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): + """ + init project base info + obs_meta_path: path of obs_meta repository + """ + self.obs_meta = obs_meta + os.chdir(self.obs_meta) + 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 de4372f9f0f7f103591ca1c66626e164506ea2be..36c1ad861c70387ebc53054c0bdd91df4f97ea77 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,10 @@ class Runner(object): return: """ log.debug("obs_meta change") + obs_pm = OBSPrjManager(self.kwargs["obs_path"]) + obs_pm.manager_action() # TODO + # add package service def _save_package_info(self): """