0.3
[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 core import *
9
10 def main():
11
12     cmd = sys.argv[1]
13     project = package = filename = None
14     # try: 
15     #     project = sys.argv[2]
16     #     package = sys.argv[3]
17     #     filename = sys.argv[4]
18     # except: 
19     #     pass
20
21     if cmd == 'init':
22         project = sys.argv[2]
23         package = sys.argv[3]
24         init_package_dir(project, package, os.path.curdir)
25         print 'Initializing %s (Project: %s, Package: %s)' % (os.curdir, project, package)
26
27     elif cmd == 'ls':
28         if len(sys.argv) == 2:
29             print '\n'.join(get_slash_source())
30         if len(sys.argv) == 3:
31             project = sys.argv[2]
32             print '\n'.join(meta_get_packagelist(project))
33         if len(sys.argv) == 4:
34             project = sys.argv[2]
35             package = sys.argv[3]
36             print '\n'.join(meta_get_filelist(project, package))
37
38     elif cmd == 'meta':
39         if len(sys.argv) == 4:
40             project = sys.argv[2]
41             package = sys.argv[3]
42             print ''.join(show_package_meta(project, package))
43             print ''.join(show_files_meta(project, package))
44         elif len(sys.argv) == 3:
45             project = sys.argv[2]
46             print ''.join(show_project_meta(project))
47
48     elif cmd == 'diff':
49         args = parseargs()
50         pacs = findpacs(args)
51
52         for p in pacs:
53             if p.todo == []:
54                 for i in p.filenamelist:
55                     if p.status(i) == 'M':
56                         p.todo.append(i)
57
58             d = []
59             for filename in p.todo:
60                 d.append('Index: %s\n' % filename)
61                 d.append('===================================================================\n')
62                 d.append(get_source_file_diff(p.dir, filename, p.rev))
63             if d:
64                 print ''.join(d)
65                 
66
67     elif cmd == 'co' or cmd == 'checkout':
68
69         try: 
70             project = sys.argv[2]
71             package = sys.argv[3]
72             filename = sys.argv[4]
73         except: 
74             pass
75
76         if filename:
77             get_source_file(project, package, filename)
78
79         elif package:
80             checkout_package(project, package)
81
82         else:
83             # all packages
84             for package in meta_get_packagelist(project):
85                 checkout_package(project, package)
86
87
88     elif cmd == 'st' or cmd == 'status':
89
90         args = parseargs()
91         pacs = findpacs(args)
92
93         for p in pacs:
94
95             # no files given as argument? Take all files in current dir
96             if not p.todo:
97                 p.todo = p.filenamelist + p.filenamelist_unvers
98
99             for filename in p.todo:
100                 s = p.status(filename)
101                 if s == 'F':
102                     print statfrmt('!', filename)
103                 elif s != ' ':
104                     print statfrmt(s, filename)
105
106
107     elif cmd == 'add':
108         if len(sys.argv) < 3:
109             print '%s requires at least one argument' % cmd
110             sys.exit(1)
111
112         filenames = parseargs()
113
114         for filename in filenames:
115             if not os.path.exists(filename):
116                 print "file '%s' does not exist" % filename
117                 sys.exit(1)
118
119         pacs = findpacs(filenames)
120
121         for pac in pacs:
122             for filename in pac.todo:
123                 if filename in exclude_stuff:
124                     continue
125
126                 pac.addfile(filename)
127                 print statfrmt('A', filename)
128
129
130     elif cmd == 'addremove':
131         args = parseargs()
132         pacs = findpacs(args)
133         for p in pacs:
134
135             p.todo = p.filenamelist + p.filenamelist_unvers
136
137             for filename in p.todo:
138                 if filename in exclude_stuff:
139                     continue
140                 state = p.status(filename)
141                 if state == '?':
142                     p.addfile(filename)
143                     print statfrmt('A', filename)
144                 elif state == '!':
145                     p.put_on_deletelist(filename)
146                     p.write_deletelist()
147                     os.unlink(os.path.join(p.storedir, filename))
148                     print statfrmt('D', filename)
149
150
151
152
153     elif cmd == 'ci' or cmd == 'checkin':
154         init_basicauth()
155
156         args = parseargs()
157
158         pacs = findpacs(args)
159
160         for p in pacs:
161             p.todo = p.filenamelist_unvers + p.filenamelist
162
163             for filename in p.todo:
164                 st = p.status(filename)
165                 if st == 'A' or st == 'M':
166                     p.todo_send.append(filename)
167                     print 'Sending        %s' % filename
168                 elif st == 'D':
169                     p.todo_delete.append(filename)
170                     print 'Deleting       %s' % filename
171
172             if not p.todo_send and not p.todo_delete:
173                 print 'nothing to do for package %s' % p.name
174                 continue
175
176             print 'Transmitting file data ', 
177             for filename in p.todo_send:
178                 put_source_file(p.prjname, p.name, os.path.join(p.dir, filename))
179                 #copy_file(filename, os.path.join(store, filename))
180             for filename in p.todo_delete:
181                 del_source_file(p.prjname, p.name, filename)
182                 p.to_be_deleted.remove(filename)
183
184             p.update_filesmeta()
185             p.write_deletelist()
186             print
187
188
189     elif cmd == 'up' or cmd == 'update':
190
191         args = parseargs()
192         pacs = findpacs(args)
193
194         for p in pacs:
195
196             # save filelist before replacing the meta file
197             saved_filenames = p.filenamelist
198             p.update_filesmeta()
199             p = Package(p.dir)
200
201             # which files do no longer exist upstream?
202             disappeared = []
203             for filename in saved_filenames:
204                 if filename not in p.filenamelist:
205                     disappeared.append(filename)
206                 
207
208             for filename in saved_filenames:
209                 if filename in disappeared:
210                     print statfrmt('D', filename)
211                     p.delfile(filename)
212                     continue
213
214             for filename in p.filenamelist:
215
216                 state = p.status(filename)
217                 if state == 'M':
218                     print 'file %s is locally modified... fixme' % filename
219                     p.updatefile(filename)
220                     print statfrmt('U', filename)
221                 elif state == '!':
222                     p.updatefile(filename)
223                     print 'Restored \'%s\'' % filename
224                 elif state == 'F':
225                     p.updatefile(filename)
226                     print statfrmt('A', filename)
227                 elif state == ' ':
228                     pass
229
230
231             p.update_pacmeta()
232
233             print 'At revision %s.' % p.rev
234                     
235
236
237             
238
239     elif cmd == 'rm' or cmd == 'delete':
240         if len(sys.argv) < 3:
241             print '%s requires at least one argument' % cmd
242             sys.exit(1)
243
244         args = parseargs()
245         pacs = findpacs(args)
246
247         for p in pacs:
248
249             for filename in p.todo:
250                 p.put_on_deletelist(filename)
251                 p.write_deletelist()
252                 try:
253                     os.unlink(os.path.join(p.dir, filename))
254                     os.unlink(os.path.join(p.storedir, filename))
255                 except:
256                     pass
257                 print statfrmt('D', filename)
258
259
260
261     elif cmd == 'id':
262         print ''.join(get_user_id(sys.argv[2]))
263
264
265     elif cmd == 'platforms':
266         if len(sys.argv) > 2:
267             project = sys.argv[2]
268             print '\n'.join(get_platforms_of_project(project))
269         else:
270             print '\n'.join(get_platforms())
271
272
273     elif cmd == 'results_meta':
274         wd = os.curdir
275         package = store_read_package(wd)
276         project = store_read_project(wd)
277         if len(sys.argv) > 2:
278             platform = sys.argv[2]
279             print ''.join(show_results_meta(project, package, platform))
280         else:
281             for platform in get_platforms_of_project(project):
282                 print ''.join(show_results_meta(project, package, platform))
283
284                 
285     elif cmd == 'results':
286         if len(sys.argv) > 3:
287             print 'getting results for more than one package is not supported'
288             print sys.exit(1)
289             
290         if len(sys.argv) == 3:
291             wd = sys.argv[2]
292         else:
293             wd = os.curdir
294         package = store_read_package(wd)
295         project = store_read_project(wd)
296
297         for platform in get_platforms_of_project(project):
298             print '\n'.join(get_results(project, package, platform))
299
300                 
301     elif cmd == 'log':
302         wd = os.curdir
303         package = store_read_package(wd)
304         project = store_read_project(wd)
305
306         platform = sys.argv[2]
307         arch = sys.argv[3]
308         print ''.join(get_log(project, package, platform, arch))
309
310
311     elif cmd == 'hist' or cmd == 'history':
312         args = parseargs()
313         pacs = findpacs(args)
314
315         for p in pacs:
316             print ''.join(get_history(p.prjname, p.name))
317
318
319     else:
320         print "unknown command '%s'" % cmd
321
322
323 if __name__ == '__main__':
324     init_basicauth()
325     main()
326