deleted subcommands file ; changed pkg working directories ; moved commands to comman...
[opensuse:pack-tools.git] / commands / getpac
1 #!/bin/bash
2 #newpac by dlovasko@suse.com
3
4 #set -x
5
6 #print usage
7 function usage
8 {
9         echo "getpac - script for advanced package downloading"
10         echo "usage: getpac PACKAGE [--opensuse PATTERN] [--suse PATTERN] [--delete PATTERN] [--delete PATTERN2] ..."
11 }
12
13 #delete all temporary files
14 function cleanup
15 {
16         test -e "$_delete_patterns" && rm -f "$_delete_patterns"
17         
18         test -e "$_opensuse_patterns" && rm -f "$_opensuse_patterns"
19         test -e "$_opensuse_mbranch" && rm -f "$_opensuse_mbranch"
20         test -e "$_opensuse_packages" && rm -f "$_opensuse_packages"
21         test -e "$_opensuse_wanted" && rm -f "$_opensuse_wanted"
22         test -e "$_opensuse_to_delete" && rm -f "$_opensuse_to_delete"
23         test -e "$_opensuse_files" && rm -f "$_opensuse_files"
24         test -e "$_opensuse_dirs" && rm -f "$_opensuse_dirs"
25
26         test -e "$_suse_patterns" && rm -f "$_suse_patterns"
27         test -e "$_suse_mbranch" && rm -f "$_suse_mbranch"
28         test -e "$_suse_packages" && rm -f "$_suse_packages"
29         test -e "$_suse_wanted" && rm -f "$_suse_wanted"
30         test -e "$_suse_to_delete" && rm -f "$_suse_to_delete"
31         test -e "$_suse_files" && rm -f "$_suse_files"
32         test -e "$_suse_dirs" && rm -f "$_suse_dirs"
33
34         test -e "$_factory_branch" && rm -f "$_factory_branch"
35         test -e "$_factory_to_delete" && rm -f "$_factory_to_delete"
36         test -e "$_factory_files" && rm -f "$_factory_files"
37 }
38
39 #when the script ends, run cleanup
40 trap cleanup EXIT
41
42 #parse arguments
43 ARGS="$(/usr/bin/getopt -a -o f --long factory -o s: --long suse: -o o: --long opensuse: -o d: --long delete: -o h --long 'help' -- "${@}")"
44 eval set -- ${ARGS}
45
46 #no arguments (only -- produced by getopt)
47 if [ "$#" = "1" ] ; then
48         usage
49         exit 1
50 fi
51
52 #files for storing argument information
53 _delete_patterns=$(mktemp --dry-run)
54 _suse_patterns=$(mktemp --dry-run)
55 _opensuse_patterns=$(mktemp --dry-run)
56
57
58 #by default, no factory
59 factory=no
60
61 #branch all opensuse and all suse packages if only package name supplied
62 if [ $# = "2" ] #contains package name and '--' 
63 then
64         echo "all" > $_opensuse_patterns
65         echo "all" > $_suse_patterns
66         factory=no
67 fi
68
69 #parse arguments
70 while [ -n "${1}" ]; do
71                 case "${1}" in
72                         -d|--delete)
73                                 echo "$2" >> $_delete_patterns
74                                 shift 2
75                         ;;
76                         -o|--opensuse)
77                                 echo "$2" >> $_opensuse_patterns
78                                 shift 2
79                         ;;
80                         -s|--suse)
81                                 echo "$2" >> $_suse_patterns
82                                 shift 2
83                         ;;
84                         -f|--factory)
85                                 factory=yes
86                                 shift 1
87                         ;;
88                         --)
89                                 shift 1
90                         ;;
91                         *)
92                                 break
93                         ;;
94                 esac
95 done
96
97 #the only left is package name
98 package_name="$1"
99 echo "Downloading package $package_name."
100
101 function opensuse
102 {
103         #if there was at least one -o option
104         if [ -e $_opensuse_patterns ] 
105         then
106                 echo ''
107                 echo "openSUSE"
108                 echo '****************'
109                 #do the mbranch
110                 _opensuse_mbranch=$(mktemp)
111                 osc -A https://api.opensuse.org mbranch "$package_name" > $_opensuse_mbranch 2>&1
112
113                 #parse output of mbranch
114                 #if the package already exists, get the existing project name and continue
115                 if grep -q '^BuildService API error: branch target package already exists:' $_opensuse_mbranch
116                 then
117                         project_name=`cat $_opensuse_mbranch | sed 's/^BuildService API error: branch target package already exists: //' | sed 's@/.*$@@'`
118
119                 #or the package name is unknown
120                 elif [ "`cat $_opensuse_mbranch`" = "BuildService API error: no packages found by search criteria" ]
121                 then
122                         echo "package not found"
123                         return 1
124
125                 #or everything was ok and continue
126                 elif grep -q 'created.$' $_opensuse_mbranch
127                 then
128                         project_name="`sed  -e 's/^Project //' -e 's/ created.$//' $_opensuse_mbranch`"
129
130                 else
131                         echo "unknown situation, exiting"
132                         return 1
133                 fi
134
135                 echo "Project name is $project_name."
136
137                 _opensuse_packages=$(mktemp)
138                 _opensuse_wanted=$(mktemp)
139                 
140                 #list all available packages inside project
141                 osc -A https://api.opensuse.org list "$project_name" > $_opensuse_packages
142
143                 #check if "all"
144                 if [ "all" = "`cat $_opensuse_patterns`" ]
145                 then
146                         #all are wanted
147                         cat $_opensuse_packages > $_opensuse_wanted
148                 else
149                         #grep only wanted
150                         grep -f $_opensuse_patterns $_opensuse_packages > $_opensuse_wanted
151                 fi
152
153                 #accept packages ending only with _Update
154                 sed -i -n -r /\(_Update$\|^patchinfo$\)/p $_opensuse_wanted
155
156                 echo "Packages that satisfy your filter:"
157                 cat $_opensuse_wanted
158
159                 _opensuse_files=$(mktemp)
160                 _opensuse_to_delete=$(mktemp)
161                 
162                 #for each package, delete unwanted files and download remaining files
163                 while read line
164                 do
165                         #if there are any delete patterns
166                         if [ -e $_delete_patterns ]
167                         then
168                                 #populate package files
169                                 osc -A https://api.opensuse.org list -u "$project_name" "$line" > $_opensuse_files
170                                 
171                                 #pick files to delete
172                                 grep -f $_delete_patterns $_opensuse_files > $_opensuse_to_delete
173
174                                 #delete remotly
175                                 osc -A https://api.opensuse.org rremove -f "$project_name" "$line" `tr '\n' ' ' < $_opensuse_to_delete`
176                         fi
177                         osc -A https://api.opensuse.org checkout "$project_name" "$line" 
178                 done < $_opensuse_wanted
179
180                 #if already exists, adapt
181                 _opensuse_dirs=$(mktemp)
182                 if [ ! -e "$package_name" ]
183                 then
184                         #rename main directory
185                         mv "$project_name" "$package_name"
186                         
187                         #obtain subdirectories
188                         ls "$package_name" > $_opensuse_dirs
189                 else
190                         #obtain subdirectories
191                         ls "$project_name" > $_opensuse_dirs
192                 fi
193                 
194                 #from every name remove the package name and opensuse text
195                 while read name
196                 do
197                         test "$name" = "patchinfo" && continue
198                         newname=$(echo "$name" | sed -e "s/$package_name.openSUSE_//" -e "s/_Update$//")
199                         
200                         if [ -e "$project_name" ]
201                         then
202                                 test -e "$package_name/$newname" && rm -rf "$package_name/$newname"
203                                 mv "$project_name/$name" "$package_name/$newname"
204                         else
205                                 mv "$package_name/$name" "$package_name/$newname"
206                         fi
207                 done < $_opensuse_dirs
208
209                 test -e $project_name && rm -rf $project_name
210         fi
211 }
212
213 ###############################################################################
214
215 function suse
216 {
217         #if there was at least one -s option
218         if [ -e $_suse_patterns ] 
219         then
220                 echo ''
221                 echo "SUSE_SLE"
222                 echo '****************'
223
224                 #do the mbranch
225                 _suse_mbranch=$(mktemp)
226                 osc -A https://api.suse.de mbranch "$package_name" > $_suse_mbranch 2>&1
227
228                 #parse output of mbranch
229                 #if the package already exists, get the existing project name and continue
230                 if grep -q '^BuildService API error: branch target package already exists:' $_suse_mbranch
231                 then
232                         project_name=`cat $_suse_mbranch | sed 's/^BuildService API error: branch target package already exists: //' | sed 's@/.*$@@'`
233
234                 #or the package name is unknown
235                 elif [ "`cat $_suse_mbranch`" = "BuildService API error: no packages found by search criteria" ]
236                 then
237                         echo "package not found"
238                         return 1
239
240                 #or everything was ok and continue
241                 elif grep -q 'created.$' $_suse_mbranch
242                 then
243                         project_name="`sed  -e 's/^Project //' -e 's/ created.$//' $_suse_mbranch`"
244
245                 else
246                         echo "unknown situation, exiting"
247                         return 1
248                 fi
249
250                 echo "Project name is $project_name."
251
252                 _suse_packages=$(mktemp)
253                 _suse_wanted=$(mktemp)
254                 
255                 #list all available packages inside project
256                 osc -A https://api.suse.de list "$project_name" > $_suse_packages
257
258                 #check if "all"
259                 if [ "all" = "`cat $_suse_patterns`" ]
260                 then
261                         #all are wanted
262                         cat $_suse_packages > $_suse_wanted
263                 else
264                         #grep only wanted
265                         grep -f $_suse_patterns $_suse_packages > $_suse_wanted
266                 fi
267
268                 #accept packages ending only with _Update_Test
269                 sed -i -n -r /\(_Update_Test$\|^patchinfo$\)/p $_suse_wanted
270
271                 echo "Packages that satisfy your filter:"
272                 cat $_suse_wanted
273
274                 _suse_files=$(mktemp)
275                 _suse_to_delete=$(mktemp)
276                 
277                 #for each package, delete unwanted files and download remaining files
278                 while read line
279                 do
280                         #if there are any delete patterns
281                         if [ -e $_delete_patterns ]
282                         then
283                                 #populate package files
284                                 osc -A https://api.suse.de list -u "$project_name" "$line" > $_suse_files
285                                 
286                                 #pick files to delete
287                                 grep -f $_delete_patterns $_suse_files > $_suse_to_delete
288
289                                 #delete remotly
290                                 osc -A https://api.suse.de rremove -f "$project_name" "$line" `tr '\n' ' ' < $_suse_to_delete`
291                         fi
292                         osc -A https://api.suse.de checkout "$project_name" "$line" 
293                 done < $_suse_wanted
294
295                 #if already exists, adapt
296                 _suse_dirs=$(mktemp)
297                 if [ ! -e "$package_name" ]
298                 then
299                         #rename main directory
300                         mv "$project_name" "$package_name"
301                         
302                         #obtain subdirectories
303                         ls "$package_name" > $_suse_dirs
304                 else
305                         #obtain subdirectories
306                         ls "$project_name" > $_suse_dirs
307                 fi
308                 
309                 #from every name remove the package name and suse text
310                 while read name
311                 do
312                         newname=$(echo "$name" | sed -e "s/$package_name.SUSE_SLE-/sle_/" -e "s/_Update_Test$//" | tr '[A-Z]' '[a-z]')
313                         
314                         if [ -e "$project_name" ]
315                         then
316                                 test -e "$package_name/$newname" && rm -rf "$package_name/$newname"
317                                 mv "$project_name/$name" "$package_name/$newname"
318                         else
319                                 mv "$package_name/$name" "$package_name/$newname"
320                         fi
321                 done < $_suse_dirs
322
323                 test -e $project_name && rm -rf $project_name
324         fi
325 }
326
327 ###############################################################################
328
329 function factory
330 {
331         #if there was option -f = we need to download also openSUSE:Factory release
332         if [ "$factory" = "yes" ]
333         then
334                 echo ''
335                 echo "openSUSE:Factory"
336                 echo '****************'
337
338                 #try to branch project from openSUSE:Factory - this will link to actual devel package
339                 _factory_branch=$(mktemp)
340                 osc -A https://api.opensuse.org branch "openSUSE:Factory" "$package_name" &> $_factory_branch
341                 retcode=$?
342
343                 #obtain project name
344                 if [ "$retcode" = "0" ]
345                 then
346                         project_name=$(cat $_factory_branch | tail -n 1 | sed 's/^.* home:/home:/' | sed 's@/'"$package_name"'$@@')
347                 else
348                         if grep -q 'branch target package already exists' $_factory_branch
349                         then
350                                 project_name=$(cat $_factory_branch | tail -n 1 | sed 's/^.* home:/home:/' | sed 's@/'"$package_name"'$@@')
351                         else
352                                 echo "unknown error, exiting"
353                                 return 1
354                         fi
355                 fi
356                 
357                 echo "Project name is $project_name."
358                 
359                 #get file list
360                 _factory_files=$(mktemp)
361                 _factory_to_delete=$(mktemp)
362                 
363                 if [ -e $_delete_patterns ]
364                 then
365                         #get file list of files to delete
366                         osc -A https://api.opensuse.org list -u "$project_name" "$package_name" > $_factory_files
367                         grep -f $_delete_patterns $_factory_files > $_factory_to_delete
368
369                         #remotely delete them
370                         osc -A https://api.opensuse.org rremove -f "$project_name" "$package_name" `tr '\n' ' ' < $_factory_to_delete`
371                 fi
372
373                 #after that, checkout/download only wanted
374                 osc -A https://api.opensuse.org checkout "$project_name" "$package_name"
375                 
376                 #directory layout
377                 if [ -e "$package_name" ]
378                 then
379                         test -e "$package_name/Factory" && rm -rf "$package_name/Factory"
380                         mv "$project_name/$package_name" "$package_name/Factory"
381                 else
382                         mkdir "$package_name"
383                         mv "$project_name/$package_name" "$package_name/Factory"
384                 fi
385
386                 test -e "$project_name" && rm -rf "$project_name"
387         fi
388 }
389
390 opensuse
391 suse
392 factory
393