sshfs-mapper: new ruby-based sshfs-mapper.

git-svn-id: https://websvn.glenux.net/svn/Upoc/sshfs-mapper/trunk@1327 eaee96b3-f302-0410-b096-c6cfd47f7835
This commit is contained in:
Glenn Y. Rolland 2009-04-03 16:27:49 +00:00
parent 652af233c8
commit 9fecc11be2
7 changed files with 320 additions and 0 deletions

View file

@ -2,9 +2,13 @@
BINDIR=$(DESTDIR)/usr/bin BINDIR=$(DESTDIR)/usr/bin
MANDIR=$(DESTDIR)/usr/share/man MANDIR=$(DESTDIR)/usr/share/man
DOCDIR=$(DESTDIR)/usr/share/doc/sshfs-mapper DOCDIR=$(DESTDIR)/usr/share/doc/sshfs-mapper
all: all:
racc -v -o mapparser.rb mapparser.y
cat mapparser.output
clean: clean:
rm -f mapparser.rb
install: install:
mkdir -p $(BINDIR) mkdir -p $(BINDIR)

101
config.rb Normal file
View file

@ -0,0 +1,101 @@
#!/usr/bin/ruby
require 'optparse'
require 'ostruct'
require 'pp'
require 'find'
class Config
attr_reader :options
def initialize()
user = if ENV['USER'] then
ENV['USER']
else
raise "Environment variable 'USER' is missing!"
end
home_dir = if ENV['HOME'] then
ENV['HOME']
else
"/home/" + user
end
xdg_dir = if ENV['XDG_CONFIG_HOME'] then
ENV['XDG_CONFIG_HOME']
else
home_dir + '/.config'
end
@options = OpenStruct.new( {
:config_dir => xdg_dir + '/sshfs-mapper',
:map_list => [],
:initialize_enable => false,
:umount_enable => false,
:target => nil,
:verbose_enable => false
} )
end
def parseFile
puts "Parsing #{@options.config_dir}/config"
puts "Parsing maps..."
Find.find( @options.config_dir ) do |path|
if File.file?( path )
if File.basename( path ) =~ /.map$/
puts "* #{File.basename( path )}"
else
Find.prune # Don't look any further into this way
end
#total_size += FileTest.size(path)
end
end
end
def parseCmd( args )
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options]"
opts.separator ""
opts.separator "Specific options:"
opts.on('-t', '--target TARGET', 'Mount only specified target') do |target|
@options.resize_enable = true
@options.resize_width = resizeX.to_i
@options.resize_height = resizeY.to_i
end
opts.on('-u', '--umount', 'Umount') do |umount|
@options.umount_enable = umount
end
opts.on('-i', '--initialize',
'Populate default configuration and example map' ) do |init|
@options.initialize_enable = init
end
opts.on('-v', '--[no-]verbose', 'Run verbosely' ) do |verbose|
@options.verbose_enable = verbose
end
end
begin
opts.parse!( args )
rescue OptionParser::ParseError => e
puts e.message
exit 1
end
@options
end
def to_s
s = []
s << "config_file = #{@options.config_file}"
s << "verbose_enable = #{@options.verbose_enable}"
s.join("\n")
end
end

33
demo.map Normal file
View file

@ -0,0 +1,33 @@
server "daneel.glenux.net" {
user "glenux";
port 22;
automount true; // default
compress true; // default
map "Media" {
user "glenux";
remote "/opt/media";
};
map "MediaSafe" {
user $USER;
remote "/home/media";
};
map "Documents" {
user $USER;
remote "/home/$USER";
};
map "Extra" {
user $USER;
remote "/opt
};
map "DocumentsPapa" {
user papa;
remote "/home/papa"
};
};

6
map.rb Normal file
View file

@ -0,0 +1,6 @@
class Map
def initialize
end
end

143
mapparser.y Normal file
View file

@ -0,0 +1,143 @@
class MapParser
rule
commands
: commands command SEMICOLON { puts "GOT A COMMAND" }
| /* none */ { result = 0 }
command
: global_blk
| server_blk
server_blk
: SERVER SEP STRING SEP OBRACE server_opts EBRACE
: SERVER SEP STRING OBRACE server_opts EBRACE
global_blk
: GLOBAL OBRACE global_opts EBRACE
server_opts
: server_opts server_opt SEMICOLON
| /* none */ { result = 0 }
server_opt
: user_opt
| port_opt
| automount_opt
| compress_opt
| reconnect_opt
user_opt
: USER QUOTE STRING QUOTE
port_opt
: PORT INTEGER
automount_opt
: AUTOMOUNT boolean { puts ( "AUTOMOUNT => " + val[1] ) }
compress_opt
: COMPRESS boolean { puts ( "COMPRESS => " + val[1] ) }
reconnect_opt
: RECONNECT boolean { puts ( "RECONNECT => " + val[1] ) }
boolean
: BOOL_TRUE { result = true }
| BOOL_FALSE { result = false }
end
---- header ----
# calc.rb : generated by racc
#
require 'strscan'
#require 'map.rb'
---- inner ----
def parse( str )
puts "parse start..."
tokens = []
scanner = StringScanner.new( str )
puts ( "scanner?" + scanner.string )
until scanner.eos?
puts "scanning.. at #{scanner.pos}"
case
when m = scanner.scan( /\/\/.*$/ )
# comments
tokens.push [:SEP, m]
when m = scanner.scan( /(\s+|\n)/ )
# whitespace and newlines
tokens.push [:SEP, m]
when m = scanner.scan( /;/ )
tokens.push [:SEMICOLON, m]
when m = scanner.scan( /\d+/ )
tokens.push [:NUMBER, m]
when m = scanner.scan( /\{/ )
tokens.push [:OBRACE, m]
when m = scanner.scan( /\}/ )
tokens.push [:EBRACE, m]
when m = scanner.scan( /user/i )
tokens.push [:USER, m]
when m = scanner.scan( /automount/i )
tokens.push [:AUTOMOUNT, m]
when m = scanner.scan( /reconnect/i )
tokens.push [:RECONNECT, m]
when m = scanner.scan( /compress/i )
tokens.push [:COMPRESS, m]
when m = scanner.scan( /port/i )
tokens.push [:PORT, m]
when m = scanner.scan( /(true|1|yes)/i )
tokens.push [:BOOL_TRUE, m]
when m = scanner.scan( /(false|0|no)/i )
tokens.push [:BOOL_FALSE, m]
when m = scanner.scan( /".+?"/i )
tokens.push [:STRING, m]
when m = scanner.scan( /map/i )
tokens.push [:MAP, m]
when m = scanner.scan( /server/i )
tokens.push [:SERVER, m]
when m = scanner.scan( /./ )
tokens.push [:UNMATCHED, m]
puts "UNMATCHED #{m} after #{scanner.pre_match}"
end
end
puts "tokenization ok"
tokens.push [false, false]
@last_value = yyparse( tokens, :each )
puts "parse end... "
end
def on_error(error_token_id, error_value, value_stack)
msg = "parse error "
msg << "after #{value_stack.last} " if value_stack.length > 1
#msg << "after #{value_stack.last} " unless value_stack.empty?
msg << "on #{token_to_str(error_token_id)} #{error_value}"
raise ParseError, msg
end
---- footer ----
puts "GOO"
bigstr = ""
STDIN.read.split(/\n/).each do |a|
puts "READING #{a}"
bigstr = bigstr + a
end
puts "PARSING NOW..."
parser = MapParser.new
begin
val = parser.parse( bigstr )
print '= ', val, "\n"
rescue ParseError
puts $!
end

0
sshfs-mapper Executable file → Normal file
View file

33
sshfs-mapper.rb Executable file
View file

@ -0,0 +1,33 @@
#!/usr/bin/ruby
require 'config'
class Map
def initialize()
end
def self.loadFromFile( filename )
end
end
class Config
end
class SshfsMapper
def initialize()
puts "-- sshfs-mapper --"
conf = Config.new
conf.parseCmd ARGV
conf.parseFile
puts conf
end
end
SshfsMapper.new