Mercurial > hg
annotate contrib/phab-clean.py @ 45095:8e04607023e5
procutil: ensure that procutil.std{out,err}.write() writes all bytes
Python 3 offers different kind of streams and it’s not guaranteed for all of
them that calling write() writes all bytes.
When Python is started in unbuffered mode, sys.std{out,err}.buffer are
instances of io.FileIO, whose write() can write less bytes for
platform-specific reasons (e.g. Linux has a 0x7ffff000 bytes maximum and could
write less if interrupted by a signal; when writing to Windows consoles, it’s
limited to 32767 bytes to avoid the "not enough space" error). This can lead to
silent loss of data, both when using sys.std{out,err}.buffer (which may in fact
not be a buffered stream) and when using the text streams sys.std{out,err}
(I’ve created a CPython bug report for that:
https://bugs.python.org/issue41221).
Python may fix the problem at some point. For now, we implement our own wrapper
for procutil.std{out,err} that calls the raw stream’s write() method until all
bytes have been written. We don’t use sys.std{out,err} for larger writes, so I
think it’s not worth the effort to patch them.
author | Manuel Jacob <me@manueljacob.de> |
---|---|
date | Fri, 10 Jul 2020 12:27:58 +0200 |
parents | 064eb65d040f |
children | c102b704edb5 |
rev | line source |
---|---|
44217
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
2 # |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
3 # A small script to automatically reject idle Diffs |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
4 # |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
5 # you need to set the PHABBOT_USER and PHABBOT_TOKEN environment variable for authentication |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
6 from __future__ import absolute_import, print_function |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
7 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
8 import datetime |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
9 import os |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
10 import sys |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
11 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
12 import phabricator |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
13 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
14 MESSAGE = """There seems to have been no activities on this Diff for the past 3 Months. |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
15 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
16 By policy, we are automatically moving it out of the `need-review` state. |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
17 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
18 Please, move it back to `need-review` without hesitation if this diff should still be discussed. |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
19 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
20 :baymax:need-review-idle: |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
21 """ |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
22 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
23 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
24 PHAB_URL = "https://phab.mercurial-scm.org/api/" |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
25 USER = os.environ.get("PHABBOT_USER", "baymax") |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
26 TOKEN = os.environ.get("PHABBOT_TOKEN") |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
27 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
28 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
29 NOW = datetime.datetime.now() |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
30 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
31 # 3 months in seconds |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
32 DELAY = 60 * 60 * 24 * 30 * 3 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
33 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
34 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
35 def get_all_diff(phab): |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
36 """Fetch all the diff that the need review""" |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
37 return phab.differential.query( |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
38 status="status-needs-review", |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
39 order="order-modified", |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
40 paths=[('HG', None)], |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
41 ) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
42 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
43 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
44 def filter_diffs(diffs, older_than): |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
45 """filter diffs to only keep the one unmodified sin <older_than> seconds""" |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
46 olds = [] |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
47 for d in diffs: |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
48 modified = int(d['dateModified']) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
49 modified = datetime.datetime.fromtimestamp(modified) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
50 d["idleFor"] = idle_for = NOW - modified |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
51 if idle_for.total_seconds() > older_than: |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
52 olds.append(d) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
53 return olds |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
54 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
55 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
56 def nudge_diff(phab, diff): |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
57 """Comment on the idle diff and reject it""" |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
58 diff_id = int(d['id']) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
59 phab.differential.createcomment( |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
60 revision_id=diff_id, message=MESSAGE, action="reject" |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
61 ) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
62 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
63 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
64 if not USER: |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
65 print( |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
66 "not user specified please set PHABBOT_USER and PHABBOT_TOKEN", |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
67 file=sys.stderr, |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
68 ) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
69 elif not TOKEN: |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
70 print( |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
71 "not api-token specified please set PHABBOT_USER and PHABBOT_TOKEN", |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
72 file=sys.stderr, |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
73 ) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
74 sys.exit(1) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
75 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
76 phab = phabricator.Phabricator(USER, host=PHAB_URL, token=TOKEN) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
77 phab.connect() |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
78 phab.update_interfaces() |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
79 print('Hello "%s".' % phab.user.whoami()['realName']) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
80 |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
81 diffs = get_all_diff(phab) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
82 print("Found %d Diffs" % len(diffs)) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
83 olds = filter_diffs(diffs, DELAY) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
84 print("Found %d old Diffs" % len(olds)) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
85 for d in olds: |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
86 diff_id = d['id'] |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
87 status = d['statusName'] |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
88 modified = int(d['dateModified']) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
89 idle_for = d["idleFor"] |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
90 msg = 'nudging D%s in "%s" state for %s' |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
91 print(msg % (diff_id, status, idle_for)) |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
92 # uncomment to actually affect phab |
064eb65d040f
contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
93 nudge_diff(phab, d) |