Class: Reviser::Components::Archiver

Inherits:
Reviser::Component show all
Extended by:
Extractors
Defined in:
lib/reviser/components/archiver.rb

Overview

Manages uncompression of archive. Archiver extracts all data in a given compressed file.

In case of the University of Lorraine, the archive contains all computing projects, compressed too.

If you want to add support of archive format, @see Extractors.

Examples:

The simple way to extract a compressed file is :

Archiver.extract(myFile, myDirectory)

Author:

  • Yann Prono

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extractors

gz, method_missing, rar, seven_zip, tar, zip

Methods inherited from Reviser::Component

#resource, #work

Constructor Details

#initialize(data) ⇒ Archiver

Initializes archive file and the directory of destination.



51
52
53
54
55
56
# File 'lib/reviser/components/archiver.rb', line 51

def initialize(data)
	super data
	@src = Cfg[:src]
	@destination = Cfg[:dest]
	@results = []
end

Class Method Details

.destination?(destination) ⇒ Boolean

Checks if the destination directory exists. else create it. For the moment, if the directory exists // TODO little input to confirm

Parameters:

  • destination (String)

    the destination directory

Returns:

  • (Boolean)


64
65
66
67
68
69
# File 'lib/reviser/components/archiver.rb', line 64

def self.destination?(destination)
	unless $rejected.include? File.basename destination
		FileUtils.rm_rf(destination) if Dir.exists? destination
		FileUtils.mkdir_p destination, :mode => 0700
	end
end

.extract(file_name, destination = '.') ⇒ Object

Extracts the archive into the destination directory.

Parameters:

  • file_name (String)

    The name of the archive.

  • destination (String) (defaults to: '.')

    The destination directory.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/reviser/components/archiver.rb', line 76

def self.extract(file_name, destination = '.')
	raise "#{file_name} not found, please check in the current directory." unless File.exist? file_name

	# Get extension of file_name to know which method calls
	ext = File.extname(file_name)
	ext = ext.delete('.')

	# Raise exception if the format is unknown by Archiver
	raise "Unknown compression format '#{ext}'" unless respond_to?(ext)

	# Check if destination exists
	self::destination? destination

	# Run extraction!
	send(ext,file_name, destination)
end

Instance Method Details

#runObject

Method which extracts an archive which contains all computing projects.

This method extracts,in first time,the archive given in the constructor and after, all extracted files.

Use this method in a global usage of Reviser! Options are for the moment :verbose



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/reviser/components/archiver.rb', line 103

def run
	@logger.h1 Logger::INFO,"First extraction - #{@src}"
	# Extract the original archive
	Archiver.extract(@src, @destination)

	@logger.h1 Logger::INFO,'Extraction of sub archives'
	
	# Extract all sub archives
	entries = Dir.entries(@destination) - $rejected
	extracted = 0

	entries.each do |entry|
		puts "----[#{extracted+1}/#{entries.size}]\t#{entry}"

		ext = File.extname entry
		basename = File.basename entry, ext
		#begin
			file_name = File.join(@destination,File.basename(entry))
			destination = File.join(@destination,basename)

			# Run extraction!
 				Archiver.extract(file_name, destination)
			extracted += 1

			@logger.h2 Logger::INFO, "extracting #{file_name} to #{destination}"
			@results << basename

		# Delete in all case the archive (useless after this step)
		FileUtils.rm_rf file_name
 		end
 		@logger.h1 Logger::INFO, "[#{extracted}/#{entries.size}] projects have been processed"

	@results
end