file_edit.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. # NOTE: This is a patched version of chef/lib/chef/utils/file_edit.rb
  18. # This version is needed to patch monkey the original until the patch
  19. # be merged at the master.
  20. # Bug realed: CHEF-2740
  21. class Chef
  22. class Util
  23. class FileEdit
  24. private
  25. attr_accessor :original_pathname, :contents, :file_edited
  26. public
  27. def initialize(filepath)
  28. @original_pathname = filepath
  29. @file_edited = false
  30. raise ArgumentError, "File doesn't exist" unless File.exist? @original_pathname
  31. raise ArgumentError, "File is blank" unless (@contents = File.new(@original_pathname).readlines).length > 0
  32. end
  33. #search the file line by line and match each line with the given regex
  34. #if matched, replace the whole line with newline.
  35. def search_file_replace_line(regex, newline)
  36. search_match(regex, newline, 'r', 1)
  37. end
  38. #search the file line by line and match each line with the given regex
  39. #if matched, replace the match (all occurances) with the replace parameter
  40. def search_file_replace(regex, replace)
  41. search_match(regex, replace, 'r', 2)
  42. end
  43. #search the file line by line and match each line with the given regex
  44. #if matched, delete the line
  45. def search_file_delete_line(regex)
  46. search_match(regex, " ", 'd', 1)
  47. end
  48. #search the file line by line and match each line with the given regex
  49. #if matched, delete the match (all occurances) from the line
  50. def search_file_delete(regex)
  51. search_match(regex, " ", 'd', 2)
  52. end
  53. #search the file line by line and match each line with the given regex
  54. #if matched, insert newline after each matching line
  55. def insert_line_after_match(regex, newline)
  56. search_match(regex, newline, 'i', 1)
  57. end
  58. #search the file line by line and match each line with the given regex
  59. #if not matched, insert newline at the end of the file
  60. def insert_line_if_no_match(regex, newline)
  61. search_match(regex, newline, 'i', 2)
  62. end
  63. #Make a copy of old_file and write new file out (only if file changed)
  64. def write_file
  65. # file_edited is false when there was no match in the whole file and thus no contents have changed.
  66. if file_edited
  67. backup_pathname = original_pathname + ".old"
  68. FileUtils.cp(original_pathname, backup_pathname, :preserve => true)
  69. File.open(original_pathname, "w") do |newfile|
  70. contents.each do |line|
  71. newfile.puts(line)
  72. end
  73. newfile.flush
  74. end
  75. end
  76. self.file_edited = false
  77. end
  78. private
  79. #helper method to do the match, replace, delete, and insert operations
  80. #command is the switch of delete, replace, and insert ('d', 'r', 'i')
  81. #method is to control operation on whole line or only the match (1 for line, 2 for match)
  82. def search_match(regex, replace, command, method)
  83. #convert regex to a Regexp object (if not already is one) and store it in exp.
  84. exp = Regexp.new(regex)
  85. #loop through contents and do the appropriate operation depending on 'command' and 'method'
  86. new_contents = []
  87. contents.each do |line|
  88. if line.match(exp)
  89. self.file_edited = true
  90. case
  91. when command == 'r'
  92. new_contents << ((method == 1) ? replace : line.gsub!(exp, replace))
  93. when command == 'd'
  94. if method == 2
  95. new_contents << line.gsub!(exp, "")
  96. end
  97. when command == 'i'
  98. new_contents << line
  99. new_contents << replace unless method == 2
  100. end
  101. else
  102. new_contents << line
  103. end
  104. end
  105. if command == 'i' && method == 2 && ! file_edited
  106. new_contents << replace
  107. self.file_edited = true
  108. end
  109. self.contents = new_contents
  110. end
  111. end
  112. end
  113. end