aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Holmes <mike.holmes@linaro.org>2017-08-04 22:19:21 -0400
committerMike Holmes <mike.holmes@linaro.org>2017-08-04 22:19:21 -0400
commitcfa31e50c43063a5e6efbee6e8b3ae314f6d2b03 (patch)
treee8f9d4f9b3dbba97231da39d2f4f18e848d0cf5d
parent8536f851f1dfef74782aed5ea4f60f9d7bfa885f (diff)
Use LDAP to find watcher company
-rw-r--r--company_interest.R96
-rwxr-xr-xlinaro-ldap-lookup.py138
2 files changed, 192 insertions, 42 deletions
diff --git a/company_interest.R b/company_interest.R
index b7ac15b..d518557 100644
--- a/company_interest.R
+++ b/company_interest.R
@@ -5,53 +5,65 @@ library(gsubfn)
library(gplots)
library(RColorBrewer)
+#clean any junk
+rm(list = ls())
+
+#need hash to work for lists
+people_company_dictionary<- new.env(hash=TRUE)
+
#functions
companies <- function (names)
{
companys = list()
for (name in names) {
for (n in name) {
- companys[[length(companys) + 1]] <-
- strapplyc(n, "@(.*)", simplify = TRUE)
+
+ company <- member_company(n)
+ if (company != '') {
+ companys[[length(companys) + 1]] <- member_company(n)
+ } else {
+ #cant find them in ldap, use the email string as a guess
+ #This means that in many cases people no linger in LDAP get counted as Lianro folks as
+ #the original member company can no longer be determined.
+ warning(paste(n,"not found in LDAP, guessing from email string"))
+ companys[[length(companys) + 1]] <- strapplyc(n, "@(.*)", simplify = TRUE)
+ }
}
}
company <- unique(companys)
-
+
return (company)
}
-display_hetmap <- function(report, m, suffix) {
- report_name <- paste(report["name"], suffix, ".png",sep = "")
-png(file=report_name, title = report["name"])
-
- #display raw data
- print(m)
- cat("Generating", report_name)
-
- # following code limits the lowest and highest color to 5%, and 95% of your range, respectively
- quantile.range <- quantile(m, probs = seq(0, 1, 0.01))
- palette.breaks <-
- seq(quantile.range["5%"], quantile.range["95%"], 0.1)
-
- # use http://colorbrewer2.org/ to find optimal divergent color palette (or set own)
- color.palette <-
- colorRampPalette(c("#FC8D59", "#FFFFBF", "#91CF60"))(length(palette.breaks) - 1)
+member_company <- function (name)
+{
+ company <- people_company_dictionary[[name]]
+ if (is.null(company)) {
+ #not in hash, need to look it up
+ ldap_command <-
+ paste ('python',
+ '/home/mike/bin/linaro-ldap-lookup.py',
+ 'mike.holmes',
+ name)
+
+ ldap_raw <- as.character(system(ldap_command, intern = TRUE))
+
+ if (length(ldap_raw) == 0) {
+ #that name could not be found
+ company <- ""
+ } else {
+ #Pass back just the company
+ ldap <- fromJSON(ldap_raw, simplifyDataFrame = TRUE)
+ company <- ldap$company
+ }
+
+ #fix any names that look like directories
+ company <- gsub("/", "_", company)
+ #save in dictionary
+ people_company_dictionary[[name]] <- company
+ }
- watcher_heatmap <-
- heatmap.2(
- m,
- dendrogram = 'row',
- Rowv = TRUE,
- Colv = FALSE,
- trace = "none",
- margins = c(12, 12),
- col = color.palette,
- breaks = palette.breaks,
- ylab = "company",
- xlab = "project"
- )
- #ls
- dev.off()
+ return (company)
}
display_barplot <- function(report, m, suffix) {
@@ -77,7 +89,7 @@ display_barplot <- function(report, m, suffix) {
dev.off()
}
-watcher_heatmap <- function(report) {
+watcher_barplot <- function(report) {
baseurl <- "https://projects.linaro.org/rest/api/2/"
fields <- "&fields=watches,key,project"
@@ -145,13 +157,12 @@ watcher_heatmap <- function(report) {
}
}
+ print ( member_company ('mike.holmes@linaro.org'))
#remove companies
# rows.to.delete <- c('linaro.org', 'broadcom.com', 'character')
# for (d in rows.to.delete) {
# m <- as.matrix(m[!grepl(d, rownames(m)),])
# }
- dir.create("company")
- setwd("company")
for (c in comp) {
v <- as.vector(m[c,])
@@ -164,7 +175,7 @@ watcher_heatmap <- function(report) {
}
-sponsor_heatmap <- function (report) {
+sponsor_barplot <- function (report) {
baseurl <- "https://projects.linaro.org/rest/api/2/"
fields <- "&fields=customfield_10101,key,project"
type <- "search?"
@@ -254,7 +265,7 @@ if (id[2] == '') {
}
if (id[3] == '') {
- home <- paste(getwd(),"/git", sep="")
+ home <- paste(getwd(),"/company", sep="")
} else {
home <- paste(id[3],"/company", sep="")
}
@@ -273,15 +284,16 @@ handle_setopt(h, httpauth = 1)
report_name <- c("ti")
team_list <-
c(
- "Security,Kernel,'Power',Virtualization,LSK,LHG,LEG,LNG,LITE,LMG,CTT,BB,LAVA,LAB,QA,SYS,TCWG"
+ "SWG,KWG,PMWG,VIRT,LSK,LHG,LEG,LNG,LITE,LMG,CTT,BB,LAVA,LAB,QA,SYS,TCWG"
)
reports <-
data.frame(name = report_name ,
team = team_list,
stringsAsFactors = FALSE)
+apply(reports, 1, sponsor_barplot)
+apply(reports, 1, watcher_barplot)
-apply(reports, 1, sponsor_heatmap)
-apply(reports, 1, watcher_heatmap)
+warning()
setwd(home) \ No newline at end of file
diff --git a/linaro-ldap-lookup.py b/linaro-ldap-lookup.py
new file mode 100755
index 0000000..b2ea8c6
--- /dev/null
+++ b/linaro-ldap-lookup.py
@@ -0,0 +1,138 @@
+#!/usr/bin/python2
+#
+# Looks up the Linaro LDAP directory for contact information using the
+# partial name provided.
+#
+# This script is intended to be used with mutt - add the following line
+# into your muttrc file (make sure this script is on the path):
+#
+# set query_command="linaro-ldap-lookup your.name"
+#
+# This script uses user's keyring to retrieve LDAP password - run it
+# from the command line to store the password for the first time:
+# $ linaro-ldap-lookup your.name anything
+#
+# It will prompt for a password and will store it into the keyring.
+#
+# Dependencies:
+# python module 'ldap'
+# used to interact with LDAP server
+#
+# python module 'keyring'
+# used for storing LDAP server password into the user's keyring
+#
+# License: GPLv2 (see gpl-2.0.txt file for details)
+#
+# By Serge Broslavsky <serge.broslavsky@linaro.org>
+#
+
+from __future__ import print_function
+
+from sys import stdin, stderr, argv, exit
+import getpass
+
+try:
+ import ldap
+except ImportError:
+ print('ldap module is not available - please install', file=stderr)
+
+try:
+ import keyring
+except ImportError:
+ print('keyring module is not available - please install', file=stderr)
+
+# server to use
+LDAP_SERVER = 'ldaps://login.linaro.org'
+
+
+def main():
+
+ if len(argv) != 3:
+ print('usage: %s <user.name> <partial-name>' % argv[0])
+ return 1
+
+ ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_ALLOW)
+ l = ldap.initialize(LDAP_SERVER)
+ username = 'uid=%s,ou=staff,ou=accounts,dc=linaro,dc=org' % \
+ argv[1]
+ try:
+ l.protocol_version = ldap.VERSION3
+ l.simple_bind_s(username,
+ get_user_password(LDAP_SERVER, argv[1]))
+ except Exception, error:
+ print('Error: ' + str(error))
+ return 2
+
+ baseDN = 'ou=staff,ou=accounts,dc=linaro,dc=org'
+ searchScope = ldap.SCOPE_SUBTREE
+ retrieveAttributes = ['mail', 'description', 'telephoneNumber',
+ 'mobile', 'postalAddress','employeeType','manager','title','labeledURI','departmentNumber','o']
+ searchFilter = 'cn=*%s*' % argv[2]
+
+ try:
+ results = l.search_st(baseDN, searchScope, searchFilter,
+ retrieveAttributes,
+ timeout=15)
+ #print('Search results from Linaro LDAP:')
+
+
+ if results:
+ for path, attrs in results:
+ mpho = attrs.get('mobile', (None,))[0]
+ lpho = attrs.get('telephoneNumber', (None,))[0]
+ addr = attrs.get('postalAddress', (None,))[0]
+ employeeType = attrs.get('employeeType', (None,))[0]
+ manager = attrs.get('manager', (None,))[0]
+ title = attrs.get('title', (None,))[0]
+ department = attrs.get('departmentNumber', (None,))[0]
+ company = attrs.get('o', (None,))[0]
+ print('{' + \
+ '"email":"' + attrs.get('mail', ('N/A',))[0] + '", ' + \
+ '"name":"' + attrs.get('description',('N/A',))[0] + '"' + \
+ ''.join((
+ (', "employeeType":"' + employeeType + '"') if employeeType is not None \
+ else '',\
+ (', "title":"' + title + '"') if title is not None \
+ else '',\
+ (', "department":"' + department + '"') if department is not None \
+ else '',\
+ (', "company":"' + company + '"') if company is not None \
+ else '',\
+ (', "manager":"' + manager + '"') if manager is not None \
+ else ''))
+ + '}')
+
+
+ return 0
+ except ldap.LDAPError, e:
+ print('Error: ' + str(e))
+ return 3
+
+#
+# Get user password
+#
+def get_user_password(server, user):
+
+ password = keyring.get_password(server, user)
+ if password:
+ return password
+
+ if stdin.isatty():
+ prompt = 'Please enter password for %s on %s:' %\
+ (user, server)
+ password = getpass.getpass(prompt)
+ else:
+ password = stdin.readline().replace('\n','')
+
+ print('Storing password into the default keyring '\
+ 'as %s on %s' % (user, server))
+ try:
+ keyring.set_password(server,user, password)
+ except keyring.errors.PasswordSetError, e:
+ print('failed to store password into the keyring: %s', str(e))
+
+ return password
+
+
+if __name__ == '__main__':
+ exit(main())