aboutsummaryrefslogtreecommitdiff
path: root/fab_update_user.py
blob: 1e84909a660d87b81ae8bf771b1475e9aac35fa2 (plain)
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
#!/usr/bin/env python
import os
import argparse
import time
import json
import multiprocessing
import sys
import collections
from phabricator import Phabricator
from wmfphablib import Phab as phabmacros
from wmfphablib import phabdb
from wmfphablib import log
from wmfphablib import config
from wmfphablib import util
from wmfphablib import vlog
from wmfphablib import epoch_to_datetime
from wmfphablib import now
from wmfphablib import return_bug_list
from wmfphablib import ipriority


def update(user):
    vlog(user)
    phab = Phabricator(config.phab_user,
                       config.phab_cert,
                       config.phab_host)

    pmig = phabdb.phdb(db=config.fabmigrate_db,
                       user=config.fabmigrate_user,
                       passwd=config.fabmigrate_passwd)

    phabm = phabmacros('', '', '')
    phabm.con = phab

    if phabdb.is_bot(user['userphid']):
        log("%s is a bot no action" % (user['user']))
        return True

    epriority = phabdb.get_user_relations_priority(user['user'], pmig)
    if epriority and epriority[0] == ipriority['update_success']:
        log('Skipping %s as already updated' % (user['user']))
        return True

    # 'author': [409, 410, 411, 404, 412],
    # 'cc': [221, 69, 203, 268, 261, 8],
    # 'created': 1410276037L,
    # 'modified': 1410276060L,
    # 'assigned': [97, 64, 150, 59, 6],
    # 'userphid': 'PHID-USER-4hsexplytovmqmcb7tq2',
    # 'user': u'chase.mp@xxx.com'}

    if user['assigned']:
        for ag in user['assigned']:
             vlog(phabm.sync_assigned(user['userphid'], ag, 'fl'))

    if user['author']:
        for a in user['author']:
            vlog(phabm.synced_authored(user['userphid'], a, 'fl'))

    if user['cc']:
        for ccd in user['cc']:
            vlog(phabdb.add_task_cc_by_ref(user['userphid'], ccd))

    current = phabdb.get_user_migration_history(user['user'], pmig)
    if current:
        log(phabdb.set_user_relations_priority(ipriority['update_success'], user['user'], pmig))
    else:
        log('%s user does not exist to update' % (user['user']))
        return False
    pmig.close()
    return True

def run_update(user, tries=1):
    if tries == 0:
        pmig = phabdb.phdb(db=config.fabmigrate_db,
                       user=config.fabmigrate_user,
                       passwd=config.fabmigrate_passwd)
        current = phabdb.get_user_migration_history(user['user'], pmig)
        if current:
           log(phabdb.set_user_relations_priority(ipriority['update_failed'], user['user'], pmig))
        else:
            log('%s user does not exist to update' % (user['user']))
        pmig.close()
        log('final fail to update %s' % (user['user'],))
        return False
    try:
        if update(user):
            log('%s done with %s' % (str(int(time.time())), user,))
            return True
    except Exception as e:
        import traceback
        tries -= 1
        time.sleep(5)
        traceback.print_exc(file=sys.stdout)
        log('failed to update %s (%s)' % (user, e))
        return run_update(user, tries=tries)

def get_user_histories(verified):
    histories = []
    pmig = phabdb.phdb(db=config.fabmigrate_db,
                       user=config.fabmigrate_user,
                       passwd=config.fabmigrate_passwd)
    #print 'verified', verified
    for v in verified:
        vlog(str(v))
        # Get user history from old fab system
        saved_history = phabdb.get_user_migration_history(v[1], pmig)
        if not saved_history:
            log('%s verified email has no saved history' % (v[1],))
            continue
        log('%s is being processed' % (v[1],))
        history = {}
        history['user'] = v[1]
        history['userphid'] = v[0]
        history['assigned'] = saved_history[0]
        history['cc'] = saved_history[1]
        history['author'] = saved_history[2]
        history['created'] = saved_history[3]
        history['modified'] = saved_history[4]
        histories.append(history)
    pmig.close()
    return [util.translate_json_dict_items(d) for d in histories]

def get_verified_users(modtime, limit=None):
    #Find the task in new Phabricator that matches our lookup
    verified = phabdb.get_verified_emails(modtime=modtime, limit=limit)
    create_times = [v[2] for v in verified]
    try:
        newest = max(create_times)
    except ValueError:
        newest = modtime
    return verified, newest

def get_verified_user(email):
    phid, email, is_verified = phabdb.get_user_email_info(email)
    log("Single verified user: %s, %s, %s" % (phid, email, is_verified))
    if is_verified:
        return [(phid, email)]
    else:
        log("%s is not a verified email" % (email,))
        return [()]

def last_finish():
    pmig = phabdb.phdb(db=config.fabmigrate_db,
                       user=config.fabmigrate_user,
                       passwd=config.fabmigrate_passwd)
    pmig.close()
    ftime = phabdb.get_user_relations_last_finish(pmig)
    return ftime or 1

def main():
    parser = argparse.ArgumentParser(description='Updates user metadata from fab')
    parser.add_argument('-a', action="store_true", default=False)
    parser.add_argument('-e', action="store", dest='email')
    parser.add_argument('-m', action="store", dest="starting_epoch", default=None)
    parser.add_argument('-v', action="store_true", default=False)
    args =  parser.parse_args()

    pmig = phabdb.phdb(db=config.fabmigrate_db,
                       user=config.fabmigrate_user,
                       passwd=config.fabmigrate_passwd)

    if args.a:
        starting_epoch = phabdb.get_user_relations_last_finish(pmig)
        users, finish_epoch = get_verified_users(starting_epoch, config.fab_limit)
    elif args.email:
        users = get_verified_user(args.email)
        starting_epoch = 0
        finish_epoch = 0
    elif args.starting_epoch:
        users, finish_epoch = get_verified_users(args.starting_epoch)
        starting_epoch = args.starting_epoch
    else:
        parser.print_help()
        sys.exit(1)

    if not any(users):
        log("Existing as there are no new verified users")
        sys.exit()

    histories = get_user_histories(filter(bool, users))
    user_count = len(histories)

    icounts = []
    for u in histories:
        c = 0
        if u['cc']:
            c += len(u['cc'])
        if u['author']:
            c += len(u['author'])
        if u['assigned']:
            c += len(u['assigned'])
        icounts.append(c)
    issue_count = sum(icounts)

    log("User Count %s" % (str(user_count)))
    log("Issue Count %s" % (str(issue_count)))

    if user_count == 0:
        log("Existing as there are no new verified users")
        sys.exit()


    pid = os.getpid()
    phabdb.user_relations_start(pid,
                                int(time.time()),
                                0,
                                starting_epoch,
                                user_count, issue_count, pmig)
    from multiprocessing import Pool
    pool = Pool(processes=config.fab_multi)
    _ =  pool.map(run_update, histories)
    complete = len(filter(bool, _))
    failed = len(_) - complete
    phabdb.user_relations_finish(pid,
                                 int(time.time()),
                                 ipriority['update_success'],
                                 finish_epoch,
                                 complete,
                                 failed,
                                 pmig)
    print '%s completed %s, failed %s' % (sys.argv[0], complete, failed)
    pmig.close()

if __name__ == '__main__':
    main()