Add a sub command qbbuild. 07/319107/1
authorxuhy <huayong.xu@samsung.com>
Tue, 15 Oct 2024 09:16:55 +0000 (17:16 +0800)
committerxuhy <huayong.xu@samsung.com>
Tue, 15 Oct 2024 09:16:55 +0000 (17:16 +0800)
Change-Id: I763e4689fce8fdd220065c10020d8a708a9d2c6a

gitbuildsys/cmd_qbbuild.py [new file with mode: 0644]
tools/gbs

diff --git a/gitbuildsys/cmd_qbbuild.py b/gitbuildsys/cmd_qbbuild.py
new file mode 100644 (file)
index 0000000..90d228d
--- /dev/null
@@ -0,0 +1,121 @@
+#!/usr/bin/python -tt
+# vim: ai ts=4 sts=4 et sw=4
+#
+
+from gitbuildsys import utils
+from gitbuildsys.conf import configmgr
+
+import os
+import re
+import requests
+import sys
+
+BUILD_REQUEST_XML_TEMPLATE = '''<?xml version='1.0' encoding='UTF-8'?>
+<com.pmease.quickbuild.BuildRequest>
+  <configurationId>{{configurationId}}</configurationId>
+  <respectBuildCondition>false</respectBuildCondition>
+  <priority>5</priority>
+  <variables>
+    <entry>
+      <string>BUILD_TYPE</string>
+      <string>Full</string>
+    </entry>
+    <entry>
+      <string>REPO_TYPE</string>
+      <string>ALL</string>
+    </entry>
+    <entry>
+      <string>BUILD_REFERENCE</string>
+      <string>Live</string>
+    </entry>
+    <entry>
+      <string>SNAPSHOT_NUM</string>
+      <string>do not need select</string>
+    </entry>
+    <entry>
+      <string>BUILD_PKG_LIST_ADD</string>
+      <string>{{commitIdList}}</string>
+    </entry>
+  </variables>
+</com.pmease.quickbuild.BuildRequest>
+'''
+
+def trigger_qb(session, url, file_path):
+    """request qb to build"""
+    req_url = "%s/rest/build_requests" % (url)
+    print("url: %s." % req_url)
+    with open(file_path, 'rb') as fobj:
+        resp = session.post(req_url, data=fobj)
+
+    conv_text = resp.text
+    print("build info: ", conv_text.strip())
+
+
+def request_build(session, url, conf_path, txt_file):
+    """generate qb build request xml file."""
+    # query configuration id from qb server.
+    full_url = "%s/rest/ids?configuration_path=%s" % (url, conf_path)
+    resp = session.get(full_url, verify=False)
+    print("response: ", resp.text)
+
+    # replace configuration id in template.
+    replaced = re.sub("{{configurationId}}", resp.text.strip(), BUILD_REQUEST_XML_TEMPLATE)
+    print("replaced: ", replaced)
+
+    # read commit id list.
+    commit_id_list = ""
+    with open(txt_file, 'r') as file:
+        for line in file:
+            commit_id_list += line.strip()
+            commit_id_list += '\n'
+    commit_id_list = commit_id_list.strip() # remove the last "\n"
+
+    # replace commit id list in template.
+    replaced = re.sub("{{commitIdList}}", commit_id_list, replaced)
+    print("replaced: ", replaced)
+
+    # create a tmp dir.
+    tmp = utils.Temp(prefix='gbs_', dirn=configmgr.get('tmpdir', 'general'), directory=True)
+    print("temp dir: ", tmp.path)
+  
+    # write build request into an xml.
+    xml_file = tmp.path + '/gbs_buldrequest.xml'
+    with open(xml_file, 'w') as file:
+        file.write(replaced)
+
+    # trigger qb to build.
+    trigger_qb(session, url, xml_file)
+
+
+def main(args):
+    """The main body"""
+    print(args.qb_account_conf)
+    print(args.qb_conf_path)
+    print(args.commit_id_file)
+
+    conf_path = args.qb_account_conf
+    if (not os.path.isabs(conf_path)):
+        conf_path = os.path.abspath(os.path.expanduser(conf_path))
+    print(conf_path)
+    with open(conf_path, 'r') as file:
+        lines = file.readlines()
+
+    config = {}
+    for line in lines:
+        line = line.strip()
+        if not line or line.startswith('#'):
+            continue
+        key, value = line.split('=', 1)
+        value = value.strip().strip('\'"')
+        config[key] = value
+    print(config)
+
+    session = requests.Session()
+    session.auth = (config['user'], config['passwd'])
+
+    txt_path = args.commit_id_file
+    if (not os.path.isabs(txt_path)):
+        txt_path = os.path.abspath(os.path.expanduser(txt_path))
+    print(txt_path)
+
+    request_build(session, config['url'], args.qb_conf_path, txt_path)
index 5ef1f5fcd7da19654a15be8b21a9de8185732fff..cc2ee65096ac284ff62bd860c86c3b5463f16c79 100755 (executable)
--- a/tools/gbs
+++ b/tools/gbs
@@ -449,6 +449,26 @@ def remotebuild_parser(parser):
     parser.set_defaults(alias="rb")
     return parser
 
+@subparser
+def qbbuild_parser(parser):
+    """trigger qb to build packages
+    Examples:
+      $ gbs qbbuild --qb-account-conf ~/qb.conf --qb-conf-path /root/tools --commit-id-file ~/commits.txt
+
+    Note: qb.conf need include the following key/value pairs:
+    url=qb server url
+    user=qb login user
+    passwd=qb login password
+    """
+
+    parser.add_argument('-A', '--qb-account-conf', type=os.path.abspath, required=True,
+                        help='path to qb account')
+    parser.add_argument('-P', '--qb-conf-path', required=True, help='qb configuration path in qb server.')
+    parser.add_argument('-F', '--commit-id-file', required=True, help='path of the file that includes commit id list.')
+
+    parser.set_defaults(alias="qb")
+    return parser
+
 @subparser
 def chroot_parser(parser):
     """chroot to build root