mirror of
https://github.com/GAM-team/GAM.git
synced 2025-07-08 05:33:34 +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 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']
|
||||
false_values = [u'off', u'no', u'disabled', u'false', u'0']
|
||||
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):
|
||||
method = getattr(service, function)
|
||||
retries = 10
|
||||
parameters = dict(kwargs.items() + extra_args.items())
|
||||
for n in range(1, retries+1):
|
||||
try:
|
||||
return method(prettyPrint=prettyPrint, **kwargs).execute()
|
||||
return method(**parameters).execute()
|
||||
except googleapiclient.errors.HttpError, e:
|
||||
try:
|
||||
error = json.loads(e.content)
|
||||
@ -504,7 +505,7 @@ def getAPIScope(api):
|
||||
u'https://www.googleapis.com/auth/drive']
|
||||
|
||||
def buildGAPIObject(api):
|
||||
global domain, customerId, prettyPrint
|
||||
global domain, customerId, extra_args
|
||||
oauth2file = getGamPath()+u'oauth2.txt'
|
||||
try:
|
||||
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)
|
||||
if os.path.isfile(getGamPath()+u'debug.gam'):
|
||||
httplib2.debuglevel = 4
|
||||
prettyPrint = True
|
||||
extra_args = {u'prettyPrint': True}
|
||||
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)
|
||||
version = getAPIVer(api)
|
||||
if api in [u'directory', u'reports']:
|
||||
@ -562,7 +569,7 @@ def buildGAPIObject(api):
|
||||
return service
|
||||
|
||||
def buildGAPIServiceObject(api, act_as=None, soft_errors=False):
|
||||
global prettyPrint
|
||||
global extra_args
|
||||
oauth2servicefile = getGamPath()+u'oauth2service'
|
||||
try:
|
||||
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)
|
||||
if os.path.isfile(getGamPath()+u'debug.gam'):
|
||||
httplib2.debuglevel = 4
|
||||
prettyPrint = True
|
||||
extra_args = {u'prettyPrint': True}
|
||||
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)
|
||||
version = getAPIVer(api)
|
||||
try:
|
||||
@ -1701,11 +1714,7 @@ def delDriveFileACL(users):
|
||||
permissionId = unicode(sys.argv[6])
|
||||
for user in users:
|
||||
drive = buildGAPIServiceObject(u'drive', user)
|
||||
if permissionId[:3].lower() == u'id:':
|
||||
permissionId = permissionId[3:]
|
||||
elif permissionId.lower() in [u'anyone']:
|
||||
pass
|
||||
else:
|
||||
if not permissionId.isnumeric() and permissionId.lower() not in [u'anyone',]:
|
||||
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)
|
||||
callGAPI(service=drive.permissions(), function=u'delete', fileId=fileId, permissionId=permissionId)
|
||||
@ -1756,7 +1765,7 @@ def updateDriveFileACL(users):
|
||||
fileId = sys.argv[5]
|
||||
permissionId = unicode(sys.argv[6])
|
||||
transferOwnership = None
|
||||
body = {}
|
||||
body = {u'type': perm_type}
|
||||
i = 7
|
||||
while i < len(sys.argv):
|
||||
if sys.argv[i].lower().replace(u'_', u'') == u'withlink':
|
||||
@ -1784,9 +1793,7 @@ def updateDriveFileACL(users):
|
||||
sys.exit(9)
|
||||
for user in users:
|
||||
drive = buildGAPIServiceObject(u'drive', user)
|
||||
if permissionId[:3].lower() == u'id:':
|
||||
permissionId = permissionId[3:]
|
||||
else:
|
||||
if not permissionId.isnumeric():
|
||||
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)
|
||||
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}]
|
||||
sys.stderr.write(u'updating user %s...\n' % 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:
|
||||
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)
|
||||
if os.path.isfile(getGamPath()+u'debug.gam'):
|
||||
httplib2.debuglevel = 4
|
||||
prettyPrint = True
|
||||
extra_args = {u'prettyPrint': True}
|
||||
else:
|
||||
prettyPrint = False
|
||||
extra_args = {u'prettyPrint': False}
|
||||
try:
|
||||
credentials = oauth2client.tools.run_flow(flow=FLOW, storage=storage, flags=flags, http=http)
|
||||
except httplib2.CertificateValidationUnsupported:
|
||||
|
Reference in New Issue
Block a user