[LrdeTools] 228: Split the binary and adapt.

https://svn.lrde.epita.fr/svn/lrdetools/trunk Index: ChangeLog from Nicolas Pouillard <ertai@lrde.epita.fr> Split the binary and adapt. * vcs/bin/vcs: Split in three parts. * vcs/lib/vcs/app.rb: New. VcsApp is the class for the vcs binary. * vcs/lib/vcs/environment.rb: New. Handle environment variables. * vcs/lib/vcs/opt_parse.rb: New. The option parser. * vcs/bin/vcs-svn, vcs/bin/vcs-prcs, vcs/bin/vcs-cvs: Simplify: remove a useless `exec'. bin/vcs | 167 +------------------------------------------------ bin/vcs-cvs | 13 +-- bin/vcs-prcs | 13 +-- bin/vcs-svn | 13 +-- lib/vcs/app.rb | 114 +++++++++++++++++++++++++++++++++ lib/vcs/environment.rb | 58 +++++++++++++++++ lib/vcs/opt_parse.rb | 77 ++++++++++++++++++++++ 7 files changed, 273 insertions(+), 182 deletions(-) Index: vcs/bin/vcs --- vcs/bin/vcs (revision 227) +++ vcs/bin/vcs (working copy) @@ -1,164 +1,9 @@ #!/usr/bin/env ruby # Author:: Nicolas Pouillard <ertai@lrde.epita.fr>. -# Copyright:: Copyright (c) 2004 LRDE. All rights reserved. +# Copyright:: Copyright (c) 2004, 2005 LRDE. All rights reserved. # License:: GNU General Public License (GPL). +# Revision:: $Id: header 98 2004-09-29 12:07:43Z ertai $ -# $LastChangedBy: ertai $ -# $Id: header 98 2004-09-29 12:07:43Z ertai $ +require File.join(File.dirname(__FILE__), '..', 'lib', 'vcs', 'app') unless defined? VcsApp - -# vcs: The version control system wrapper. -# -# Vcs is a wrapper over any version control system. - -require 'pathname' -require 'logger' -require 'optparse' -require 'set' - -VCS_VERSION = '0.3.0' -VCS_PATH = Pathname.new(__FILE__).expand_path -VCS_DIR, VCS = VCS_PATH.split -LIB = VCS_DIR + '..' + 'lib' -$: << LIB.to_s - -dir = Pathname.pwd -while not dir.root? and not (vcs_dir = dir + 'vcs').exist? - dir = dir + '..' -end -$: << vcs_dir - -LOG = Logger.new(STDOUT) -if severity = ENV['VCS_SEVERITY'] - LOG.level = Logger.const_get(severity.upcase) -else - LOG.level = Logger::INFO -end -def LOG.format_message(severity, timestamp, msg, progname) - progname += ': ' unless progname.nil? or progname.empty? - "#{VCS}: #{progname}#{severity.downcase}: #{msg}\n" -end - -Pathname.glob("{#{LIB}/**,#{vcs_dir}}/*.rb") do |file| - LOG.debug { file.basename.to_s } - begin - require "vcs/#{file.basename}" - rescue LoadError - begin - require file.to_s - rescue LoadError => ex - raise ex - end - end -end - -ALL_VCS_NAMES = [] -ALL_VCS_BY_NAME = {} -ObjectSpace.each_object(Class) do |aClass| - if aClass != Vcs and aClass.ancestors.include? Vcs - name = aClass.to_s.to_sym - ALL_VCS_NAMES << name - ALL_VCS_BY_NAME[name] = aClass - end -end - -OPTIONS = {} - -OPTS = OptionParser.new do |opts| - - opts.banner = "Usage: #{VCS} [options] <file>*" - opts.separator '' - - opts.on('-c', '--vcs TYPE', ALL_VCS_NAMES, 'Set your vcs') do |aVcs| - OPTIONS[:vcs_type] = aVcs - end - - opts.on('-l', '--vcs-list', 'List all vcs') do |aVcs| - STDERR.puts 'Vcs list:' - ALL_VCS_NAMES.each { |n| STDERR.puts " - #{n}" } - exit - end - - opts.on('--mk-alias', 'Put the result of this command in your .zshrc or .bashrc') do - ALL_VCS_NAMES.each do |n| - n = n.to_s.downcase - if VCS_PATH.executable? - puts "alias #{n}=#{VCS_PATH}-#{n} ;" - else - puts "alias #{n}=vcs-#{n} ;" - end - end - exit - end - - opts.on('-C', '--[no-]check', 'Check your vcs configuration') do |check| - if check - Vcs.new('vcs').call_conf_checkers - exit - end - end - - opts.on('-d', '--debug LEVEL', 'Set debug level') do |severity| - LOG.level = Logger.const_get(severity.upcase) - end - - opts.on_tail('-h', '--help', 'Show this message') do |test| - STDERR.puts OPTS - STDERR.puts "\n\nReport bugs to <ertai@lrde.epita.fr>." - exit - end - - opts.on_tail('--version', 'Show version') do - STDOUT.puts "Vcs version: #{VCS_VERSION}" - exit - end - -end - -ENV['LC_ALL'] = 'C' -if ENV.has_key? 'FULLNAME' - FULLNAME = ENV['FULLNAME'] -else - require 'etc' - ENV['FULLNAME'] = (Etc.getpwnam ENV['USER']).gecos - LOG.warn "Need FULLNAME in the environement (default: #{ENV['FULLNAME']})" -end -env = %w[ EMAIL FULLNAME EDITOR PAGER ] -if env.all? { |s| ENV[s] } - EDITOR = ENV['EDITOR'].to_cmd - PAGER = ENV['PAGER'].to_cmd - EMAIL = ENV['EMAIL'] -else - env.each { |s| LOG.error "Need #{s} in the environement" unless ENV[s] } - exit -end -FULL_EMAIL = "#{FULLNAME} <#{EMAIL}>" - -def main - vcs = nil - begin - OPTS.parse!(ARGV) - unless OPTIONS.has_key? :vcs_type - STDERR.puts OPTS - STDERR.puts "\nSpecify at least a Vcs type, or use a wrapped command\n" + - 'like svn, cvs, prcs in the vcs bin directory.' - exit - end - vcs = ALL_VCS_BY_NAME[OPTIONS[:vcs_type]].new - - if ARGV.empty? - meth = :help! - else - meth = ARGV.shift.sub(/([^!])$/, '\1!') - end - - vcs.send(meth, *ARGV) - - rescue Exception - vcs.call_handlers unless vcs.nil? - LOG.debug { raise } - LOG.error $!.to_s.sub(/\.$/, '') unless $!.to_s == 'exit' - end -end - -main() +VcsApp.new(__FILE__).run Index: vcs/lib/vcs/app.rb --- vcs/lib/vcs/app.rb (revision 0) +++ vcs/lib/vcs/app.rb (revision 0) @@ -0,0 +1,114 @@ +# Copyright:: Copyright (c) 2005 LRDE. All rights reserved. +# Author:: Nicolas Pouillard <ertai@lrde.epita.fr>. +# License:: Gnu General Public License. +# Revision:: $Id$ + +require 'pathname' +require 'logger' + +LOG = Logger.new(STDOUT) +if severity = ENV['VCS_SEVERITY'] + LOG.level = Logger.const_get(severity.upcase) +else + LOG.level = Logger::INFO +end +def LOG.format_message(severity, timestamp, msg, progname) + progname += ': ' unless progname.nil? or progname.empty? + "vcs: #{progname}#{severity.downcase}: #{msg}\n" +end + + +# vcs: The version control system wrapper. +# +# Vcs is a wrapper over any version control system. +class VcsApp + + @@vcs = Pathname.new(__FILE__).expand_path.dirname + @@dir = @@vcs.parent + require @@vcs + 'vcs' unless defined? Vcs + @@dir.load_path! + + cattr_accessor :all_vcs + cattr_accessor :by_name + cattr_accessor :dir + cattr_accessor :path + + dir = Pathname.pwd + while not dir.root? and not (vcs_dir = dir + 'vcs').exist? + dir = dir + '..' + end + vcs_dir = vcs_dir.expand_path + vcs_dir.load_path! + + HOME = ENV['HOME'].to_path + vcs_user_conf = HOME/'.vcsrc' + if vcs_user_conf.exist? + Vcs.user_conf = OpenStruct.new(YAML.load(vcs_user_conf.read)) + end + + Pathname.glob("{#{@@vcs},#{vcs_dir}}/*.rb") do |file| + next if file.to_s =~ /\/(app|opt_parse|vcs)\.rb$/ + LOG.debug { file.basename.to_s } + # begin + # require "vcs/#{file.basename}" + # p "eheho" + # rescue LoadError + # begin + require file.to_s + # rescue LoadError => ex + # raise ex + # end + # end + end + + @@all_vcs ||= [] + @@by_name ||= {} + + if @@all_vcs.empty? + ObjectSpace.each_object(Class) do |aClass| + if aClass != Vcs and aClass.ancestors.include? Vcs + name = aClass.to_s.to_sym + @@all_vcs << name + @@by_name[name.to_s.downcase] = aClass + end + end + end + + require 'vcs/opt_parse' + + @@parser = Vcs::OptParse.new + + def initialize ( __file__, vcs_type=nil ) + @@path = __file__ + Vcs.default ||= vcs_type + end + + def run + vcs = nil + begin + @@parser.parse! ARGV + if Vcs.default.nil? + STDERR.puts @@parser + STDERR.puts "\nSpecify at least a Vcs type, or use a wrapped command\n" + + 'like svn, cvs, prcs in the vcs bin directory.' + exit + end + vcs = @@by_name[Vcs.default.to_s.downcase].new + + if ARGV.empty? + meth = :help! + else + meth = ARGV.shift.sub(/([^!])$/, '\1!') + end + + vcs.send(meth, *ARGV) + + rescue Exception + vcs.call_handlers unless vcs.nil? + LOG.debug { raise } + LOG.error $!.to_s.sub(/\.$/, '') unless $!.to_s == 'exit' + end + end + +end # class VcsApp + Property changes on: vcs/lib/vcs/app.rb ___________________________________________________________________ Name: svn:keywords + Id Index: vcs/lib/vcs/environment.rb --- vcs/lib/vcs/environment.rb (revision 0) +++ vcs/lib/vcs/environment.rb (revision 0) @@ -0,0 +1,58 @@ +# Copyright:: Copyright (c) 2005 LRDE. All rights reserved. +# Author:: Nicolas Pouillard <ertai@lrde.epita.fr>. +# License:: Gnu General Public License. +# Revision:: $Id$ + +class Vcs + + class << self + + def fullname + env('FULLNAME') { Etc.getpwnam(user).gecos } + end + + def editor + env('EDITOR').to_cmd + end + + def pager + env('PAGER').to_cmd + end + + def full_email + "#{fullname} <#{email}>" + end + + def method_missing ( meth ) + env meth.to_s.upcase + end + + @@vars ||= {} + @@env_status ||= true + + def env ( name, &block ) + return @@vars[name] if @@vars.has_key? name + @@vars[name] = + if var = ENV[name] + var + elsif block.nil? + LOG.error "Need #{name} in the environement" + @@env_status = false + "!!! #{name} not set !!!" + else + default = block[] + LOG.warn "Need #{name} in the environement (default: #{default})" + default + end + end + + def check_env + %w[ email fullname editor pager ].each { |m| send(m) } + end + + end # class << self + + add_conf_checker :check_env + +end # class Vcs + Property changes on: vcs/lib/vcs/environment.rb ___________________________________________________________________ Name: svn:keywords + Id Index: vcs/bin/vcs-cvs --- vcs/bin/vcs-cvs (revision 227) +++ vcs/bin/vcs-cvs (working copy) @@ -1,10 +1,9 @@ #!/usr/bin/env ruby -# Author: Nicolas Pouillard <ertai@lrde.epita.fr>. -# Copyright: Copyright (c) 2004 LRDE. All rights reserved. -# License: GNU General Public License (GPL). +# Author:: Nicolas Pouillard <ertai@lrde.epita.fr>. +# Copyright:: Copyright (c) 2004 LRDE. All rights reserved. +# License:: GNU General Public License (GPL). +# Revision:: $Id: header 98 2004-09-29 12:07:43Z ertai $ -# $LastChangedBy: ertai $ -# $Id: header 98 2004-09-29 12:07:43Z ertai $ +require File.join(File.dirname(__FILE__), '..', 'lib', 'vcs', 'app') unless defined? VcsApp -VCS='Cvs' -exec("vcs --vcs #{VCS} -- '#{ARGV.join(%q[' '])}'") +VcsApp.new(__FILE__, :cvs).run Index: vcs/bin/vcs-prcs --- vcs/bin/vcs-prcs (revision 227) +++ vcs/bin/vcs-prcs (working copy) @@ -1,10 +1,9 @@ #!/usr/bin/env ruby -# Author: Nicolas Pouillard <ertai@lrde.epita.fr>. -# Copyright: Copyright (c) 2004 LRDE. All rights reserved. -# License: GNU General Public License (GPL). +# Author:: Nicolas Pouillard <ertai@lrde.epita.fr>. +# Copyright:: Copyright (c) 2004 LRDE. All rights reserved. +# License:: GNU General Public License (GPL). +# Revision:: $Id$ -# $LastChangedBy: ertai $ -# $Id: header 98 2004-09-29 12:07:43Z ertai $ +require File.join(File.dirname(__FILE__), '..', 'lib', 'vcs', 'app') unless defined? VcsApp -VCS='Prcs' -exec('vcs', '--vcs', VCS, '--', *ARGV) +VcsApp.new(__FILE__, :prcs).run Index: vcs/bin/vcs-svn --- vcs/bin/vcs-svn (revision 227) +++ vcs/bin/vcs-svn (working copy) @@ -1,10 +1,9 @@ #!/usr/bin/env ruby -# Author: Nicolas Pouillard <ertai@lrde.epita.fr>. -# Copyright: Copyright (c) 2004 LRDE. All rights reserved. -# License: GNU General Public License (GPL). +# Author:: Nicolas Pouillard <ertai@lrde.epita.fr>. +# Copyright:: Copyright (c) 2004 LRDE. All rights reserved. +# License:: GNU General Public License (GPL). +# Revision:: $Id$ -# $LastChangedBy: ertai $ -# $Id: header 98 2004-09-29 12:07:43Z ertai $ +require File.join(File.dirname(__FILE__), '..', 'lib', 'vcs', 'app') unless defined? VcsApp -VCS='Svn' -exec('vcs', '--vcs', VCS, '--', *ARGV) +VcsApp.new(__FILE__, :svn).run Index: vcs/lib/vcs/opt_parse.rb --- vcs/lib/vcs/opt_parse.rb (revision 0) +++ vcs/lib/vcs/opt_parse.rb (revision 0) @@ -0,0 +1,77 @@ +# Author:: Nicolas Pouillard <ertai@lrde.epita.fr>. +# Copyright:: Copyright (c) 2005 LRDE. All rights reserved. +# License:: GNU General Public License (GPL). +# Revision:: $Id$ + + +class Vcs + + class OptParse + + def initialize + @option_parser = @@option_parser.dup + end + + def parse! ( argv ) + @option_parser.parse! argv + end + + def parse ( argv ) + @option_parser.parse argv + end + + @@option_parser = OptionParser.new do |o| + + o.banner = "Usage: vcs [options] <file>*" + o.separator '' + + o.on('-c', '--vcs TYPE', VcsApp.all_vcs, 'Set your vcs') do |aVcs| + p aVcs + Vcs.default = aVcs + end + + o.on('-l', '--vcs-list', 'List all vcs') do |aVcs| + STDERR.puts 'Vcs list:' + VcsApp.all_vcs.each { |n| STDERR.puts " - #{n}" } + exit + end + + o.on('--mk-alias', 'Put the result of this command in your .zshrc or .bashrc') do + VcsApp.all_vcs.each do |n| + n = n.to_s.downcase + if VcsApp.path.executable? + puts "alias #{n}=#{VcsApp.path}-#{n} ;" + else + puts "alias #{n}=vcs-#{n} ;" + end + end + exit + end + + o.on('-C', '--[no-]check', 'Check your vcs configuration') do |check| + if check + Vcs.new('vcs').call_conf_checkers + exit + end + end + + o.on('-d', '--debug LEVEL', 'Set debug level') do |severity| + LOG.level = Logger.const_get(severity.upcase) + end + + o.on_tail('-h', '--help', 'Show this message') do |test| + STDERR.puts o + STDERR.puts "\n\nReport bugs to <ertai@lrde.epita.fr>." + exit + end + + o.on_tail('--version', 'Show version') do + STDOUT.puts "Vcs version: #{Vcs.version}" + exit + end + + end + + end # class OptParse + +end # class Vcs Property changes on: vcs/lib/vcs/opt_parse.rb ___________________________________________________________________ Name: svn:keywords + Id
participants (1)
-
Nicolas Pouillard