Annotation of /trunk/pkgtools/mage2mysql.sh
Parent Directory | Revision Log
Revision 802 -
(hide annotations)
(download)
(as text)
Tue Feb 10 20:01:36 2009 UTC (15 years, 9 months ago) by niro
File MIME type: application/x-sh
File size: 4193 byte(s)
Tue Feb 10 20:01:36 2009 UTC (15 years, 9 months ago) by niro
File MIME type: application/x-sh
File size: 4193 byte(s)
-added mage2mysql conversion script used by the online pakage db of our homepage - initial release
1 | niro | 802 | #!/bin/sh |
2 | # mage2mysql | ||
3 | |||
4 | # mage functions | ||
5 | source /home/tjoke/svn/alx/alx-src/trunk/tinyalxconfig-ng/functions/mysqlfunctions | ||
6 | source /usr/lib/mage/mage4.functions.sh | ||
7 | source /etc/mage.rc.global | ||
8 | source /etc/mage.rc | ||
9 | |||
10 | # sql settings | ||
11 | SQL_USER=mage | ||
12 | SQL_PASS=mage | ||
13 | SQL_HOST=localhost | ||
14 | SQL_DB=mage | ||
15 | |||
16 | create_database() | ||
17 | { | ||
18 | # create categories table | ||
19 | mysqldo "CREATE TABLE IF NOT EXISTS categories ( | ||
20 | id int(10) NOT NULL auto_increment, | ||
21 | pcat varchar(255) NOT NULL, | ||
22 | PRIMARY KEY (id) | ||
23 | )" | ||
24 | |||
25 | # create packages table | ||
26 | mysqldo "CREATE TABLE IF NOT EXISTS packages ( | ||
27 | id int(10) NOT NULL auto_increment, | ||
28 | pname varchar(255) NOT NULL, | ||
29 | pver varchar(255) NOT NULL, | ||
30 | pbuild varchar(255) NOT NULL, | ||
31 | pcat varchar(255) NOT NULL, | ||
32 | state varchar(255) NOT NULL, | ||
33 | description varchar(255) default NULL, | ||
34 | homepage varchar(255) default NULL, | ||
35 | pkgtype varchar(255) default NULL, | ||
36 | depend varchar(255) default NULL, | ||
37 | sdepend varchar(255) default NULL, | ||
38 | provide varchar(255) default NULL, | ||
39 | time datetime NOT NULL, | ||
40 | flag int(1) NOT NULL default '1', | ||
41 | PRIMARY KEY (id) | ||
42 | )" | ||
43 | } | ||
44 | |||
45 | evaluate_categories() | ||
46 | { | ||
47 | local i | ||
48 | local pcat | ||
49 | |||
50 | # ${MAGEDIR} is known from mage.rc{,.global} | ||
51 | for i in ${MAGEDIR}/* | ||
52 | do | ||
53 | pcat="${i##*/}" | ||
54 | |||
55 | # exclude includes | ||
56 | [[ ${pcat} = include ]] && continue | ||
57 | |||
58 | id=$(mysqldo "SELECT id FROM categories WHERE pcat='${pcat}'") | ||
59 | |||
60 | # only add if not found | ||
61 | if [[ -z ${id} ]] | ||
62 | then | ||
63 | mysqldo "INSERT INTO categories ( pcat ) VALUES ( '${pcat}' )" | ||
64 | fi | ||
65 | done | ||
66 | } | ||
67 | |||
68 | evaluate_magefile() | ||
69 | { | ||
70 | local i | ||
71 | local MAGEFILE="$1" | ||
72 | local PKGNAME | ||
73 | local PCAT | ||
74 | local PNAME | ||
75 | local PVER | ||
76 | local PBUILD | ||
77 | local STATE | ||
78 | local DESCRIPTION | ||
79 | local HOMEPAGE | ||
80 | local PKGTYPE | ||
81 | local DEPEND | ||
82 | local SDEPEND | ||
83 | local PROVIDE | ||
84 | |||
85 | # read needed variables within the magefile | ||
86 | for i in PKGNAME STATE HOMEPAGE PKGTYPE DEPEND SDEPEND PROVIDE | ||
87 | do | ||
88 | eval "${i}='$(get_value_from_magefile ${i} ${MAGEFILE})'" | ||
89 | done | ||
90 | |||
91 | # the description may contain some escape characters like ' and " | ||
92 | # which break the above loop | ||
93 | DESCRIPTION="$(get_value_from_magefile ${i} ${MAGEFILE})" | ||
94 | |||
95 | # get var from the name of the magefile | ||
96 | PCAT="$(magename2pcat ${MAGEFILE})" | ||
97 | PNAME="$(magename2pname ${MAGEFILE})" | ||
98 | PVER="$(magename2pver ${MAGEFILE})" | ||
99 | PBUILD="$(magename2pbuild ${MAGEFILE})" | ||
100 | |||
101 | # remove >= etc from depend and sdepend | ||
102 | DEPEND=$(echo ${DEPEND} | sed 's/[^a-zA-Z0-9]\{2\}\ //g') | ||
103 | SDEPEND=$(echo ${SDEPEND} | sed 's/[^a-zA-Z0-9]\{2\}\ //g') | ||
104 | |||
105 | # check if there is a pname for the same state | ||
106 | local id=$(mysqldo "SELECT id FROM packages WHERE pname='${PNAME}' AND state='${STATE}'") | ||
107 | |||
108 | # get categorie id | ||
109 | local pcatid=$(mysqldo "SELECT id FROM categories WHERE pcat='${PCAT}'") | ||
110 | |||
111 | # get current time | ||
112 | local time="$(date +%F\ %T)" | ||
113 | |||
114 | # found, then update | ||
115 | if [[ ! -z ${id} ]] | ||
116 | then | ||
117 | mysqldo "UPDATE packages SET | ||
118 | pname='${PNAME}', | ||
119 | pver='${PVER}', | ||
120 | pbuild='${PBUILD}', | ||
121 | pcat='${pcatid}', | ||
122 | state='${STATE}', | ||
123 | description='${DESCRIPTION}', | ||
124 | homepage='${HOMEPAGE}', | ||
125 | pkgtype='${PKGTYPE}', | ||
126 | depend='${DEPEND}', | ||
127 | sdepend='${SDEPEND}', | ||
128 | provide='${PROVIDE}', | ||
129 | time='${time}', | ||
130 | flag='1' | ||
131 | WHERE id='${id}'" | ||
132 | |||
133 | else | ||
134 | # else import to database as new | ||
135 | mysqldo "INSERT INTO packages ( | ||
136 | pname, | ||
137 | pver, | ||
138 | pbuild, | ||
139 | pcat, | ||
140 | state, | ||
141 | description, | ||
142 | homepage, | ||
143 | pkgtype, | ||
144 | depend, | ||
145 | sdepend, | ||
146 | provide, | ||
147 | time, | ||
148 | flag | ||
149 | ) | ||
150 | VALUES ( | ||
151 | '${PNAME}', | ||
152 | '${PVER}', | ||
153 | '${PBUILD}', | ||
154 | '${pcatid}', | ||
155 | '${STATE}', | ||
156 | '${DESCRIPTION}', | ||
157 | '${HOMEPAGE}', | ||
158 | '${PKGTYPE}', | ||
159 | '${DEPEND}', | ||
160 | '${SDEPEND}', | ||
161 | '${PROVIDE}', | ||
162 | '${time}', | ||
163 | '1' | ||
164 | )" | ||
165 | fi | ||
166 | } | ||
167 | |||
168 | reset_flags() | ||
169 | { | ||
170 | # set all flags to zero | ||
171 | mysqldo "UPDATE packages SET flag='0'" | ||
172 | } | ||
173 | |||
174 | prune_old_packages() | ||
175 | { | ||
176 | # debug | ||
177 | # echo "Debug: following ids are requested for deletion:" | ||
178 | # mysqldo "SELECT id FROM packages WHERE flag='0'" | ||
179 | |||
180 | # delete them | ||
181 | mysqldo "DELETE FROM packages WHERE flag='0'" | ||
182 | } | ||
183 | |||
184 | echo "Processing Categories ..." | ||
185 | evaluate_categories | ||
186 | |||
187 | echo "Resetting Flags ..." | ||
188 | reset_flags | ||
189 | |||
190 | # all magefiles | ||
191 | ALL_MAGEFILES="$(find ${MAGEDIR} -name \*.mage | sort)" | ||
192 | |||
193 | for magefile in ${ALL_MAGEFILES} | ||
194 | do | ||
195 | echo "Processing Magefile ${magefile}" | ||
196 | evaluate_magefile ${magefile} | ||
197 | done | ||
198 | |||
199 | echo "Pruning outdated Packages ..." | ||
200 | prune_old_packages |
Properties
Name | Value |
---|---|
svn:keywords | Id |