require 'open-uri' require 'build-tool/vcs/base' require 'fileutils' module BuildTool; module VCS class ArchiveConfiguration < BaseConfiguration def name "archive" end def vcs( mod ) @module = mod Archive.new( self ) end end # class ArchiveConfiguration class Archive < Base class ArchiveError < BuildTool::Error; end # ### Attributes # def name "archive" end def fetching_supported? true end def archive_local_path "#{File.dirname( local_path )}/#{archive_name}" end def archive_name File.basename( archive_url ) end def archive_url "#{repository.url}/#{remote_path}" end # ### METHODS # def checkedout? File.exist? archive_local_path end def clone # Check if the archive is already downloaded if File.exist? archive_local_path logger.debug "Archive already fetched. Skipping." return 0 end # If the archive doesn't exist we assume the unpacked archive # should not exist too. if local_path_exist? logger.debug "We fetch a new archive. Remove the old sources." FileUtils.rm_rf local_path if !$noop end # Create the directories parent dir. FileUtils.mkdir_p( File.dirname( local_path ) ) if !$noop # Download the archive logger.trace "get #{archive_name} from #{archive_url} to #{archive_local_path}" if !$noop target = open( archive_local_path, "wb" ) open( archive_url ) { |file| while c = file.getc target.putc c end } target.close() end # Determine if the archive has a top level directory topdir = guess_top_level_directory() if archive_name =~ /(\.tgz|\.tar\.gz)$/ if self.class.execute( "gunzip -c #{archive_local_path} | tar xf -", File.dirname(local_path) ) != 0 raise StandardError, "Failed to unpack the archive: $?" end else raise NotImplementedError, "No idea how to unpack the archive" end # Rename the top level directory to our desired path logger.trace "mv #{File.dirname(local_path)}/#{topdir} #{local_path}" if !$noop FileUtils.move( "#{File.dirname(local_path)}/#{topdir}", local_path ) end 0 end def fetch() if !checkedout? clone else # No update for archives end return true end def guess_top_level_directory return "" if $noop topdir = nil if archive_name =~ /(\.tgz|\.tar\.gz)$/ if self.class.execute( "gunzip -c #{archive_local_path} | tar tf -" ) { |line| rc = /^([^\/]*)\//.match( line ) if topdir and topdir != rc[1] raise StandardError, "Unable to determine toplevel directory for archive #{archive_name}!" end topdir = rc[1] } != 0 raise StandardError, "Failed to unpack the archive: $?" end else raise NotImplementedError, "No idea how to unpack the archive" end return topdir end def rebase 0 end end # class Archive end; end