Skip to content

Instantly share code, notes, and snippets.

@izackp
Created September 21, 2021 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izackp/353e0f46964fef1fc75b5844b62594e5 to your computer and use it in GitHub Desktop.
Save izackp/353e0f46964fef1fc75b5844b62594e5 to your computer and use it in GitHub Desktop.
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