[Title] Refactored to use "PROCESS" when logging its build steps
authordonghee yang <donghee.yang@samsung.com>
Fri, 21 Sep 2012 15:11:38 +0000 (00:11 +0900)
committerdonghee yang <donghee.yang@samsung.com>
Fri, 21 Sep 2012 15:11:38 +0000 (00:11 +0900)
build-svr
src/build_server/GitBuildProject.rb
src/builder/Builder.rb
src/common/execute_with_log [new file with mode: 0755]
src/common/utils.rb
src/pkg_server/downloader.rb
upgrade

index fd881ffc7532c28bedcc5b31feab14f2d26d0fb9..78e48bd8af30a75093dad48d8c5fa1eadf93cbde 100755 (executable)
--- a/build-svr
+++ b/build-svr
@@ -90,7 +90,7 @@ begin
                                while(true)
                                        log.info "Build Server[#{option[:name]}] Start - PORT:[#{option[:port]}]"
                                        # Start child process
-                                       cmd = Utils.execute_shell_generate("#{File.dirname(__FILE__)}/build-svr start -n #{option[:name]} -p #{option[:port]} --CHILD")
+                                       cmd = Utils.generate_shell_command("#{File.dirname(__FILE__)}/build-svr start -n #{option[:name]} -p #{option[:port]} --CHILD")
                                        IO.popen(cmd)
                                        pid = Process.wait
 
@@ -101,7 +101,7 @@ begin
                                                break
                                        elsif ($?.exitstatus == 99) then # DIBS UPGRADE
                                                cmd = "#{File.dirname(__FILE__)}/upgrade -l #{File.dirname(__FILE__)} -S -t BUILDSERVER -n #{option[:name]} -p #{option[:port]}" 
-                                               cmd = Utils.execute_shell_generate(cmd)
+                                               cmd = Utils.generate_shell_command(cmd)
                                                puts cmd
                                                Utils.spawn(cmd)
                                                log.info cmd
index d06af21c2021f2b687f76c1d67d0fac37425f44d..3ac261c1b4d1fbe7462e8d3baca273ebc7042a7b 100644 (file)
@@ -249,9 +249,12 @@ class GitBuildProject < CommonProject
 
     def git_cmd(cmd, working_dir, log)
         build_command = "cd \"#{working_dir}\";#{@server.git_bin_path} #{cmd}"
-               ret = Utils.execute_shell_with_log(build_command,log)
-               
-               return ret
+               pid, status = Utils.execute_shell_with_log(build_command,log.path)
+               if status.exitstatus != 0 then 
+                       return false 
+               else
+                       return true
+               end
        end
 
 
index f0087fef4099e446f8745869f2ad2068e99e89f1..f9eeecbd34e38cd5971f3a4325826dc32489310b 100644 (file)
@@ -362,11 +362,11 @@ class Builder
 
                env_def =
                        "BUILD_TARGET_OS=#{os} \
-                        TARGET_OS=#{os} \
-                        TARGET_OS_CATEGORY=#{os_category} \
-                        SRCDIR=\"#{src_path2}\" \
-                        ROOTDIR=\"#{build_root_dir}\" \
-                        VERSION=\"#{version}\" "
+TARGET_OS=#{os} \
+TARGET_OS_CATEGORY=#{os_category} \
+SRCDIR=\"#{src_path2}\" \
+ROOTDIR=\"#{build_root_dir}\" \
+VERSION=\"#{version}\" "
 
                # check script file
                script_file = "#{src_path}/package/build.#{@host_os}"
@@ -426,15 +426,16 @@ class Builder
                        f.puts "#{target}"
                        f.puts "echo \"success\""
                end
-               Utils.execute_shell_with_log( "chmod +x #{src_path}/.build.sh", @log )
+               Utils.execute_shell( "chmod +x #{src_path}/.build.sh", @log )
         build_command = "cd \"#{src_path}\";" + env_def + "./.build.sh"
 
                # execute script
-               if not Utils.execute_shell_with_log( build_command, @log ) then
+               pid, status = Utils.execute_shell_with_log( build_command, @log.path ) 
+               if status.exitstatus != 0 then
                        @log.error( "Failed on build script: \"#{target}\"", Log::LV_USER)
                        return false
                else
-                       Utils.execute_shell_with_log( "rm -rf #{src_path}/.build.sh", @log )
+                       Utils.execute_shell( "rm -rf #{src_path}/.build.sh", @log )
                        return true
                end
        end
@@ -618,7 +619,7 @@ class Builder
                        # zip   
                        @log.info( "Creating package file ... #{pkg.package_name}_#{pkg.version}_#{os}.zip", Log::LV_USER)
                        @log.info("cd \"#{install_dir}\"; zip -r -y #{src_path}/#{pkg.package_name}_#{pkg.version}_#{os}.zip *")
-                       Utils.execute_shell_with_log("cd \"#{install_dir}\"; zip -r -y #{src_path}/#{pkg.package_name}_#{pkg.version}_#{os}.zip *", @log)
+                       Utils.execute_shell_with_log("cd \"#{install_dir}\"; zip -r -y #{src_path}/#{pkg.package_name}_#{pkg.version}_#{os}.zip *", @log.path)
                        if not File.exist? "#{src_path}/#{pkg.package_name}_#{pkg.version}_#{os}.zip" then
                                return false
                        end
diff --git a/src/common/execute_with_log b/src/common/execute_with_log
new file mode 100755 (executable)
index 0000000..3a0bddd
--- /dev/null
@@ -0,0 +1,35 @@
+#!/usr/bin/ruby
+
+require 'logger'
+
+# parse arguments
+cmd = ARGV[0]
+log_path = ARGV[1]
+
+# if log path specified, generate log
+if not log_path.nil? then
+       # create logger
+       log = Logger.new(log_path)
+
+       # execute and write log
+       IO.popen("#{cmd} 2>&1") { |io|
+               io.each { |line|
+               log.info line
+               }
+       }
+
+       # return exit code
+       exit $?.exitstatus
+#$?.to_i
+else
+       # execute command & wait
+       pipe = IO.popen("#{cmd}")
+       pid, status = Process.waitpid2(pipe.pid)
+
+       # return exit code
+       if pid.nil? or status.nil? then
+               exit 1
+       else
+               exit status.exitstatus
+       end
+end
index ea2bf959661ab6f9315f819f620d03d17affdfdc..e74e27d98cccea2379dfc8821b8cfacf5dff63ce 100644 (file)
@@ -138,9 +138,8 @@ class Utils
         return 0
        end
 
-    def Utils.execute_shell_generate(cmd, os_category = nil)
-               result_lines = []
 
+    def Utils.generate_shell_command(cmd, os_category = nil)
                if os_category.nil? then os_category = get_os_category( HOST_OS ) end
 
                if os_category == "windows" then
@@ -155,13 +154,9 @@ class Utils
 
     def Utils.execute_shell(cmd, os_category = nil)
         ret = false
-               if os_category.nil? then os_category = get_os_category( HOST_OS ) end
-
-               if os_category == "windows" then
-                       mingw_path = "sh.exe -c "
-                       cmd = cmd.gsub("\"", "\\\"")
-            cmd = mingw_path + "\"#{cmd}\""
-               end
+               
+               # generate command
+               cmd = generate_shell_command(cmd, os_category)
 
                `#{cmd}`
         if $?.to_i == 0 then ret = true else ret = false end 
@@ -173,13 +168,8 @@ class Utils
     def Utils.execute_shell_return(cmd, os_category = nil)
                result_lines = []
 
-               if os_category.nil? then os_category = get_os_category( HOST_OS ) end
-
-               if os_category == "windows" then
-                       mingw_path = "sh.exe -c "
-                       cmd = cmd.gsub("\"", "\\\"")
-            cmd = mingw_path + "\"#{cmd}\""
-               end
+               # generate command
+               cmd = generate_shell_command(cmd, os_category)
 
                # get result
                IO.popen("#{cmd} 2>&1") { |io|
@@ -196,41 +186,34 @@ class Utils
        end
 
     def Utils.execute_shell_return_ret(cmd, os_category = nil)
-               if os_category.nil? then os_category = get_os_category( HOST_OS ) end
 
-               if os_category == "windows" then
-                       mingw_path = "sh.exe -c "
-                       cmd = cmd.gsub("\"", "\\\"")
-            cmd = mingw_path + "\"#{cmd}\""
-               end
+               # generate command
+               cmd = generate_shell_command(cmd, os_category)
 
         return `#{cmd}`
     end
 
-       def Utils.execute_shell_with_log(cmd, log, os_category = nil)
 
-               if os_category.nil? then os_category = get_os_category( HOST_OS ) end
+       # create process and log its all output(stderr, stdout)
+       # can decide whether wait or not
+       def Utils.execute_shell_with_log(cmd, log_path, wait=true)
 
-               if os_category == "windows" then
-                       mingw_path = "sh.exe -c "
-                       cmd = cmd.gsub("\"", "\\\"")
-            cmd = mingw_path + "\"#{cmd}\""
-               end
+               # call execute
+               cmd = "'#{File.dirname(__FILE__)}/execute_with_log' '#{cmd}' '#{log_path}'"
+
+               # generate command
+               cmd = generate_shell_command(cmd, nil)
 
                # print log
-               IO.popen("#{cmd} 2>&1") { |io|
-                       io.each do |line|
-                               log.output line
-                       end
-               }
-               
-        if $?.to_i == 0 then 
-                       return true 
-               else 
-                       return false 
-               end 
+               pipe = IO.popen("#{cmd} 2>&1")
+               if wait then
+                       return Process.waitpid2(pipe.pid)
+               else
+                       return [pipe.pid,nil]
+               end
        end
 
+
     def Utils.spawn(cmd, os_category = nil)
                
                if os_category.nil? then os_category = get_os_category( HOST_OS ) end
index 7e5a8c885a626addaba5dc5806a0b1a6e9bb6cf5..982fde0ff5a0ebea055d5fe14c867ec8c77eab00 100644 (file)
@@ -45,7 +45,8 @@ class FileDownLoader
 
                logger.info "Downloading #{url}"
         if is_remote then
-            ret = Utils.execute_shell_with_log( "wget #{url} -O #{fullpath} -nv", logger )
+            pid,status = Utils.execute_shell_with_log( "wget #{url} -O #{fullpath} -nv", logger )
+                       ret = status.exitstatus != 0 ? false : true
             #ret = Utils.execute_shell( "wget #{url} -O #{fullpath} -q")
         else
             if not File.exist? url then
diff --git a/upgrade b/upgrade
index b93eefe395dfc82fb7b6f829f89c0139c5797e0f..9635015b2d1d5c35376f65ad708dcf9e812c37fc 100755 (executable)
--- a/upgrade
+++ b/upgrade
@@ -153,7 +153,7 @@ begin
                        cmd = "#{UPGRADE_CMD} -I -l #{dibs_path} -u #{pkg_svr_url}"
                end
 
-               cmd = Utils.execute_shell_generate(cmd)
+               cmd = Utils.generate_shell_command(cmd)
                Utils.spawn(cmd)
 
        else
@@ -231,7 +231,7 @@ begin
                                end             
 
                                # Start Build server
-                               cmd = Utils.execute_shell_generate("#{dibs_path}/build-svr start -n #{svr_name} -p #{svr_port}")
+                               cmd = Utils.generate_shell_command("#{dibs_path}/build-svr start -n #{svr_name} -p #{svr_port}")
                                Utils.spawn(cmd)
 
                                log.info("Upgrade Complete", Log::LV_USER)
@@ -239,7 +239,7 @@ begin
 
                        else # PACKAGE SERVER
                                # Start Build server
-                               cmd = Utils.execute_shell_generate("#{dibs_path}/pkg-svr start -n #{svr_name} -p #{svr_port}")
+                               cmd = Utils.generate_shell_command("#{dibs_path}/pkg-svr start -n #{svr_name} -p #{svr_port}")
                                Utils.spawn(cmd)
 
                                log.info("Upgrade Complete", Log::LV_USER)