<commit_before>#!/usr/bin/lua
-- WANT_JSON

local Ansible = require("ansible")
local File    = require("fileutils")

local function join(list, sep)
	local cur = ""
	for i, v in ipairs(list) do
		if i ~= 1 then
			cur = string.format("%s%s%s", cur, sep, v)
		else
			cur = v
		end
	end
	return cur
end

function write_changes(module, lines, dest)
	-- FIXME: we do not support validate, sorry
	module:unslurp(dest, join(lines, "\n") .. "\n")
end

function check_file_attrs(module, changed, message, diff)
	file_args = module:get_params()
	if File.set_fs_attributes_if_different(module, file_args, changed, diff) then
		if changed then
			message = message .. " and "
		end
		changed = true
		message = message .. "ownership or perms changed"
	end

	return message, changed
end

local function splitlines(content)
	local lines = {}
	for line in string.gmatch(content, "[^\n]+") do
		lines[#lines + 1] = line
	end
	return lines
end

local function append(t1, t2)
	for k,v in ipairs(t2) do
		t1[#t1 + 1] = v
	end
	return t1
end


local function rstrip(str, chars)
	return string.gsub(str, string.format("[%s]+$", chars), "")
end

local function filter(matcher, list)
	local tmp = {}
	for i,v in ipairs(list) do
		if matcher(v) then
			tmp[#tmp + 1] = v
		end
	end
	return tmp
end

function present(module, dest, regexp, line, insertafter, insertbefore, create, backup, backrefs)
	diff = {before="", after="", before_header=dest .. " (content)", after_header=dest .. " (content)"}

	local lines
	if not File.exists(dest) then
		if not create then
			module:fail_json({rc=257, msg='Destination ' .. dest .. ' does not exist!'})
		end
		local destpath = File.dirname(dest)
		if not File.exists(destpath) and not module:check_mode() then
			local status, errstr = File.mkdirs(destpath)
			if not status then
				module:fail_json({msg="Failed to create path components for " .. destpath .. ": " .. errstr})
			end
		end
		lines = {}
	else
		lines = splitlines(module:slurp(dest))
	end

	-- No diff mode on memory restrained devices...
	-- if module:_diff then
	-- 	diff['before'] = -- ''.join(lines
	-- end
	
	local mre = regexp
	
	local insre = nil
	if insertafter ~= nil and insertafter ~= 'BOF' and insertafter ~= 'EOF' then
		insre = insertafter
	elseif insertbefore ~= nil and insertbefore ~= 'BOF' then
		insre = insertbefore
	end


	-- matchno is the line num where the regexp has been found
	-- borano  is the line num where the insertafter/insertbefore has been found
	local matchno, borano = -1, -1
	local m = nil
	for lineno, cur_line in ipairs(lines) do
		if regexp ~= nil then
			match_found = string.match(cur_line, mre)
		else
			match_found = line == rstrip(cur_line, '\r\n')
		end
		if match_found then
			matchno = lineno
			m = cur_line
		elseif insre ~= nil and string.match(cur_line, insre) then
			if insertafter then
				-- + 1 for the next line
				borano = lineno + 1
			end
			if insertbefore then
				-- + 1 for the previous line
				borano = lineno
			end
		end
	end

	local msg = ''
	local changed = false

	-- Regexp matched a line in the file
	if matchno ~= -1 then
		local new_line
		if backrefs then
			new_line = string.gsub(m, mre, line)<commit_msg>Leave note about bug in `lineinfile`<commit_after>#!/usr/bin/lua
-- WANT_JSON

local Ansible = require("ansible")
local File    = require("fileutils")

local function join(list, sep)
	local cur = ""
	for i, v in ipairs(list) do
		if i ~= 1 then
			cur = string.format("%s%s%s", cur, sep, v)
		else
			cur = v
		end
	end
	return cur
end

function write_changes(module, lines, dest)
	-- FIXME: we do not support validate, sorry
	module:unslurp(dest, join(lines, "\n") .. "\n")
end

function check_file_attrs(module, changed, message, diff)
	file_args = module:get_params()
	if File.set_fs_attributes_if_different(module, file_args, changed, diff) then
		if changed then
			message = message .. " and "
		end
		changed = true
		message = message .. "ownership or perms changed"
	end

	return message, changed
end

local function splitlines(content)
	local lines = {}
	for line in string.gmatch(content, "[^\n]+") do
		lines[#lines + 1] = line
	end
	return lines
end

local function append(t1, t2)
	for k,v in ipairs(t2) do
		t1[#t1 + 1] = v
	end
	return t1
end


local function rstrip(str, chars)
	return string.gsub(str, string.format("[%s]+$", chars), "")
end

local function filter(matcher, list)
	local tmp = {}
	for i,v in ipairs(list) do
		if matcher(v) then
			tmp[#tmp + 1] = v
		end
	end
	return tmp
end

function present(module, dest, regexp, line, insertafter, insertbefore, create, backup, backrefs)
	diff = {before="", after="", before_header=dest .. " (content)", after_header=dest .. " (content)"}

	local lines
	if not File.exists(dest) then
		if not create then
			module:fail_json({rc=257, msg='Destination ' .. dest .. ' does not exist!'})
		end
		local destpath = File.dirname(dest)
		if not File.exists(destpath) and not module:check_mode() then
			local status, errstr = File.mkdirs(destpath)
			if not status then
				module:fail_json({msg="Failed to create path components for " .. destpath .. ": " .. errstr})
			end
		end
		lines = {}
	else
		lines = splitlines(module:slurp(dest))
	end

	-- No diff mode on memory restrained devices...
	-- if module:_diff then
	-- 	diff['before'] = -- ''.join(lines
	-- end
	
	local mre = regexp
	
	local insre = nil
	if insertafter ~= nil and insertafter ~= 'BOF' and insertafter ~= 'EOF' then
		insre = insertafter
	elseif insertbefore ~= nil and insertbefore ~= 'BOF' then
		insre = insertbefore
	end


	-- matchno is the line num where the regexp has been found
	-- borano  is the line num where the insertafter/insertbefore has been found
	local matchno, borano = -1, -1
	local m = nil
	for lineno, cur_line in ipairs(lines) do
		if regexp ~= nil then
			-- FIXME: lua patterns are not regexes
			match_found = string.match(cur_line, mre)
		else
			match_found = line == rstrip(cur_line, '\r\n')
		end
		if match_found then
			matchno = lineno
			m = cur_line
		elseif insre ~= nil and string.match(cur_line, insre) then
			if insertafter then
				-- + 1 for the next line
				borano = lineno + 1
			end
			if insertbefore then
				-- + 1 for the previous line
				borano = lineno
			end
		end
	end

	local msg = ''
	local changed = false

	-- Regexp matched a line in the file
	if matchno ~= -1 then
		local new_line
		if backrefs then
			new_line = string.gsub(m, mre, line)
{'fraction_non_alphanumeric': 0.11492281303602059, 'fraction_numerical': 0.005925463901450179, 'mean_word_length': 3.3396481732070367, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7514037', 'n_tokens_mistral': 2442, 'n_tokens_neox': 2222, 'n_words': 754}