mirror of
https://github.com/GAM-team/GAM.git
synced 2025-07-09 22:23:35 +00:00
Update device files (#661)
* Update device files info cros/print crosactivity - device files are filtered by dote just line time rnages, recent users print cros - device files can be printed so user can identify ones to download * Allow download of all deviceFiles fields
This commit is contained in:
77
src/gam.py
77
src/gam.py
@ -9238,6 +9238,19 @@ def _filterTimeRanges(activeTimeRanges, startDate, endDate):
|
|||||||
filteredTimeRanges.append(timeRange)
|
filteredTimeRanges.append(timeRange)
|
||||||
return filteredTimeRanges
|
return filteredTimeRanges
|
||||||
|
|
||||||
|
def _filterDeviceFiles(deviceFiles, startTime, endTime):
|
||||||
|
if startTime is None and endTime is None:
|
||||||
|
return deviceFiles
|
||||||
|
filteredDeviceFiles = []
|
||||||
|
for deviceFile in deviceFiles:
|
||||||
|
createTime = datetime.datetime.strptime(deviceFile[u'createTime'], u'%Y-%m-%dT%H:%M:%S.%fZ')
|
||||||
|
if ((startTime is None) or (createTime >= startTime)) and ((endTime is None) or (createTime <= endTime)):
|
||||||
|
filteredDeviceFiles.append(deviceFile)
|
||||||
|
return filteredDeviceFiles
|
||||||
|
|
||||||
|
def _getFilterDate(dateStr):
|
||||||
|
return datetime.datetime.strptime(dateStr, YYYYMMDD_FORMAT)
|
||||||
|
|
||||||
def doGetCrosInfo():
|
def doGetCrosInfo():
|
||||||
cd = buildGAPIObject(u'directory')
|
cd = buildGAPIObject(u'directory')
|
||||||
deviceId = sys.argv[3]
|
deviceId = sys.argv[3]
|
||||||
@ -9268,10 +9281,10 @@ def doGetCrosInfo():
|
|||||||
noLists = True
|
noLists = True
|
||||||
i += 1
|
i += 1
|
||||||
elif myarg in CROS_START_ARGUMENTS:
|
elif myarg in CROS_START_ARGUMENTS:
|
||||||
startDate = datetime.datetime.strptime(sys.argv[i+1], u'%Y-%m-%d')
|
startDate = _getFilterDate(sys.argv[i+1])
|
||||||
i += 2
|
i += 2
|
||||||
elif myarg in CROS_END_ARGUMENTS:
|
elif myarg in CROS_END_ARGUMENTS:
|
||||||
endDate = datetime.datetime.strptime(sys.argv[i+1], u'%Y-%m-%d')
|
endDate = _getFilterDate(sys.argv[i+1])
|
||||||
i += 2
|
i += 2
|
||||||
elif myarg == u'listlimit':
|
elif myarg == u'listlimit':
|
||||||
listLimit = int(sys.argv[i+1])
|
listLimit = int(sys.argv[i+1])
|
||||||
@ -9357,13 +9370,16 @@ def doGetCrosInfo():
|
|||||||
for recentUser in recentUsers[:min(lenRU, listLimit or lenRU)]:
|
for recentUser in recentUsers[:min(lenRU, listLimit or lenRU)]:
|
||||||
print u' type: {0}'.format(recentUser[u'type'])
|
print u' type: {0}'.format(recentUser[u'type'])
|
||||||
print u' email: {0}'.format(recentUser.get(u'email', [u'Unknown', u'UnmanagedUser'][recentUser[u'type'] == u'USER_TYPE_UNMANAGED']))
|
print u' email: {0}'.format(recentUser.get(u'email', [u'Unknown', u'UnmanagedUser'][recentUser[u'type'] == u'USER_TYPE_UNMANAGED']))
|
||||||
deviceFiles = cros.get(u'deviceFiles', [])
|
deviceFiles = _filterDeviceFiles(cros.get(u'deviceFiles', []), startDate, endDate)
|
||||||
lenDF = len(deviceFiles)
|
lenDF = len(deviceFiles)
|
||||||
if lenDF:
|
if lenDF:
|
||||||
print u' deviceFiles'
|
print u' deviceFiles'
|
||||||
for deviceFile in deviceFiles[:min(lenDF, listLimit or lenDF)]:
|
for deviceFile in deviceFiles[:min(lenDF, listLimit or lenDF)]:
|
||||||
print u' {0}: {1}'.format(deviceFile[u'type'], deviceFile[u'createTime'])
|
print u' {0}: {1}'.format(deviceFile[u'type'], deviceFile[u'createTime'])
|
||||||
if downloadfile:
|
if downloadfile:
|
||||||
|
deviceFiles = cros.get(u'deviceFiles', [])
|
||||||
|
lenDF = len(deviceFiles)
|
||||||
|
if lenDF:
|
||||||
if downloadfile == u'latest':
|
if downloadfile == u'latest':
|
||||||
deviceFile = deviceFiles[-1]
|
deviceFile = deviceFiles[-1]
|
||||||
else:
|
else:
|
||||||
@ -9378,8 +9394,8 @@ def doGetCrosInfo():
|
|||||||
_, content = cd._http.request(deviceFile[u'downloadUrl'])
|
_, content = cd._http.request(deviceFile[u'downloadUrl'])
|
||||||
writeFile(downloadfilename, content, continueOnError=True)
|
writeFile(downloadfilename, content, continueOnError=True)
|
||||||
print u'Downloaded: {0}'.format(downloadfilename)
|
print u'Downloaded: {0}'.format(downloadfilename)
|
||||||
elif downloadfile:
|
elif downloadfile:
|
||||||
print u'ERROR: no files to download.'
|
print u'ERROR: no files to download.'
|
||||||
|
|
||||||
def doGetMobileInfo():
|
def doGetMobileInfo():
|
||||||
cd = buildGAPIObject(u'directory')
|
cd = buildGAPIObject(u'directory')
|
||||||
@ -11011,10 +11027,10 @@ def doPrintCrosActivity():
|
|||||||
selectActiveTimeRanges = selectDeviceFiles = selectRecentUsers = True
|
selectActiveTimeRanges = selectDeviceFiles = selectRecentUsers = True
|
||||||
i += 1
|
i += 1
|
||||||
elif myarg in CROS_START_ARGUMENTS:
|
elif myarg in CROS_START_ARGUMENTS:
|
||||||
startDate = datetime.datetime.strptime(sys.argv[i+1], u'%Y-%m-%d')
|
startDate = _getFilterDate(sys.argv[i+1])
|
||||||
i += 2
|
i += 2
|
||||||
elif myarg in CROS_END_ARGUMENTS:
|
elif myarg in CROS_END_ARGUMENTS:
|
||||||
endDate = datetime.datetime.strptime(sys.argv[i+1], u'%Y-%m-%d')
|
endDate = _getFilterDate(sys.argv[i+1])
|
||||||
i += 2
|
i += 2
|
||||||
elif myarg == u'listlimit':
|
elif myarg == u'listlimit':
|
||||||
listLimit = int(sys.argv[i+1])
|
listLimit = int(sys.argv[i+1])
|
||||||
@ -11062,9 +11078,9 @@ def doPrintCrosActivity():
|
|||||||
row[u'recentUsers.email'] = delimiter.join([recent_user.get(u'email', [u'Unknown', u'UnmanagedUser'][recent_user[u'type'] == u'USER_TYPE_UNMANAGED']) for recent_user in recentUsers[:min(lenRU, listLimit or lenRU)]])
|
row[u'recentUsers.email'] = delimiter.join([recent_user.get(u'email', [u'Unknown', u'UnmanagedUser'][recent_user[u'type'] == u'USER_TYPE_UNMANAGED']) for recent_user in recentUsers[:min(lenRU, listLimit or lenRU)]])
|
||||||
csvRows.append(row)
|
csvRows.append(row)
|
||||||
if selectDeviceFiles:
|
if selectDeviceFiles:
|
||||||
devicefiles = cros.get(u'deviceFiles', [])
|
deviceFiles = _filterDeviceFiles(cros.get(u'deviceFiles', []), startDate, endDate)
|
||||||
lenDF = len(devicefiles)
|
lenDF = len(deviceFiles)
|
||||||
for deviceFile in devicefiles[:min(lenDF, listLimit or lenDF)]:
|
for deviceFile in deviceFiles[:min(lenDF, listLimit or lenDF)]:
|
||||||
new_row = row.copy()
|
new_row = row.copy()
|
||||||
new_row[u'deviceFiles.type'] = deviceFile[u'type']
|
new_row[u'deviceFiles.type'] = deviceFile[u'type']
|
||||||
new_row[u'deviceFiles.createTime'] = deviceFile[u'createTime']
|
new_row[u'deviceFiles.createTime'] = deviceFile[u'createTime']
|
||||||
@ -11092,7 +11108,7 @@ def doPrintCrosDevices():
|
|||||||
sortHeaders = False
|
sortHeaders = False
|
||||||
query = projection = orderBy = sortOrder = orgUnitPath = None
|
query = projection = orderBy = sortOrder = orgUnitPath = None
|
||||||
noLists = False
|
noLists = False
|
||||||
selectActiveTimeRanges = selectRecentUsers = False
|
selectActiveTimeRanges = selectDeviceFiles = selectRecentUsers = False
|
||||||
startDate = endDate = None
|
startDate = endDate = None
|
||||||
listLimit = 0
|
listLimit = 0
|
||||||
i = 3
|
i = 3
|
||||||
@ -11109,7 +11125,7 @@ def doPrintCrosDevices():
|
|||||||
i += 1
|
i += 1
|
||||||
elif myarg == u'nolists':
|
elif myarg == u'nolists':
|
||||||
noLists = True
|
noLists = True
|
||||||
selectActiveTimeRanges = selectRecentUsers = False
|
selectActiveTimeRanges = selectDeviceFiles = selectRecentUsers = False
|
||||||
i += 1
|
i += 1
|
||||||
elif myarg in CROS_ACTIVE_TIME_RANGES_ARGUMENTS:
|
elif myarg in CROS_ACTIVE_TIME_RANGES_ARGUMENTS:
|
||||||
projection = u'FULL'
|
projection = u'FULL'
|
||||||
@ -11118,6 +11134,13 @@ def doPrintCrosDevices():
|
|||||||
if fieldsList:
|
if fieldsList:
|
||||||
fieldsList.append(u'activeTimeRanges')
|
fieldsList.append(u'activeTimeRanges')
|
||||||
i += 1
|
i += 1
|
||||||
|
elif myarg in CROS_DEVICE_FILES_ARGUMENTS:
|
||||||
|
projection = u'FULL'
|
||||||
|
selectDeviceFiles = True
|
||||||
|
noLists = False
|
||||||
|
if fieldsList:
|
||||||
|
fieldsList.append(u'deviceFiles')
|
||||||
|
i += 1
|
||||||
elif myarg in CROS_RECENT_USERS_ARGUMENTS:
|
elif myarg in CROS_RECENT_USERS_ARGUMENTS:
|
||||||
projection = u'FULL'
|
projection = u'FULL'
|
||||||
selectRecentUsers = True
|
selectRecentUsers = True
|
||||||
@ -11126,10 +11149,10 @@ def doPrintCrosDevices():
|
|||||||
fieldsList.append(u'recentUsers')
|
fieldsList.append(u'recentUsers')
|
||||||
i += 1
|
i += 1
|
||||||
elif myarg in CROS_START_ARGUMENTS:
|
elif myarg in CROS_START_ARGUMENTS:
|
||||||
startDate = datetime.datetime.strptime(sys.argv[i+1], u'%Y-%m-%d')
|
startDate = _getFilterDate(sys.argv[i+1])
|
||||||
i += 2
|
i += 2
|
||||||
elif myarg in CROS_END_ARGUMENTS:
|
elif myarg in CROS_END_ARGUMENTS:
|
||||||
endDate = datetime.datetime.strptime(sys.argv[i+1], u'%Y-%m-%d')
|
endDate = _getFilterDate(sys.argv[i+1])
|
||||||
i += 2
|
i += 2
|
||||||
elif myarg == u'listlimit':
|
elif myarg == u'listlimit':
|
||||||
listLimit = int(sys.argv[i+1])
|
listLimit = int(sys.argv[i+1])
|
||||||
@ -11179,14 +11202,18 @@ def doPrintCrosDevices():
|
|||||||
for field in fieldNameList.lower().replace(u',', u' ').split():
|
for field in fieldNameList.lower().replace(u',', u' ').split():
|
||||||
if field in CROS_ARGUMENT_TO_PROPERTY_MAP:
|
if field in CROS_ARGUMENT_TO_PROPERTY_MAP:
|
||||||
addFieldToCSVfile(field, CROS_ARGUMENT_TO_PROPERTY_MAP, fieldsList, fieldsTitles, titles)
|
addFieldToCSVfile(field, CROS_ARGUMENT_TO_PROPERTY_MAP, fieldsList, fieldsTitles, titles)
|
||||||
|
if field in CROS_ACTIVE_TIME_RANGES_ARGUMENTS:
|
||||||
|
projection = u'FULL'
|
||||||
|
selectActiveTimeRanges = True
|
||||||
|
noLists = False
|
||||||
|
elif field in CROS_DEVICE_FILES_ARGUMENTS:
|
||||||
|
projection = u'FULL'
|
||||||
|
selectDeviceFiles = True
|
||||||
|
noLists = False
|
||||||
if field in CROS_RECENT_USERS_ARGUMENTS:
|
if field in CROS_RECENT_USERS_ARGUMENTS:
|
||||||
projection = u'FULL'
|
projection = u'FULL'
|
||||||
selectRecentUsers = True
|
selectRecentUsers = True
|
||||||
noLists = False
|
noLists = False
|
||||||
elif field in CROS_ACTIVE_TIME_RANGES_ARGUMENTS:
|
|
||||||
projection = u'FULL'
|
|
||||||
selectActiveTimeRanges = True
|
|
||||||
noLists = False
|
|
||||||
else:
|
else:
|
||||||
print u'ERROR: %s is not a valid argument for "gam print cros fields"' % field
|
print u'ERROR: %s is not a valid argument for "gam print cros fields"' % field
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
@ -11205,9 +11232,8 @@ def doPrintCrosDevices():
|
|||||||
orderBy=orderBy, sortOrder=sortOrder, fields=fields, maxResults=GC_Values[GC_DEVICE_MAX_RESULTS])
|
orderBy=orderBy, sortOrder=sortOrder, fields=fields, maxResults=GC_Values[GC_DEVICE_MAX_RESULTS])
|
||||||
for cros in all_cros:
|
for cros in all_cros:
|
||||||
cros = _checkTPMVulnerability(cros)
|
cros = _checkTPMVulnerability(cros)
|
||||||
if (not noLists) and (not selectActiveTimeRanges) and (not selectRecentUsers):
|
if (not noLists) and (not selectActiveTimeRanges) and (not selectRecentUsers) and (not selectDeviceFiles):
|
||||||
for cros in all_cros:
|
for cros in all_cros:
|
||||||
cros.pop(u'deviceFiles', None)
|
|
||||||
if u'notes' in cros:
|
if u'notes' in cros:
|
||||||
cros[u'notes'] = cros[u'notes'].replace(u'\n', u'\\n')
|
cros[u'notes'] = cros[u'notes'].replace(u'\n', u'\\n')
|
||||||
addRowTitlesToCSVfile(flatten_json(cros, listLimit=listLimit), csvRows, titles)
|
addRowTitlesToCSVfile(flatten_json(cros, listLimit=listLimit), csvRows, titles)
|
||||||
@ -11217,6 +11243,8 @@ def doPrintCrosDevices():
|
|||||||
titles.extend([u'activeTimeRanges.date', u'activeTimeRanges.activeTime', u'activeTimeRanges.duration', u'activeTimeRanges.minutes'])
|
titles.extend([u'activeTimeRanges.date', u'activeTimeRanges.activeTime', u'activeTimeRanges.duration', u'activeTimeRanges.minutes'])
|
||||||
if selectRecentUsers:
|
if selectRecentUsers:
|
||||||
titles.extend([u'recentUsers.email', u'recentUsers.type'])
|
titles.extend([u'recentUsers.email', u'recentUsers.type'])
|
||||||
|
if selectDeviceFiles:
|
||||||
|
titles.extend([u'deviceFiles.type', u'deviceFiles.createTime'])
|
||||||
for cros in all_cros:
|
for cros in all_cros:
|
||||||
if u'notes' in cros:
|
if u'notes' in cros:
|
||||||
cros[u'notes'] = cros[u'notes'].replace(u'\n', u'\\n')
|
cros[u'notes'] = cros[u'notes'].replace(u'\n', u'\\n')
|
||||||
@ -11228,11 +11256,13 @@ def doPrintCrosDevices():
|
|||||||
row[attrib] = cros[attrib]
|
row[attrib] = cros[attrib]
|
||||||
activeTimeRanges = _filterTimeRanges(cros.get(u'activeTimeRanges', []), startDate, endDate) if selectActiveTimeRanges else []
|
activeTimeRanges = _filterTimeRanges(cros.get(u'activeTimeRanges', []), startDate, endDate) if selectActiveTimeRanges else []
|
||||||
recentUsers = cros.get(u'recentUsers', []) if selectRecentUsers else []
|
recentUsers = cros.get(u'recentUsers', []) if selectRecentUsers else []
|
||||||
if noLists or (not activeTimeRanges and not recentUsers):
|
deviceFiles = _filterDeviceFiles(cros.get(u'deviceFiles', []), startDate, endDate) if selectDeviceFiles else []
|
||||||
|
if noLists or (not activeTimeRanges and not recentUsers and not deviceFiles):
|
||||||
csvRows.append(row)
|
csvRows.append(row)
|
||||||
else:
|
else:
|
||||||
lenATR = len(activeTimeRanges)
|
lenATR = len(activeTimeRanges)
|
||||||
lenRU = len(recentUsers)
|
lenRU = len(recentUsers)
|
||||||
|
lenDF = len(deviceFiles)
|
||||||
for i in xrange(min(listLimit, max(lenATR, lenRU)) if listLimit else max(lenATR, lenRU)):
|
for i in xrange(min(listLimit, max(lenATR, lenRU)) if listLimit else max(lenATR, lenRU)):
|
||||||
new_row = row.copy()
|
new_row = row.copy()
|
||||||
if i < lenATR:
|
if i < lenATR:
|
||||||
@ -11243,6 +11273,9 @@ def doPrintCrosDevices():
|
|||||||
if i < lenRU:
|
if i < lenRU:
|
||||||
new_row[u'recentUsers.email'] = recentUsers[i].get(u'email', [u'Unknown', u'UnmanagedUser'][recentUsers[i][u'type'] == u'USER_TYPE_UNMANAGED'])
|
new_row[u'recentUsers.email'] = recentUsers[i].get(u'email', [u'Unknown', u'UnmanagedUser'][recentUsers[i][u'type'] == u'USER_TYPE_UNMANAGED'])
|
||||||
new_row[u'recentUsers.type'] = recentUsers[i][u'type']
|
new_row[u'recentUsers.type'] = recentUsers[i][u'type']
|
||||||
|
if i < lenDF:
|
||||||
|
new_row[u'deviceFiles.type'] = deviceFiles[i][u'type']
|
||||||
|
new_row[u'deviceFiles.createTime'] = deviceFiles[i][u'createTime']
|
||||||
csvRows.append(new_row)
|
csvRows.append(new_row)
|
||||||
if sortHeaders:
|
if sortHeaders:
|
||||||
sortCSVTitles([u'deviceId',], titles)
|
sortCSVTitles([u'deviceId',], titles)
|
||||||
|
Reference in New Issue
Block a user