smc-get 0.1.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,139 @@
1
+ #Encoding: UTF-8
2
+ ################################################################################
3
+ # This file is part of smc-get.
4
+ # Copyright (C) 2010-2011 Entertaining Software, Inc.
5
+ # Copyright (C) 2011 Marvin Gülker
6
+ #
7
+ # This program is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
+ ################################################################################
20
+
21
+ require "pathname"
22
+ require 'tempfile'
23
+ require "fileutils"
24
+ begin
25
+ require "psych"
26
+ YAML = Psych
27
+ rescue LoadError
28
+ require 'yaml'
29
+ end
30
+ require 'uri'
31
+ require 'net/https'
32
+
33
+ require_relative "./errors"
34
+ require_relative "./package"
35
+
36
+ #This is the main module of smc-get and it's namespace.
37
+ module SmcGet
38
+
39
+ #Root directory of the smc-get program and libraries.
40
+ ROOT_DIR = Pathname.new(__FILE__).dirname.parent.parent
41
+ #Subdirectory for executable files.
42
+ BIN_DIR = ROOT_DIR + "bin"
43
+ #Subdirectory for library files.
44
+ LIB_DIR = ROOT_DIR + "lib"
45
+ #Subdirectory for configuration files.
46
+ CONFIG_DIR = ROOT_DIR + "config"
47
+
48
+ #Directory where the package specifications are saved to. Relative to the
49
+ #data directory in SmcGet.datadir.
50
+ PACKAGE_SPECS_DIR = "packages".freeze
51
+ #Directory where a package's music files are saved to. Relative to the
52
+ #data directory in SmcGet.datadir.
53
+ PACKAGE_MUSIC_DIR = "music/contrib-music".freeze
54
+ #Directory where a package's graphics files are saved to. Relative to the
55
+ #data directory given in SmcGet.datadir
56
+ PACKAGE_GRAPHICS_DIR = "pixmaps/contrib-graphics".freeze
57
+ #Directory where a package's level files are saved to. Relative to the
58
+ #data directory given in SmcGet.datadir.
59
+ PACKAGE_LEVELS_DIR = "levels".freeze
60
+ #The name of the file containing the list of all levels in the
61
+ #repository.
62
+ PACKAGE_LIST_FILE = "#{PACKAGE_SPECS_DIR}/packages.lst".freeze
63
+ #The version of smc-get.
64
+ VERSION = Pathname.new(__FILE__).dirname.expand_path.join("..", "..", "VERSION.txt").read.chomp
65
+
66
+ class << self
67
+
68
+ #The URL of the repository.
69
+ attr_reader :repo_url
70
+ #The directory where SMC is installed.
71
+ attr_reader :datadir
72
+
73
+ @repo_url = nil
74
+ @datadir = nil
75
+
76
+ #Initializes the library. Pass in the URL from which you want
77
+ #to downloaded packages (most likely Luiji's contributed level
78
+ #repository at <tt>https://github.com/Luiji/Secret-Maryo-Chronicles-Contributed-Levels/raw/master/</tt>)
79
+ #and the directory where SMC is installed (something along the lines of
80
+ #<b>/usr/share/smc</b>). Note you *have* to call this method before
81
+ #you can make use of the smc-get library; otherwise you'll get bombed
82
+ #by SmcGet::Errors::LibraryNotInitialized exceptions.
83
+ #
84
+ #You may call this method more than once if you want to reinitialize
85
+ #the library to use other resources.
86
+ def setup(repo_url, datadir)
87
+ @repo_url = repo_url
88
+ @datadir = Pathname.new(datadir)
89
+ end
90
+
91
+ # Download the specified raw file from the repository to the specified
92
+ # output file. URL should be everything in the URL after
93
+ # SmcGet.repo_url.
94
+ # Yields the currently downloaded file and how many percent of that file have
95
+ # already been downloaded if a block is given.
96
+ def download(url, output) # :nodoc:
97
+ Errors::LibraryNotInitialized.throw_if_needed!
98
+ # Make url friendly.
99
+ url = URI::Parser.new.escape(url)
100
+ # Create directories if needed.
101
+ FileUtils.mkdir_p(File.dirname(output))
102
+ # Download file.
103
+ File.open(output, "w") do |outputfile|
104
+ uri = URI.parse(@repo_url + url)
105
+
106
+ request = Net::HTTP.new(uri.host, uri.port)
107
+ request.use_ssl = true #GitHub uses SSL
108
+
109
+ begin
110
+ request.start do
111
+ #1. Establish connection
112
+ request.request_get(uri.path) do |response|
113
+ raise(Errors::DownloadFailedError.new(url), "ERROR: Received HTTP error code #{response.code}.") unless response.code == "200"
114
+ #2. Get what size the file is
115
+ final_size = response.content_length
116
+ current_size = 0
117
+ #Ensure the first value the user sees are 0%
118
+ yield(url, 0) if block_given?
119
+ #3. Get the actual file in parts and report percent done.
120
+ response.read_body do |part|
121
+ outputfile.write(part)
122
+
123
+ current_size += part.size
124
+ percent = (current_size.to_f / final_size.to_f) * 100
125
+ yield(url, percent) if block_given?
126
+ end
127
+ end
128
+ #Ensure the last value the user sees are 100%
129
+ yield(url, 100) if block_given?
130
+ end
131
+ rescue Timeout::Error
132
+ raise(Errors::ConnectionTimedOutError.new(url), "ERROR: Connection timed out.")
133
+ end
134
+ end
135
+ end
136
+
137
+ end
138
+
139
+ end
@@ -0,0 +1,121 @@
1
+ #!/usr/bin/env ruby
2
+ #Encoding: UTF-8
3
+ ################################################################################
4
+ # This file is part of smc-get.
5
+ # Copyright (C) 2010-2011 Entertaining Software, Inc.
6
+ # Copyright (C) 2011 Marvin Gülker
7
+ #
8
+ # This program is free software: you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation, either version 3 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+ ################################################################################
21
+
22
+ require "fileutils"
23
+ require "test/unit"
24
+
25
+ if RUBY_VERSION =~ /^1.9/
26
+ require_relative "../lib/smc_get/smc_get"
27
+ else #For 1.8 fans
28
+ require File.join(File.expand_path(File.dirname(__FILE__)), "..", "smc_get")
29
+ end
30
+
31
+ class SmcGetCUITest < Test::Unit::TestCase
32
+ #Directory from which packages are downloaded for the test.
33
+ TEST_REPO = "https://github.com/Luiji/Secret-Maryo-Chronicles-Contributed-Levels/raw/master/"
34
+ #Directory where we will download everything into. It's "testdir" right in this directory.
35
+ TEST_DIR = File.join(File.expand_path(File.dirname(__FILE__)), "testdir")
36
+ TEST_PACKAGES_DIR = File.join(TEST_DIR, "packages")
37
+ TEST_PACKAGES_MUSIC_DIR = File.join(TEST_PACKAGES_DIR, "contrib-music")
38
+ TEST_PACKAGES_GRAPHICS_DIR = File.join(TEST_PACKAGES_DIR, "contrib-graphics")
39
+ TEST_PACKAGES_LEVELS_DIR = File.join(TEST_PACKAGES_DIR, "levels")
40
+ #Test configuration file's location. Inside the test dir, so we're able to
41
+ #delete it together with everything else.
42
+ TEST_CONFIG_FILE = File.join(TEST_DIR, "testconf.yml")
43
+
44
+ #The configuration file we use for this tests.
45
+ TEST_CONFIG =<<CONFIG
46
+ ---
47
+ data_directory: #{TEST_DIR}
48
+ repo_url: "https://github.com/Luiji/Secret-Maryo-Chronicles-Contributed-Levels/raw/master/"
49
+ CONFIG
50
+
51
+ #List of pacakges to install and inspect wheather they
52
+ #are correctly installed or not.
53
+ TEST_PACKAGES = %w[icy-mountain christmas-2010]
54
+ #To test wheather smc-get does know what to do in case of non-existing packages.
55
+ TEST_INVALID_PACKAGES = %w[sdhrfg jhjjjj]
56
+
57
+ TEST_SEARCH_TERMS = ["icy", "chr.stmas"]
58
+ TEST_SEARCH_TERMS_NOT_FOUND = ["dfgd", "^icy$"]
59
+
60
+ #The command to run smc-get
61
+ SMC_GET = File.join(File.expand_path(File.dirname(__FILE__)), "..", "bin", "smc-get")
62
+
63
+ #Test initialization. Write the config file and ensure the directory we
64
+ #want to download into is available. Run before EACH test.
65
+ def setup
66
+ FileUtils.mkdir_p(TEST_DIR)
67
+ File.open(TEST_CONFIG_FILE, "w"){|f| f.write(TEST_CONFIG)}
68
+ end
69
+
70
+ #Cleanup afterwards. Delete our testing directory. Run after EACH test.
71
+ def teardown
72
+ FileUtils.rm_rf(TEST_DIR)
73
+ end
74
+
75
+ #Executes <tt>smc-get str</tt> and returns true on success, false
76
+ #otherwise. Ensures the test configuration file is loaded.
77
+ def smc_get(str)
78
+ system("#{SMC_GET} -c #{TEST_CONFIG_FILE} #{str}")
79
+ end
80
+
81
+ def test_install
82
+ TEST_PACKAGES.each do |pkg|
83
+ smc_get "install #{pkg}"
84
+ assert(File.file?(File.join(TEST_PACKAGES_DIR, "#{pkg}.yml")), "Package file not found: #{pkg}.yml.")
85
+ end
86
+ TEST_INVALID_PACKAGES.each do |pkg|
87
+ assert(!smc_get("install #{pkg}"), "Command completed on invalid packages without error.")
88
+ end
89
+ end
90
+
91
+ def test_uninstall
92
+ TEST_PACKAGES.each do |pkg|
93
+ assert(!smc_get("uninstall #{pkg}"), "Uninstalled a noninstalled package successfully.")
94
+ smc_get "install #{pkg}"
95
+ smc_get "uninstall #{pkg}"
96
+ assert(!File.exists?(File.join(TEST_PACKAGES_DIR, "#{pkg}.yml")), "Found after uninstalling: #{pkg}.yml")
97
+ end
98
+ TEST_INVALID_PACKAGES.each do |pkg|
99
+ assert(!smc_get("uninstall #{pkg}"), "Uninstalled an invalid package successfully.")
100
+ end
101
+ end
102
+
103
+ def test_getinfo
104
+ TEST_PACKAGES.each do |pkg|
105
+ assert(smc_get("getinfo #{pkg}"), "Couldn't retrieve info for #{pkg}.")
106
+ end
107
+ TEST_INVALID_PACKAGES.each do |pkg|
108
+ assert(!smc_get("getinfo #{pkg}"), "Retrieved info for broken package sucessfully.")
109
+ end
110
+ end
111
+
112
+ def test_search
113
+ TEST_SEARCH_TERMS.each do |query|
114
+ assert(smc_get("search #{query}"), "Did not find packages it ought to find.")
115
+ end
116
+ TEST_SEARCH_TERMS_NOT_FOUND.each do |query|
117
+ assert(!smc_get("search #{query}"), "Did find packages for invalid query.")
118
+ end
119
+ end
120
+
121
+ end
@@ -0,0 +1,170 @@
1
+ #!/usr/bin/env ruby
2
+ #Encoding: UTF-8
3
+ ################################################################################
4
+ # This file is part of smc-get.
5
+ # Copyright (C) 2010-2011 Entertaining Software, Inc.
6
+ # Copyright (C) 2011 Marvin Gülker
7
+ #
8
+ # This program is free software: you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation, either version 3 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+ ################################################################################
21
+
22
+ require "fileutils"
23
+ require "test/unit"
24
+
25
+ if RUBY_VERSION =~ /^1.9/
26
+ require_relative "../lib/smc_get/smc_get"
27
+ else #For 1.8 fans
28
+ require File.join(File.expand_path(File.dirname(__FILE__)), "..", "smc_get")
29
+ end
30
+
31
+ #Test case for SmcGet. If the tests defined here don't fail, everything
32
+ #works well.
33
+ class SmcGetLibraryTest < Test::Unit::TestCase
34
+
35
+ #Directory from which packages are downloaded for the test.
36
+ TEST_REPO = "https://github.com/Luiji/Secret-Maryo-Chronicles-Contributed-Levels/raw/master/"
37
+ #Directory where we will download everything into. It's "testdir" right in this directory.
38
+ TEST_DIR = File.join(File.expand_path(File.dirname(__FILE__)), "testdir")
39
+ TEST_PACKAGES_DIR = File.join(TEST_DIR, "packages")
40
+ TEST_PACKAGES_MUSIC_DIR = File.join(TEST_PACKAGES_DIR, "contrib-music")
41
+ TEST_PACKAGES_GRAPHICS_DIR = File.join(TEST_PACKAGES_DIR, "contrib-graphics")
42
+ TEST_PACKAGES_LEVELS_DIR = File.join(TEST_PACKAGES_DIR, "levels")
43
+ #Test configuration file's location. Inside the test dir, so we're able to
44
+ #delete it together with everything else.
45
+ TEST_CONFIG_FILE = File.join(TEST_DIR, "testconf.yml")
46
+
47
+ #The configuration file we use for this tests.
48
+ TEST_CONFIG =<<CONFIG
49
+ ---
50
+ data-directory: #{TEST_DIR}
51
+ CONFIG
52
+
53
+ #List of pacakges to install and inspect wheather they
54
+ #are correctly installed or not.
55
+ TEST_PACKAGES = %w[icy-mountain christmas-2010]
56
+ #To test wheather smc-get does know what to do in case of non-existing packages.
57
+ TEST_INVALID_PACKAGES = %w[sdhrfg jhjjjj]
58
+
59
+ TEST_SEARCH_TERMS = ["icy", /icy/]
60
+ TEST_SEARCH_TERMS_NOT_FOUND = ["dfgd", /^nonexistant$/]
61
+
62
+ #Test initialization. Write the config file and ensure the directory we
63
+ #want to download into is available. Run before EACH test.
64
+ def setup
65
+ FileUtils.mkdir_p(TEST_DIR)
66
+ File.open(TEST_CONFIG_FILE, "w"){|f| f.write(TEST_CONFIG)}
67
+ SmcGet.setup(TEST_REPO, TEST_DIR)
68
+ end
69
+
70
+ #Cleanup afterwards. Delete our testing directory. Run after EACH test.
71
+ def teardown
72
+ FileUtils.rm_rf(TEST_DIR)
73
+ end
74
+
75
+ #Tests the library's install routine.
76
+ def test_install
77
+ TEST_PACKAGES.each do |pkg|
78
+ puts "Test installing #{pkg}"
79
+ package = SmcGet::Package.new(pkg)
80
+ package.install
81
+
82
+ assert(package.spec_file.file?, "Package config file for #{pkg} not found.")
83
+
84
+ pkg_config = YAML.load_file(package.spec_file.to_s)
85
+
86
+ Dir.chdir(TEST_DIR) do
87
+ [%w[levels levels], %w[music music/contrib-music], %w[graphics pixmaps/contrib-graphics]].each do |part, dir|
88
+ next unless pkg_config.has_key?(part)
89
+ pkg_config[part].each do |file|
90
+ assert(File.file?(File.join(dir, file)), "File not found after installing: #{file}.")
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ TEST_INVALID_PACKAGES.each do |pkg|
97
+ assert_raises(SmcGet::Errors::NoSuchPackageError){SmcGet::Package.new(pkg).install}
98
+ end
99
+ assert_equal(TEST_PACKAGES.count, SmcGet::Package.installed_packages.count)
100
+ end
101
+
102
+ #def test_incorrect_install
103
+ # #Maybe installed twice?
104
+ #end
105
+
106
+ def test_uninstall
107
+ TEST_PACKAGES.each do |pkg|
108
+ puts "Test uninstalling #{pkg}"
109
+ package = SmcGet::Package.new(pkg)
110
+ package.install #We can't uninstall a package that is not installed
111
+
112
+ pkg_config = YAML.load_file(package.spec_file)
113
+
114
+ package.uninstall
115
+ assert(!package.spec_file.exist?, "File found after uninstalling: #{package.spec_file}.")
116
+
117
+ Dir.chdir(TEST_DIR) do
118
+ [%w[levels levels], %w[music music/contrib-music], %w[graphics pixmaps/contrib-graphics]].each do |part, dir|
119
+ next unless pkg_config.has_key?(part)
120
+ pkg_config[part].each do |file|
121
+ assert(!File.exists?(File.join(dir, file)), "File found after uninstalling: #{file}.")
122
+ end
123
+ end
124
+ end
125
+ end
126
+
127
+ TEST_INVALID_PACKAGES.each do |pkg|
128
+ assert_raises(SmcGet::Errors::NoSuchPackageError){SmcGet::Package.new(pkg).uninstall}
129
+ end
130
+ assert_equal(0, SmcGet::Package.installed_packages.count)
131
+ end
132
+
133
+ def test_getinfo
134
+ TEST_PACKAGES.each do |pkg|
135
+ puts "Test getting info about #{pkg}"
136
+ package = SmcGet::Package.new(pkg)
137
+ package.install #We can't check specs from packages not installed
138
+
139
+ pkg_config = YAML.load_file(package.spec_file)
140
+
141
+ assert_equal(pkg_config, package.getinfo)
142
+ end
143
+
144
+ TEST_INVALID_PACKAGES.each do |pkg|
145
+ assert_raises(SmcGet::Errors::NoSuchPackageError){SmcGet::Package.new(pkg).getinfo}
146
+ end
147
+ end
148
+
149
+ def test_search
150
+ TEST_SEARCH_TERMS.each do |query|
151
+ ary = SmcGet::Package.search(query)
152
+ pkg = ary[0]
153
+ assert_not_equal(0, ary.size)
154
+
155
+ pkg.install
156
+
157
+ ary = SmcGet::Package.search(query, [:pkgname], true)
158
+ assert_not_equal(0, ary.size)
159
+
160
+ pkg.uninstall
161
+
162
+ ary = SmcGet::Package.search(query, [:pkgname], true)
163
+ assert_equal(0, ary.size)
164
+ end
165
+ TEST_SEARCH_TERMS_NOT_FOUND.each do |query|
166
+ assert_equal(0, SmcGet::Package.search(query).size)
167
+ end
168
+ end
169
+
170
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smc-get
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Luiji Maryo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-06 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: |
18
+ smc-get is a apt-get like tool to manage levels that have
19
+ been uploaded to the Secret Maryo Chronicles contributed
20
+ levels repository. You can install, remove and list the
21
+ levels you have installed and search for new ones that
22
+ are available online.
23
+
24
+ email: luiji@users.sourceforge.net
25
+ executables:
26
+ - smc-get
27
+ extensions: []
28
+
29
+ extra_rdoc_files:
30
+ - README.rdoc
31
+ - COPYING
32
+ files:
33
+ - bin/smc-get
34
+ - lib/smc_get/smc_get.rb
35
+ - lib/smc_get/gui.rb
36
+ - lib/smc_get/cui_commands/uninstall.rb
37
+ - lib/smc_get/cui_commands/getinfo.rb
38
+ - lib/smc_get/cui_commands/command.rb
39
+ - lib/smc_get/cui_commands/install.rb
40
+ - lib/smc_get/cui_commands/version.rb
41
+ - lib/smc_get/cui_commands/list.rb
42
+ - lib/smc_get/cui_commands/help.rb
43
+ - lib/smc_get/cui_commands/search.rb
44
+ - lib/smc_get/cui.rb
45
+ - lib/smc_get/errors.rb
46
+ - lib/smc_get/package.rb
47
+ - lib/smc_get.rb
48
+ - test/test_smc-get-cui.rb
49
+ - test/test_smc-get-lib.rb
50
+ - config/smc-get.yml
51
+ - COPYING
52
+ - README.rdoc
53
+ - VERSION.txt
54
+ has_rdoc: true
55
+ homepage: https://github.com/Luiji/smc-get
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options:
60
+ - -t
61
+ - smc-get RDocs
62
+ - -m
63
+ - README.rdoc
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 1.9.2
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ requirements:
79
+ - Secret Maryo Chronicles
80
+ rubyforge_project:
81
+ rubygems_version: 1.5.0
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Manages Secret Maryo Chronicles levels.
85
+ test_files:
86
+ - test/test_smc-get-cui.rb
87
+ - test/test_smc-get-lib.rb