Skip to content

Commit

Permalink
Added option stripnameprefix <String> to `gam create|copy|update dr…
Browse files Browse the repository at this point in the history
…ivefile`
  • Loading branch information
taers232c committed Jun 6, 2021
1 parent 3ce69c7 commit fc386d3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
8 changes: 5 additions & 3 deletions src/GamCommands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4594,7 +4594,8 @@ gam <UserTypeEntity> show datastudiopermissions
usecontentasindexabletext

gam <UserTypeEntity> create|add drivefile [drivefilename <DriveFileName>]
<DriveFileCreateAttribute>* [enforcesingleparent <Boolean>]
<DriveFileCreateAttribute>* [stripnameprefix <String>]
[enforcesingleparent <Boolean>]
[csv [todrive <ToDriveAttribute>*]] [returnidonly|returnlinkonly|returneditlinkonly|showdetails]

<DriveFileUpdateAttribute> ::=
Expand All @@ -4610,7 +4611,7 @@ gam <UserTypeEntity> create|add drivefile [drivefilename <DriveFileName>]
(removeanyownerparentname <DriveFolderName>))

gam <UserTypeEntity> update drivefile <DriveFileEntity> [copy] [returnidonly|returnlinkonly]
[retainname | (newfilename <DriveFileName>)]
[retainname | (newfilename <DriveFileName>)] [stripnameprefix <String>]
<DriveFileUpdateAttribute>* [enforcesingleparent <Boolean>]
[gsheet|csvsheet <SheetEntity>] [charset <CharSet>] [columndelimiter <Character>]

Expand All @@ -4620,7 +4621,8 @@ gam <UserTypeEntity> update drivefile <DriveFileEntity> [copy] [returnidonly|ret
(modifieddate|modifiedtime <Time>)|
(mimetype <MimeType>)

gam <UserTypeEntity> copy drivefile <DriveFileEntity> [newfilename <DriveFileName>]
gam <UserTypeEntity> copy drivefile <DriveFileEntity>
[newfilename <DriveFileName>] [stripnameprefix <String>]
[summary [<Boolean>]] [excludetrashed] [returnidonly|returnlinkonly]
<DriveFileCopyAttribute>* [enforcesingleparent <Boolean>]
[mergewithparent [<Boolean>]] [recursive [depth <Number>]]
Expand Down
17 changes: 17 additions & 0 deletions src/GamUpdate.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
6.03.33

Added option `stripnameprefix <String>` to `gam create|copy|update drivefile` to aid in the following situation.
You have a CSV file listing some files owned by an external user; your ultimate goal is to move them to a Shared Drive.
This is not possible so you decide to make locally owned copies, backup the externally owned originals and then rename
the copies to have the original name and then move those copies to thw Shared Drive.
```
# Make copies of the externally owned files
gam csv ./ExtFiles.csv gam user [email protected] copy drivefile "~id"
# You now have `FileName` and 'Copy of Filename`; move the originals to a backup folder
gam csv ./ExtFiles.csv gam user [email protected] update drivefile "~id" parentid <BackupParentFolderID>
# Generate a list of the "Copy of " copied files
gam redirect csv ./CopyOfFiles.csv user [email protected] print filelist fields id,name query "name contains 'Copy of '"
# Rename the files stripping off the "Copy of " prefix
gam csv ./CopyOfFiles.csv gam user [email protected] update drivefile "~id" newfilename "~name" stripnameprefix "Copy of "
```

6.03.32

Added option `notdomain <RegularExpression>` to `<PermissionMatch>` to make it easier to
Expand Down
42 changes: 31 additions & 11 deletions src/gam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"""

__author__ = 'Ross Scroggs <[email protected]>'
__version__ = '6.03.32'
__version__ = '6.03.33'
__license__ = 'Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)'

import base64
Expand Down Expand Up @@ -34212,8 +34212,9 @@ def _copyMaterialsError(fileId, errMsg):
[Ent.COURSE, self.courseId, Ent.COURSE_MATERIAL_DRIVEFILE, fileId])
if self.csvPF:
self.csvPF.WriteRow({'courseId': self.courseId, 'ownerId': self.ownerId, 'fileId': result['id']})
except (GAPI.fileNotFound, GAPI.forbidden, GAPI.internalError, GAPI.insufficientFilePermissions,
GAPI.unknownError, GAPI.cannotCopyFile, GAPI.badRequest, GAPI.fileNeverWritable) as e:
except (GAPI.fileNotFound, GAPI.forbidden, GAPI.internalError, GAPI.insufficientFilePermissions, GAPI.unknownError,
GAPI.invalid, GAPI.cannotCopyFile, GAPI.badRequest, GAPI.responsePreparationFailure, GAPI.fileNeverWritable,
GAPI.teamDrivesSharingRestrictionNotAllowed, GAPI.rateLimitExceeded, GAPI.userRateLimitExceeded) as e:
_copyMaterialsError(fileId, str(e))
except (GAPI.serviceNotAvailable, GAPI.authError, GAPI.domainPolicy) as e:
_copyMaterialsError(fileId, str(e))
Expand Down Expand Up @@ -39015,6 +39016,7 @@ def _identifyRoot():
DFA_LOCALFILEPATH = 'localFilepath'
DFA_LOCALFILENAME = 'localFilename'
DFA_LOCALMIMETYPE = 'localMimeType'
DFA_STRIPNAMEPREFIX = 'stripNamePrefix'
DFA_OCRLANGUAGE = 'ocrLanguage'
DFA_ENFORCE_SINGLE_PARENT = 'enforceSingleParent'
DFA_PARENTID = 'parentId'
Expand Down Expand Up @@ -39267,6 +39269,7 @@ def initDriveFileAttributes():
DFA_LOCALFILEPATH: None,
DFA_LOCALFILENAME: None,
DFA_LOCALMIMETYPE: None,
DFA_STRIPNAMEPREFIX: None,
DFA_OCRLANGUAGE: None,
DFA_ENFORCE_SINGLE_PARENT: False,
DFA_PARENTID: None,
Expand Down Expand Up @@ -39378,6 +39381,8 @@ def getDriveFileAttribute(myarg, body, parameters, updateCmd):
if body.get('mimeType') is None:
body['mimeType'] = 'application/octet-stream'
parameters[DFA_LOCALMIMETYPE] = body['mimeType']
elif myarg =='stripnameprefix':
parameters[DFA_STRIPNAMEPREFIX] = getString(Cmd.OB_STRING, minLen=0)
elif myarg in {'convert', 'ocr'}:
deprecatedArgument(myarg)
elif myarg == 'ocrlanguage':
Expand Down Expand Up @@ -42772,7 +42777,8 @@ def writeReturnIdLink(returnIdLink, mimeType, result):
}

# gam <UserTypeEntity> create|add drivefile [drivefilename <DriveFileName>]
# <DriveFileCreateAttribute>* [enforcesingleparent <Boolean>]
# <DriveFileCreateAttribute>* [stripnameprefix <String>]
# [enforcesingleparent <Boolean>]
# [csv [todrive <ToDriveAttribute>*]] [returnidonly|returnlinkonly|returneditlinkonly|showdetails]
def createDriveFile(users):
csvPF = media_body = None
Expand All @@ -42796,6 +42802,8 @@ def createDriveFile(users):
csvPF.GetTodriveParameters()
else:
getDriveFileAttribute(myarg, body, parameters, False)
if 'name' in body and parameters[DFA_STRIPNAMEPREFIX] and body['name'].startswith(parameters[DFA_STRIPNAMEPREFIX]):
body['name'] = body['name'][len(parameters[DFA_STRIPNAMEPREFIX]):]
if parameters[DFA_LOCALFILEPATH]:
if parameters[DFA_LOCALFILEPATH] != '-' and parameters[DFA_PRESERVE_FILE_TIMES]:
setPreservedFileTimes(body, parameters, False)
Expand Down Expand Up @@ -43108,7 +43116,7 @@ def checkDriveFileShortcut(users):
csvPF.writeCSVfile('Check Shortcuts')

# gam <UserTypeEntity> update drivefile <DriveFileEntity> [copy] [returnidonly|returnlinkonly]
# [retainname | (newfilename <DriveFileName>)]
# [retainname | (newfilename <DriveFileName>)] [stripnameprefix <String>]
# <DriveFileUpdateAttribute>* [enforcesingleparent <Boolean>]
# [gsheet|csvsheet <SheetEntity>] [charset <String>] [columndelimiter <Character>]
def updateDriveFile(users):
Expand Down Expand Up @@ -43148,6 +43156,8 @@ def updateDriveFile(users):
getDriveFileAttribute(myarg, body, parameters, True)
if assignLocalName and parameters[DFA_LOCALFILENAME] and parameters[DFA_LOCALFILENAME] != '-':
body['name'] = parameters[DFA_LOCALFILENAME]
if 'name' in body and parameters[DFA_STRIPNAMEPREFIX] and body['name'].startswith(parameters[DFA_STRIPNAMEPREFIX]):
body['name'] = body['name'][len(parameters[DFA_STRIPNAMEPREFIX]):]
if operation == 'update' and parameters[DFA_LOCALFILEPATH]:
if parameters[DFA_LOCALFILEPATH] != '-' and parameters[DFA_PRESERVE_FILE_TIMES]:
setPreservedFileTimes(body, parameters, True)
Expand Down Expand Up @@ -43308,8 +43318,10 @@ def updateDriveFile(users):
else:
entityModifierNewValueItemValueListActionPerformed([Ent.USER, user, Ent.DRIVE_FILE, fileId],
Act.MODIFIER_TO, result['name'], [Ent.DRIVE_FILE_ID, result['id']], j, jcount)
except (GAPI.fileNotFound, GAPI.forbidden, GAPI.internalError, GAPI.insufficientFilePermissions,
GAPI.unknownError, GAPI.invalid, GAPI.cannotCopyFile, GAPI.badRequest, GAPI.cannotModifyViewersCanCopyContent) as e:
except (GAPI.fileNotFound, GAPI.forbidden, GAPI.internalError, GAPI.insufficientFilePermissions, GAPI.unknownError,
GAPI.invalid, GAPI.cannotCopyFile, GAPI.badRequest, GAPI.responsePreparationFailure, GAPI.fileNeverWritable,
GAPI.teamDrivesSharingRestrictionNotAllowed, GAPI.rateLimitExceeded, GAPI.userRateLimitExceeded,
GAPI.cannotModifyViewersCanCopyContent) as e:
entityActionFailedWarning([Ent.USER, user, Ent.DRIVE_FILE, fileId], str(e), j, jcount)
except (GAPI.serviceNotAvailable, GAPI.authError, GAPI.domainPolicy) as e:
userSvcNotApplicableOrDriveDisabled(user, str(e), i, count)
Expand Down Expand Up @@ -43407,6 +43419,7 @@ def initCopyMoveOptions(move):
'sourceDriveId': None,
'destDriveId': None,
'newFilename': None,
'stripNamePrefix': None,
'summary': False,
'mergeWithParent': False,
'mergeWithParentRetain': False,
Expand All @@ -43425,6 +43438,8 @@ def initCopyMoveOptions(move):
def getCopyMoveOptions(myarg, copyMoveOptions, copyCmd):
if myarg == 'newfilename':
copyMoveOptions['newFilename'] = getString(Cmd.OB_DRIVE_FILE_NAME)
elif myarg =='stripnameprefix':
copyMoveOptions['stripNamePrefix'] = getString(Cmd.OB_STRING, minLen=0)
elif myarg == 'summary':
copyMoveOptions['summary'] = getBoolean()
elif myarg == 'mergewithparent':
Expand Down Expand Up @@ -43650,7 +43665,8 @@ def _getCopyMoveTargetInfo(drive, user, i, count, j, jcount, source, destFilenam
COPY_TOP_PARENTS_CHOICES = {'all': COPY_ALL_PARENTS, 'none': COPY_NO_PARENTS}
COPY_SUB_PARENTS_CHOICES = {'all': COPY_ALL_PARENTS, 'none': COPY_NO_PARENTS, 'nonpath': COPY_NONPATH_PARENTS}

# gam <UserTypeEntity> copy drivefile <DriveFileEntity> [newfilename <DriveFileName>]
# gam <UserTypeEntity> copy drivefile <DriveFileEntity>
# [newfilename <DriveFileName>] [stripnameprefix <String>]
# [summary [<Boolean>]] [excludetrashed] [returnidonly|returnlinkonly]
# <DriveFileCopyAttribute>*
# [mergewithparent [<Boolean>]] [recursive [depth <Number>]]
Expand Down Expand Up @@ -43822,7 +43838,8 @@ def _recursiveFolderCopy(drive, user, i, count, j, jcount, source, newFolderTitl
_copyPermissions(drive, user, i, count, k, kcount, Ent.DRIVE_FILE, childId, childTitle, result['id'], result['name'],
statistics, STAT_FILE_PERMISSIONS_FAILED, copyMoveOptions)
except (GAPI.fileNotFound, GAPI.forbidden, GAPI.internalError, GAPI.insufficientFilePermissions, GAPI.unknownError,
GAPI.cannotCopyFile, GAPI.badRequest, GAPI.responsePreparationFailure, GAPI.rateLimitExceeded, GAPI.userRateLimitExceeded) as e:
GAPI.invalid, GAPI.cannotCopyFile, GAPI.badRequest, GAPI.responsePreparationFailure, GAPI.fileNeverWritable,
GAPI.teamDrivesSharingRestrictionNotAllowed, GAPI.rateLimitExceeded, GAPI.userRateLimitExceeded) as e:
entityActionFailedWarning([Ent.USER, user, Ent.DRIVE_FILE, childTitle], str(e), k, kcount)
_incrStatistic(statistics, STAT_FILE_FAILED)
Ind.Decrement()
Expand Down Expand Up @@ -43926,6 +43943,8 @@ def _recursiveFolderCopy(drive, user, i, count, j, jcount, source, newFolderTitl
destFilename = sourceFilename
else:
destFilename = f'Copy of {sourceFilename}'
if copyMoveOptions['stripNamePrefix'] and destFilename.startswith(copyMoveOptions['stripNamePrefix']):
destFilename = destFilename[len(copyMoveOptions['stripNamePrefix']):]
targetChildren = _getCopyMoveTargetInfo(drive, user, i, count, j, jcount, source, destFilename, newParentId, statistics, parentParms)
if targetChildren is None:
continue
Expand Down Expand Up @@ -43991,8 +44010,9 @@ def _recursiveFolderCopy(drive, user, i, count, j, jcount, source, newFolderTitl
if copyMoveOptions['copyFilePermissions']:
_copyPermissions(drive, user, i, count, j, jcount, Ent.DRIVE_FILE, sourceId, sourceFilename, result['id'], result['name'],
statistics, STAT_FILE_PERMISSIONS_FAILED, copyMoveOptions)
except (GAPI.fileNotFound, GAPI.forbidden, GAPI.internalError, GAPI.insufficientFilePermissions,
GAPI.unknownError, GAPI.cannotCopyFile, GAPI.badRequest, GAPI.fileNeverWritable) as e:
except (GAPI.fileNotFound, GAPI.forbidden, GAPI.internalError, GAPI.insufficientFilePermissions, GAPI.unknownError,
GAPI.invalid, GAPI.cannotCopyFile, GAPI.badRequest, GAPI.responsePreparationFailure, GAPI.fileNeverWritable,
GAPI.teamDrivesSharingRestrictionNotAllowed, GAPI.rateLimitExceeded, GAPI.userRateLimitExceeded) as e:
entityActionFailedWarning([Ent.USER, user, Ent.DRIVE_FILE_OR_FOLDER_ID, fileId], str(e), j, jcount)
_incrStatistic(statistics, STAT_FILE_FAILED)
except (GAPI.serviceNotAvailable, GAPI.authError, GAPI.domainPolicy) as e:
Expand Down
3 changes: 2 additions & 1 deletion src/gam/gamlib/glgapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@
DATASTUDIO_THROW_REASONS = [INVALID_ARGUMENT, SERVICE_NOT_AVAILABLE, BAD_REQUEST, NOT_FOUND, PERMISSION_DENIED]
DRIVE_USER_THROW_REASONS = [SERVICE_NOT_AVAILABLE, AUTH_ERROR, DOMAIN_POLICY]
DRIVE_ACCESS_THROW_REASONS = DRIVE_USER_THROW_REASONS+[FILE_NOT_FOUND, FORBIDDEN, INTERNAL_ERROR, INSUFFICIENT_FILE_PERMISSIONS, UNKNOWN_ERROR, INVALID]
DRIVE_COPY_THROW_REASONS = DRIVE_ACCESS_THROW_REASONS+[CANNOT_COPY_FILE, BAD_REQUEST, RESPONSE_PREPARATION_FAILURE, RATE_LIMIT_EXCEEDED, USER_RATE_LIMIT_EXCEEDED]
DRIVE_COPY_THROW_REASONS = DRIVE_ACCESS_THROW_REASONS+[CANNOT_COPY_FILE, BAD_REQUEST, RESPONSE_PREPARATION_FAILURE, TEAMDRIVES_SHARING_RESTRICTION_NOT_ALLOWED,
RATE_LIMIT_EXCEEDED, USER_RATE_LIMIT_EXCEEDED]
DRIVE_GET_THROW_REASONS = DRIVE_USER_THROW_REASONS+[FILE_NOT_FOUND]
DRIVE3_CREATE_ACL_THROW_REASONS = [INVALID, INVALID_SHARING_REQUEST, OWNERSHIP_CHANGE_ACROSS_DOMAIN_NOT_PERMITTED,
NOT_FOUND, TEAMDRIVE_DOMAIN_USERS_ONLY_RESTRICTION,
Expand Down

0 comments on commit fc386d3

Please sign in to comment.