rwdmovies 0.90 → 0.91

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/Readme.txt +379 -80
  2. data/code/superant.com.rwdmovies/amazonsearch.rb +2 -2
  3. data/code/superant.com.rwdtinkerbackwindow/controlclient.rb +25 -26
  4. data/code/superant.com.rwdtinkerbackwindow/helptexthashtinkerwin2.rb +29 -5
  5. data/code/superant.com.rwdtinkerbackwindow/installapplet.rb +6 -3
  6. data/code/superant.com.rwdtinkerbackwindow/installgemapplet.rb +6 -7
  7. data/code/superant.com.rwdtinkerbackwindow/installremotegem.rb +19 -0
  8. data/code/superant.com.rwdtinkerbackwindow/listgemdirs.rb +12 -0
  9. data/code/superant.com.rwdtinkerbackwindow/listgemzips.rb +2 -2
  10. data/code/superant.com.rwdtinkerbackwindow/listzips.rb +22 -7
  11. data/code/superant.com.rwdtinkerbackwindow/openhelpwindowtinkerwin2.rb +3 -0
  12. data/code/superant.com.rwdtinkerbackwindow/remotegemlist.rb +24 -0
  13. data/code/superant.com.rwdtinkerbackwindow/removeapplet.rb +10 -6
  14. data/code/superant.com.rwdtinkerbackwindow/saveconfigurationrecord.rb +1 -1
  15. data/configuration/language.dist +7 -0
  16. data/configuration/rwdapplicationidentity.dist +3 -0
  17. data/configuration/{rwdmovies.cnf → rwdmovies.dist} +4 -1
  18. data/configuration/{rwdtinker.cnf → rwdtinker.dist} +3 -4
  19. data/configuration/tinkerwin2variables.dist +17 -0
  20. data/extras/rconftool.rb +380 -0
  21. data/extras/zip/ioextras.rb +114 -0
  22. data/extras/zip/stdrubyext.rb +111 -0
  23. data/extras/zip/tempfile_bugfixed.rb +195 -0
  24. data/extras/zip/zip.rb +1377 -0
  25. data/extras/zip/zipfilesystem.rb +558 -0
  26. data/extras/zip/ziprequire.rb +61 -0
  27. data/gui/frontwindow0/superant.com.rwdmovies/2openmoviephoto.rwd +6 -4
  28. data/gui/tinkerbackwindows/superant.com.tinkerbackwindow/40rwdlistzips.rwd +9 -10
  29. data/gui/tinkerbackwindows/superant.com.tinkerbackwindow/45installremotezip.rwd +44 -0
  30. data/init.rb +42 -37
  31. data/rwd_files/HowTo_Movies.txt +6 -0
  32. data/rwd_files/HowTo_Tinker.txt +25 -1
  33. data/rwd_files/moviestmp.jpg +0 -0
  34. data/rwdconfig.dist +6 -0
  35. data/rwdmovies-0.91.gem +0 -0
  36. data/tests/checkdepends.sh +4 -0
  37. data/tests/cleancnf.sh +5 -0
  38. data/tests/makedist.rb +29 -0
  39. data/tests/rdep.rb +354 -0
  40. metadata +26 -21
  41. data/configuration/language.cnf +0 -5
  42. data/configuration/rwdapplicationidentity.cnf +0 -3
  43. data/configuration/rwdmoviesversion.cnf +0 -2
  44. data/configuration/rwdtinkerversion.cnf +0 -2
  45. data/configuration/tinkerwin2variables.cnf +0 -8
  46. data/configuration/tinkerwin2version.cnf +0 -3
  47. data/installed/rwdtinkerwin2-0.5.inf +0 -8
  48. data/lang/alanguagehashbegin.rb +0 -4
  49. data/lang/languagehash.rb +0 -4
  50. data/lang/templangfile.rb +0 -16
  51. data/lang/vlanguagehashend.rb +0 -6
  52. data/lang/wlocallangstart.rb +0 -5
  53. data/lang/xlocallangfile.rb +0 -16
  54. data/lang/zlocallangend.rb +0 -2
  55. data/zips/rwdwcalc-0.50.zip +0 -0
@@ -1,10 +1,10 @@
1
1
 
2
2
 
3
- require 'amazon/search'
4
3
 
4
+ require 'amazon/search'
5
5
  include Amazon::Search
6
- def runmoviesearch
7
6
 
7
+ def runmoviesearch
8
8
  req = Request.new(DEV_TOKEN)
9
9
  testmovie = @a_moviefilename
10
10
  s = ['keyword_search', "#{testmovie}", 'video', LITE, 'Name']
@@ -1,28 +1,22 @@
1
- #
1
+ # Bind message passing socket to rwdtinker application
2
2
 
3
3
  def runcontrolcommand
4
4
  require 'socket'
5
5
 
6
6
  # Change this line...
7
- ip = '127.0.0.1'
7
+ ip = '127.0.0.1'
8
8
  # And this one...
9
- port = $rwdcontrolport1
10
- if @a_remoteportinput
11
- port = @a_remoteportinput.to_i
12
- end
13
- # Done!
9
+ port = $rwdcontrolport1
10
+ if @a_remoteportinput
11
+ port = @a_remoteportinput.to_i
12
+ end
14
13
 
15
-
16
14
  commandtext = @a_remotecommandinput.to_s
17
-
18
- remotehostnotfound = true
19
-
20
- client = UDPSocket::new()
21
- client.send(commandtext, 0, ip, port)
22
-
23
-
24
- if (commandtext == "stop")
25
- begin
15
+ remotehostnotfound = true
16
+ if (commandtext == "stop")
17
+ begin
18
+ client = UDPSocket::new()
19
+ client.send(commandtext, 0, ip, port)
26
20
  @socket = UDPSocket::new()
27
21
  @socket.bind(ip, port)
28
22
  # Rescue the "Address in use" error
@@ -51,17 +45,20 @@ begin
51
45
  @remotecommandresult = "Remote Host not running"
52
46
  end
53
47
  @socket.close
54
- end
48
+ # Close the socket after use
49
+ client.close()
50
+ end
55
51
 
56
- remotehostnotfound = true
57
- if (commandtext == "query")
58
- begin
52
+ remotehostnotfound = true
53
+ if (commandtext == "query")
54
+ begin
55
+ client = UDPSocket::new()
56
+ client.send(commandtext, 0, ip, port)
59
57
  @socket = UDPSocket::new()
60
58
  @socket.bind(ip, port)
61
59
  # Rescue the "Address in use" error
62
60
  rescue Errno::EADDRINUSE
63
61
 
64
-
65
62
  databack, info = client.recv(128)
66
63
  @remotecommandresult = databack
67
64
  @lastremotecommand = "rwdtinker remote: Port #{port} on host #{ip} "
@@ -82,15 +79,17 @@ begin
82
79
  # Rescue all other errors
83
80
  end
84
81
  @socket.close
85
- end
86
- if remotehostnotfound
87
- @remotecommandresult = "Remote Host not running"
88
- end
82
+
89
83
  # Close the socket after use
90
84
  client.close()
85
+ end
91
86
 
87
+ if remotehostnotfound
88
+ @remotecommandresult = "Remote Host not running"
89
+ end
92
90
  end
93
91
 
92
+ # list the remote control options on the screen
94
93
  def showremoteportoptions
95
94
 
96
95
  @remoteportoptions = $rwdcontrolports.rwd_options
@@ -1,11 +1,16 @@
1
-
1
+ # help text files used by context sensitive help
2
2
 
3
3
  Helptext.update( :tinkerwin2_help => " You can view the list of applets you can install
4
4
  by clicking on the list files button
5
5
 
6
6
  You can download an applet created by someone else
7
7
 
8
- put the file in the zips directory",
8
+ put the file in the zips directory
9
+
10
+ rwdtinker applets packaged as RubyGems can be used if you have RubyGems installed.
11
+ You can download rwdtinker gems at the 'Install Remote Applets' tab
12
+ to install applets you need the ruby zlib library installed
13
+ ",
9
14
  :applet_viewing => "You can get a list of all installed applets by clicking the 'listfiles button'
10
15
  To see the text of a install file
11
16
  Type in the name of the file you want.
@@ -28,10 +33,29 @@
28
33
  chose the applet you want\n
29
34
  click on 'install applet'\n
30
35
  If all goes well the applet should be installed.\n
31
- You will need to restart rwdtinker to see the new screens",
36
+ You will need to restart rwdtinker to see the new screens
37
+ to install applets you need the ruby zlib library installed",
38
+
32
39
  :applet_tinkerbackeditconfiguration => " You can edit the configuration
33
40
 
34
41
  Remember to reload the configuration variables if you want your
35
- changes used."
36
-
42
+ changes used.",
43
+ :remotegemapplet_install => " To download and install use the 'Install Remote Applets'
44
+
45
+ First click on 'List applets in the Remote GEM Repository available for downloading'
46
+ then go to the very bottom to click on the applet you want
47
+ then on the top click 'install remote Gem applet package'
48
+
49
+ This installs the package in the Gem system
50
+ You still need to install it into rwdtinker - do that in the 'List Zips' tab
51
+
52
+ For this feature to work, you need
53
+ RubyGems installed
54
+ Variables set correctly in the tinerwin2.cnf file
55
+ $xpcommand, $gemdirectory_withgemfiles and $geminstalled_directory
56
+ the defaults as shipped will normally be for a Debian Linux system
57
+ On Debian Linux, you would usually need to be running rwdtinker as root in order to install RubyGems
58
+
59
+ You can view the list of rwdtinker applets you have installed already by clicking 'view already installed GEM applets'
60
+ "
37
61
  )
@@ -1,9 +1,12 @@
1
+ # code in install rwdtinker applets - now uses ruby.zip module
1
2
  def installapplet
2
- require 'fileutils'
3
- require 'extras/zip/zip'
3
+
4
4
  nametext = "%s" % [@a_installapplet]
5
-
5
+
6
6
  begin # exception trapped block
7
+
8
+
9
+ require 'extras/zip/zip'
7
10
  fullname = nametext + ".zip"
8
11
  fileName = File.join($zipslocation,fullname)
9
12
 
@@ -1,3 +1,4 @@
1
+ # install rwdtinker applet from a long directory location
1
2
  def installgemapplet
2
3
  require 'fileutils'
3
4
  require 'extras/zip/zip'
@@ -6,16 +7,14 @@
6
7
  begin # exception trapped block
7
8
  fullname = nametext + ".zip"
8
9
 
9
-
10
- zf = Zip::ZipFile.new(fullname)
11
- zf.sort.each {
12
- |entry|
13
- zf.extract(entry.to_s, entry.to_s)
10
+ zf = Zip::ZipFile.new(fullname)
11
+ zf.sort.each {
12
+ |entry|
13
+ zf.extract(entry.to_s, entry.to_s)
14
14
  }
15
- @installapplettext = Message[:applet_installed]
15
+ @installapplettext = Message[:applet_installed]
16
16
 
17
17
  rescue
18
18
  @installapplettext = "unzip error - applet may not be installed correctly"
19
19
  end # exception rescue
20
-
21
20
  end
@@ -0,0 +1,19 @@
1
+ # download gem of rwdtinker applet
2
+ def superantcominstallremotegemapplet
3
+ require 'fileutils'
4
+
5
+ tempnametext = "%s" % [@superantcominstallremoteappletinput]
6
+ temparray = tempnametext.split(/ /)
7
+ nametext = temparray[0]
8
+ begin # exception trapped block
9
+
10
+
11
+ commandline = $xpcommand + "gem install " + nametext
12
+
13
+ @superantcominstallremotegemappletresult = `#{commandline}`
14
+
15
+ rescue
16
+ @superantcominstallremotegemappletresult = "error during gem install!"
17
+ end # exception rescue
18
+
19
+ end
@@ -0,0 +1,12 @@
1
+ # this code is to list the gem directories that are installed
2
+ def superantcomshowgemfiledirs
3
+
4
+ fileList = Dir.new($gemdirectory_withgemfiles).entries.sort.delete_if { |x| ! (x =~ /\Arwdzip/) }
5
+ @superantcomshowgemappletdirsresult = fileList.rwd_method("superantcomshowgemappletdirsname")
6
+ end
7
+
8
+ def superantcomshowgemappletdirsname(inffile)
9
+ applet = inffile
10
+ @a_gemfiledirsinput = applet
11
+
12
+ end
@@ -1,7 +1,7 @@
1
1
  # this code is to list the zips
2
2
 
3
3
 
4
- def fillhypernotename(inffile)
4
+ def fillzipappletname(inffile)
5
5
  applet = inffile.gsub(/\.zip$/, "")
6
6
  @a_installapplet = applet
7
7
 
@@ -50,5 +50,5 @@ def listgemzips
50
50
  findzipfiles(x + "/**")
51
51
  end
52
52
  end
53
- @zipslistresult = $zipsarray.rwd_method("fillhypernotename")
53
+ @zipslistresult = $zipsarray.rwd_method("fillzipappletname")
54
54
  end
@@ -1,16 +1,31 @@
1
1
  # this code is to list the zipped applets that can be installed
2
- def listzipfilestoinstall
3
- if File.exist?($zipslocation)
4
- fileList = Dir.new($zipslocation).entries.sort.delete_if { |x| ! (x =~ /zip$/) }
5
- @zipslistresult = fileList.rwd_method("fillziptoinstallname")
6
- end
7
- end
2
+
8
3
 
9
4
  def fillziptoinstallname(inffile)
10
5
  applet = inffile.gsub(/\.zip$/, "")
11
6
  @a_installapplet = applet
12
7
 
13
8
  end
9
+
14
10
 
15
11
 
16
-
12
+
13
+ def listzipfilestoinstall
14
+ startlangdir = File.join($progdir, $zipslocation )
15
+ #get a list of the files and subdirectories on the starting directory only
16
+
17
+
18
+ a = Array.new(Dir["#{startlangdir}/*"].entries.sort)
19
+
20
+ #loop through and get the files names
21
+ a.each do |x|
22
+ # only zip files
23
+ if x =~ /\.zip/
24
+ # puts "#{x}" #adds the file to the array to display on the screen
25
+ $zipsarraylocal = $zipsarraylocal | ["#{x}"]
26
+ end
27
+
28
+ end
29
+
30
+ @zipslistresult = $zipsarraylocal.rwd_method("fillziptoinstallname")
31
+ end
@@ -24,6 +24,9 @@ def runhelpwindowtinkerwin2
24
24
  $help_topic = "remote_control"
25
25
 
26
26
  end
27
+ if @rwd_tab == "superantcomremotezipsinstall"
28
+ $help_topic = "remotegemapplet_install"
29
+ end
27
30
 
28
31
 
29
32
  if @rwd_window != "rwdtinkerhelpwindow"
@@ -0,0 +1,24 @@
1
+ # This tinker method is to retrieve the list of rwdzip applets available to install from the remote repository
2
+
3
+ def superantcomremotegemlist
4
+ gemcommandoption = "search --remote "
5
+ gemfilename = "rwdzip"
6
+ commandtext = "gem search --remote rwdzip"
7
+ commandtemp = $xpcommand + commandtext
8
+ begin # exception trapped block
9
+
10
+ tempremotegemappletsresult = `#{commandtemp} 2>&1`
11
+ @superantcomremotegemappletsfullresult = tempremotegemappletsresult
12
+ fileList = tempremotegemappletsresult.split(/\n/).delete_if { |x| ! (x =~ /\Arwdzip/) }
13
+
14
+ @superantcomremotegemappletsresult = fileList.rwd_method("superantcomremotegemappletdirsname")
15
+ rescue SystemCallError, StandardError
16
+ @superantcomremotegemappletsresult = "system call error: " + $!
17
+ end # exception rescue
18
+ end
19
+
20
+ def superantcomremotegemappletdirsname(inffile)
21
+ applet = inffile
22
+ @superantcominstallremoteappletinput = applet
23
+
24
+ end
@@ -10,19 +10,23 @@
10
10
 
11
11
  fd.each { |oneline|
12
12
  if count == 1
13
- commandline = "rm -r " + oneline
14
- system( commandline+' > temp.output' )
13
+ if File.exist?(oneline.chop)
14
+ FileUtils.rm_rf( oneline.chop)
15
+ end
16
+
15
17
  end
16
18
  count = 1
17
19
  }
18
20
  fd.close
19
- commandline = "rm " + fileName
21
+
22
+ FileUtils.rm_rf( fileName)
23
+
20
24
 
21
- system(commandline)
22
25
  @removeapplettext = "applet files removed! - restart rwd to finish uninstalling"
23
26
 
24
27
  rescue
25
- @removeapplettext = "\n" + "error"
26
- end # exception rescue
28
+ @removeapplettext = "\n" + "error"
29
+
30
+ end # exception rescue
27
31
 
28
32
  end
@@ -1,4 +1,4 @@
1
- # this method adds a new address record
1
+ # this method saves the configuration record
2
2
  def saveconfigurationrecord
3
3
  require 'fileutils'
4
4
  if @a_configurationfilename
@@ -0,0 +1,7 @@
1
+ ##VERSION:1.57
2
+ #language selection file
3
+ ##NAME:$lang:0
4
+ #$lang = "jp"
5
+ $lang= "en"
6
+ #$lang= "nl"
7
+ #$lang= "es"
@@ -0,0 +1,3 @@
1
+ ##VERSION:1.57
2
+ $rwdapplicationidentity = "rwdtinker main application"
3
+
@@ -1,7 +1,10 @@
1
-
1
+ ##VERSION:0.91
2
+ ##NAME:$rwdmovies_directory:0
2
3
  $rwdmovies_directory="movies"
4
+ ##NAME:$tmpmoviepicture_directory:0
3
5
  $tmpmoviepicture_directory = "/tmp"
4
6
 
5
7
 
6
8
  DEV_TOKEN = "D23XFCO2UKJY82" # your development token
7
9
 
10
+ RwdMoviesVersion = "0.91"
@@ -1,12 +1,9 @@
1
-
1
+ ##VERSION:1.58
2
2
  ConfigurationDir = "configuration" # for use in program - init.rb has this value without using this constant
3
3
  CodeName = "rwdtinker"
4
4
  CodeNameFile = CodeName + ".rb"
5
- LangNameFile = "rwdlanguage.rb"
6
5
  RWDFile = "rwdtinker.rwd"
7
6
  LangDir = "lang"
8
- TempLangHashFile = LangDir + "/" + "templangfile.rb"
9
- LocalLangHashFile = LangDir + "/" + "xlocallangfile.rb"
10
7
  CodeDir = "code"
11
8
  GuiDir = "gui"
12
9
  $help_topic = "general_help"
@@ -14,3 +11,5 @@ LocalLangHashFile = LangDir + "/" + "xlocallangfile.rb"
14
11
  $prevouswindow ="main"
15
12
  $rwdcontrolports =["13713","13714","13715","13716","13717","13718"]
16
13
  $port = 7705
14
+
15
+ RwdTinkerVersion = "1.58"
@@ -0,0 +1,17 @@
1
+ ##VERSION:1.57
2
+ TinkerWin2ConfigurationFileName = "tinkerwin2variables.cnf" # this file name - do not change
3
+ ##NAME:$zipslocation:0
4
+ $zipslocation = "zips" # location of applets to add
5
+ $zipsarray = [""] ; $zipsarraylocal = [""]
6
+ ##NAME:$xpcommand:0
7
+ $xpcommand = " " # Linux machines
8
+ # $xpcommand = "cmd /C " # XP machines with cmd shell
9
+ ##NAME:$gemdirectory_withgemfiles:0
10
+ $gemdirectory_withgemfiles = "/usr/lib/ruby/gems/1.8/gems"
11
+ # $gemdirectory_withgemfiles = "c:\\ruby\\lib\\ruby\\gems\\1.8\\gems" # XP machine
12
+ ##NAME:$geminstalled_directory:0
13
+ # $geminstalled_directory = "/ruby/lib/ruby/gems/1.8/gems" # XP machine
14
+ $geminstalled_directory = "/usr/lib/ruby/gems/1.8/gems" # Debian Linux
15
+
16
+ RwdTinkerWin2Version = "0.8"
17
+
@@ -0,0 +1,380 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # Copyright (c) 2005 Brian Candler
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to
7
+ # deal in the Software without restriction, including without limitation the
8
+ # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9
+ # sell copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
+ # IN THE SOFTWARE.
22
+
23
+ ##########################################################################
24
+ # rconftool is a reimplementation of Sam Varshavchik's sysconftool in Ruby.
25
+ # See http://www.courier-mta.org/sysconftool/ for details of the original.
26
+ # Its purpose is to keep configuration files "fresh" when upgrading an
27
+ # application from one version to another, ensuring that all necessary
28
+ # settings are present and obsolete ones removed.
29
+ #
30
+ # rconftool can be called as a library function or from the command line. It
31
+ # can also install groups of files recursively from one directory tree into
32
+ # another.
33
+ ##########################################################################
34
+
35
+ require 'fileutils'
36
+
37
+ module Rconftool
38
+ VERSION = "0.1"
39
+ class NoVersionLine < RuntimeError; end
40
+
41
+ # This module function installs a single source (.dist) file to a target
42
+ # location, having first merged in any compatible settings from the
43
+ # target file if it existed previously [if it does not exist, any settings
44
+ # from 'oldfile' are used instead]
45
+ #
46
+ # If the distfile is not in sysconftool format (i.e. doesn't have a
47
+ # ##VERSION: header within the first 20 lines), then for safety it is only
48
+ # installed if the target file does not already exist. No attempt at data
49
+ # merging is made in that case.
50
+
51
+ def self.install(distfile, targetfile=nil, oldfile=nil, opt={})
52
+ debug = opt[:debug] || $stdout
53
+
54
+ targetfile ||= distfile
55
+ if opt[:strip_regexp]
56
+ targetfile = targetfile.sub(opt[:strip_regexp], '')
57
+ oldfile = oldfile.sub(opt[:strip_regexp], '') if oldfile
58
+ end
59
+ if opt[:add_suffix]
60
+ targetfile = targetfile + opt[:add_suffix]
61
+ oldfile = oldfile + opt[:add_suffix] if oldfile
62
+ end
63
+ raise Errno::EEXIST, "#{distfile}: dist and target filenames are the same" if distfile == targetfile
64
+
65
+ # Read in the source (.dist) file
66
+ begin
67
+ src = ConfigFile.new(distfile)
68
+ rescue NoVersionLine
69
+ # Fallback behaviour when installing a file which is not in sysconftool
70
+ # format: we install the file only if it doesn't already exist
71
+ if File.exist?(targetfile)
72
+ debug << "#{targetfile}: already exists, skipping\n"
73
+ return
74
+ end
75
+ return if opt[:noclobber]
76
+ copyfrom = (oldfile and File.exist?(oldfile)) ? oldfile : distfile
77
+ if File.symlink?(copyfrom)
78
+ File.symlink(File.readlink(copyfrom), targetfile)
79
+ debug << "#{targetfile}: symlink copied from #{copyfrom}\n"
80
+ else
81
+ FileUtils.cp copyfrom, targetfile, :preserve=>true
82
+ debug << "#{targetfile}: copied from #{copyfrom}\n"
83
+ end
84
+ return
85
+ end
86
+
87
+ # OK, so we have a sysconftool file to install. Read in the existing
88
+ # target file, or if that does not exist, the oldfile
89
+ begin
90
+ old = ConfigFile.new
91
+ old.read(targetfile)
92
+ rescue NoVersionLine
93
+ # That's OK; the old target will be renamed to .bak
94
+ rescue Errno::ENOENT
95
+ begin
96
+ target_missing = true
97
+ old.read(oldfile) if oldfile
98
+ rescue Errno::ENOENT, NoVersionLine
99
+ end
100
+ end
101
+
102
+ # Same VERSION? No merge is required
103
+ if src.version == old.version and not opt[:force]
104
+ if target_missing
105
+ FileUtils.cp oldfile, targetfile, :preserve=>true
106
+ debug << "#{targetfile}: same VERSION, copied from #{oldfile}\n"
107
+ return
108
+ end
109
+ debug << "#{targetfile}: same VERSION, no change\n"
110
+ return
111
+ end
112
+
113
+ # Merge in old settings (note: any settings which are in targetfile but
114
+ # not in distfile will be silently dropped)
115
+ debug << "#{targetfile}:\n"
116
+ src.settings[1..-1].each do |src_setting|
117
+ name = src_setting.name
118
+ old_setting = old[name]
119
+ unless old_setting
120
+ debug << " #{name}: new\n"
121
+ next
122
+ end
123
+ if old_setting.version == src_setting.version
124
+ debug << " #{name}: unchanged\n"
125
+ src_setting.add_comment("\n DEFAULT SETTING from #{distfile}:\n")
126
+ src_setting.add_comment(src_setting.content)
127
+ src_setting.content = old_setting.content
128
+ next
129
+ end
130
+ # Otherwise, must install updated setting and comment out
131
+ # the current setting for reference
132
+ debug << " #{name}: UPDATED\n"
133
+ src_setting.add_comment("\n Previous setting (inserted by rconftool):\n\n")
134
+ src_setting.add_comment(old_setting.content)
135
+ end
136
+
137
+ return if opt[:noclobber]
138
+
139
+ # Write out the new file and carry forward permissions
140
+ begin
141
+ tempfile = targetfile+".new#{$$}"
142
+ src.write(tempfile)
143
+ st = File.stat(distfile)
144
+ begin
145
+ File.chown(st.uid, st.gid, tempfile)
146
+ rescue Errno::EPERM
147
+ end
148
+ File.chmod(st.mode, tempfile)
149
+ File.rename(targetfile, targetfile+".bak") unless target_missing
150
+ File.rename(tempfile, targetfile)
151
+ rescue
152
+ File.delete(tempfile) rescue nil
153
+ raise
154
+ end
155
+ end
156
+
157
+ HEADER_ID = '__header__'
158
+
159
+ # Object to represent a single setting
160
+
161
+ class Setting
162
+ attr_reader :name, :version
163
+ attr_accessor :content
164
+
165
+ def initialize(name, version)
166
+ @name = name.gsub(/\s+/,'')
167
+ @version = version.gsub(/s+/,'')
168
+ @comment = ""
169
+ @content = ""
170
+ @in_content = false
171
+ end
172
+ def <<(str)
173
+ @in_content = true unless /\A#/ =~ str
174
+ if @in_content
175
+ @content << str
176
+ else
177
+ @comment << str
178
+ end
179
+ end
180
+ # Add text to 'comment' portion of setting, prefixing each line with '#'
181
+ def add_comment(str)
182
+ @comment << str.gsub(/^/,'#')
183
+ end
184
+ def to_s
185
+ return "#{@comment}#{@content}" if @name == HEADER_ID
186
+ return "##NAME: #{@name}:#{@version}\n#{@comment}#{@content}"
187
+ end
188
+ end # class Setting
189
+
190
+ # Object to represent an entire configuration file. It consists of
191
+ # an array of Setting objects, with the first one having a special name
192
+ # (__header__). We also keep a hash of setting name => setting object
193
+ # to enable us to find a particular setting quickly.
194
+
195
+ class ConfigFile
196
+ attr_reader :version, :settings
197
+
198
+ def initialize(filename=nil)
199
+ read(filename) if filename
200
+ end
201
+
202
+ # fetch a setting by name
203
+ def [](item)
204
+ @settings_hash[item]
205
+ end
206
+
207
+ def read(filename)
208
+ @version = nil
209
+ curr_setting = Setting.new(HEADER_ID,'')
210
+ @settings = [curr_setting]
211
+ @settings_hash = {}
212
+
213
+ File.open(filename) do |f|
214
+ # VERSION header must occur within first 20 lines
215
+ 20.times do
216
+ line = f.gets
217
+ break unless line
218
+ curr_setting << line
219
+ if line =~ /\A##VERSION:/
220
+ @version = line
221
+ break
222
+ end
223
+ end
224
+ raise NoVersionLine, "#{filename}: No VERSION line found" unless @version
225
+
226
+ while line = f.gets
227
+ unless line =~ /\A##NAME:(.*):(.*)/
228
+ curr_setting << line
229
+ next
230
+ end
231
+ curr_setting = Setting.new($1,$2)
232
+ @settings << curr_setting
233
+ @settings_hash[curr_setting.name] = curr_setting
234
+ end
235
+ end
236
+ end
237
+
238
+ def write(filename)
239
+ File.open(filename,"w") do |f|
240
+ @settings.each do |s|
241
+ f << s.to_s
242
+ end
243
+ end
244
+ end
245
+ end # class ConfigFile
246
+
247
+ # Yield directory contents recursively, without doing chdir(). Note
248
+ # that yielded pathnames are relative to the base directory given;
249
+ # so that, for example, you can simulate 'cp -r /foo/bar/ /baz/' by
250
+ # recurse_dir("/foo/bar") { |n| copy("/foo/bar/"+n,"/baz/"+n) unless
251
+ # File.directory?("/foo/bar/"+n) }
252
+ # Current behaviour is that if a directory is a symlink, we follow it.
253
+ # (Perhaps the block we yield should return true/false?)
254
+
255
+ def self.recurse_dir(base)
256
+ base = base+File::SEPARATOR unless base[-1,1] == File::SEPARATOR
257
+ dirs = ['']
258
+ while dir = dirs.pop
259
+ yield dir unless dir == ''
260
+ Dir.foreach(base+dir) do |n|
261
+ next if n == '.' || n == '..'
262
+ target = dir + n
263
+ if File.directory?(base+target)
264
+ dirs << target+File::SEPARATOR
265
+ next
266
+ end
267
+ yield target
268
+ end
269
+ end
270
+ end
271
+
272
+ class Processor
273
+ attr_reader :o
274
+
275
+ # Parse command-line options and set the @o options hash
276
+
277
+ def initialize(argv=nil)
278
+ require 'optparse'
279
+
280
+ @o = { :strip_regexp => /\.dist\z/ }
281
+ return unless argv
282
+ opts = OptionParser.new do |opts|
283
+ opts.banner = "rconftool version #{VERSION}"
284
+ opts.separator "Usage: #{$0} [options]"
285
+ opts.separator ""
286
+ opts.separator "Specific options:"
287
+
288
+ opts.on("-n", "--noclobber", "Dummy run") do
289
+ @o[:noclobber] = true
290
+ end
291
+ opts.on("-f", "--force", "Update files even if VERSION is same") do
292
+ @o[:force] = true
293
+ end
294
+ opts.on("-q", "--quiet", "No progress reporting") do
295
+ @o[:debug] = ""
296
+ end
297
+ opts.on("--targetdir DIR", "Where to write merged config files") do |dir|
298
+ @o[:targetdir] = dir
299
+ end
300
+ opts.on("--olddir DIR", "If file does not exist in targetdir,",
301
+ "try to merge from here") do |dir|
302
+ @o[:olddir] = dir
303
+ end
304
+ opts.on("--[no-]recursive", "Traverse directories recursively") do |v|
305
+ @o[:recursive] = v
306
+ end
307
+ opts.on("--strip-suffix FOO", "Remove suffix FOO from target filenames",
308
+ "(default .dist)") do |suffix|
309
+ @o[:strip_regexp] = /#{Regexp.escape(suffix)}\z/
310
+ end
311
+ opts.on("-a", "--add-suffix FOO", "Add suffix FOO to target filenames") do |suffix|
312
+ @o[:add_suffix] = suffix
313
+ end
314
+
315
+ opts.on_tail("-?", "--help", "Show this message") do
316
+ puts opts
317
+ exit
318
+ end
319
+ end
320
+ opts.parse!(argv)
321
+ end
322
+
323
+ # Process a list of files, [src1,src2,...]. If recursive mode has been
324
+ # enabled, then subdirectories of destdir are created as necessary
325
+ # when 'src' is a directory, and the mode/ownership of these newly
326
+ # created directories is copied from the original.
327
+
328
+ def run(files)
329
+ done_work = false
330
+ files.each do |f|
331
+ if not File.directory?(f)
332
+ dst = old = nil
333
+ dst = @o[:targetdir] + File::SEPARATOR + File.basename(f) if @o[:targetdir]
334
+ old = @o[:olddir] + File::SEPARATOR + File.basename(f) if @o[:olddir]
335
+ Rconftool::install(f, dst, old, @o)
336
+ elsif not @o[:recursive]
337
+ raise Errno::EISDIR, "#{f} (not copied). Need --recursive?"
338
+ else
339
+ Rconftool::recurse_dir(f) do |nf|
340
+ src = f + File::SEPARATOR + nf
341
+ dst = old = nil
342
+ dst = @o[:targetdir] + File::SEPARATOR + nf if @o[:targetdir]
343
+ old = @o[:olddir] + File::SEPARATOR + nf if @o[:olddir]
344
+ if File.directory?(src)
345
+ if dst and not File.directory?(dst)
346
+ orig = File.stat(src)
347
+ Dir.mkdir(dst, orig.mode)
348
+ begin
349
+ File.chown(orig.uid, orig.gid, dst)
350
+ rescue Errno::EPERM
351
+ end
352
+ end
353
+ else
354
+ Rconftool::install(src, dst, old, @o)
355
+ end
356
+ end
357
+ end
358
+ done_work = true
359
+ end
360
+ unless done_work
361
+ $stderr.puts "Usage: #{$0} [options] src1 src2 ...\n"+
362
+ "Try #{$0} --help for more information\n"
363
+ exit 1
364
+ end
365
+ end
366
+ end # class Processor
367
+
368
+ end # module Rconftool
369
+
370
+ # Run from command line?
371
+ if __FILE__ == $0
372
+
373
+ begin
374
+ s = Rconftool::Processor.new(ARGV)
375
+ s.run(ARGV)
376
+ rescue Exception => e
377
+ $stderr.puts "#{$0}: #{e}"
378
+ end
379
+
380
+ end