file_edit.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #
  2. # Author:: Nuo Yan (<nuo@opscode.com>)
  3. # Copyright:: Copyright (c) 2009 Opscode, Inc.
  4. # License:: Apache License, Version 2.0
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. require 'fileutils'
  18. require 'tempfile'
  19. class Chef
  20. class Util
  21. class FileEdit
  22. private
  23. attr_accessor :original_pathname, :contents, :file_edited
  24. public
  25. def initialize(filepath)
  26. @original_pathname = filepath
  27. @file_edited = false
  28. raise ArgumentError, "File doesn't exist" unless File.exist? @original_pathname
  29. raise ArgumentError, "File is blank" unless (@contents = File.new(@original_pathname).readlines).length > 0
  30. end
  31. #search the file line by line and match each line with the given regex
  32. #if matched, replace the whole line with newline.
  33. def search_file_replace_line(regex, newline)
  34. search_match(regex, newline, 'r', 1)
  35. end
  36. #search the file line by line and match each line with the given regex
  37. #if matched, replace the match (all occurances) with the replace parameter
  38. def search_file_replace(regex, replace)
  39. search_match(regex, replace, 'r', 2)
  40. end
  41. #search the file line by line and match each line with the given regex
  42. #if matched, delete the line
  43. def search_file_delete_line(regex)
  44. search_match(regex, " ", 'd', 1)
  45. end
  46. #search the file line by line and match each line with the given regex
  47. #if matched, delete the match (all occurances) from the line
  48. def search_file_delete(regex)
  49. search_match(regex, " ", 'd', 2)
  50. end
  51. #search the file line by line and match each line with the given regex
  52. #if matched, insert newline after each matching line
  53. def insert_line_after_match(regex, newline)
  54. search_match(regex, newline, 'i', 1)
  55. end
  56. #search the file line by line and match each line with the given regex
  57. #if not matched, insert newline at the end of the file
  58. def insert_line_if_no_match(regex, newline)
  59. search_match(regex, newline, 'i', 2)
  60. end
  61. #Make a copy of old_file and write new file out (only if file changed)
  62. def write_file
  63. # file_edited is false when there was no match in the whole file and thus no contents have changed.
  64. if file_edited
  65. backup_pathname = original_pathname + ".old"
  66. FileUtils.cp(original_pathname, backup_pathname, :preserve => true)
  67. File.open(original_pathname, "w") do |newfile|
  68. contents.each do |line|
  69. newfile.puts(line)
  70. end
  71. newfile.flush
  72. end
  73. end
  74. self.file_edited = false
  75. end
  76. private
  77. #helper method to do the match, replace, delete, and insert operations
  78. #command is the switch of delete, replace, and insert ('d', 'r', 'i')
  79. #method is to control operation on whole line or only the match (1 for line, 2 for match)
  80. def search_match(regex, replace, command, method)
  81. #convert regex to a Regexp object (if not already is one) and store it in exp.
  82. exp = Regexp.new(regex)
  83. #loop through contents and do the appropriate operation depending on 'command' and 'method'
  84. new_contents = []
  85. contents.each do |line|
  86. if line.match(exp)
  87. self.file_edited = true
  88. case
  89. when command == 'r'
  90. new_contents << ((method == 1) ? replace : line.gsub!(exp, replace))
  91. when command == 'd'
  92. if method == 2
  93. new_contents << line.gsub!(exp, "")
  94. end
  95. when command == 'i'
  96. new_contents << line
  97. new_contents << replace unless method == 2
  98. end
  99. else
  100. new_contents << line
  101. end
  102. end
  103. if command == 'i' && method == 2 && ! file_edited
  104. new_contents << replace
  105. self.file_edited = true
  106. end
  107. self.contents = new_contents
  108. end
  109. end
  110. end
  111. end