forked from jensl/critic
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dbutils.py
1073 lines (843 loc) · 39.3 KB
/
dbutils.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- mode: python; encoding: utf-8 -*-
#
# Copyright 2012 Jens Lindström, Opera Software ASA
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import base
import gitutils
from htmlutils import jsify
import configuration
#import review.utils as review_utils
from log.commitset import CommitSet
import dbaccess
from dbaccess import IntegrityError
import os
import os.path
import time
class Session():
def __init__(self):
self.__atexit = []
self.storage = { "Repository": {}, "User": {}, "Commit": {}, "CommitUserTime": {} }
self.profiling = {}
def atexit(self, fn):
self.__atexit.append(fn)
def close(self):
for fn in self.__atexit:
try: fn(self)
except: pass
def disableProfiling(self):
self.profiling = None
def recordProfiling(self, item, duration, rows=None, repetitions=1):
if self.profiling is not None:
count, accumulated_ms, maximum_ms, accumulated_rows, maximum_rows = self.profiling.get(item, (0, 0.0, 0.0, None, None))
count += repetitions
accumulated_ms += 1000 * duration
maximum_ms = max(maximum_ms, 1000 * duration)
if rows is not None:
if accumulated_rows is None: accumulated_rows = 0
if maximum_rows is None: maximum_rows = 0
accumulated_rows += rows
maximum_rows = max(maximum_rows, rows)
self.profiling[item] = count, accumulated_ms, maximum_ms, accumulated_rows, maximum_rows
class Database(Session):
class Cursor():
def __init__(self, db, cursor, profiling):
self.__db = db
self.__cursor = cursor
self.__profiling = self.__db.profiling is not None
self.__rows = None
def __iter__(self):
if not self.__profiling:
return iter(self.__cursor)
else:
return iter(self.__rows)
def __getitem__(self, index):
if not self.__profiling:
return self.__cursor[index]
else:
return self.__rows[index]
def fetchone(self):
if not self.__profiling:
return self.__cursor.fetchone()
elif self.__rows:
row = self.__rows[0]
self.__rows = self.__rows[1:]
return row
else:
return None
def fetchall(self):
if not self.__profiling:
return self.__cursor.fetchall()
else:
return self.__rows
def execute(self, query, params=None):
if not self.__profiling:
self.__cursor.execute(query, params)
else:
before = time.time()
self.__cursor.execute(query, params)
try:
self.__rows = self.__cursor.fetchall()
except dbaccess.ProgrammingError:
self.__rows = None
after = time.time()
self.__db.recordProfiling(query, after - before, rows=len(self.__rows) if self.__rows else 0)
def executemany(self, query, params):
if self.__profiling is None:
self.__cursor.executemany(query, params)
else:
before = time.time()
params = list(params)
self.__cursor.executemany(query, params)
after = time.time()
self.__db.recordProfiling(query, after - before, repetitions=len(params))
def __init__(self):
Session.__init__(self)
self.__connection = dbaccess.connect()
def cursor(self):
return Database.Cursor(self, self.__connection.cursor(), self.profiling)
def commit(self):
before = time.time()
self.__connection.commit()
after = time.time()
self.recordProfiling("<commit>", after - before, 0)
def rollback(self):
before = time.time()
self.__connection.rollback()
after = time.time()
self.recordProfiling("<rollback>", after - before, 0)
def close(self):
Session.close(self)
self.__connection.close()
class NoSuchUser(base.Error):
def __init__(self, name):
super(NoSuchUser, self).__init__("No such user: %s" % name)
self.name = name
class User():
def __init__(self, user_id, name, email, fullname, status):
self.id = user_id
self.name = name
self.email = email
self.fullname = fullname
self.status = status
self.preferences = {}
self.__resources = {}
def __eq__(self, other):
if self.isAnonymous(): return False
elif isinstance(other, User):
if other.isAnonymous(): return False
else: return self.id == other.id
elif isinstance(other, int):
return self.id == other
elif isinstance(other, str):
return self.name == other
else:
raise base.Error, "invalid comparison"
def __ne__(self, other):
return not (self == other)
def __int__(self):
return self.id
def __repr__(self):
return "User(%r, %r, %r, %r)" % (self.id, self.name, self.email, self.fullname)
def __hash__(self):
return hash(self.id)
@staticmethod
def makeAnonymous():
return User(None, None, None, None, 'anonymous')
def isAnonymous(self):
return self.status == 'anonymous'
def hasRole(self, db, role):
cursor = db.cursor()
cursor.execute("SELECT 1 FROM userroles WHERE uid=%s AND role=%s", (self.id, role))
return bool(cursor.fetchone())
def loadPreferences(self, db):
if not self.preferences:
cursor = db.cursor()
cursor.execute("""SELECT preferences.item, type, COALESCE(integer, default_integer), COALESCE(string, default_string)
FROM preferences
LEFT OUTER JOIN userpreferences ON (preferences.item=userpreferences.item
AND userpreferences.uid=%s)""",
(self.id,))
for item, preference_type, integer, string in cursor:
if preference_type == "boolean":
self.preferences[item] = bool(integer)
elif preference_type == "integer":
self.preferences[item] = integer
else:
self.preferences[item] = string
def getPreference(self, db, item):
if item not in self.preferences:
cursor = db.cursor()
cursor.execute("""SELECT type, COALESCE(integer, default_integer), COALESCE(string, default_string)
FROM preferences
LEFT OUTER JOIN userpreferences ON (preferences.item=userpreferences.item
AND userpreferences.uid=%s)
WHERE preferences.item=%s""", (self.id, item))
row = cursor.fetchone()
if not row: raise Exception, "invalid preference: %s" % item
preference_type, integer, string = row
if preference_type == "boolean":
self.preferences[item] = bool(integer)
elif preference_type == "integer":
self.preferences[item] = integer
else:
self.preferences[item] = string
return self.preferences[item]
def setPreference(self, db, item, value):
self.loadPreferences(db)
if self.preferences[item] != value:
cursor = db.cursor()
cursor.execute("DELETE FROM userpreferences WHERE uid=%s AND item=%s", [self.id, item])
cursor.execute("SELECT type FROM preferences WHERE item=%s", [item])
value_type = cursor.fetchone()[0]
if value_type in ('boolean', 'integer'):
cursor.execute("INSERT INTO userpreferences (uid, item, integer) VALUES (%s, %s, %s)", [self.id, item, int(value)])
else:
cursor.execute("INSERT INTO userpreferences (uid, item, string) VALUES (%s, %s, %s)", [self.id, item, str(value)])
def getDefaultRepository(self, db):
return gitutils.Repository.fromName(db, self.getPreference(db, "defaultRepository"))
def getResource(self, db, name):
if name in self.__resources:
return self.__resources[name]
cursor = db.cursor()
cursor.execute("SELECT revision, source FROM userresources WHERE uid=%s AND name=%s ORDER BY revision DESC FETCH FIRST ROW ONLY", (self.id, name))
row = cursor.fetchone()
if row and row[1] is not None:
resource = self.__resources[name] = ("\"critic.rev.%d\"" % row[0], row[1])
return resource
path = os.path.join(configuration.paths.INSTALL_DIR, "resources", name)
mtime = os.stat(path).st_mtime
resource = self.__resources[name] = ("\"critic.mtime.%d\"" % mtime, open(path).read())
return resource
def getCriticURLs(self, db):
url_types = self.getPreference(db, 'email.urlType').split(",")
cursor = db.cursor()
cursor.execute("SELECT key, url_prefix FROM systemidentities")
url_prefixes = dict(cursor)
return [url_prefixes[url_type] for url_type in url_types]
def getFirstName(self):
return self.fullname.split(" ")[0]
def getJSConstructor(self, db=None):
if self.isAnonymous():
return "new User(null, null, null, null, null, { ui: {} }"
if db:
options = ("{ ui: { keyboardShortcuts: %s, resolveIssueWarning: %s, convertIssueToNote: %s, asynchronousReviewMarking: %s } }" %
("true" if self.getPreference(db, "ui.keyboardShortcuts") else "false",
"true" if self.getPreference(db, "ui.resolveIssueWarning") else "false",
"true" if self.getPreference(db, "ui.convertIssueToNote") else "false",
"true" if self.getPreference(db, "ui.asynchronousReviewMarking") else "false"))
else:
options = "{ ui: {} }"
return "new User(%d, %s, %s, %s, %s, %s)" % (self.id, jsify(self.name), jsify(self.email), jsify(self.fullname), jsify(self.status), options)
def getJS(self, db=None, name="user"):
return "var %s = %s;" % (name, self.getJSConstructor(db))
def getAbsence(self, db):
cursor = db.cursor()
cursor.execute("SELECT until FROM userabsence WHERE uid=%s", (self.id,))
row = cursor.fetchone()
return "absent until %04d-%02d-%02d" % (row[0].year, row[0].month, row[0].day)
def retire(self, db):
import review.utils
review.utils.retireUser(db, self)
@staticmethod
def cache(db, user):
storage = db.storage["User"]
storage[user.id] = user
if user.name: storage["n:" + user.name] = user
if user.email: storage["e:" + user.email] = user
return user
@staticmethod
def fromId(db, user_id):
cached_user = db.storage["User"].get(user_id)
if cached_user: return cached_user
else:
cursor = db.cursor()
cursor.execute("SELECT name, email, fullname, status FROM users WHERE id=%s", (user_id,))
row = cursor.fetchone()
if not row: return None
name, email, fullname, status = row
return User.cache(db, User(user_id, name, email, fullname, status))
@staticmethod
def fromIds(db, user_ids):
need_fetch = []
cache = db.storage["User"]
for user_id in user_ids:
if user_id not in cache:
need_fetch.append(user_id)
if need_fetch:
cursor = db.cursor()
cursor.execute("SELECT id, name, email, fullname, status FROM users WHERE id=ANY (%s)", (need_fetch,))
for user_id, name, email, fullname, status in cursor:
User.cache(db, User(user_id, name, email, fullname, status))
return [cache.get(user_id) for user_id in user_ids]
@staticmethod
def fromEmail(db, email):
cached_user = db.storage["User"].get("e:" + email)
if cached_user: return cached_user
else:
cursor = db.cursor()
cursor.execute("SELECT id, name, fullname, status FROM users WHERE email=%s", (email,))
row = cursor.fetchone()
if not row: return None
user_id, name, fullname, status = row
return User.cache(db, User(user_id, name, email, fullname, status))
@staticmethod
def fromName(db, name):
cached_user = db.storage["User"].get("n:" + name)
if cached_user: return cached_user
else:
cursor = db.cursor()
cursor.execute("SELECT id, email, fullname, status FROM users WHERE name=%s", (name,))
row = cursor.fetchone()
if not row: raise NoSuchUser, name
user_id, email, fullname, status = row
return User.cache(db, User(user_id, name, email, fullname, status))
def find_directory(db, path):
path = path.strip("/")
cursor = db.cursor()
try:
cursor.execute("SELECT finddirectory(%s)", (path,))
directory_id = cursor.fetchone()[0]
if directory_id is not None: return directory_id
except: pass
if "/" in path:
directory, name = path.rsplit("/", 1)
directory = find_directory(db, directory)
else:
directory, name = 0, path
cursor.execute("INSERT INTO directories (directory, name) VALUES (%s, %s) RETURNING id", (directory, name))
return cursor.fetchone()[0]
def is_directory(db, path):
cursor = db.cursor()
cursor.execute("SELECT finddirectory(%s)", (path,))
directory_id = cursor.fetchone()[0]
if directory_id is None: return False
cursor.execute("SELECT 1 FROM files WHERE directory=%s LIMIT 1", (directory_id,))
return bool(cursor.fetchone())
def is_file(db, path):
cursor = db.cursor()
cursor.execute("SELECT findfile(%s)", (path,))
file_id = cursor.fetchone()[0]
if file_id is None: return False
cursor.execute("SELECT 1 FROM fileversions WHERE file=%s LIMIT 1", (file_id,))
return bool(cursor.fetchone())
def find_file(db, path):
path = path.lstrip("/")
cursor = db.cursor()
try:
cursor.execute("SELECT findfile(%s)", (path,))
file_id = cursor.fetchone()[0]
if file_id is not None: return file_id
except: pass
if "/" in path:
directory, name = path.rsplit("/", 1)
directory = find_directory(db, directory)
else:
directory, name = 0, path
cursor.execute("INSERT INTO files (directory, name) VALUES (%s, %s) RETURNING id", (directory, name))
return cursor.fetchone()[0]
def find_files(db, files):
for file in files:
file.id = find_file(db, path=file.path)
def find_directory_file(db, path):
path = path.strip("/")
file_id = find_file(db, path)
if "/" in path:
directory_id = find_directory(db, path.rsplit("/", 1)[0])
else:
directory_id = 0
return directory_id, file_id
def describe_directory(db, directory_id):
cursor = db.cursor()
cursor.execute("SELECT fulldirectoryname(%s)", (directory_id,))
return cursor.fetchone()[0].rstrip("/")
def describe_file(db, file_id):
cursor = db.cursor()
cursor.execute("SELECT fullfilename(%s)", (file_id,))
return cursor.fetchone()[0]
def explode_path(db, invalid=None, file_id=None, directory_id=None):
assert invalid is None
assert (file_id is None) != (directory_id is None)
cursor = db.cursor()
path = []
if file_id is not None:
cursor.execute("SELECT * FROM filepath(%s)", (file_id,))
else:
path.append(directory_id)
if not directory_id: return path
cursor.execute("SELECT * FROM directorypath(%s)", (directory_id,))
for (directory_id,) in cursor:
path.insert(0, directory_id)
return path
def contained_files(db, directory_id):
cursor = db.cursor()
cursor.execute("SELECT file_out FROM containedfiles(%s)", (directory_id,))
return [file_id for (file_id,) in cursor]
class ReviewState:
def __init__(self, review, accepted, pending, reviewed, issues):
self.review = review
self.accepted = accepted
self.pending = pending
self.reviewed = reviewed
self.issues = issues
def getPercentReviewed(self):
if self.pending + self.reviewed:
return 100.0 * self.reviewed / (self.pending + self.reviewed)
else:
return 50.0
def getProgress(self):
if self.pending + self.reviewed == 0:
return "?? %"
percent = self.getPercentReviewed()
if int(percent) > 0 and (percent < 99.0 or percent == 100.0):
return "%d %%" % int(percent)
elif percent > 0:
precision = 1
while precision < 10:
progress = ("%%.%df" % precision) % percent
if progress[-1] != '0': break
precision += 1
return progress + " %"
else:
return "No progress"
def getIssues(self):
if self.issues: return "%d issue%s" % (self.issues, "s" if self.issues > 1 else "")
else: return ""
def __str__(self):
if self.review.state == 'dropped': return "Dropped..."
elif self.review.state == 'closed': return "Finished!"
elif self.accepted: return "Accepted!"
else:
progress = self.getProgress()
issues = self.getIssues()
if issues: return "%s and %s" % (progress, issues)
else: return progress
class Review:
def __init__(self, review_id, owners, review_type, branch, state, serial, summary, description, applyfilters, applyparentfilters):
self.id = review_id
self.owners = owners
self.type = review_type
self.repository = branch.repository
self.branch = branch
self.state = state
self.serial = serial
self.summary = summary
self.description = description
self.reviewers = []
self.watchers = {}
self.changesets = []
self.commentchains = None
self.applyfilters = applyfilters
self.applyparentfilters = applyparentfilters
self.filters = None
self.relevant_files = None
self.draft_status = None
@staticmethod
def isAccepted(db, review_id):
cursor = db.cursor()
cursor.execute("SELECT 1 FROM reviewfiles WHERE review=%s AND state='pending' LIMIT 1", (review_id,))
if cursor.fetchone(): return False
cursor.execute("SELECT 1 FROM commentchains WHERE review=%s AND type='issue' AND state='open' LIMIT 1", (review_id,))
if cursor.fetchone(): return False
return True
def accepted(self, db):
if self.state != 'open': return False
else: return Review.isAccepted(db, self.id)
def getReviewState(self, db):
cursor = db.cursor()
cursor.execute("""SELECT state, SUM(deleted) + SUM(inserted)
FROM reviewfiles
WHERE reviewfiles.review=%s
GROUP BY state""",
(self.id,))
pending = 0
reviewed = 0
for state, count in cursor.fetchall():
if state == "pending": pending = count
else: reviewed = count
cursor.execute("""SELECT count(id)
FROM commentchains
WHERE review=%s
AND type='issue'
AND state='open'""",
(self.id,))
issues = cursor.fetchone()[0]
return ReviewState(self, self.accepted(db), pending, reviewed, issues)
def containsCommit(self, db, commit):
commit_id = None
commit_sha1 = None
if isinstance(commit, gitutils.Commit):
if commit.id: commit_id = commit.id
else: commit_sha1 = commit.sha1
elif isinstance(commit, str):
commit_sha1 = self.repository.revparse(commit)
elif isinstance(commit, int):
commit_id = commit
else:
raise TypeError
cursor = db.cursor()
if commit_id is not None:
cursor.execute("""SELECT 1
FROM reviewchangesets
JOIN changesets ON (id=changeset)
WHERE reviewchangesets.review=%s
AND changesets.child=%s""",
(self.id, commit_id))
else:
cursor.execute("""SELECT 1
FROM reviewchangesets
JOIN changesets ON (changesets.id=reviewchangesets.changeset)
JOIN commits ON (commits.id=changesets.child)
WHERE reviewchangesets.review=%s
AND commits.sha1=%s""",
(self.id, commit_sha1))
return cursor.fetchone() is not None
def getCommentChains(self, db, user, skip=None):
import review.comment
import time
if self.commentchains is None:
if "commits" in skip and "lines" in skip:
self.commentchains = review.comment.CommentChain.fromReview(db, self, user)
else:
cursor = db.cursor()
if user: cursor.execute("SELECT id FROM commentchains WHERE review=%s AND (state!='draft' OR uid=%s) ORDER BY id DESC", [self.id, user.id])
else: cursor.execute("SELECT id FROM commentchains WHERE review=%s AND state!='draft' ORDER BY id DESC", [self.id])
self.commentchains = [review.comment.CommentChain.fromId(db, id, user, review=self, skip=skip) for (id,) in cursor.fetchall()]
return self.commentchains
def getJS(self):
return "var review = critic.review = { id: %d, branch: { id: %d, name: %r }, owners: [ %s ], serial: %d };" % (self.id, self.branch.id, self.branch.name, ", ".join(owner.getJSConstructor() for owner in self.owners), self.serial)
def getETag(self, db, user=None):
etag = "review%d.serial%d" % (self.id, self.serial)
if user:
items = self.getDraftStatus(db, user)
if any(items.values()):
etag += ".draft%d" % hash(tuple(sorted(items.items())))
cursor = db.cursor()
cursor.execute("SELECT id FROM reviewrebases WHERE review=%s AND uid=%s AND new_head IS NULL", (self.id, user.id))
row = cursor.fetchone()
if row:
etag += ".rebase%d" % row[0]
return '"%s"' % etag
def getURL(self, db, user=None, indent=0):
indent = " " * indent
if db and user:
url_prefixes = user.getCriticURLs(db)
else:
url_prefixes = [getURLPrefix(db)]
return "\n".join(["%s%s/r/%d" % (indent, url_prefix, self.id) for url_prefix in url_prefixes])
def getRecipients(self, db):
cursor = db.cursor()
cursor.execute("SELECT uid, include FROM reviewrecipientfilters WHERE review=%s ORDER BY uid ASC", (self.id,))
included = set(owner.id for owner in self.owners)
excluded = set()
for uid, include in cursor:
if include: included.add(uid)
elif uid not in self.owners: excluded.add(uid)
cursor.execute("SELECT uid FROM reviewusers WHERE review=%s", (self.id,))
recipients = []
for (user_id,) in cursor:
if user_id in excluded: continue
elif user_id not in included and 0 in excluded: continue
user = User.fromId(db, user_id)
if user.status != "retired":
recipients.append(user)
return recipients
def getDraftStatus(self, db, user):
if self.draft_status is None:
import review.utils
self.draft_status = review.utils.countDraftItems(db, user, self)
return self.draft_status
def incrementSerial(self, db):
self.serial += 1
db.cursor().execute("UPDATE reviews SET serial=%s WHERE id=%s", [self.serial, self.id])
def close(self, db, user):
self.serial += 1
db.cursor().execute("UPDATE reviews SET state='closed', serial=%s, closed_by=%s WHERE id=%s", (self.serial, user.id, self.id))
def drop(self, db, user):
self.serial += 1
db.cursor().execute("UPDATE reviews SET state='dropped', serial=%s, closed_by=%s WHERE id=%s", (self.serial, user.id, self.id))
def reopen(self, db, user):
self.serial += 1
db.cursor().execute("UPDATE reviews SET state='open', serial=%s, closed_by=NULL WHERE id=%s", (self.serial, self.id))
def setSummary(self, db, summary):
self.serial += 1
self.summary = summary
db.cursor().execute("UPDATE reviews SET summary=%s, serial=%s WHERE id=%s", [self.summary, self.serial, self.id])
def setDescription(self, db, description):
self.serial += 1
self.description = description
db.cursor().execute("UPDATE reviews SET description=%s, serial=%s WHERE id=%s", [self.description, self.serial, self.id])
def addOwner(self, db, owner):
if not owner in self.owners:
self.serial += 1
self.owners.append(owner)
cursor = db.cursor()
cursor.execute("SELECT 1 FROM reviewusers WHERE review=%s AND uid=%s", (self.id, owner.id))
if cursor.fetchone():
cursor.execute("UPDATE reviewusers SET owner=TRUE WHERE review=%s AND uid=%s", (self.id, owner.id))
else:
cursor.execute("INSERT INTO reviewusers (review, uid, owner) VALUES (%s, %s, TRUE)", (self.id, owner.id))
cursor.execute("SELECT id FROM trackedbranches WHERE repository=%s AND local_name=%s", (self.repository.id, self.branch.name))
row = cursor.fetchone()
if row:
trackedbranch_id = row[0]
cursor.execute("INSERT INTO trackedbranchusers (branch, uid) VALUES (%s, %s)", (trackedbranch_id, owner.id))
def removeOwner(self, db, owner):
if owner in self.owners:
self.serial += 1
self.owners.remove(owner)
cursor = db.cursor()
cursor.execute("UPDATE reviewusers SET owner=FALSE WHERE review=%s AND uid=%s", (self.id, owner.id))
cursor.execute("SELECT id FROM trackedbranches WHERE repository=%s AND local_name=%s", (self.repository.id, self.branch.name))
row = cursor.fetchone()
if row:
trackedbranch_id = row[0]
cursor.execute("DELETE FROM trackedbranchusers WHERE branch=%s AND uid=%s", (trackedbranch_id, owner.id))
def getReviewFilters(self, db):
cursor = db.cursor()
cursor.execute("SELECT directory, file, type, NULL, uid FROM reviewfilters WHERE review=%s", (self.id,))
return cursor.fetchall() or None
def getFilteredTails(self):
commitset = CommitSet(self.branch.commits)
return commitset.getFilteredTails(self.branch.repository)
def getRelevantFiles(self, db, user):
if not self.filters:
from review.filters import Filters
self.filters = Filters()
self.filters.load(db, review=self)
self.relevant_files = self.filters.getRelevantFiles(db, self)
cursor = db.cursor()
cursor.execute("SELECT assignee, file FROM fullreviewuserfiles WHERE review=%s", (self.id,))
for user_id, file_id in cursor:
self.relevant_files.setdefault(user_id, set()).add(file_id)
return self.relevant_files.get(user.id, set())
def getUserAssociation(self, db, user):
cursor = db.cursor()
association = []
if user in self.owners:
association.append("owner")
cursor.execute("""SELECT 1
FROM reviewchangesets
JOIN changesets ON (changesets.id=reviewchangesets.changeset)
JOIN commits ON (commits.id=changesets.child)
JOIN gitusers ON (gitusers.id=commits.author_gituser)
JOIN usergitemails USING (email)
WHERE reviewchangesets.review=%s
AND usergitemails.uid=%s""",
(self.id, user.id))
if cursor.fetchone():
association.append("author")
cursor.execute("SELECT COUNT(*) FROM fullreviewuserfiles WHERE review=%s AND assignee=%s", (self.id, user.id))
if cursor.fetchone()[0] != 0:
association.append("reviewer")
elif user not in self.owners:
cursor.execute("SELECT 1 FROM reviewusers WHERE review=%s AND uid=%s", (self.id, user.id))
if cursor.fetchone():
association.append("watcher")
if not association:
association.append("none")
return ", ".join(association)
@staticmethod
def fromId(db, review_id, branch=None, load_commits=True, profiler=None):
cursor = db.cursor()
cursor.execute("SELECT type, branch, state, serial, summary, description, applyfilters, applyparentfilters FROM reviews WHERE id=%s", [review_id])
row = cursor.fetchone()
if not row: return None
type, branch_id, state, serial, summary, description, applyfilters, applyparentfilters = row
if profiler: profiler.check("Review.fromId: basic")
if branch is None:
branch = Branch.fromId(db, branch_id, load_review=False, load_commits=load_commits, profiler=profiler)
cursor.execute("SELECT uid FROM reviewusers WHERE review=%s AND owner", (review_id,))
owners = User.fromIds(db, [user_id for (user_id,) in cursor])
if profiler: profiler.check("Review.fromId: owners")
review = Review(review_id, owners, type, branch, state, serial, summary, description, applyfilters, applyparentfilters)
branch.review = review
# Reviewers: all users that have at least one review file assigned to them.
cursor.execute("""SELECT DISTINCT uid, assignee IS NOT NULL, type
FROM reviewusers
LEFT OUTER JOIN fullreviewuserfiles ON (fullreviewuserfiles.review=reviewusers.review AND assignee=uid)
WHERE reviewusers.review=%s""",
(review_id,))
reviewers = []
watchers = []
watcher_types = {}
for user_id, is_reviewer, user_type in cursor.fetchall():
if is_reviewer:
reviewers.append(user_id)
elif user_id not in review.owners:
watchers.append(user_id)
watcher_types[user_id] = user_type
review.reviewers = User.fromIds(db, reviewers)
for watcher in User.fromIds(db, watchers):
review.watchers[watcher] = watcher_types[watcher]
if profiler: profiler.check("Review.fromId: users")
if load_commits:
review.branch.loadCommits(db)
cursor.execute("""SELECT id
FROM reviewchangesets
JOIN changesets ON (id=changeset)
WHERE review=%s
AND child=ANY (%s)""", (review_id, [commit.id for commit in review.branch.commits]))
review.changesets = [changeset_id for (changeset_id,) in cursor.fetchall()]
if profiler: profiler.check("Review.fromId: load commits")
return review
@staticmethod
def fromBranch(db, branch):
if branch:
cursor = db.cursor()
cursor.execute("SELECT id FROM reviews WHERE branch=%s", [branch.id])
row = cursor.fetchone()
if not row: return None
else: return Review.fromId(db, row[0], branch)
else:
return None
@staticmethod
def fromName(db, repository, name):
return Review.fromBranch(db, Branch.fromName(db, repository, name))
@staticmethod
def fromArgument(db, argument):
try:
return Review.fromId(db, int(argument))
except:
branch = Branch.fromName(db, str(argument))
if not branch: return None
return Review.fromBranch(db, branch)
class Branch:
def __init__(self, id, repository, name, head, base, tail, branch_type, review_id):
self.id = id
self.repository = repository
self.name = name
self.head = head
self.base = base
self.tail = tail
self.type = branch_type
self.review_id = review_id
self.review = None
self.commits = None
def __eq__(self, other):
return self.id == other.id
def __ne__(self, other):
return self.id != other.id
def contains(self, db, commit):
cursor = db.cursor()
if isinstance(commit, gitutils.Commit) and commit.id is not None:
cursor.execute("SELECT 1 FROM reachable WHERE branch=%s AND commit=%s", [self.id, commit.id])
else:
cursor.execute("SELECT 1 FROM reachable, commits WHERE branch=%s AND commit=id AND sha1=%s", [self.id, str(commit)])
return cursor.fetchone() is not None
def getJSConstructor(self):
if self.base:
return "new Branch(%d, %s, %s)" % (self.id, jsify(self.name), self.base.getJSConstructor())
else:
return "new Branch(%d, %s, null)" % (self.id, jsify(self.name))
def getJS(self):
return "var branch = %s;" % self.getJSConstructor()
def loadCommits(self, db):
if self.commits is None:
cursor = db.cursor()
cursor.execute("SELECT commits.id, commits.sha1 FROM reachable, commits WHERE reachable.branch=%s AND reachable.commit=commits.id", [self.id])
self.commits = []
for commit_id, sha1 in cursor:
self.commits.append(gitutils.Commit.fromSHA1(db, self.repository, sha1, commit_id=commit_id))
def rebase(self, db, base):
cursor = db.cursor()
def findReachable(head, base_branch_id, force_include=set()):
bases = [base_branch_id]
while True:
cursor.execute("SELECT base FROM branches WHERE id=%s", (bases[-1],))
branch_id = cursor.fetchone()[0]
if branch_id is None: break
bases.append(branch_id)
expression = "SELECT 1 FROM reachable, commits WHERE branch IN (%s) AND commit=id AND sha1=%%s" % ", ".join(["%s"] * len(bases))
def exclude(sha1):
cursor.execute(expression, bases + [sha1])
return cursor.fetchone() is not None
stack = [head.sha1]
processed = set()
values = []
while stack:
sha1 = stack.pop(0)
if sha1 not in processed:
processed.add(sha1)
commit = gitutils.Commit.fromSHA1(db, self.repository, sha1)
if sha1 in force_include or not exclude(sha1):
values.append(commit.getId(db))
for sha1 in commit.parents:
if sha1 not in processed and (sha1 in force_include or not exclude(sha1)):
stack.append(sha1)
return values
cursor.execute("SELECT COUNT(*) FROM reachable WHERE branch=%s", (self.id,))
old_count = cursor.fetchone()[0]
if base.base and base.base.id == self.id:
self.loadCommits(db)
cursor.execute("SELECT count(*) FROM reachable WHERE branch=%s", (base.id,))
base_old_count = cursor.fetchone()[0]
base_reachable = findReachable(base.head, self.base.id, set([commit.sha1 for commit in self.commits]))
base_new_count = len(base_reachable)
cursor.execute("DELETE FROM reachable WHERE branch=%s", [base.id])
cursor.executemany("INSERT INTO reachable (branch, commit) VALUES (%s, %s)", [(base.id, commit) for commit in base_reachable])
cursor.execute("UPDATE branches SET base=%s WHERE id=%s", [self.base.id, base.id])
base.base = self.base
base.commits = None