summaryrefslogtreecommitdiffstats
path: root/acfupdate-model.lua
blob: c6b87a2fb53dd4b3279d4fd636dcd7057b4a2fbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
module (..., package.seeall)

-- Load libraries
require("fs")
require("processinfo")

local packagename = "subversion"
local svnurl = "svn://svn.alpinelinux.org/acf/"

-- ################################################################################
-- LOCAL FUNCTIONS

-- Make sure to escape special characters before calling this function
local function querycmd ( cmdline )
	local cmd = io.popen( cmdline )
	local cmd_result = cmd:read("*a") or "unknown"
	cmd:close()
	return cmd_result
end

local function svndir(archive)
	local retval = "/usr/share/acf/"
	if archive == "skins/" then
		retval = retval.."www/skins/"
	elseif archive and archive ~= "core/" then
		retval = retval.."app/"..archive
	end
	return retval
end

local function determinerepository(repository)
	if not repository then
		-- try to determine the archive from svn
		local cmdresult = querycmd("/usr/bin/svn info "..svndir())
		if string.find(cmdresult, "Repository Root:") then
			repository = string.match(cmdresult, "Repository Root:%s+(%S+)")
		-- or if not defined, use the default
		else
			repository = svnurl
		end
	end
	return repository
end
		
-- ################################################################################
-- PUBLIC FUNCTIONS
function read(repository)
	repository = determinerepository(repository)
	local status = {}

	local value, errtxt = processinfo.package_version(packagename)
	status.version = cfe({
		label="Program version",
		value=value,
		errtxt=errtxt,
	})

	status.repository = cfe({ value=repository, label="SVN Repository" })
	if "" == querycmd("/usr/bin/svn list "..format.escapespecialcharacters(repository)) then
		status.repository.errtxt = "Repository cannot be reached"
	end

	return cfe({ type="group", value=status, descr="ACF Update Info" })
end												

function update(repository, sessiondata)
	repository = determinerepository(repository)
	local cmdresult = {}
	local mustrestart
	
	function work(list)
		local dir = svndir(list)
		-- If we have the directory already, but not from archive, delete it
		if posix.stat(dir) and not posix.stat(dir..".svn") then
			fs.remove_directory(dir)
			if list == "core/" then
				-- We have to restart the web server because we've just deleted the 
				mustrestart = true
			end
		end
		local updateresult = querycmd("/usr/bin/svn co "..format.escapespecialcharacters(repository).."/"..list.."trunk "..dir.." 2>&1")
		-- Hide projects without updates
		if (string.match(updateresult, "^Checked out revision.*")) then updateresult = "" end
		table.insert(cmdresult, {name=list, updates=updateresult})
	end

	if "" ~= querycmd("/usr/bin/svn list "..format.escapespecialcharacters(repository)) then
		work("core/")
		for list in string.gmatch((querycmd("/usr/bin/svn list " .. format.escapespecialcharacters(repository) )), "%S+") do
			if list~="core/" and list~="sandbox/" then
				work(list)
			end
		end
	end
	-- Destroy the menu and permissions session data so it's recalculated
	if sessiondata then sessiondata.menu = nil end
	if sessiondata then sessiondata.permissions = nil end

	if mustrestart then
		-- FIXME
		--processinfo.daemoncontrol("mini_httpd", "restart")
	end

	return cfe({ type="structure", value=cmdresult, label="SVN update Result"})
end

function diffs(repository)
	repository = determinerepository(repository)
	local cmdresult = {}
	for list in string.gmatch((querycmd("/usr/bin/svn list " .. format.escapespecialcharacters(repository) )), "%S+") do
		if (list ~= "sandbox/") then
			local updateresult = querycmd("/usr/bin/svn diff "..svndir(list).." 2>&1")
			if updateresult ~= "" then
				table.insert(cmdresult, {name=list, updates=updateresult})
			end
		end
	end
	return cfe({ type="structure", value=cmdresult, label="SVN diff Result" })
end

function status(repository)
	repository = determinerepository(repository)
	local cmdresult = {}
	for list in string.gmatch((querycmd("/usr/bin/svn list " .. format.escapespecialcharacters(repository) )), "%S+") do
		if (list ~= "sandbox/") then
			local updateresult = querycmd("/usr/bin/svn st -u "..svndir(list).." 2>&1")
			-- Hide projects without diffs
			if (string.match(updateresult, "^Status against revision.*")) then updateresult = "" end
			table.insert(cmdresult, {name=list, updates=updateresult})
		end
	end
	return cfe({ type="structure", value=cmdresult, label="SVN status Result" })
end

function log (repository)
	repository = determinerepository(repository)
	local enddate = tostring(os.date("%Y-%m-%d",  (os.time() - (3600 * 24) * 7)))
	local svnresult = querycmd("/usr/bin/svn log -v -rHEAD:{".. enddate .. "} " .. format.escapespecialcharacters(repository) )
	return cfe({ type="longtext", value=svnresult, label="SVN log Result" })
end