def testAliceAndBadPhid(self):
        users = phlcon_user.query_users_from_usernames(
            self.conduit, [self.test_user])
        self.assertEqual(len(users), 1)
        self.assertEqual(users[0].userName, self.test_user)

        bad_phid = "asd9f87"
        phidDict = phlcon_user.make_phid_username_dict(
            self.conduit, [users[0].phid, bad_phid])
        self.assertIsNone(phidDict)
    def testAliceUsername(self):
        users = phlcon_user.query_users_from_usernames(
            self.conduit, [self.test_user])
        self.assertEqual(len(users), 1)
        self.assertEqual(users[0].userName, self.test_user)

        userDict = phlcon_user.make_username_phid_dict(
            self.conduit, [self.test_user])
        self.assertEqual(len(userDict), 1)
        self.assertEqual(userDict[self.test_user], users[0].phid)

        username = users[0].userName
        phid = users[0].phid
        phidDict = phlcon_user.make_phid_username_dict(self.conduit, [phid])
        self.assertEqual(len(phidDict), 1)
        self.assertEqual(phidDict[phid], username)
def _translate_user_phids(conduit, results):
    # gather user PHIDs
    user_phids = set()
    for r in results:
        user_phids.add(r["authorPHID"])
        for u in r["ccs"]:
            user_phids.add(u)
        for u in r["reviewers"]:
            user_phids.add(u)

    # get the names back
    phidToUser = {}
    user_phids = list(user_phids)
    if user_phids:
        phidToUser = phlcon_user.make_phid_username_dict(conduit, user_phids)

    # do the translation
    for r in results:
        r[u"authorUsername"] = phidToUser[r["authorPHID"]]
        r[u"ccUsernames"] = [phidToUser[u] for u in r["ccs"]]
        r[u"reviewerUsernames"] = [phidToUser[u] for u in r["reviewers"]]
def _translate_user_phids(conduit, results):
    # gather user PHIDs
    user_phids = set()
    for r in results:
        user_phids.add(r["authorPHID"])
        for u in r["ccs"]:
            user_phids.add(u)
        for u in r["reviewers"]:
            user_phids.add(u)

    # get the names back
    phidToUser = {}
    user_phids = list(user_phids)
    if user_phids:
        phidToUser = phlcon_user.make_phid_username_dict(conduit, user_phids)

    # do the translation
    for r in results:
        r[u"authorUsername"] = phidToUser[r["authorPHID"]]
        r[u"ccUsernames"] = [phidToUser[u] for u in r["ccs"]]
        r[u"reviewerUsernames"] = [phidToUser[u] for u in r["reviewers"]]
 def testBadPhid(self):
     bad_phid = "asd9f87"
     phidDict = phlcon_user.make_phid_username_dict(
         self.conduit, [bad_phid])
     self.assertIsNone(phidDict)
def process(args):
    conduit = phlsys_makeconduit.make_conduit(args.uri, args.user, args.cert)
    me = conduit.get_user()

    d = {}

    def processUserField(name, param, add_me):
        d[name] = param
        if add_me:
            d[name].append(me)

    processUserField("authors", args.authors, args.author_me)
    processUserField("reviewers", args.reviewers, args.reviewer_me)
    processUserField("ccs", args.ccs, args.cc_me)
    processUserField("subscribers", args.subscribers, args.subscriber_me)
    processUserField(
        "responsibleUsers",
        args.responsible_users,
        args.responsible_me)

    users = [u for users in d.itervalues() for u in users]
    users = list(set(users))
    userToPhid = {}
    if users:
        userToPhid = phlcon_user.make_username_phid_dict(conduit, users)

    # XXX: check for duplicates in author and reviewer
    # XXX: check for bad userToPhid
    for key in d.iterkeys():
        d[key] = [userToPhid[u] for u in d[key]]

    if args.ids:
        d["ids"] = args.ids

    if args.ids_stdin:
        ids = [int(i) for i in " ".join(sys.stdin.readlines()).split()]
        d["ids"] = args.ids + ids

    if args.status_type:
        d["status"] = "status-" + args.status_type

    if args.arcanist_projects:
        d["arcanistProjects"] = args.arcanist_projects

    if args.max_results:
        d["limit"] = args.max_results

    if args.offset_results:
        d["offset"] = args.offset_results

    # perform the query
    results = conduit.call("differential.query", d)

    # apply filters

    if args.statuses:
        new_results = []
        for r in results:
            if r["statusName"] in args.statuses:
                new_results.append(r)
        results = new_results

    if args.update_min_age:
        now = datetime.datetime.now()
        new_results = []
        for r in results:
            modified = datetime.datetime.fromtimestamp(
                float(r["dateModified"]))
            age = now - modified
            if age >= args.update_min_age:
                new_results.append(r)
        results = new_results

    if args.update_max_age:
        now = datetime.datetime.now()
        new_results = []
        for r in results:
            modified = datetime.datetime.fromtimestamp(
                float(r["dateModified"]))
            age = now - modified
            if age <= args.update_max_age:
                new_results.append(r)
        results = new_results

    # apply transformations

    if args.translate:
        # gather user PHIDs
        user_phids = set()
        for r in results:
            user_phids.add(r["authorPHID"])
            for u in r["ccs"]:
                user_phids.add(u)
            for u in r["reviewers"]:
                user_phids.add(u)

        # get the names back
        phidToUser = {}
        user_phids = list(user_phids)
        if user_phids:
            phidToUser = phlcon_user.make_phid_username_dict(
                conduit, user_phids)

        # do the translation
        for r in results:
            r[u"authorUsername"] = phidToUser[r["authorPHID"]]
            r[u"ccUsernames"] = [phidToUser[u] for u in r["ccs"]]
            r[u"reviewerUsernames"] = [phidToUser[u] for u in r["reviewers"]]

    for r in results:
        r[u"humanTimeSinceDateModified"] = humanTimeSince(
            datetime.datetime.fromtimestamp(float(r["dateModified"])))
        r[u"humanTimeSinceDateCreated"] = humanTimeSince(
            datetime.datetime.fromtimestamp(float(r["dateCreated"])))

    # output results

    if not args.format_type and not args.format_string:
        args.format_type = "short"
    if args.format_type:
        if args.format_type == "json":
            print json.dumps(results, sort_keys=True, indent=2)
        elif args.format_type == "python":
            pprint.pprint(results)
        elif args.format_type == "short":
            shortTemplate = string.Template("$id / $statusName / $title")
            for x in results:
                print shortTemplate.safe_substitute(x)
        elif args.format_type == "ids":
            shortTemplate = string.Template("$id")
            for x in results:
                print shortTemplate.safe_substitute(x)
        else:
            raise Exception("unsupported format")
    else:
        template = string.Template(args.format_string)
        for x in results:
            print template.safe_substitute(x)