Permalink
Browse files

Replaced DirectoryManager with NewDirectoryManager

  • Loading branch information...
1 parent b39d0ba commit 0ae5bd3f6e7e07d03b7eaf1d358539586ba28153 @rjsvaljean rjsvaljean committed Aug 4, 2011
Showing with 33 additions and 213 deletions.
  1. +23 −204 lib/directory_manager.rb
  2. +0 −1 lib/handlers/all.rb
  3. +7 −5 lib/handlers/appliance.rb
  4. +1 −1 lib/handlers/file.rb
  5. +1 −1 lib/handlers/helper.rb
  6. +1 −1 lib/ssc.rb
View
227 lib/directory_manager.rb
@@ -1,8 +1,9 @@
module SSC
- module NewDirectoryManager
+ module DirectoryManager
class LocalStorageFile
- def initialize(file_name)
+ def initialize(file_name, path= nil)
+ path = path ? path : File.expand_path('.')
@location= File.join(File.expand_path('.'), file_name)
end
@@ -57,20 +58,20 @@ def empty_list?
end
class PackageFile < LocalStorageFile
- def initialize
- super("software")
+ def initialize(path= nil)
+ super("software", path)
end
end
class RepositoryFile < LocalStorageFile
- def initialize
- super("repositories")
+ def initialize(path= nil)
+ super("repositories", path)
end
end
class FileListFile < LocalStorageFile
- def initialize
- super("files/.file_list")
+ def initialize(path= nil)
+ super("files/.file_list", path)
end
def pop(section)
@@ -91,18 +92,28 @@ def is_uploaded?(file_name)
end
class ApplianceDirectory
- def initialize(name, options = {})
+ attr_reader :path
+ attr_reader :files
+
+ def initialize(name= '', options = {})
@name= name
@path= File.join(Dir.pwd, name)
+ @files = if Dir.exist?(@path)
+ {:package => PackageFile.new(@path),
+ :repository => RepositoryFile.new(@path),
+ :file_list => FileListFile.new(@path)}
+ else
+ {}
+ end
@options= options
end
def create
FileUtils.mkdir_p(@name)
FileUtils.mkdir_p(File.join(@name, 'files'))
- FileUtils.touch(File.join(@name, 'repositories'))
- FileUtils.touch(File.join(@name, 'software'))
- FileUtils.touch(File.join(@name, 'files/.file_list'))
+ @files[:repository] = FileUtils.touch(File.join(@name, 'repositories'))[0]
+ @files[:package] = FileUtils.touch(File.join(@name, 'software'))[0]
+ @files[:file_list] = FileUtils.touch(File.join(@name, 'files/.file_list'))[0]
File.open(File.join(@name, '.sscrc'), 'w') do |file|
file.write(@options.stringify_keys.to_yaml)
end
@@ -114,196 +125,4 @@ def valid?
end
end
end
-
- module DirectoryManager
-
- def self.included(base)
- base.extend ClassMethods
- base.send :include, InstanceMethods
- end
-
- module ClassMethods
- def create_appliance_directory(appliance_dir, username, password, appliance_id)
- FileUtils.mkdir_p(appliance_dir)
- FileUtils.mkdir_p(File.join(appliance_dir, 'files'))
- FileUtils.touch(File.join(appliance_dir, 'repositories'))
- FileUtils.touch(File.join(appliance_dir, 'software'))
- FileUtils.touch(File.join(appliance_dir, 'files/.file_list'))
- File.open(File.join(appliance_dir, '.sscrc'), 'w') do |file|
- file.write("username: #{username}\n"+
- "password: #{password}\n"+
- "appliance_id: #{appliance_id}")
- end
- File.join(Dir.pwd, appliance_dir)
- end
-
- def manage(local_source)
- self.class.class_variable_set('@@appliance_directory', Dir.pwd)
- if appliance_directory_valid?(Dir.pwd)
- file= File.join(Dir.pwd, local_source)
- self.class.class_variable_set('@@local_source', file) if File.exist?(file)
- end
- end
-
- private
-
- def appliance_directory_valid?(dir)
- config_file= File.join(dir, '.sscrc')
- File.exist?(config_file) && File.read(config_file).match(/appliance_id:\ *\d+/)
- end
-
- end
-
- module InstanceMethods
- include Thor::Actions
-
- # Save data to local storage file
- # @param [String] section The section of the document that is to be saved
- # @param [Array] list The data in Array format which will be merged with existing data
- def save(section, list)
- safe_get_source_file do |source|
- parsed_file= YAML::load(File.read(source))
- # YAML::load returns false if file is empty
- parsed_file= {} unless parsed_file
- final_list= list
- if parsed_file[section]
- current_list= parsed_file[section]
- final_list= current_list | final_list
- end
- parsed_file[section]= final_list
- File.open(source, 'w') {|f| f.write parsed_file.to_yaml}
- end
- end
-
- # Reads data from the local storage file
- # @param [String] section (optional) This is the top-level section
- # of the storage file that is to be read. It can be left blank to
- # return all sections of the file
- # @return [String] Either the whole file of the specified section
- def read(section = nil)
- safe_get_source_file do |source|
- if section
- parsed_file= YAML::load(File.read(source))
- parsed_file[section]
- else
- File.read(source)
- end
- end
- end
-
- private
-
-
- # Wrapper to check existence of source file and other sanity checks
- # It takes a block with one argument - the path of the source file
- def safe_get_source_file
- source= self.class.class_variable_get('@@local_source')
- source= File.join(Dir.pwd, source)
- if File.exist?(source)
- yield source
- else
- raise "Couldn't find the local source file" unless options.remote?
- end
- end
-
- def find_file_id(file_name)
- file_list= File.join(self.class.class_variable_get('@@local_source'), '.file_list')
- parsed_file= YAML::load(File.read(file_list))
- if parsed_file["list"]
- files= parsed_file["list"].select{|i| i.keys[0] == file_name}
- if files.length < 1
- raise ArgumentError, "file not found"
- else
- files[0][file_name]["id"]
- end
- else
- raise ArgumentError, "file not found"
- end
- end
-
- def full_local_file_path(file)
- full_path= File.join(self.class.class_variable_get('@@local_source'), file)
- end
-
- def show_file(file)
- full_path= full_local_file_path(file)
- if File.exist?(full_path)
- File.read(full_path)
- else
- raise ArgumentError, "file not found"
- end
- end
-
- def find_diff(remote, local)
- `diff #{remote} #{local}`
- end
-
- def initiate_file(file_dir, file_name, id, options= {})
- source_file= File.join(file_dir, file_name)
- destination_file= full_local_file_path(file_name)
- file_list= File.join(self.class.class_variable_get('@@local_source'), '.file_list')
- if File.exist?(source_file)
- FileUtils.cp(source_file, destination_file)
- parsed_file= YAML::load(File.read(file_list)) || {}
- File.open(file_list, 'w') do |f|
- if id # if the file has been uploaded
- parsed_file['list']= [] unless parsed_file['list']
- parsed_file['list'] |= [{file_name => {
- "id" => id,
- "path" => file_dir}}]
- else
- parsed_file['add']= [] unless parsed_file['add']
- params= ( {"path" => file_dir} ).merge(options)
- parsed_file['add'] |= [{file_name => params}]
- end
- f.write(parsed_file.to_yaml)
- end
- destination_file
- else
- raise ArgumentError, "File does not exist"
- end
- end
-
- def list_local_files
- source= self.class.class_variable_get('@@local_source')
- parsed_file= YAML::load File.read(File.join(source, '.file_list'))
- parsed_file = {} unless parsed_file
- parsed_file["list"]
- end
-
- def parse_file_list
- source= self.class.class_variable_get('@@local_source')
- file_list= File.read(File.join(source, '.file_list'))
- YAML::load(file_list)
- end
-
- def write_to_file(file, data)
- written= []
- existing_lines= file.readlines.collect{|i| i.strip}
- file.write("\n") if existing_lines.last != '' and existing_lines != []
- data.each do |line|
- unless existing_lines.include?( line )
- file.write(line+"\n")
- written << line
- end
- end
- end
-
- def file_list_empty?
- safe_get_source_file do |source|
- !YAML::load File.read(File.join(source, '.file_list'))
- end
- end
-
- # Checks if the local source file has a list
- # @return [Boolean] true if there is no list
- def no_local_list?
- safe_get_source_file do |source|
- list= YAML::load(File.read source)
- !list || list == nil || list == {} || list == []
- end
- end
-
- end
- end
end
View
1 lib/handlers/all.rb
@@ -11,7 +11,6 @@ module Handler
class Base < Thor
include Helper
- include DirectoryManager
API_URL= 'https://susestudio.com/api/v1/user'
View
12 lib/handlers/appliance.rb
@@ -11,11 +11,13 @@ def create(appliance_name)
params= {:name => appliance_name}
params.merge!(:arch => options.arch) if options.arch
appliance= StudioApi::Appliance.clone(options.source_id, params)
- appliance_dir= self.class.create_appliance_directory(appliance_dir, options.username, options.password, appliance.id)
- say_array ["Created: ", appliance_dir,
- File.join(appliance_dir, 'files'),
- File.join(appliance_dir, 'repositories'),
- File.join(appliance_dir, 'software') ]
+ appliance_params= {
+ :username => options.username,
+ :password => options.password,
+ :appliance_id => appliance.id }
+ appliance_dir= ApplianceDirectory.new(appliance_name, appliance_params)
+ appliance_dir.create
+ say_array(["Created: ", appliance_dir.path] + appliance_dir.files.values)
end
desc "appliance list", "list all appliances"
View
2 lib/handlers/file.rb
@@ -4,7 +4,7 @@ module SSC
module Handler
class OverlayFile < Base
- include NewDirectoryManager
+ include DirectoryManager
no_tasks do
cattr_reader :local_source
View
2 lib/handlers/helper.rb
@@ -51,7 +51,7 @@ def get_config
module InstanceMethods
- include NewDirectoryManager
+ include DirectoryManager
# Establish connection to Suse Studio with username, password
View
2 lib/ssc.rb
@@ -10,7 +10,7 @@ module SSC
module SSC
class Client < Handler::Base
- include NewDirectoryManager
+ include DirectoryManager
register Handler::Appliance, :appliance, "appliance", "manage appliances"
register Handler::Repository, :repository, "repository","manage repositories"

0 comments on commit 0ae5bd3

Please sign in to comment.