How to run a script shell in google colab for Swift?

Hello, I would like to run the following cell on Colab to enable my Google drive, and setup a link to easily save the exported files.

import Python
let drive = Python.import(“google.colab”).drive
drive.mount("/content/gdrive", force_remount: True)
let driveLink = “/content/fastai-v3-swift/”
!ln -sv “/content/gdrive/My Drive/fastai-v3” .
let _ = Python.import(“google.colab”).sys.path.insert(0, slink)

I get the following error:

error: <Cell 10>:8:3: error: consecutive statements on a line must be separated by ‘;’ ln -sv “/content/gdrive/My Drive/fastai-v3” .

It seems that the syntax to run shell commands is not working for me. I also tried %%, %%shell without luck.

How to run a script shell in google colab for swift?

Finally, if there is a snippet, or better way to do what I am trying please let me know.

Hi QinLu
Did you manage to enter your authorisation code from google. I could not.

Can you try this ( not my original work)
import Foundation
let task = Process()
task.executableURL = URL(fileURLWithPath: “/usr/share/locale/ln”)

task.arguments = ["-sv", “/content/gdrive/My Drive/fastai-v3” ]
let outputPipe = Pipe()
let errorPipe = Pipe()

task.standardOutput = outputPipe
task.standardError = errorPipe
try task.run()
let outputData = outputPipe.fileHandleForReading.readDataToEndOfFile()
let errorData = errorPipe.fileHandleForReading.readDataToEndOfFile()
let output = String(decoding: outputData, as: UTF8.self)
let error = String(decoding: errorData, as: UTF8.self)

Hello @Conwyn,

No, I couldn’t manage to enter the authorization code from google.

Thank you for your solution, I think it should work when we get Drive on.

I will ask for the Google Drive authorization issue.

Thank you.

Hi QinLu

In colab python you can type %sx read -p “hello” and this displays hello and a entry box. The colab swift does not have this functionality which is maybe why we can not enter the gdrive authorisation code.

Also the sample code I copied does not work. The code below does but the Pipe logic above does not work tonight.

import Foundation
let task = Process()
task.executableURL = URL(fileURLWithPath: “/bin/ls”)
task.arguments = ["-al"]
try task.run()

Note the revised URL above I used the whereis command in python !whereis ls to determine the location.

Hi Qinlu

No progress made but somebody may have seen this before.

I was thinking if we could authenticate ourselves and then invoke gdrive we would avoid the invisible input box.

So let us try to authenticate ourselves. We get the URL listed and copy the code but

import Foundation

let task = Process()
let outputPipe = Pipe()
let inputPipe = Pipe()
let errorPipe = Pipe()

//task.standardOutput = outputPipe
//task.standardError = errorPipe
task.standardInput = inputPipe

task.executableURL = URL(fileURLWithPath: “/tools/google-cloud-sdk/bin/gcloud”)

task.arguments = [
“auth”,
“login”,
“–enable-gdrive-access”,
“–no-launch-browser”,
“–quiet”
]

try task.run()

Use URL to retrieve code

let input = “4/rw…”
let data = Data(input.utf8)
inputPipe.fileHandleForWriting.write(data)

But then it prints a new URL as if I typed in the wrong code.

Regards Conwyn

@Conwyn,

There was some discussion in Python textfield output not working.

Cheers,

Hi QinLu

I have an alternative solution.

Google has a facility called a service account. This is designed for inter server communication. You create one using console.developers.google.com. You download a JSON.
Inside you will find an email, private key, and information to determine the public key.
Returning to your normal Google Drive you create a share quoting the service account email.

We can now return to google colab. We add to the first line
%install ‘.package(url: “https://github.com/IBM-Swift/Swift-JWT.git”, from: “3.0.0”)’ SwiftJWT

we also import FoundationNetworking and SwiftIBM.

We then paste the JSON into a variable. This creates a security issue because you have just shown your private key.

The next step is to request a access token from Google which lasts for one hour maximum.

This is achieved by taking the email address and SHA256 and signing with the private key.

Google has the public key so verifies your request.

You can then request directory listings of the shared directory, download, upload and delete.

So your swift program can download from Google Drive and store on the local file. Process with Deep Learning and copy the output back to your Google Drive.

I have tested this with a Google Colab session and it worked.

Regards Conwyn