From 2e6811d2d4617dd238fdb140a6209c4843de5e44 Mon Sep 17 00:00:00 2001 From: Jay Lee Date: Wed, 23 Dec 2015 06:26:45 -0500 Subject: [PATCH 1/6] Limit cache filenames to 64 chars to prevent long paths from confusing windows --- src/httplib2/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/httplib2/__init__.py b/src/httplib2/__init__.py index 6fa3cc60..0b7a5696 100644 --- a/src/httplib2/__init__.py +++ b/src/httplib2/__init__.py @@ -255,8 +255,8 @@ def safename(filename): filename = re_slash.sub(",", filename) # limit length of filename - if len(filename)>200: - filename=filename[:200] + if len(filename)>64: + filename=filename[:64] return ",".join((filename, filemd5)) NORMALIZE_SPACE = re.compile(r'(?:\r\n)?[ \t]+') From ac3dbd25f330b9e82c5b83bfcd349082fc1994ed Mon Sep 17 00:00:00 2001 From: Jay Lee Date: Wed, 23 Dec 2015 06:32:43 -0500 Subject: [PATCH 2/6] If userid isn't in domain, return blank --- src/gam.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gam.py b/src/gam.py index b40f62eb..0441608f 100755 --- a/src/gam.py +++ b/src/gam.py @@ -1704,7 +1704,7 @@ def buildUserIdToNameMap(): def user_from_userid(userid): if not GM_Globals[GM_MAP_USER_ID_TO_NAME]: buildUserIdToNameMap() - return GM_Globals[GM_MAP_USER_ID_TO_NAME][userid] + return GM_Globals[GM_MAP_USER_ID_TO_NAME].get(userid, '') SERVICE_NAME_TO_ID_MAP = { u'Drive': u'55656082996', From ade2d0ae5447f38a7c76779d57534a5da815e11f Mon Sep 17 00:00:00 2001 From: Jay Lee Date: Wed, 23 Dec 2015 06:50:57 -0500 Subject: [PATCH 3/6] handle no results on Gmail profile --- src/gam.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/gam.py b/src/gam.py index 0441608f..5333cd15 100755 --- a/src/gam.py +++ b/src/gam.py @@ -1608,6 +1608,7 @@ def doPrintAdmins(): cd = buildGAPIObject(u'directory') roleId = None userKey = None + todrive = False i = 3 while i < len(sys.argv): if sys.argv[i].lower() == u'user': @@ -1623,6 +1624,9 @@ def doPrintAdmins(): print u'ERROR: %s is not a valid role' % role sys.exit(5) i += 2 + elif sys.argv[i].lower() == u'todrive': + todrive = True + i += 1 else: print u'ERROR: %s is not a valid argument for "gam print admins".' % sys.argv[i] sys.exit(2) @@ -1653,7 +1657,7 @@ def doPrintAdmins(): admins_attrib[0][u'orgUnit'] = u'orgUnit' admin_attrib[u'orgUnit'] = orgUnit admins_attrib.append(admin_attrib) - output_csv(admins_attrib, admins_attrib[0], u'Admins', False) + output_csv(admins_attrib, admins_attrib[0], u'Admins', todrive) def buildOrgUnitIdToNameMap(): cd = buildGAPIObject(u'directory') @@ -4558,10 +4562,11 @@ def showGmailProfile(users): if not gmail: continue results = callGAPI(service=gmail.users(), function=u'getProfile', userId=u'me', soft_errors=True) - for item in results: - if item not in profiles[0]: - profiles[0][item] = item - profiles.append(results) + if results: + for item in results: + if item not in profiles[0]: + profiles[0][item] = item + profiles.append(results) output_csv(csv_list=profiles, titles=profiles[0], list_type=u'Gmail Profiles', todrive=todrive) def updateLabels(users): From 1dd36424bec8c850cb0b19662fa7a47c243c5f32 Mon Sep 17 00:00:00 2001 From: Jay Lee Date: Wed, 23 Dec 2015 06:55:22 -0500 Subject: [PATCH 4/6] showLabels handle non-Gmail users --- src/gam.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/gam.py b/src/gam.py index 5333cd15..6e3255ab 100755 --- a/src/gam.py +++ b/src/gam.py @@ -4534,16 +4534,17 @@ def showLabels(users): sys.exit(2) for user in users: gmail = buildGAPIServiceObject(u'gmail', user) - labels = callGAPI(service=gmail.users().labels(), function=u'list', userId=user) - for label in labels[u'labels']: - if label[u'type'] == u'system' and not show_system: - continue - print convertUTF8(label[u'name']) - for a_key in label: - if a_key == u'name': + labels = callGAPI(service=gmail.users().labels(), function=u'list', userId=user, soft_errors=True) + if labels: + for label in labels[u'labels']: + if label[u'type'] == u'system' and not show_system: continue - print u' %s: %s' % (a_key, label[a_key]) - print u'' + print convertUTF8(label[u'name']) + for a_key in label: + if a_key == u'name': + continue + print u' %s: %s' % (a_key, label[a_key]) + print u'' def showGmailProfile(users): todrive = False From 327e09291ba458ed4ff3af176353f37505bc97c8 Mon Sep 17 00:00:00 2001 From: Jay Lee Date: Wed, 23 Dec 2015 08:06:22 -0500 Subject: [PATCH 5/6] disable discovery cache Disable discovery cache as it broke CSV commands on windows with lock errors. The cache is new in googleapiclient 1.4.2 which was upgraded after GAM 3.61: https://github.com/google/google-api-python-client/commit/30125120b48860d00ba4e96d56a86d9d02a90960 down the line, we should investigate actual issue with cache as enabling it would improve GAM performance. --- src/gam.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gam.py b/src/gam.py index 6e3255ab..0eea22e2 100755 --- a/src/gam.py +++ b/src/gam.py @@ -809,7 +809,7 @@ def buildGAPIObject(api): if api in [u'directory', u'reports', u'datatransfer']: api = u'admin' try: - service = googleapiclient.discovery.build(api, version, http=http) + service = googleapiclient.discovery.build(api, version, http=http, cache_discovery=False) except googleapiclient.errors.UnknownApiNameOrVersion: service = getServiceFromDiscoveryDocument(api, version, http) except httplib2.ServerNotFoundError as e: @@ -870,7 +870,7 @@ def buildGAPIServiceObject(api, act_as, soft_errors=False): cache=GC_Values[GC_CACHE_DIR])) version = getAPIVer(api) try: - return googleapiclient.discovery.build(api, version, http=http) + return googleapiclient.discovery.build(api, version, http=http, cache_discovery=False) except googleapiclient.errors.UnknownApiNameOrVersion: return getServiceFromDiscoveryDocument(api, version, http) except httplib2.ServerNotFoundError as e: From 2235c10df7d2b732965fbaa52728f03fcc53bf00 Mon Sep 17 00:00:00 2001 From: Jay Lee Date: Wed, 23 Dec 2015 09:05:03 -0500 Subject: [PATCH 6/6] handle blank lines it batch --- src/gam.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/gam.py b/src/gam.py index 0eea22e2..5d9388e6 100755 --- a/src/gam.py +++ b/src/gam.py @@ -9001,6 +9001,8 @@ try: items = list() for line in f: argv = shlex.split(line) + if not argv: + continue if (argv[0] in [u'#', u' ', u''] or len(argv) < 2) and argv != [u'commit-batch']: continue elif argv[0] not in [u'gam', u'commit-batch']: