New feature : C# Create update-nuget job , Function : update Nuget package to Nuget...
authorYonghee Han <onstudy@samsung.com>
Fri, 6 Jan 2017 08:12:10 +0000 (17:12 +0900)
committerSoonKyu Park <sk7.park@samsung.com>
Sat, 4 Feb 2017 06:16:24 +0000 (15:16 +0900)
Change-Id: Ie7c3d9b2b55041fb91114ddf6f6c6bd4f1c16c7c

job_create_snapshot.py
job_update_nuget.py [new file with mode: 0755]
packaging/jenkins-scripts.spec
scripts/nuget.exe [new file with mode: 0755]

index b536854..60b6588 100755 (executable)
@@ -345,6 +345,10 @@ def main():
 
     trigger_next("post-snapshot", data)
 
+    # Update NUGET pacakges to server
+    if os.getenv("NUGET_UPDATE_ENABLE","0") == "1":
+        trigger_next("update-nuget", data)
+
     if buildmonitor_enabled:
         if buildmonitor_skip == 1:
             print 'snapdiff empty, buildmonitor_skipped!!\n'
diff --git a/job_update_nuget.py b/job_update_nuget.py
new file mode 100755 (executable)
index 0000000..cddc01b
--- /dev/null
@@ -0,0 +1,120 @@
+#!/usr/bin/env python
+# vim: ai ts=4 sts=4 et sw=4
+#
+# Copyright (c) 2014, 2015, 2016 Samsung Electronics.Co.Ltd.
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the Free
+# Software Foundation; version 2 of the License
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+# for more details.
+#
+import sys
+import os
+import glob
+import shutil
+import subprocess
+import tempfile
+
+from common.buildtrigger import trigger_info, trigger_next
+
+class LocalError(Exception):
+    """Local error exception."""
+    pass
+
+def execute(cmd, cwd = None):
+    try:
+        ret_code = subprocess.call(cmd, shell=True, cwd=cwd)
+    except OSError as err:
+        raise RuntimeException("Execution of %s failed: %s" %
+                               (cmd, str(err)))
+    return ret_code
+
+def nuget_init(nuget_exe, nuget_source, nuget_apikey):
+    """ Init Nuget """
+    cmd = '%s setApiKey %s -Source %s' \
+           % (nuget_exe, nuget_apikey, nuget_source)
+    return execute(cmd)
+
+def nuget_push(nuget_exe, nuget_source, nupkg, tmpdir):
+    """ Push Nuget """
+    cmd = '%s push %s -Source %s' % (nuget_exe, nupkg, nuget_source)
+    return execute(cmd, tmpdir)
+
+def extract_rpms(repodir):
+    """ Extract rpms """
+    ngrpms = glob.glob(os.path.join(repodir, '*-nuget-*.rpm'))
+    tmpdir = tempfile.mkdtemp(prefix = 'nuget')
+    for rpm in ngrpms:
+        execute('rpm2cpio %s | cpio -idmv' % rpm, tmpdir)
+    return tmpdir
+
+def push_nuget_files(nuget_exe, nuget_source, tmpdir):
+    """ Push Nuget files """
+    nupkgs = glob.glob(os.path.join(tmpdir, 'nuget/*.nupkg'))
+    for nupkg in nupkgs:
+        nuget_push(nuget_exe, nuget_source, os.path.relpath(nupkg, tmpdir), tmpdir)
+
+def main():
+    """ Script entry point. """
+    print '---[JOB STARTED]-------------------------'
+
+    # get trigger_info
+    content = trigger_info(os.getenv("TRIGGER_INFO"))
+    project = content.get("project")
+    repo_path = content.get("repo_path")
+
+    # get NUGET configuration
+    if os.getenv("NUGET_UPDATE_ENABLE","0") != "1":
+        print "Skipping as NUGET_UPDATE_ENABLE=%s is not enabled " \
+                  %(os.getenv("NUGET_UPDATE_ENABLE"))
+        return 0
+    if not project in os.getenv("NUGET_PROJECT"):
+        print "Skipping as %s project is not present in nuget project %s " \
+                  %(project, os.getenv("NUGET_PROJECT"))
+        return 0
+
+    nuget_exe = "mono %s/jenkins-scripts/scripts/nuget.exe" %(os.getenv('WORKSPACE'))
+    nuget_source = os.getenv("NUGET_SOURCE")
+    nuget_apikey = os.getenv("NUGET_APIKEY")
+
+    base_path = os.getenv('PATH_REPO_BASE')
+
+    # Initialize NuGet
+    nuget_init(nuget_exe, nuget_source, nuget_apikey)
+
+    for repository in content.get("repo").keys():
+        for arch in content.get("repo")[repository].get("archs"):
+            if arch != "armv7l":
+                continue
+            else:
+                repo_arch_path = os.path.join(base_path,
+                                              repo_path,
+                                              "repos",
+                                              repository,
+                                              "packages",
+                                              arch
+                                             )
+                print "repo arch path = %s" %repo_arch_path
+                # Extract *-nuget-*.rpm files to tmpdir
+                tmpdir = extract_rpms(repo_arch_path)
+                print tmpdir
+                # Push .nupkg files in tmpdir to NuGet server
+                push_nuget_files(nuget_exe, nuget_source, tmpdir)
+                # Cleanup tmpdir
+                shutil.rmtree(tmpdir)
+
+    print '---[JOB ENDED]---------------------------'
+    return 0
+
+if __name__ == "__main__":
+    try:
+        sys.exit(main())
+    except LocalError, error:
+        print error
+        sys.exit(1)
+
+
index 117a6cc..bb2bd53 100644 (file)
@@ -136,6 +136,7 @@ fi
 %{destdir}/job_sync_obs.py
 %{destdir}/job_sync_snapdiff.py
 %{destdir}/job_buildmonitor.py
+%{destdir}/job_update_nuget.py
 %dir %{destdir}/templates
 %{destdir}/templates/index.html
 %{destdir}/job_update_local_git.py
@@ -144,6 +145,7 @@ fi
 %dir %{destdir}/scripts
 %{destdir}/scripts/check_section.sh
 %{destdir}/scripts/get_git_desc_info.sh
+%{destdir}/scripts/nuget.exe
 %{destdir}/job_ref_snapshot_info_update.py
 %{destdir}/job_make_dep_graph.py
 %{destdir}/common/dep_graph.php.template
diff --git a/scripts/nuget.exe b/scripts/nuget.exe
new file mode 100755 (executable)
index 0000000..324daa8
Binary files /dev/null and b/scripts/nuget.exe differ