- reorganize source to build a python module
[opensuse:osc.git] / osc / commandline.py
1 #!/usr/bin/python
2
3 # Copyright (C) 2006 Peter Poeml.  All rights reserved.
4 # This program is free software; it may be used, copied, modified
5 # and distributed under the terms of the GNU General Public Licence,
6 # either version 2, or (at your option) any later version.
7
8 from osclib import *
9
10
11 def main():
12
13     cmd = sys.argv[1]
14     project = package = filename = None
15     # try: 
16     #     project = sys.argv[2]
17     #     package = sys.argv[3]
18     #     filename = sys.argv[4]
19     # except: 
20     #     pass
21
22     if cmd == 'init':
23         project = sys.argv[2]
24         package = sys.argv[3]
25         init_package_dir(project, package, os.path.curdir)
26         print 'Initializing %s (Project: %s, Package: %s)' % (os.curdir, project, package)
27
28     elif cmd == 'ls':
29         if len(sys.argv) == 2:
30             print '\n'.join(get_slash_source())
31         if len(sys.argv) == 3:
32             project = sys.argv[2]
33             print '\n'.join(meta_get_packagelist(project))
34         if len(sys.argv) == 4:
35             project = sys.argv[2]
36             package = sys.argv[3]
37             print '\n'.join(meta_get_filelist(project, package))
38
39     elif cmd == 'meta':
40         if len(sys.argv) == 4:
41             project = sys.argv[2]
42             package = sys.argv[3]
43             print ''.join(show_package_meta(project, package))
44             print ''.join(show_files_meta(project, package))
45         elif len(sys.argv) == 3:
46             project = sys.argv[2]
47             print ''.join(show_project_meta(project))
48
49     elif cmd == 'diff':
50         wd = os.curdir
51         package = store_read_package(wd)
52         project = store_read_project(wd)
53         if len(sys.argv) > 2:
54             filename = sys.argv[2]
55         if filename:
56             print get_source_file_diff(project, package, filename)
57         else:
58             d = []
59             for filename in meta_get_filelist(project, package):
60                 d.append(get_source_file_diff(project, package, filename))
61             print ''.join(d)
62                 
63
64     elif cmd == 'co':
65
66         try: 
67             project = sys.argv[2]
68             package = sys.argv[3]
69             filename = sys.argv[4]
70         except: 
71             pass
72
73         if filename:
74             get_source_file(project, package, filename)
75
76         elif package:
77             checkout_package(project, package)
78
79         else:
80             # all packages
81             for package in meta_get_packagelist(project):
82                 checkout_package(project, package)
83
84
85     elif cmd == 'st' or cmd == 'status':
86
87         if len(sys.argv) > 2:
88             args = sys.argv[2:]
89         else:
90             args = [ os.curdir ]
91         #print args
92
93         for arg in args:
94
95             if os.path.isfile(arg):
96                 wd = os.path.dirname(arg)
97                 filenames = [ os.path.basename(arg) ]
98             elif os.path.isdir(arg):
99                 wd = arg
100                 package = store_read_package(wd)
101                 project = store_read_project(wd)
102                 filenames = meta_get_filelist(project, package)
103
104                 # add files which are not listed in _meta
105                 for i in os.listdir(arg):
106                     if i not in filenames and i not in exclude_stuff:
107                         filenames.insert(0, i)
108
109             os.chdir(wd)
110             check_store_version()
111
112             filelist = localmeta_get_filelist()
113             for filename in filenames:
114                 s = get_file_status(project, package, filename, filelist=filelist)
115                 #if not s.startswith(' '):
116                 #    print s
117                 print s
118
119
120     elif cmd == 'add':
121         filenames = sys.argv[2:]
122         for filename in filenames:
123             if not os.path.exists(filename):
124                 print "file '%s' does not exist" % filename
125                 sys.exit(1)
126         for filename in filenames:
127             localmeta_addfile(filename)
128             print 'A   ', filename
129
130     elif cmd == 'addremove':
131         if len(sys.argv) > 2:
132             args = sys.argv[2:]
133         else:
134             args = [ os.curdir ]
135
136         for arg in args:
137
138             if os.path.isfile(arg):
139                 wd = os.path.dirname(arg)
140                 filenames = [ os.path.basename(arg) ]
141             elif os.path.isdir(arg):
142                 wd = arg
143                 package = store_read_package(wd)
144                 project = store_read_project(wd)
145                 filenames = meta_get_filelist(project, package)
146
147                 # add files which are not listed in _meta
148                 for i in os.listdir(arg):
149                     if i not in filenames and i not in exclude_stuff:
150                         filenames.insert(0, i)
151
152
153
154             filelist = localmeta_get_filelist()
155             for filename in filenames:
156                 st = get_file_status(project, package, filename, filelist=filelist)
157                 if st.startswith('?'):
158                     localmeta_addfile(filename)
159                     print 'A   ', filename
160                 elif st.startswith('!'):
161                     print 'D   ', filename
162                     localmeta_removefile(filename)
163
164
165
166     elif cmd == 'ci' or cmd == 'checkin':
167         if len(sys.argv) > 2:
168             args = sys.argv[2:]
169         else:
170             args = [ os.curdir ]
171         #print args
172
173         for arg in args:
174
175             if os.path.isfile(arg):
176                 wd = os.path.dirname(arg)
177                 filenames = [ os.path.basename(arg) ]
178             elif os.path.isdir(arg):
179                 wd = arg
180                 package = store_read_package(wd)
181                 project = store_read_project(wd)
182                 filenames = meta_get_filelist(project, package)
183
184                 # add files which are not listed in _meta
185                 for i in os.listdir(arg):
186                     if i not in filenames and i not in exclude_stuff:
187                         filenames.insert(0, i)
188
189             os.chdir(wd)
190
191             files_to_send = []
192             files_to_delete = []
193
194             filelist = localmeta_get_filelist()
195             for filename in filenames:
196                 st = get_file_status(project, package, filename, filelist=filelist)
197                 if st.startswith('A') or st.startswith('M'):
198                     files_to_send.append(filename)
199                     print 'Sending        %s' % filename
200                 elif st.startswith('D'):
201                     files_to_delete.append(filename)
202                     print 'Deleting       %s' % filename
203
204             if not files_to_send and not files_to_delete:
205                 print 'nothing to do'
206                 sys.exit(0)
207
208             print 'Transmitting file data ', 
209             for filename in files_to_send:
210                 put_source_file(project, package, filename)
211                 copy_file(filename, os.path.join(store, filename))
212             for filename in files_to_delete:
213                 del_source_file(project, package, filename)
214             print
215
216
217     elif cmd == 'up' or cmd == 'update':
218
219         if len(sys.argv) > 2:
220             args = sys.argv[2:]
221         else:
222             args = [ os.curdir ]
223         #print args
224
225         for arg in args:
226
227             if os.path.isfile(arg):
228                 wd = os.path.dirname(arg)
229                 filenames = [ os.path.basename(arg) ]
230             elif os.path.isdir(arg):
231                 wd = arg
232                 package = store_read_package(wd)
233                 project = store_read_project(wd)
234
235                 ## add files which are not listed in _meta
236                 #for i in os.listdir(arg):
237                 #    if i not in filenames and i not in exclude_stuff:
238                 #        filenames.insert(0, i)
239
240             olddir = os.getcwd()
241             os.chdir(wd)
242             check_store_version()
243
244             # save filelist before replacing the meta file
245             filenames = localmeta_get_filelist()
246             os.chdir(store)
247             # update filelist
248             f = open('_files', 'w')
249             f.write(''.join(show_files_meta(project, package)))
250             f.close()
251
252             # which files do no longer exist upstream?
253             disappeared = []
254             upstream_files = meta_get_filelist(project, package)
255             for filename in filenames:
256                 if filename not in upstream_files:
257                     disappeared.append(filename)
258                 
259
260             for filename in filenames:
261                 if filename in disappeared:
262                     print 'D    %s' % filename
263                     os.unlink(filename)
264                     continue
265
266                 get_source_file(project, package, filename)
267                 wcfilename = os.path.join(os.pardir, os.path.basename(filename))
268
269                 if not os.path.exists(wcfilename):
270                     print 'A    %s' % filename
271                     copy_file(filename, wcfilename)
272
273                 elif dgst(wcfilename) != dgst(filename):
274                     print 'U    %s' % filename
275                     copy_file(filename, wcfilename)
276
277                 else:
278                     pass
279
280             # get current meta file
281             f = open('_meta', 'w')
282             f.write(''.join(show_package_meta(project, package)))
283             f.close()
284
285             os.chdir(olddir)
286                     
287
288
289             
290
291     elif cmd == 'rm' or cmd == 'delete':
292         if len(sys.argv) > 2:
293             args = sys.argv[2:]
294         else:
295             print '%s requires at least one argument' % cmd
296             sys.exit(1)
297
298         for arg in args:
299
300             olddir = os.getcwd()
301
302             wd = os.path.dirname(arg) or os.curdir
303             filename = arg
304
305             os.chdir(wd)
306
307             localmeta_removefile(filename)
308             print 'D    %s' % filename
309
310             os.chdir(olddir)
311
312
313     elif cmd == 'id':
314         print ''.join(get_user_id(sys.argv[2]))
315
316     elif cmd == 'platforms':
317         if len(sys.argv) > 2:
318             project = sys.argv[2]
319             print '\n'.join(get_platforms_of_project(project))
320         else:
321             print '\n'.join(get_platforms())
322
323
324
325     elif cmd == 'results_meta':
326         wd = os.curdir
327         package = store_read_package(wd)
328         project = store_read_project(wd)
329         if len(sys.argv) > 2:
330             platform = sys.argv[2]
331             print ''.join(show_results_meta(project, package, platform))
332         else:
333             for platform in get_platforms_of_project(project):
334                 print ''.join(show_results_meta(project, package, platform))
335                 
336     elif cmd == 'results':
337         if len(sys.argv) > 3:
338             print 'getting results for more than one package is not supported'
339             print sys.exit(1)
340             
341         if len(sys.argv) == 3:
342             wd = sys.argv[2]
343         else:
344             wd = os.curdir
345         package = store_read_package(wd)
346         project = store_read_project(wd)
347
348         for platform in get_platforms_of_project(project):
349             print '\n'.join(get_results(project, package, platform))
350
351                 
352     elif cmd == 'log':
353         wd = os.curdir
354         package = store_read_package(wd)
355         project = store_read_project(wd)
356
357         platform = sys.argv[2]
358         arch = sys.argv[3]
359         print ''.join(get_log(project, package, platform, arch))
360
361     else:
362         print "unknown command '%s'" % cmd
363
364 if __name__ == '__main__':
365     init_basicauth()
366     main()
367