Permalink
Browse files

merged with dsl branch

  • Loading branch information...
2 parents f2f281f + e3623dd commit af0663f2b235bd2cd466cc09a977801ce1b42106 @swissmanu committed Feb 22, 2012
View
@@ -0,0 +1,2 @@
+.DS_Store
+*.gem
View
@@ -1,9 +1,62 @@
# HSRHelpers
Some Ruby scripts which might be helpful for HSR students :-)
-## Todo
-* Move configuration and task definition from sync.rb into an external
- YAML file. (done, thanks @mweibel)
-* DSL for defining sync jobs (see `dsl` branch)
-* Commandline interface (see `dsl` branch)
-* Create GEM (see `dsl` branch)
+## Installation
+At the moment, you have to build the gemspec by yourself:
+
+ gem build hsrhelpers.gemspec
+
+This will build most commonly a file like `hsrhelpers-X.Y.Z.gem`. Install this file with:
+
+ gem install hsrhelpers-X.Y.Z.gem
+
+Afterwards, you can call the HSRHelpers by invoking `hsrhelpers` on your commandline.
+
+## Sync
+Create a job file:
+
+```ruby
+# HSRScripts Job
+# Mirrors script folders from the HSR share to the local machine and copies
+# the files afterwards into the local dropbox folder.
+HsrHelpers::Sync::Job.new(:hsrscripts) do
+ # Tweaking the paths. Not related to the sync-tool, but for convenience:
+ local_hsr_root = '/Users/manuel/HSR'
+ current_semester = 'Sem 6 - 2012'
+ local_path = "#{local_hsr_root}/#{current_semester}/"
+ dropbox_path = "/Users/manuel/Dropbox/HSR/#{current_semester}"
+
+ # Mount the script share.
+ # Note: You can use :hsrscripts afterwards in the one_way statement to
+ # reference to this share.
+ mount :hsrscripts do |mount|
+ type :smb
+ path 'smb://__USERNAME__:__PASSWORD__@c206.hsr.ch/skripte'
+ end
+
+ # One-way-mirroring of specified folders
+ one_way :server_to_local do |one_way|
+ one_way.source_base ':hsrscripts/Informatik/Fachbereich'
+ one_way.target_base local_path
+ one_way.mirror 'Betriebssystemkonzepte/BsKon', 'BsKon/Skripteserver'
+ one_way.mirror 'Datenbanksysteme_2/Dbs2', 'Datenbanksysteme_2/Dbs2'
+ one_way.mirror 'Software-Engineering_3/SE3', 'SE3/Skripteserver'
+ one_way.mirror 'Verteilte_SW-Systeme/Vss', 'Vss/Skripteserver'
+ exclude 'Thumbs.db'
+ end
+
+ # Mirror to dropbox folder:
+ one_way :dropbox do |one_way|
+ one_way.mirror local_path, dropbox_path
+ end
+
+ # Unmount share:
+ unmount :hsrscripts
+
+end
+```
+
+Afterwards, invoke the `hsrhelpers sync` command and pass your job file:
+
+ hsrhelpers sync myjobfile # if in same directory
+ hsrhelpers sync /path/to/myjobfile.rb # if calling from somewhere else
View
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+# encoding: utf-8
+
+require File.expand_path("../../lib/hsrhelpers", __FILE__)
+require File.expand_path("../../lib/hsrhelpers/cli", __FILE__)
+HsrHelpers::CLI.start;
View
@@ -0,0 +1,31 @@
+# encoding: utf-8
+
+require File.expand_path(File.dirname(__FILE__) + '/lib/hsrhelpers')
+
+Gem::Specification.new do |gem|
+
+ ##
+ # General configuration / information
+ gem.name = 'hsrhelpers'
+ gem.version = HsrHelpers::Version.current
+ gem.platform = Gem::Platform::RUBY
+ gem.authors = 'Manuel Alabor'
+ gem.email = 'manuel@alabor.me'
+ gem.homepage = 'http://www.github.com/swissmanu/hsrhelpers'
+ gem.summary = ''
+
+ ##
+ # Files and folder that need to be compiled in to the Ruby Gem
+ gem.files = %x[git ls-files].split("\n")
+ #gem.test_files = %x[git ls-files -- {spec}/*].split("\n")
+ gem.require_path = 'lib'
+
+ ##
+ # HSRHelpers CLI executable
+ gem.executables = ['hsrhelpers']
+
+ ##
+ # Gem dependencies
+ gem.add_dependency 'thor', ['~> 0.14.6']
+
+end
View
@@ -0,0 +1,36 @@
+# Load Ruby core libraries:
+require 'fileutils'
+require 'yaml'
+
+# Load 3rd party libraries
+require 'thor'
+
+##
+# Loads the HsrHelpers
+module HsrHelpers
+
+ ##
+ # Internal paths
+ LIBRARY_PATH = File.join(File.dirname(__FILE__), "hsrhelpers")
+ SYNC_PATH = File.join(LIBRARY_PATH, "sync")
+
+ ##
+ # Core HSRHelpers files:
+ autoload :Base, File.join(LIBRARY_PATH, "base")
+ autoload :CLI, File.join(LIBRARY_PATH, "cli")
+ autoload :Configuration, File.join(LIBRARY_PATH, "configuration")
+ autoload :Logger, File.join(LIBRARY_PATH, "logger")
+ autoload :Version, File.join(LIBRARY_PATH, "version")
+
+ ##
+ # Sync submodule:
+ module Sync
+ autoload :Runner, File.join(SYNC_PATH, "runner")
+ autoload :Job, File.join(SYNC_PATH, "job")
+ autoload :Utils, File.join(SYNC_PATH, "utils")
+ autoload :OneWayMirror, File.join(SYNC_PATH, "onewaymirror")
+ autoload :Mount, File.join(SYNC_PATH, "mount")
+ autoload :Unmount, File.join(SYNC_PATH, "unmount")
+ end
+
+end
View
@@ -0,0 +1,20 @@
+module HsrHelpers
+ class Base
+
+ ##
+ # Returns the path of a temporary directory
+ def self.temp_dir options = {:remove => true}
+ @temp_dir ||= begin
+ require 'tmpdir'
+ require 'fileutils'
+ called_from = File.basename caller.first.split(':').first, ".rb"
+ path = File.join(Dir::tmpdir, "#{called_from}_#{Time.now.to_i}_#{rand(1000)}")
+ Dir.mkdir(path)
+ at_exit {FileUtils.rm_rf(path) if File.exists?(path)} if options[:remove]
+ File.new path
+ end
+ end
+
+ end
+
+end
View
@@ -0,0 +1,33 @@
+module HsrHelpers
+
+ ##
+ # Provides a command line interface to the submodules of HSRHelper using
+ # Thor.
+ class CLI < Thor
+
+ ##
+ # Include Submodule-Runners:
+ include Sync::Runner
+
+ ##
+ # [Sync]
+ # Triggers the Sync-utility to run jobs from files.
+ desc 'sync JOBFILES', "Loads one or more job files and runs their jobs.\n\nExamples:\n" +
+ "\sRuns /Users/manuel/HSR/hsrscripts.rb:\n\s\s\shsrhelpers sync /Users/manuel/HSR/hsrscripts.rb\n" +
+ "\sRuns hsrscripts.rb & dropbox.rb from current directory\n\s\s\shsrhelpers sync hsrscripts,dropbox"
+ def sync(jobs)
+ jobs = jobs.split(',')
+ jobs = jobs.map! { |j| j.strip }
+ sync! jobs
+ end
+
+ ##
+ # Display the version of HsrHelpers
+ map "-v" => :version
+ desc "version", "Display version information"
+ def version
+ puts "HsrHelpers #{HsrHelpers::Version.current}"
+ end
+
+ end
+end
@@ -0,0 +1,35 @@
+require "yaml"
+
+module HsrHelpers
+
+ class Configuration
+
+ ##
+ # Holds a loaded configuration
+ @@config = nil
+
+ ##
+ # Return a specific config value
+ def self.get(*keys)
+ Configuration.ensure_config_is_loaded
+
+ value = @config
+ keys.each do |key|
+ if !value.has_key?(key)
+ value = nil
+ break
+ end
+ value = value[key]
+ end
+
+ value
+ end
+
+ def self.ensure_config_is_loaded
+ @config = YAML.load_file(File.expand_path("../../../config.yml", __FILE__)) if @config.nil?
+ end
+
+ end
+
+end
+
View
@@ -0,0 +1,69 @@
+module HsrHelpers
+
+ ##
+ # thanks https://github.com/meskyanichi/backup
+ class Logger
+
+ def self.message(string)
+ puts loggify(:message, string, :green) unless quiet?
+ end
+
+ def self.error(string)
+ puts loggify(:error, string, :red) unless quiet?
+ end
+
+ def self.warn(string)
+ puts loggify(:warning, string, :yellow) unless quiet?
+ end
+
+ def self.normal(string)
+ puts string unless quiet?
+ end
+
+ ##
+ # Builds the string in a log format with the date/time, the type (colorized)
+ # based on whether it's a message, notice or error, and the message itself.
+ # ANSI color codes are only used in the console, and are not written to the log
+ # since it doesn't do anything and just adds more unnecessary bloat to the log file
+ def self.loggify(type, string, color = false)
+ return "[#{type}] #{string}" unless color
+ "[#{send(color, type)}] #{string}"
+ end
+
+ ##
+ # Invokes the #colorize method with the provided string
+ # and the color code "32" (for green)
+ def self.green(string)
+ colorize(string, 32)
+ end
+
+ ##
+ # Invokes the #colorize method with the provided string
+ # and the color code "33" (for yellow)
+ def self.yellow(string)
+ colorize(string, 33)
+ end
+
+ ##
+ # Invokes the #colorize method the with provided string
+ # and the color code "31" (for red)
+ def self.red(string)
+ colorize(string, 31)
+ end
+
+ ##
+ # Wraps the provided string in colorizing tags to provide
+ # easier to view output to the client
+ def self.colorize(string, code)
+ "\e[#{code}m#{string}\e[0m"
+ end
+
+ ##
+ # Returns 'true' (boolean) if the QUIET constant is defined
+ # By default it isn't defined, only when initializing Backup using
+ # the '--quite' (or '-q') option in the CLI (e.g. backup perform -t my_backup --quiet)
+ def self.quiet?
+ const_defined?(:QUIET) && QUIET
+ end
+ end
+end
View
@@ -0,0 +1,66 @@
+module HsrHelpers
+ module Sync
+ class Job
+
+ class << self
+ def all
+ @all ||= []
+ end
+
+ def find(label)
+ label = label.to_s
+
+ all.each do |job|
+ return job if job.label == label
+ end
+ #raise Error!
+ puts "error finding job with label '#{label}'!"
+ end
+ end
+
+ attr_reader :label
+ attr_accessor :one_way_mirrors
+ attr_accessor :mounts
+ attr_accessor :unmounts
+
+ def initialize(label, &block)
+ @label = label.to_s
+
+ @one_way_mirrors = Array.new
+ @mounts = Array.new
+ @unmounts = Array.new
+
+ instance_eval(&block) if block_given?
+ Job.all << self
+ end
+
+ def one_way(name, &block)
+ @one_way_mirrors << OneWayMirror.new(self, name, &block)
+ end
+
+ def mount(name, &block)
+ @mounts << Mount.new(self, name, &block)
+ end
+
+ def unmount(name)
+ @unmounts << Unmount.new(self, name)
+ end
+
+ ##
+ # Runs this job.
+ def perform!
+ Logger.message "Start job \"#{@label}\""
+ begin
+ if @one_way_mirrors.any?
+ @mounts.each { |m| m.perform! }
+ @one_way_mirrors.each { |m| m.perform! }
+ @unmounts.each { |u| u.perform! }
+ end
+ rescue => exception
+ puts exception
+ end
+ end
+
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit af0663f

Please sign in to comment.