OpenGrok

Cross Reference: getpkg.rb
xref: /milax/i86pc/mini_constructor/pkg/getpkg.rb
Home | History | Annotate | Line # | Download | only in pkg
      1 #!/usr/bin/ruby
      2 #                                                                                                             
      3 # CDDL HEADER START                                                                                           
      4 #                                                                                                             
      5 # The contents of this file are subject to the terms of the                                                   
      6 # Common Development and Distribution License (the "License").                                                
      7 # You may not use this file except in compliance with the License.                                            
      8 #                                                                                                             
      9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE                                         
     10 # or http://www.opensolaris.org/os/licensing.                                                                 
     11 # See the License for the specific language governing permissions                                             
     12 # and limitations under the License.                                                                          
     13 #                                                                                                             
     14 # When distributing Covered Code, include this CDDL HEADER in each                                            
     15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.                                           
     16 # If applicable, add the following below this CDDL HEADER, with the                                           
     17 # fields enclosed by brackets "[]" replaced with your own identifying                                         
     18 # information: Portions Copyright [yyyy] [name of copyright owner]                                            
     19 #                                                                                                             
     20 # CDDL HEADER END                                                                                             
     21 #                                                                                                             
     22 # Copyright 2007 Sun Microsystems, Inc.  All rights reserved.                                                 
     23 # Use is subject to license terms.                                                                            
     24 #                                                                                                             
     25 # Small hack script to get pkg contents from repo without pkg
     26 # http://www.milax.org
     27 #
     28 
     29 require 'net/http'   
     30 require 'fileutils'                                                                                          
     31 require 'thread'
     32 
     33 
     34     if ARGV.length == 0 || ARGV.length < 2
     35 	puts "Usage: getpkg <inst_dir> <manifest url>" 
     36 	exit 0 
     37     end 
     38     # dir must have "/" at the end
     39     base = ARGV[0]
     40     if !base.match(/\/$/)                                                                                     
     41         base = base + "/"                                                                                             
     42     end              
     43     url = ARGV[1].split("pkg.opensolaris.org")
     44     # get manifest from repo
     45     Net::HTTP.start("pkg.opensolaris.org") { |http|
     46     resp = http.get(url[1])
     47     open("manifest", "wb") { |file|
     48 	file.write(resp.body)
     49 	}
     50     }
     51 
     52     # read manifest
     53     f = File.new("manifest")
     54     print "Processing files "	
     55 
     56     begin
     57     while (line = f.readline)
     58 	    a = %w[ | / - \\ ]
     59 
     60     	    t = Thread.new {
     61 		line.chomp
     62 		# get dir path
     63 	    	if line =~ /^dir/
     64 		    dir = line.scan(/path=[^\s]*/).to_s.split("=")[1]
     65 	            # get mode,group,owner,hash
     66 		    mode = line.scan(/mode=[0-9]*/).to_s.split("=")[1]
     67 	    	    owner = line.scan(/owner=[a-z]*/).to_s.split("=")[1].to_s
     68 		    group = line.scan(/group=[a-z]*/).to_s.split("=")[1].to_s
     69 		    FileUtils.mkdir_p(base + dir, :mode=>Integer(mode))
     70 		    FileUtils.chown(owner,group, base + dir)
     71 		    
     72 		end
     73 		# get file name, path    
     74 	    	if line =~ /^file/
     75 		    path = line.scan(/path=[^\s]*/).to_s.split("=")[1]
     76 		    if path.match(/\//)
     77 			filetmp = path.split("/")
     78 			file = filetmp[(filetmp.size-1)] 
     79 			fdir = path.gsub(file,"")
     80 		    end
     81 	            # get mode,group,owner,hash
     82 		    hash = line.split(/\s/)[1].to_s
     83 		    mode = line.scan(/mode=[0-9]*/).to_s.split("=")[1]
     84 		    owner = line.scan(/owner=[a-z]*/).to_s.split("=")[1].to_s
     85 		    group = line.scan(/group=[a-z]*/).to_s.split("=")[1].to_s
     86 	    
     87 		    # get file from repo, gunzip, chown and chmod
     88 		    Net::HTTP.start("pkg.opensolaris.org") { |http|                                                               
     89 		    resp = http.get("/file/0/" + hash)                      
     90 		    
     91 		    FileUtils.mkdir_p base + fdir
     92 		    open(base + fdir + file + ".gz", "wb") { |filed|                                                                               
     93 			filed.write(resp.body)
     94 			filed.chmod( Integer(mode) )
     95 			}                                                                                                            
     96 		    }                                                                                                            
     97 		    
     98 		    system("gunzip -f " + base + fdir + file + ".gz")
     99 		    FileUtils.chown(owner,group, base + fdir + file)
    100 		    
    101 	    end	
    102 	    } 
    103 	    # small spinner
    104 	    while t.alive?
    105 		print a.unshift(a.pop).last
    106 		sleep 0.1
    107 		print "\b"
    108 	    end
    109 	    t.exit	
    110     end
    111     rescue EOFError
    112 	f.close
    113     end
    114 # now processing links
    115     f = File.new("manifest")
    116     begin
    117     while (line = f.readline)
    118 	    a = %w[ | / - \\ ]
    119 	    t= Thread.new {
    120     		line.chomp
    121 
    122 		if line =~ /^link|^hardlink/
    123 		    path = line.scan(/path=[^\s]*/).to_s.split("=")[1]
    124 		    if path.match(/\//)
    125 			filetmp = path.split("/")
    126 			file = filetmp[(filetmp.size-1)] 
    127 			fdir = path.gsub(file,"")
    128 		    end
    129 		    target = line.scan(/target=[^\s]*/).to_s.split("=")[1]
    130 		    FileUtils.mkdir_p base + fdir
    131 		    Dir.chdir base + fdir
    132 		    if line =~ /^link/
    133 			File.symlink(target,file)      
    134 		    elsif line =~ /^hardlink/
    135 			File.link(target,file)      
    136 		    end
    137     		end
    138 	    } 
    139     
    140 	    while t.alive?
    141 		print a.unshift(a.pop).last
    142 		sleep 0.1
    143 		print "\b"
    144 	    end
    145 	    t.exit
    146     end
    147     rescue EOFError
    148 	f.close
    149     end
    150     puts
    151 
    152 
    153