Class: BuildTool::VCS::Archive

Inherits:
Base
  • Object
show all
Defined in:
lib/build-tool/vcs/archive.rb

Overview

class ArchiveConfiguration

Defined Under Namespace

Classes: ArchiveError, FileNotFoundError

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#check_for_sshkey, #configure, #gc, #initialize, #local_path, #local_path_exist?, #prepare_for_rebase, #ready_for_fetch, #ready_for_rebase, #recipe

Constructor Details

This class inherits a constructor from BuildTool::VCS::Base

Instance Method Details

#apply_patches(patches) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/build-tool/vcs/archive.rb', line 95

def apply_patches( patches )
    patches.each do |patch|
        full_path = self.recipe.find_first_config_file( "custom/#{config.module.name}/patches/#{patch}.patch" )
        if full_path.nil?
            raise FileNotFoundError, "Patch '#{patch}' not found from module #{config.module.name}."
        end
        logger.info "Applying patch #{patch}"
        self.class.execute( "patch -p1 -i #{full_path}", local_path )
    end

end

#apply_patches_after_rebase?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/build-tool/vcs/archive.rb', line 71

def apply_patches_after_rebase?
    true
end

#archive_local_pathObject



75
76
77
# File 'lib/build-tool/vcs/archive.rb', line 75

def archive_local_path
    "#{File.dirname( local_path )}/#{archive_name}"
end

#archive_nameObject



79
80
81
# File 'lib/build-tool/vcs/archive.rb', line 79

def archive_name
    File.basename( archive_url )
end

#archive_urlObject



83
84
85
# File 'lib/build-tool/vcs/archive.rb', line 83

def archive_url
    "#{config.repository.url}/#{config.remote_path}"
end

#checkedout?Boolean

METHODS

Returns:

  • (Boolean)


91
92
93
# File 'lib/build-tool/vcs/archive.rb', line 91

def checkedout?
    File.exist? archive_local_path
end

#clone(verbose = false) ⇒ Object



107
108
109
110
111
# File 'lib/build-tool/vcs/archive.rb', line 107

def clone( verbose = false )
    fetch( verbose )
    rebase( verbose )
    0
end

#fetch(verbose = false) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/build-tool/vcs/archive.rb', line 113

def fetch( verbose = false )
    # Check if the archive is already downloaded
    if File.exist? archive_local_path
        logger.trace "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.trace "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.info"downloading #{archive_url}"
    if !$noop

        begin

            # We first open the archive. Will throw an exception if the url is invalid.
            open( archive_url ) do |file|

                begin

                    # Create the local file
                    open( archive_local_path, "wb" ) do |target|

                        # Copy
                        while c = file.getc
                            target.putc( c )
                        end

                    end

                rescue

                    # Clean up
                    File.unlink( archive_local_path ) if File.exist?( archive_local_path )
                    raise

                end

            end

        rescue StandardError => e

            msg = "Failed to download #{archive_url}: #{e.to_s}"
            logger.error( msg )
            raise StandardError, msg

        end

    end
    return 0
end

#fetching_supported?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/build-tool/vcs/archive.rb', line 63

def fetching_supported?
    true
end

#guess_top_level_directoryObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/build-tool/vcs/archive.rb', line 173

def guess_top_level_directory
    return "<topdir>" if $noop

    # Find the command to list the archive content
    if archive_name =~ /(\.tgz|\.tar\.gz)$/
        cmd =  "gzip -dc #{archive_local_path} | tar tf -"
    elsif archive_name =~ /(\.txz|\.tar\.xz)$/
        cmd = "xz -dc #{archive_local_path} | tar tf -"
    elsif archive_name =~ /(\.tar\.bz2)$/
        cmd = "bzip2 -dc #{archive_local_path} | tar tf -"
    else
        raise NotImplementedError, "'#{archive_name}' has a unsupported format."
    end

    # Then check for a toplevel directory.
    topdir = nil
    self.class.execute( cmd ) do |line|
        rc = /^([^\/]*)\/?/.match( line )
        if topdir
            if topdir != rc[1]
                return false
            end
        else
            topdir = rc[1]
        end
    end

    return topdir
end

#nameObject

Attributes



59
60
61
# File 'lib/build-tool/vcs/archive.rb', line 59

def name
    "archive"
end

#patches_supported?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/build-tool/vcs/archive.rb', line 67

def patches_supported?
    true
end

#prepare_for_fetchObject



245
246
247
248
# File 'lib/build-tool/vcs/archive.rb', line 245

def prepare_for_fetch
    # If our server has an associated ssh-key, add it to the ssh-agent.
    return check_for_sshkey( config.repository.sshkey )
end

#rebase(verbose = false) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/build-tool/vcs/archive.rb', line 203

def rebase( verbose = false )

    # Check if the archive is already downloaded
    if !File.exist? archive_local_path
        logger.debug "Archive not fetched. Skipping."
        return 0
    end

    if local_path_exist?
        return 0
    end

    # Check if there is a common top level dir
    # eg. my-package-0.4.7
    if guess_top_level_directory()
        strip_components = 1
    else
        strip_components = 0
    end

    # Get the command to unpack
    if archive_name =~ /(\.tgz|\.tar\.gz)$/
        cmd = "gunzip -dc #{archive_local_path} | tar --strip-components=#{strip_components} --extract --file=- --directory=#{local_path}"
    elsif archive_name =~ /(\.txz|\.tar\.xz)$/
        cmd = "xz -dc #{archive_local_path} | tar --strip-components=#{strip_components} --extract --file=- --directory=#{local_path}"
    elsif archive_name =~ /\.tar\.bz2$/
        cmd = "bzip2 -dc #{archive_local_path} | tar --strip-components=#{strip_components} --extract --file=- --directory=#{local_path}"
    else
        raise NotImplementedError, "'#{archive_name}' has unsupported format."
    end

    # Create the directory we want to use to checkout
    FileUtils.mkdir_p local_path if !$noop

    # Unpack
    if self.class.execute( cmd ) != 0
        raise StandardError, "Failed to unpack the archive: $?"
    end

    0
end