right_scraper 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,77 @@
1
+ #--
2
+ # Copyright: Copyright (c) 2010 RightScale, Inc.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # 'Software'), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ module RightScale
25
+
26
+ class SvnScraper < ScraperBase
27
+
28
+ # Check whether it is possible to perform an incremental update of the repo
29
+ #
30
+ # === Return
31
+ # true:: Scrape directory contains files belonging to the scraped repo and protocol supports
32
+ # incremental updates
33
+ # false:: Otherwise
34
+ def incremental_update?
35
+ return false unless File.directory?(@repo_dir)
36
+ Dir.chdir(@repo_dir) do
37
+ info = `svn info`
38
+ $?.success? && info =~ (/^URL: (.*)$/) && $1 == @repo.url
39
+ end
40
+ end
41
+
42
+ # Scrape SVN repository, see RightScale::Scraper#scrape
43
+ #
44
+ # === Return
45
+ # true:: Always return true
46
+ def scrape_imp
47
+ msg = @incremental ? "Updating " : "Checking out "
48
+ msg += "SVN repository '#{@repo.display_name}'"
49
+ @callback.call(msg, is_step=true) if @callback
50
+ if @incremental
51
+ svn_cmd = "svn update --non-interactive --quiet" +
52
+ (@repo.first_credential ? " --username #{@repo.first_credential}" : '') +
53
+ (@repo.second_credential ? " --password #{@repo.second_credential}" : '') +
54
+ ' 2>&1'
55
+ Dir.chdir(@repo_dir) do
56
+ res = `#{svn_cmd}`
57
+ if $? != 0
58
+ @callback.call("Failed to update repo: #{res}, falling back to checkout", is_step=false) if @callback
59
+ FileUtils.rm_rf(@repo_dir)
60
+ @incremental = false
61
+ end
62
+ end
63
+ end
64
+ if !@incremental
65
+ svn_cmd = "svn checkout #{@repo.url} #{@repo_dir} --non-interactive --quiet" +
66
+ (!@repo.tag.nil? && !@repo.tag.empty? ? " --revision #{@repo.tag}" : '') +
67
+ (@repo.first_credential ? " --username #{@repo.first_credential}" : '') +
68
+ (@repo.second_credential ? " --password #{@repo.second_credential}" : '') +
69
+ ' 2>&1'
70
+ res = `#{svn_cmd}`
71
+ @errors << res if $? != 0
72
+ end
73
+ true
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,31 @@
1
+ #--
2
+ # Copyright: Copyright (c) 2010 RightScale, Inc.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # 'Software'), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ # Explicitely list required files to make IDEs happy
25
+ require 'fileutils'
26
+ require File.join(File.dirname(__FILE__), 'right_scraper', 'scraper_base')
27
+ require File.join(File.dirname(__FILE__), 'right_scraper', 'scrapers', 'download_scraper')
28
+ require File.join(File.dirname(__FILE__), 'right_scraper', 'scrapers', 'git_scraper')
29
+ require File.join(File.dirname(__FILE__), 'right_scraper', 'scrapers', 'svn_scraper')
30
+ require File.join(File.dirname(__FILE__), 'right_scraper', 'repository')
31
+ require File.join(File.dirname(__FILE__), 'right_scraper', 'scraper')
@@ -0,0 +1,55 @@
1
+ # Copyright: Copyright (c) 2010 RightScale, Inc.
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # 'Software'), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'rubygems'
23
+
24
+ spec = Gem::Specification.new do |spec|
25
+ spec.name = 'right_scraper'
26
+ spec.version = '1.0.0'
27
+ spec.authors = ['Raphael Simon']
28
+ spec.email = 'raphael@rightscale.com'
29
+ spec.homepage = 'https://github.com/rightscale/right_scraper'
30
+ spec.platform = Gem::Platform::RUBY
31
+ spec.summary = 'Download and update remote repositories'
32
+ spec.has_rdoc = true
33
+ spec.rdoc_options = ["--main", "README.rdoc", "--title", "RightScraper"]
34
+ spec.extra_rdoc_files = ["README.rdoc"]
35
+ spec.required_ruby_version = '>= 1.8.6'
36
+ spec.rubyforge_project = %q{right_scraper}
37
+ spec.require_path = 'lib'
38
+
39
+ spec.description = <<-EOF
40
+ RightScraper provides a simple interface to download and keep local copies of remote
41
+ repositories up-to-date using the following protocols:
42
+ * git: RightScraper will clone then pull repos from git
43
+ * SVN: RightScraper will checkout then update SVN repositories
44
+ * tarballs: RightScraper will download, optionally uncompress and expand a given tar file
45
+ EOF
46
+
47
+ candidates = Dir.glob("{lib,spec}/**/*") +
48
+ ["LICENSE", "README.rdoc", "Rakefile", "right_scraper.gemspec"]
49
+ spec.files = candidates.sort
50
+ end
51
+
52
+ if $PROGRAM_NAME == __FILE__
53
+ Gem.manage_gems if Gem::RubyGemsVersion.to_f < 1.0
54
+ Gem::Builder.new(spec).build
55
+ end
@@ -0,0 +1,87 @@
1
+ #--
2
+ # Copyright: Copyright (c) 2010 RightScale, Inc.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # 'Software'), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ require File.join(File.dirname(__FILE__), 'spec_helper')
25
+ require 'scraper_base'
26
+ require 'repository'
27
+ require File.join('scrapers', 'download_scraper')
28
+
29
+ describe RightScale::DownloadScraper do
30
+
31
+ include RightScale::SpecHelpers
32
+
33
+ # Create download repository following given layout
34
+ # Update @repo_path with path to repository
35
+ # Delete any previously created repo
36
+ def setup_download_repo
37
+ @download_repo_path = File.expand_path(File.join(File.dirname(__FILE__), '__download_repo'))
38
+ @repo_path = File.join(File.dirname(__FILE__), '__repo')
39
+ @repo_content = [ 'file1', { 'folder1' => [ 'file2', 'file3' ] }, { 'folder2' => [ { 'folder3' => [ 'file4' ] } ] } ]
40
+ FileUtils.rm_rf(@download_repo_path)
41
+ create_file_layout(@download_repo_path, @repo_content)
42
+ @download_file = File.expand_path(File.join(File.dirname(__FILE__), '__download_file.tar'))
43
+ Dir.chdir(@download_repo_path) do
44
+ res, status = exec("tar cf #{@download_file} *")
45
+ raise "Failed to create tarball: #{res}" unless status.success?
46
+ end
47
+ end
48
+
49
+ # Cleanup after ourselves
50
+ def delete_download_repo
51
+ FileUtils.rm_rf(@download_repo_path) if @download_repo_path
52
+ @download_repo_path = nil
53
+ FileUtils.rm_rf(@repo_path) if @repo_path
54
+ @repo_path = nil
55
+ File.delete(@download_file) if File.exist?(@download_file)
56
+ end
57
+
58
+ context 'given a download repository' do
59
+
60
+ before(:all) do
61
+ setup_download_repo
62
+ end
63
+
64
+ before(:each) do
65
+ @scraper = RightScale::DownloadScraper.new(@repo_path)
66
+ @repo = RightScale::Repository.from_hash(:display_name => 'test repo',
67
+ :repo_type => :download,
68
+ :url => "file://#{@download_file}")
69
+ end
70
+
71
+ after(:all) do
72
+ delete_download_repo
73
+ end
74
+
75
+ it 'should scrape' do
76
+ messages = []
77
+ @scraper.scrape(@repo) { |m, progress| messages << m if progress }
78
+ puts "\n **ERRORS: #{@scraper.error_message}\n" unless @scraper.succeeded?
79
+ @scraper.succeeded?.should be_true
80
+ messages.size.should == 1
81
+ File.directory?(@scraper.repo_dir.should be_true)
82
+ extract_file_layout(@scraper.repo_dir).should == @repo_content
83
+ end
84
+
85
+ end
86
+
87
+ end
@@ -0,0 +1,108 @@
1
+ #--
2
+ # Copyright: Copyright (c) 2010 RightScale, Inc.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # 'Software'), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ require File.join(File.dirname(__FILE__), 'spec_helper')
25
+ require 'scraper_base'
26
+ require 'repository'
27
+ require File.join('scrapers', 'git_scraper')
28
+
29
+ describe RightScale::GitScraper do
30
+
31
+ include RightScale::SpecHelpers
32
+
33
+ # Create git repository following given layout
34
+ # Update @repo_path with path to repository
35
+ # Delete any previously created repo
36
+ def setup_git_repo
37
+ @origin_path = File.expand_path(File.join(File.dirname(__FILE__), '__origin'))
38
+ @repo_path = File.join(File.dirname(__FILE__), '__repo')
39
+ @repo_content = [ 'file1', { 'folder1' => [ 'file2', 'file3' ] }, { 'folder2' => [ { 'folder3' => [ 'file4' ] } ] } ]
40
+ FileUtils.rm_rf(@origin_path)
41
+ FileUtils.mkdir_p(@origin_path)
42
+ Dir.chdir(@origin_path) do
43
+ res, status = exec("git init --bare")
44
+ raise "Failed to initialize remote git repository: #{res}" unless status.success?
45
+ end
46
+ FileUtils.rm_rf(@repo_path)
47
+ res, status = exec("git clone --quiet #{@origin_path} #{@repo_path}")
48
+ raise "Failed to initialize git repository: #{res}" unless status.success?
49
+ create_file_layout(@repo_path, @repo_content)
50
+ Dir.chdir(@repo_path) do
51
+ res, status = exec("git add .")
52
+ res, status = exec("git commit --quiet -am 'Initial Commit'") if status.success?
53
+ res, status = exec("git push origin master") if status.success?
54
+ raise "Failed to setup git repository: #{res}" unless status.success?
55
+ end
56
+ end
57
+
58
+ # Cleanup after ourselves
59
+ def delete_git_repo
60
+ FileUtils.rm_rf(@origin_path) if @origin_path
61
+ @origin_path = nil
62
+ FileUtils.rm_rf(@repo_path) if @repo_path
63
+ @repo_path = nil
64
+ end
65
+
66
+ context 'given a git repository' do
67
+
68
+ before(:all) do
69
+ setup_git_repo
70
+ end
71
+
72
+ before(:each) do
73
+ @scraper = RightScale::GitScraper.new(@repo_path)
74
+ @repo = RightScale::Repository.from_hash(:display_name => 'test repo',
75
+ :repo_type => :git,
76
+ :url => @origin_path)
77
+ end
78
+
79
+ after(:all) do
80
+ delete_git_repo
81
+ end
82
+
83
+ it 'should scrape' do
84
+ messages = []
85
+ @scraper.scrape(@repo) { |m, progress| messages << m if progress }
86
+ puts "\n **ERRORS: #{@scraper.error_message}\n" unless @scraper.succeeded?
87
+ @scraper.succeeded?.should be_true
88
+ messages.size.should == 1
89
+ File.directory?(@scraper.repo_dir.should be_true)
90
+ extract_file_layout(@scraper.repo_dir, [ '.git', '.ssh' ]).should == @repo_content
91
+ end
92
+
93
+ it 'should scrape incrementally' do
94
+ @scraper.scrape(@repo)
95
+ puts "\n **ERRORS: #{@scraper.error_message}\n" unless @scraper.succeeded?
96
+ @scraper.incremental_update?.should be_true
97
+ messages = []
98
+ @scraper.scrape(@repo) { |m, progress| messages << m if progress }
99
+ puts "\n **ERRORS: #{@scraper.error_message}\n" unless @scraper.succeeded?
100
+ @scraper.succeeded?.should be_true
101
+ messages.size.should == 1
102
+ File.directory?(@scraper.repo_dir.should be_true)
103
+ extract_file_layout(@scraper.repo_dir, [ '.git', '.ssh' ]).should == @repo_content
104
+ end
105
+
106
+ end
107
+
108
+ end
data/spec/rcov.opts ADDED
@@ -0,0 +1 @@
1
+ --exclude "spec/*"~
@@ -0,0 +1,45 @@
1
+ #--
2
+ # Copyright: Copyright (c) 2010 RightScale, Inc.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # 'Software'), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ require File.join(File.dirname(__FILE__), 'spec_helper')
25
+ require 'repository'
26
+
27
+ describe RightScale::Repository do
28
+
29
+ it 'should be initializable from a hash' do
30
+ repo = RightScale::Repository.from_hash(:display_name => 'display_name',
31
+ :repo_type => 'repo_type',
32
+ :url => 'url',
33
+ :tag => 'tag',
34
+ :first_credential => 'first_credential',
35
+ :second_credential => 'second_credential')
36
+ repo.should be_kind_of(RightScale::Repository)
37
+ repo.display_name.should == 'display_name'
38
+ repo.repo_type.should == 'repo_type'
39
+ repo.url.should == 'url'
40
+ repo.tag.should == 'tag'
41
+ repo.first_credential.should == 'first_credential'
42
+ repo.second_credential.should == 'second_credential'
43
+ end
44
+
45
+ end
@@ -0,0 +1,41 @@
1
+ #--
2
+ # Copyright: Copyright (c) 2010 RightScale, Inc.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # 'Software'), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ require File.join(File.dirname(__FILE__), 'spec_helper')
25
+ require 'scraper_base'
26
+
27
+ describe RightScale::ScraperBase do
28
+
29
+ before(:each) do
30
+ @base = RightScale::ScraperBase.new('/tmp')
31
+ end
32
+
33
+ it 'should initialize the scrape directory' do
34
+ @base.root_dir.should == '/tmp'
35
+ end
36
+
37
+ it 'should default to non incremental updates' do
38
+ @base.send(:incremental_update?).should be_false
39
+ end
40
+
41
+ end
@@ -0,0 +1,67 @@
1
+ #--
2
+ # Copyright: Copyright (c) 2010 RightScale, Inc.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # 'Software'), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ require File.join(File.dirname(__FILE__), 'spec_helper')
25
+ require File.join('scrapers', 'git_scraper')
26
+ require File.join('scrapers', 'svn_scraper')
27
+ require File.join('scrapers', 'download_scraper')
28
+ require 'scraper'
29
+
30
+ describe RightScale::Scraper do
31
+
32
+ before(:each) do
33
+ @scraper = RightScale::Scraper.new('/tmp')
34
+ @mock_scraper = flexmock('MockScraper')
35
+ mock_scraper_klass = flexmock('MockScraperClass', :new => @mock_scraper)
36
+ RightScale::SCRAPERS.merge!(:mock => mock_scraper_klass)
37
+ end
38
+
39
+ after(:all) do
40
+ RightScale::SCRAPERS.delete(:mock)
41
+ end
42
+
43
+ it 'should scrape' do
44
+ repo = RightScale::Repository.new
45
+ repo.repo_type = :mock
46
+ @mock_scraper.should_receive(:scrape).with(repo, Proc).and_return(true)
47
+ @mock_scraper.should_receive(:succeeded?).and_return(true)
48
+ @mock_scraper.should_receive(:repo_dir).and_return('42')
49
+ @scraper.scrape(repo) { }.should be_true
50
+ @scraper.repo_dir.should == '42'
51
+ end
52
+
53
+ it 'should scrape from a hash' do
54
+ @mock_scraper.should_receive(:scrape).with(RightScale::Repository, Proc).and_return(true)
55
+ @mock_scraper.should_receive(:succeeded?).and_return(true)
56
+ @mock_scraper.should_receive(:repo_dir).and_return('42')
57
+ @scraper.scrape({:repo_type => :mock}) { }.should be_true
58
+ end
59
+
60
+ it 'should report failures' do
61
+ @mock_scraper.should_receive(:scrape).with(RightScale::Repository, Proc).and_return(true)
62
+ @mock_scraper.should_receive(:succeeded?).and_return(false)
63
+ @mock_scraper.should_receive(:repo_dir).and_return('42')
64
+ @scraper.scrape({:repo_type => :mock}) { }.should be_false
65
+ end
66
+
67
+ end
@@ -0,0 +1,111 @@
1
+ #--
2
+ # Copyright: Copyright (c) 2010 RightScale, Inc.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # 'Software'), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ require 'rubygems'
25
+ require 'flexmock'
26
+ require 'spec'
27
+ require 'find'
28
+
29
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'right_scraper')))
30
+
31
+ Spec::Runner.configuration.mock_with :flexmock
32
+
33
+ # Helper module
34
+ module RightScale
35
+
36
+ module SpecHelpers
37
+
38
+ # Set the 'verbose' environment variable for debugging a failing spec
39
+ VERBOSE='verbose'
40
+
41
+ # Execute given shell command and return output and exit code
42
+ # Allows centralizing logging/output
43
+ #
44
+ # === Parameters
45
+ # cmd(String):: Command to be run
46
+ #
47
+ # === Return
48
+ # res, process status(Array):: Pair whose first element is the output of the command
49
+ # and second element is the process exit status
50
+ def exec(cmd)
51
+ puts "+ [#{Dir.pwd}] #{cmd}" if ENV[VERBOSE]
52
+ res = `#{cmd} 2>&1`
53
+ puts res unless res.empty? if ENV[VERBOSE]
54
+ return res, $?
55
+ end
56
+
57
+ # Create file layout from given array
58
+ # Strings in array correspond to files while Hashes correspond to folders
59
+ # File content is equal to filename
60
+ #
61
+ # === Parameters
62
+ # Path(String):: Path where layout should be created
63
+ # layout(Array):: Describe the file layout to be created
64
+ #
65
+ # === Return
66
+ # true:: Always return true
67
+ def create_file_layout(path, layout)
68
+ FileUtils.mkdir_p(path)
69
+ layout.each do |elem|
70
+ if elem.is_a?(Hash)
71
+ elem.each do |k, v|
72
+ full_path = File.join(path, k)
73
+ FileUtils.mkdir_p(full_path)
74
+ create_file_layout(full_path, v)
75
+ end
76
+ else
77
+ File.open(File.join(path, elem.to_s), 'w') { |f| f.puts elem.to_s }
78
+ end
79
+ end
80
+ true
81
+ end
82
+
83
+ # Extract array representing file layout for given directory
84
+ #
85
+ # === Parameters
86
+ # path(String):: Path to directory whose layout is to be retrieved
87
+ # layout(Array):: Array being updated with layout, same as return value, empty array by default
88
+ # ignore(Array):: Optional: Name of files or directories that should be ignored
89
+ #
90
+ # === Return
91
+ # layout(Array):: Corresponding layout as used by 'create_file_layout'
92
+ def extract_file_layout(path, ignore=[])
93
+ return [] unless File.directory?(path)
94
+ layout = []
95
+ ignore += [ '.', '..' ]
96
+ Dir.foreach(path) do |f|
97
+ next if ignore.include?(f)
98
+ full_path = File.join(path, f)
99
+ if File.directory?(full_path)
100
+ layout << { f => extract_file_layout(full_path, ignore) }
101
+ else
102
+ layout << f
103
+ end
104
+ end
105
+ layout
106
+ end
107
+
108
+
109
+ end
110
+
111
+ end