Skip to content

Commit 702dce1

Browse files
committed
Utility for Downloading repository as zip and extracting them to AIDP workspace.
1 parent cb61262 commit 702dce1

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"metadata": {
3+
"kernelspec": {
4+
"display_name": "python3",
5+
"name": "notebook"
6+
},
7+
"language_info": {
8+
"file_extension": ".py",
9+
"mimetype": "text/x-python",
10+
"name": "python"
11+
},
12+
"Last_Active_Cell_Index": 2
13+
},
14+
"nbformat_minor": 5,
15+
"nbformat": 4,
16+
"cells": [
17+
{
18+
"id": "80e69eae-cd44-4046-9f24-4cde652dbc0f",
19+
"cell_type": "markdown",
20+
"source": "Oracle AI Data Platform v1.0\n\nCopyright © 2025, Oracle and/or its affiliates.\n\nLicensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/",
21+
"metadata": {
22+
"type": "markdown"
23+
}
24+
},
25+
{
26+
"id": "f2356308-64bd-4fcb-ae88-5869a227eb2a",
27+
"cell_type": "markdown",
28+
"source": "### Sample Code: Downloading Notebooks/Files from Git.\n\nThis example demonstrates how to download notebook/files from Git as ZIP and extract them to AIDP Workspace. It uses requests python library.\n\n\n**Note:** \n\n- Replace all placeholders (e.g., `<GIT_USERNAME>`, `<GIT_PAT>`, `<AIDP_WORKSPACE_PATH>`, `<GIT_REPO_NAME>`, `<GIT_BRANCH>` etc.) with values specific to your environment before running the notebook. \n- Use with caution: Notebook is designed for downloading all files from repo and unarchive to provided workspace path.\n- This notebook should be executed from AIDP.",
29+
"metadata": {
30+
"type": "markdown"
31+
}
32+
},
33+
{
34+
"id": "065a7b5a-727d-4297-a502-e92c074f626f",
35+
"cell_type": "code",
36+
"source": "# Username of Github\ngit_username = \"<GIT_USERNAME>\"\n# Access Token of Github\ngit_pat = \"<GIT_PAT>\"\n# AIDP Workspace path to download repository\naidp_path = \"<AIDP_WORKSPACE_PATH>\"\n# Github Repository Name\ngit_repo_name = \"<GIT_REPO_NAME>\"\n# Github Branch Name\ngit_branch = \"<GIT_BRANCH>\"",
37+
"metadata": {
38+
"execution": {
39+
"iopub.status.busy": "2025-11-04T14:37:15.709Z"
40+
},
41+
"type": "python",
42+
"trusted": true
43+
},
44+
"outputs": [],
45+
"execution_count": 1
46+
},
47+
{
48+
"id": "d730a44f-6582-40eb-8c72-f88519b01e23",
49+
"cell_type": "code",
50+
"source": "import requests\nimport zipfile\nimport io\nimport os\n\nif not git_pat:\n raise ValueError(\"GIT_PERSONAL_ACCESS_TOKEN not set\")\n\nzip_url = f\"https://api.github.com/repos/{git_username}/{git_repo_name}/zipball/{git_branch}\"\n\ntry:\n # Headers for PAT authentication\n headers = {\n \"Authorization\": f\"token {git_pat}\",\n \"Accept\": \"application/vnd.github.v3+json\"\n }\n\n print(f\"Downloading ZIP from {zip_url}...\")\n response = requests.get(zip_url, headers=headers, stream=True)\n response.raise_for_status()\n\n # Extract the ZIP content\n with zipfile.ZipFile(io.BytesIO(response.content)) as zip_ref:\n \n top_level_folder = os.path.commonprefix(zip_ref.namelist())\n # Extract all files\n zip_ref.extractall(aidp_path)\n print(\"Repository downloaded and extracted successfully.\")\n\nexcept requests.exceptions.RequestException as e:\n print(f\"Error downloading repository: {e}\")\nexcept zipfile.BadZipFile:\n print(\"Downloaded file is not a valid ZIP file.\")\nexcept Exception as e:\n print(f\"An unexpected error occurred: {e}\")\n",
51+
"metadata": {
52+
"execution": {
53+
"iopub.status.busy": "2025-11-04T14:37:29.146Z"
54+
},
55+
"type": "python",
56+
"trusted": true
57+
},
58+
"outputs": [
59+
{
60+
"data": {
61+
"text/plain": "Downloading ZIP from https://api.github.com/repos/nishgit/git-sdk-test/zipball/main...\n"
62+
},
63+
"metadata": {},
64+
"output_type": "display_data"
65+
},
66+
{
67+
"data": {
68+
"text/plain": "Repository downloaded and extracted successfully.\n"
69+
},
70+
"metadata": {},
71+
"output_type": "display_data"
72+
}
73+
],
74+
"execution_count": 1
75+
}
76+
]
77+
}

0 commit comments

Comments
 (0)