diff --git a/core/project_manager.py b/core/project_manager.py
index f7f2678d7dfe6ec833ffd4a46fbe63cb1c9bd445..a2510e1948a15ee8d3c6e30712524eb331e9b536 100644
--- a/core/project_manager.py
+++ b/core/project_manager.py
@@ -54,6 +54,7 @@ class OBSPrjManager(object):
message = ret.split("\n")
log.info(message)
for msg in message:
+ msg = ' '.join(msg.split())
log.info(msg)
if "OBS_PRJ_meta" not in msg:
continue
@@ -62,7 +63,12 @@ class OBSPrjManager(object):
continue
tmp = {}
tmp["action_type"] = action_type
- tmp["branch"] = msg.split(" ")[1].split("/")[1]
+ if "multi" in msg:
+ tmp["multi_dir"] = msg.split(" ")[1].split("/")[1]
+ tmp["branch"] = msg.split(" ")[1].split("/")[2]
+ else:
+ tmp["multi_dir"] = ""
+ 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]
@@ -77,14 +83,14 @@ class OBSPrjManager(object):
log.info(self.commit_msg)
return 0
- def _create(self, branch, name, meta_file):
+ def _create(self, multi_dir, 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)
+ obs_prj_dir = os.path.join(multi_dir, branch, name)
log.info("obs project dir path: %s" % obs_prj_dir)
if not os.path.exists(obs_prj_dir):
if name.endswith(":Bak"):
@@ -114,7 +120,7 @@ class OBSPrjManager(object):
cmd = "osc api -X PUT /source/%s/_meta -T %s" % (name, meta_file)
os.system(cmd)
- def _delete(self, branch, name):
+ def _delete(self, multi_dir, branch, name):
"""
delete project
branch: branch name
@@ -124,12 +130,12 @@ class OBSPrjManager(object):
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)
+ git push" % (os.path.join(multi_dir, branch, name), name)
for i in range(5):
if os.system(cmd) == 0:
break
- def _rename(self, branch, old_name, new_name, new_meta):
+ def _rename(self, multi_dir, branch, old_name, new_name, new_meta):
"""
rename project
branch: branch name
@@ -142,12 +148,12 @@ class OBSPrjManager(object):
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))
+ git push" % (os.path.join(multi_dir, branch, old_name), \
+ os.path.join(multi_dir, branch, new_name), \
+ os.path.join(multi_dir, branch, old_name), \
+ os.path.join(multi_dir, branch, new_name), \
+ os.path.join(multi_dir, branch, old_name), \
+ os.path.join(multi_dir, branch, new_name))
log.info(cmd)
for i in range(5):
if os.system(cmd) == 0:
@@ -162,13 +168,13 @@ class OBSPrjManager(object):
return res
for msg in self.commit_msg:
if msg["action_type"] == "A":
- self._create(msg["branch"], msg["name"], msg["file"])
+ self._create(msg["multi_dir"], 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"])
+ self._delete(msg["multi_dir"], msg["branch"], msg["name"])
elif msg["action_type"].startswith("R"):
- self._rename(msg["branch"], msg["old_name"], msg["new_name"], msg["new_file"])
+ self._rename(msg["multi_dir"], msg["branch"], msg["old_name"], msg["new_name"], msg["new_file"])
return 0
diff --git a/test/config.ini b/test/config.ini
new file mode 100644
index 0000000000000000000000000000000000000000..f87706e860fce72a854675809b9dc880626b2038
--- /dev/null
+++ b/test/config.ini
@@ -0,0 +1,9 @@
+[gitee]
+user =
+passwd =
+email =
+[obs]
+url =
+user =
+passwd =
+
diff --git a/test/func.py b/test/func.py
new file mode 100644
index 0000000000000000000000000000000000000000..cb6ad59446924cae8fbc08e2db495d73f2990295
--- /dev/null
+++ b/test/func.py
@@ -0,0 +1,141 @@
+#/bin/env python3
+# -*- encoding=utf8 -*-
+#******************************************************************************
+# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
+# licensed under the Mulan PSL v2.
+# You can use this software according to the terms and conditions of the Mulan PSL v2.
+# You may obtain a copy of Mulan PSL v2 at:
+# http://license.coscl.org.cn/MulanPSL2
+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+# PURPOSE.
+# See the Mulan PSL v2 for more details.
+# Author: miao_kaibo
+# Create: 2020-10-20
+# ******************************************************************************
+
+"""
+common function for testing
+"""
+import os
+import sys
+import configparser
+
+
+
+
+class ConfParser(configparser.ConfigParser):
+ """
+ rewrite optionxform function
+ """
+ def __init__(self, defaults=None):
+ """
+ init
+ """
+ configparser.ConfigParser.__init__(self, defaults=None)
+
+ def optionxform(self, optionstr):
+ """
+ delete old function lower()
+ """
+ return optionstr
+
+
+class SetEnv(object):
+ """
+ get parmers from config.ini file
+ """
+ def __init__(self):
+ """
+ init parmers by config.ini
+ return: None
+ """
+ current_path = os.path.join(os.path.split(os.path.realpath(__file__))[0])
+ config_path = os.path.join(current_path, "config.ini")
+ self.config = ConfParser()
+ self.config.read(config_path, encoding='utf-8')
+ self._obs_info = None
+ self._gitee_info = None
+ self.obs_info = {}
+ self.gitee_info = {}
+ self.home_dir = os.environ['HOME']
+ self.oscrc = os.path.join(self.home_dir, ".oscrc")
+ self.oscrc_bak = os.path.join(self.home_dir, ".oscrc.bak")
+ self.gitee_info_bak = {"user":"", "email":""}
+
+ def set_oscrc(self):
+ self._obs_info = self.config.options("obs")
+ for info in self._obs_info:
+ self.obs_info[info] = self.config.get("obs", info)
+ if os.path.exists(self.oscrc):
+ cmd = "mv %s %s" % (self.oscrc, self.oscrc_bak)
+ if os.system(cmd) == 0:
+ print("oscrc file bakup sucessfull")
+ else:
+ print("oscrc file backup failed")
+ exit(1)
+ cmd = "touch %s && echo '[general]\napiurl=%s\n[%s]\nuser=%s\npass=%s' > %s" % \
+ (self.oscrc, self.obs_info["url"], self.obs_info["url"], self.obs_info["user"], \
+ self.obs_info["passwd"], self.oscrc)
+ ret=os.popen(cmd).read()
+
+ def unset_oscrc(self):
+ if os.path.exists(self.oscrc_bak):
+ cmd = "rm -rf %s && mv %s %s" % (self.oscrc, self.oscrc_bak, self.oscrc)
+ else:
+ cmd = "rm -rf %s" % self.oscrc
+ ret=os.popen(cmd).read()
+
+ def set_gitee(self):
+ ret = os.popen("git config --global user.name").read()
+ self.gitee_info_bak["user"] = ret.strip("\n")
+ ret = os.popen("git config --global user.email").read()
+ self.gitee_info_bak["email"] = ret.strip("\n")
+ self._gitee_info = self.config.options("gitee")
+ for info in self._gitee_info:
+ self.gitee_info[info] = self.config.get("gitee", info)
+ cmd = "git config --global user.name '%s' && git config --global user.email '%s'" % \
+ (self.gitee_info["user"], self.gitee_info["email"])
+ ret = os.popen(cmd).read()
+ print(ret)
+
+ def unset_gitee(self):
+
+ cmd = "git config --global user.name '%s' && git config --global user.email '%s'" % \
+ (self.gitee_info_bak["user"], self.gitee_info_bak["email"])
+ ret = os.popen(cmd).read()
+ print(ret)
+
+
+class CommonFunc(object):
+ def __init__(self):
+ pass
+
+ def pull_from_gitee_repo(self, user, passwd, url, branch, repo):
+ passwd = passwd.replace("@", "%40")
+ url = url.split("//")[0] + "//" + user + ":" + passwd + "@" + url.split("//")[1]
+ cmd = "git clone --depth 2 %s -b %s %s" % (url, branch, repo)
+ print(cmd)
+ ret = os.popen(cmd).read()
+ repo_path = os.path.join(os.getcwd(), repo)
+ return repo_path
+
+ def commit_to_gitee_repo(self, repo_path, *kwargs):
+ os.chdir(repo_path)
+ for f in kwargs:
+ cmd = "git add %s" % f
+ ret = os.popen(cmd).read()
+ cmd = "git commit -m test"
+ if os.system(cmd) == 0:
+ return True
+ else:
+ return False
+
+
+if __name__ == "__main__":
+ import time
+ p = SetEnv()
+ p.set_oscrc()
+ p.unset_oscrc()
+ p.set_gitee()
+ p.unset_gitee()
diff --git a/test/test_project.py b/test/test_project.py
new file mode 100644
index 0000000000000000000000000000000000000000..53bf7f3244174cc0afab7b049eb542e03ecdaba0
--- /dev/null
+++ b/test/test_project.py
@@ -0,0 +1,197 @@
+#/bin/env python3
+# -*- encoding=utf8 -*-
+#******************************************************************************
+# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
+# licensed under the Mulan PSL v2.
+# You can use this software according to the terms and conditions of the Mulan PSL v2.
+# You may obtain a copy of Mulan PSL v2 at:
+# http://license.coscl.org.cn/MulanPSL2
+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+# PURPOSE.
+# See the Mulan PSL v2 for more details.
+# Author: miao_kaibo
+# Create: 2021-04-22
+# ******************************************************************************
+"""
+test all: python3 -m pytest -s test_project.py
+test one class: python3 -m pytest -s test_project.py::TestCase
+test one class one case: python3 -m pytest -s test_project.py::TestCase::test_1
+"""
+import os
+import sys
+import pytest
+current_path = os.path.join(os.path.split(os.path.realpath(__file__))[0])
+sys.path.append(os.path.join(current_path, ".."))
+from func import SetEnv, CommonFunc
+from core.project_manager import OBSPrjManager
+
+
+S = SetEnv()
+C = CommonFunc()
+repo_path = None
+P = None
+
+
+class TestCase(object):
+ def setup_class(self):
+ S.set_oscrc()
+ S.set_gitee()
+ cmd = "osc list | grep home:{0}:test".format(S.obs_info["user"])
+ if os.system(cmd) == 0:
+ cmd = "osc api -X DELETE /source/home:{0}:test".format(S.obs_info["user"])
+ global repo_path
+ global P
+ repo_path = C.pull_from_gitee_repo(S.gitee_info["user"], S.gitee_info["passwd"], \
+ "https://gitee.com/{0}/obs_meta".format(S.gitee_info["user"]), "master", "obs_meta")
+ P = OBSPrjManager(repo_path)
+
+ def teardown_class(self):
+ S.unset_oscrc()
+ S.unset_gitee()
+ cmd = "rm -rf %s" % repo_path
+ if os.system(cmd) == 0:
+ assert True
+ else:
+ assert False, "fail to clear {0} after testing".format(repo_path)
+
+ def test_1(self):
+ """
+ test for creating project for multi-version
+ """
+ assert os.path.exists(repo_path), "{0} not exist".format(repo_path)
+ file_msg = """
+