-
-
Save izackp/353e0f46964fef1fc75b5844b62594e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import osproc | |
import strutils | |
#import zip/zipfiles | |
type Project = tuple[searchName: string, name: string] | |
const projCapture:Project = (searchName:"Capture", name:"Capture") | |
const projPhotoDay:Project = (searchName:"PhotoDay", name:"PhotoDay") | |
proc findOrCreateTempDir(): string = | |
let tmpDir = getTempDir() | |
result = tmpDir / "archiveBackup" | |
createDir(result) | |
proc topDirectoryName(path:string) : string = | |
let parent = parentDir(path) | |
let name = relativePath(path, parent) | |
return name | |
proc main() = | |
let sourcePath = expandTilde("~/Library/Developer/Xcode/Archives") | |
let destPath = expandTilde("~/iFlix/Projects/iOS") | |
let tmpPath = findOrCreateTempDir() | |
const projectList = [projCapture,projPhotoDay] | |
var destFileList = newSeq[string]() | |
for eachFile in walkDirRec(destPath): | |
destFileList.add(extractFilename(eachFile)) | |
echo "Found ", destFileList.len, " files at ", destPath | |
block RunBackup: | |
for eachProject in projectList: | |
var matchingSrcDirList = newSeq[string]() | |
echo "Backing up project: ", eachProject.name | |
for eachDir in walkDirRec(sourcePath, {pcDir}): | |
let dirName = topDirectoryName(eachDir) | |
if eachProject.searchName in dirName and ".xcarchive" in dirName: | |
#echo "Found: ", dirName | |
matchingSrcDirList.add(eachDir) | |
echo "Found ", matchingSrcDirList.len, " archives at ", sourcePath | |
let destProjPath = destPath / eachProject.name | |
createDir(destProjPath) | |
for eachArchive in matchingSrcDirList: | |
let zipName = topDirectoryName(eachArchive) & ".zip" | |
let zipPath = tmpPath / zipName | |
if zipName in destFileList: | |
echo "File already exists: ", zipName | |
continue | |
if fileExists(zipPath): | |
removeFile(zipPath) | |
echo "Zipping: ", eachArchive | |
let cmdZip = "ditto -c -k --sequesterRsrc --keepParent \"" & eachArchive & "\" \"" & zipPath & "\"" | |
if execCmd(cmdZip) != 0: | |
echo "Error running command: " & cmdZip | |
#removeFile(zipPath) | |
break RunBackup | |
defer: removeFile(zipPath) | |
#[ No execptions or errors returned.. | |
var zip: ZipArchive | |
if not zip.open(zipPath, fmWrite): | |
echo "Opening zip failed: ", zipPath | |
break RunBackup | |
for eachFile in walkDirRec(eachArchive): | |
let internalFilePath = relativePath(eachFile, parentDir) | |
echo "Adding: ", internalFilePath | |
zip.addFile(internalFilePath, eachFile) | |
zip.close() | |
]# | |
echo "Backing up: ", zipPath | |
let cmd = "cp \"" & zipPath & "\" \"" & destProjPath / zipName & "\"" | |
if execCmd(cmd) != 0: | |
echo "Error running command: " & cmd | |
#removeFile(zipPath) | |
break RunBackup | |
#[ Causes osxfuse / rclone to crash | |
try: | |
#copyFile(zipPath, destProjPath / zipName) | |
except: | |
let | |
e = getCurrentException() | |
msg = getCurrentExceptionMsg() | |
echo "Got exception ", repr(e), " with message ", msg | |
break RunBackup]# | |
when isMainModule: | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment