mirror of
https://github.com/GAM-team/GAM.git
synced 2025-07-08 13:43:35 +00:00
look for extra-args.txt file to read additional GAPI arguments from.
This commit is contained in:
47
gam.py
47
gam.py
@ -40,7 +40,7 @@ import oauth2client.file
|
|||||||
import oauth2client.tools
|
import oauth2client.tools
|
||||||
import uritemplate
|
import uritemplate
|
||||||
|
|
||||||
global true_values, false_values, prettyPrint, customerId, domain, usergroup_types
|
global true_values, false_values, extra_args, customerId, domain, usergroup_types
|
||||||
true_values = [u'on', u'yes', u'enabled', u'true', u'1']
|
true_values = [u'on', u'yes', u'enabled', u'true', u'1']
|
||||||
false_values = [u'off', u'no', u'disabled', u'false', u'0']
|
false_values = [u'off', u'no', u'disabled', u'false', u'0']
|
||||||
usergroup_types = [u'user', u'users', u'group', u'ou', u'org',
|
usergroup_types = [u'user', u'users', u'group', u'ou', u'org',
|
||||||
@ -369,9 +369,10 @@ def callGData(service, function, soft_errors=False, throw_errors=[], **kwargs):
|
|||||||
def callGAPI(service, function, silent_errors=False, soft_errors=False, throw_reasons=[], retry_reasons=[], **kwargs):
|
def callGAPI(service, function, silent_errors=False, soft_errors=False, throw_reasons=[], retry_reasons=[], **kwargs):
|
||||||
method = getattr(service, function)
|
method = getattr(service, function)
|
||||||
retries = 10
|
retries = 10
|
||||||
|
parameters = dict(kwargs.items() + extra_args.items())
|
||||||
for n in range(1, retries+1):
|
for n in range(1, retries+1):
|
||||||
try:
|
try:
|
||||||
return method(prettyPrint=prettyPrint, **kwargs).execute()
|
return method(**parameters).execute()
|
||||||
except googleapiclient.errors.HttpError, e:
|
except googleapiclient.errors.HttpError, e:
|
||||||
try:
|
try:
|
||||||
error = json.loads(e.content)
|
error = json.loads(e.content)
|
||||||
@ -504,7 +505,7 @@ def getAPIScope(api):
|
|||||||
u'https://www.googleapis.com/auth/drive']
|
u'https://www.googleapis.com/auth/drive']
|
||||||
|
|
||||||
def buildGAPIObject(api):
|
def buildGAPIObject(api):
|
||||||
global domain, customerId, prettyPrint
|
global domain, customerId, extra_args
|
||||||
oauth2file = getGamPath()+u'oauth2.txt'
|
oauth2file = getGamPath()+u'oauth2.txt'
|
||||||
try:
|
try:
|
||||||
oauth2file = getGamPath()+os.environ[u'OAUTHFILE']
|
oauth2file = getGamPath()+os.environ[u'OAUTHFILE']
|
||||||
@ -527,9 +528,15 @@ def buildGAPIObject(api):
|
|||||||
http = httplib2.Http(ca_certs=getGamPath()+u'cacert.pem', disable_ssl_certificate_validation=disable_ssl_certificate_validation, cache=cache)
|
http = httplib2.Http(ca_certs=getGamPath()+u'cacert.pem', disable_ssl_certificate_validation=disable_ssl_certificate_validation, cache=cache)
|
||||||
if os.path.isfile(getGamPath()+u'debug.gam'):
|
if os.path.isfile(getGamPath()+u'debug.gam'):
|
||||||
httplib2.debuglevel = 4
|
httplib2.debuglevel = 4
|
||||||
prettyPrint = True
|
extra_args = {u'prettyPrint': True}
|
||||||
else:
|
else:
|
||||||
prettyPrint = False
|
extra_args = {u'prettyPrint': False}
|
||||||
|
if os.path.isfile(getGamPath()+u'extra-args.txt'):
|
||||||
|
import ConfigParser
|
||||||
|
config = ConfigParser.ConfigParser()
|
||||||
|
config.optionxform = str
|
||||||
|
config.read(getGamPath()+u'extra-args.txt')
|
||||||
|
extra_args.update(dict(config.items(u'extra-args')))
|
||||||
http = credentials.authorize(http)
|
http = credentials.authorize(http)
|
||||||
version = getAPIVer(api)
|
version = getAPIVer(api)
|
||||||
if api in [u'directory', u'reports']:
|
if api in [u'directory', u'reports']:
|
||||||
@ -562,7 +569,7 @@ def buildGAPIObject(api):
|
|||||||
return service
|
return service
|
||||||
|
|
||||||
def buildGAPIServiceObject(api, act_as=None, soft_errors=False):
|
def buildGAPIServiceObject(api, act_as=None, soft_errors=False):
|
||||||
global prettyPrint
|
global extra_args
|
||||||
oauth2servicefile = getGamPath()+u'oauth2service'
|
oauth2servicefile = getGamPath()+u'oauth2service'
|
||||||
try:
|
try:
|
||||||
oauth2servicefile = getGamPath()+os.environ[u'OAUTHSERVICEFILE']
|
oauth2servicefile = getGamPath()+os.environ[u'OAUTHSERVICEFILE']
|
||||||
@ -606,9 +613,15 @@ def buildGAPIServiceObject(api, act_as=None, soft_errors=False):
|
|||||||
http = httplib2.Http(ca_certs=getGamPath()+u'cacert.pem', disable_ssl_certificate_validation=disable_ssl_certificate_validation, cache=cache)
|
http = httplib2.Http(ca_certs=getGamPath()+u'cacert.pem', disable_ssl_certificate_validation=disable_ssl_certificate_validation, cache=cache)
|
||||||
if os.path.isfile(getGamPath()+u'debug.gam'):
|
if os.path.isfile(getGamPath()+u'debug.gam'):
|
||||||
httplib2.debuglevel = 4
|
httplib2.debuglevel = 4
|
||||||
prettyPrint = True
|
extra_args = {u'prettyPrint': True}
|
||||||
else:
|
else:
|
||||||
prettyPrint = False
|
extra_args = {u'prettyPrint': False}
|
||||||
|
if os.path.isfile(getGamPath()+u'extra-args.txt'):
|
||||||
|
import ConfigParser
|
||||||
|
config = ConfigParser.ConfigParser()
|
||||||
|
config.optionxform = str
|
||||||
|
config.read(getGamPath()+u'extra-args.txt')
|
||||||
|
extra_args.update(dict(config.items(u'extra-args')))
|
||||||
http = credentials.authorize(http)
|
http = credentials.authorize(http)
|
||||||
version = getAPIVer(api)
|
version = getAPIVer(api)
|
||||||
try:
|
try:
|
||||||
@ -1701,11 +1714,7 @@ def delDriveFileACL(users):
|
|||||||
permissionId = unicode(sys.argv[6])
|
permissionId = unicode(sys.argv[6])
|
||||||
for user in users:
|
for user in users:
|
||||||
drive = buildGAPIServiceObject(u'drive', user)
|
drive = buildGAPIServiceObject(u'drive', user)
|
||||||
if permissionId[:3].lower() == u'id:':
|
if not permissionId.isnumeric() and permissionId.lower() not in [u'anyone',]:
|
||||||
permissionId = permissionId[3:]
|
|
||||||
elif permissionId.lower() in [u'anyone']:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
permissionId = callGAPI(service=drive.permissions(), function=u'getIdForEmail', email=permissionId, fields=u'id')[u'id']
|
permissionId = callGAPI(service=drive.permissions(), function=u'getIdForEmail', email=permissionId, fields=u'id')[u'id']
|
||||||
print u'Removing permission for %s from %s' % (permissionId, fileId)
|
print u'Removing permission for %s from %s' % (permissionId, fileId)
|
||||||
callGAPI(service=drive.permissions(), function=u'delete', fileId=fileId, permissionId=permissionId)
|
callGAPI(service=drive.permissions(), function=u'delete', fileId=fileId, permissionId=permissionId)
|
||||||
@ -1756,7 +1765,7 @@ def updateDriveFileACL(users):
|
|||||||
fileId = sys.argv[5]
|
fileId = sys.argv[5]
|
||||||
permissionId = unicode(sys.argv[6])
|
permissionId = unicode(sys.argv[6])
|
||||||
transferOwnership = None
|
transferOwnership = None
|
||||||
body = {}
|
body = {u'type': perm_type}
|
||||||
i = 7
|
i = 7
|
||||||
while i < len(sys.argv):
|
while i < len(sys.argv):
|
||||||
if sys.argv[i].lower().replace(u'_', u'') == u'withlink':
|
if sys.argv[i].lower().replace(u'_', u'') == u'withlink':
|
||||||
@ -1784,9 +1793,7 @@ def updateDriveFileACL(users):
|
|||||||
sys.exit(9)
|
sys.exit(9)
|
||||||
for user in users:
|
for user in users:
|
||||||
drive = buildGAPIServiceObject(u'drive', user)
|
drive = buildGAPIServiceObject(u'drive', user)
|
||||||
if permissionId[:3].lower() == u'id:':
|
if not permissionId.isnumeric():
|
||||||
permissionId = permissionId[3:]
|
|
||||||
else:
|
|
||||||
permissionId = callGAPI(service=drive.permissions(), function=u'getIdForEmail', email=permissionId, fields=u'id')[u'id']
|
permissionId = callGAPI(service=drive.permissions(), function=u'getIdForEmail', email=permissionId, fields=u'id')[u'id']
|
||||||
print u'updating permissions for %s to file %s' % (permissionId, fileId)
|
print u'updating permissions for %s to file %s' % (permissionId, fileId)
|
||||||
result = callGAPI(service=drive.permissions(), function=u'patch', fileId=fileId, permissionId=permissionId, transferOwnership=transferOwnership, body=body)
|
result = callGAPI(service=drive.permissions(), function=u'patch', fileId=fileId, permissionId=permissionId, transferOwnership=transferOwnership, body=body)
|
||||||
@ -4271,7 +4278,7 @@ def doUpdateUser(users):
|
|||||||
body[u'emails'] = [{u'type': u'custom', u'customType': u'former_employee', u'primary': False, u'address': user_primary}]
|
body[u'emails'] = [{u'type': u'custom', u'customType': u'former_employee', u'primary': False, u'address': user_primary}]
|
||||||
sys.stderr.write(u'updating user %s...\n' % user)
|
sys.stderr.write(u'updating user %s...\n' % user)
|
||||||
if do_update_user:
|
if do_update_user:
|
||||||
result = callGAPI(service=cd.users(), function=u'patch', soft_errors=True, userKey=user, body=body)
|
result = callGAPI(service=cd.users(), function=u'patch', soft_errors=True, userKey=user, body=body, fields='primaryEmail')
|
||||||
if do_admin_user:
|
if do_admin_user:
|
||||||
result2 = callGAPI(service=cd.users(), function=u'makeAdmin', soft_errors=True, userKey=user, body={u'status': is_admin})
|
result2 = callGAPI(service=cd.users(), function=u'makeAdmin', soft_errors=True, userKey=user, body={u'status': is_admin})
|
||||||
|
|
||||||
@ -7266,9 +7273,9 @@ access or an 'a' to grant action-only access.
|
|||||||
http = httplib2.Http(ca_certs=certFile, disable_ssl_certificate_validation=disable_ssl_certificate_validation)
|
http = httplib2.Http(ca_certs=certFile, disable_ssl_certificate_validation=disable_ssl_certificate_validation)
|
||||||
if os.path.isfile(getGamPath()+u'debug.gam'):
|
if os.path.isfile(getGamPath()+u'debug.gam'):
|
||||||
httplib2.debuglevel = 4
|
httplib2.debuglevel = 4
|
||||||
prettyPrint = True
|
extra_args = {u'prettyPrint': True}
|
||||||
else:
|
else:
|
||||||
prettyPrint = False
|
extra_args = {u'prettyPrint': False}
|
||||||
try:
|
try:
|
||||||
credentials = oauth2client.tools.run_flow(flow=FLOW, storage=storage, flags=flags, http=http)
|
credentials = oauth2client.tools.run_flow(flow=FLOW, storage=storage, flags=flags, http=http)
|
||||||
except httplib2.CertificateValidationUnsupported:
|
except httplib2.CertificateValidationUnsupported:
|
||||||
|
Reference in New Issue
Block a user