|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "80e69eae-cd44-4046-9f24-4cde652dbc0f", |
| 6 | + "metadata": { |
| 7 | + "type": "markdown" |
| 8 | + }, |
| 9 | + "source": [ |
| 10 | + "Oracle AI Data Platform v1.0\n", |
| 11 | + "\n", |
| 12 | + "Copyright © 2025, Oracle and/or its affiliates.\n", |
| 13 | + "\n", |
| 14 | + "Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "id": "f2356308-64bd-4fcb-ae88-5869a227eb2a", |
| 20 | + "metadata": { |
| 21 | + "type": "markdown" |
| 22 | + }, |
| 23 | + "source": [ |
| 24 | + "### Sample Code: Downloading Notebooks/Files from Git.\n", |
| 25 | + "\n", |
| 26 | + "This example demonstrates how to download notebook/files from Git as ZIP and extract them to AIDP Workspace. It uses requests python library.\n", |
| 27 | + "\n", |
| 28 | + "\n", |
| 29 | + "**Note:** \n", |
| 30 | + "\n", |
| 31 | + "- Replace all placeholders (e.g., `<GIT_USERNAME>`, `<GIT_PAT>`, `<AIDP_WORKSPACE_PATH>`, `<GIT_REPO_NAME>`, `<GIT_BRANCH>`, `<IS_PRIVATE_REPO>` etc.) with values specific to your environment before running the notebook. \n", |
| 32 | + "- GIT_PAT is mandatory only for private repository.\n", |
| 33 | + "- Use with caution: Notebook is designed for downloading all files from repo and unarchive to provided workspace path.\n", |
| 34 | + "- This notebook should be executed from AIDP.\n", |
| 35 | + "- Hardcoding Git token into the notebook leaves credentials vulnerable. It is strongly advised to manage the token securely by loading it dynamically from an environment variable, a secure configuration file, or a OCI Vault." |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": null, |
| 41 | + "id": "065a7b5a-727d-4297-a502-e92c074f626f", |
| 42 | + "metadata": { |
| 43 | + "execution": { |
| 44 | + "iopub.status.busy": "2025-11-04T14:37:15.709Z" |
| 45 | + }, |
| 46 | + "trusted": true, |
| 47 | + "type": "python" |
| 48 | + }, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "# Username of Github\n", |
| 52 | + "git_username = \"<GIT_USERNAME>\"\n", |
| 53 | + "# Access Token of Github, needed for Private Repository.\n", |
| 54 | + "git_pat = \"<GIT_PAT>\"\n", |
| 55 | + "# AIDP Workspace path to download repository\n", |
| 56 | + "aidp_path = \"<AIDP_WORKSPACE_PATH>\"\n", |
| 57 | + "# Github Repository Name\n", |
| 58 | + "git_repo_name = \"<GIT_REPO_NAME>\"\n", |
| 59 | + "# Github Branch Name\n", |
| 60 | + "git_branch = \"<GIT_BRANCH>\"\n", |
| 61 | + "# Field 'is_private_repo' should be provided as a boolean (True/False).\n", |
| 62 | + "is_private_repo = \"<IS_PRIVATE_REPO>\"" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "execution_count": null, |
| 68 | + "id": "d730a44f-6582-40eb-8c72-f88519b01e23", |
| 69 | + "metadata": { |
| 70 | + "execution": { |
| 71 | + "iopub.status.busy": "2025-11-04T14:37:29.146Z" |
| 72 | + }, |
| 73 | + "trusted": true, |
| 74 | + "type": "python" |
| 75 | + }, |
| 76 | + "outputs": [], |
| 77 | + "source": [ |
| 78 | + "import requests\n", |
| 79 | + "import zipfile\n", |
| 80 | + "import io\n", |
| 81 | + "\n", |
| 82 | + "zip_url = f\"https://api.github.com/repos/{git_username}/{git_repo_name}/zipball/{git_branch}\"\n", |
| 83 | + "\n", |
| 84 | + "try:\n", |
| 85 | + " headers = {\n", |
| 86 | + " \"Accept\": \"application/vnd.github.v3+json\"\n", |
| 87 | + " }\n", |
| 88 | + "\n", |
| 89 | + " #Authorization requires only for Private Repo.\n", |
| 90 | + " if is_private_repo:\n", |
| 91 | + " if not git_pat:\n", |
| 92 | + " raise ValueError(\"GIT_PERSONAL_ACCESS_TOKEN not set\")\n", |
| 93 | + " headers[\"Authorization\"] = f\"token {git_pat}\"\n", |
| 94 | + " \n", |
| 95 | + " print(f\"Downloading ZIP from {zip_url}...\")\n", |
| 96 | + " response = requests.get(zip_url, headers=headers, stream=True)\n", |
| 97 | + " response.raise_for_status()\n", |
| 98 | + "\n", |
| 99 | + " # Extract the ZIP content\n", |
| 100 | + " with zipfile.ZipFile(io.BytesIO(response.content)) as zip_ref:\n", |
| 101 | + " \n", |
| 102 | + " top_level_folder = os.path.commonprefix(zip_ref.namelist())\n", |
| 103 | + " # Extract all files\n", |
| 104 | + " zip_ref.extractall(aidp_path)\n", |
| 105 | + " print(\"Repository downloaded and extracted successfully.\")\n", |
| 106 | + "\n", |
| 107 | + "except requests.exceptions.RequestException as e:\n", |
| 108 | + " print(f\"Error downloading repository: {e}\")\n", |
| 109 | + "except zipfile.BadZipFile:\n", |
| 110 | + " print(\"Downloaded file is not a valid ZIP file.\")\n", |
| 111 | + "except Exception as e:\n", |
| 112 | + " print(f\"An unexpected error occurred: {e}\")" |
| 113 | + ] |
| 114 | + } |
| 115 | + ], |
| 116 | + "metadata": { |
| 117 | + "Last_Active_Cell_Index": 2, |
| 118 | + "kernelspec": { |
| 119 | + "display_name": "python3", |
| 120 | + "name": "notebook" |
| 121 | + }, |
| 122 | + "language_info": { |
| 123 | + "file_extension": ".py", |
| 124 | + "mimetype": "text/x-python", |
| 125 | + "name": "python" |
| 126 | + } |
| 127 | + }, |
| 128 | + "nbformat": 4, |
| 129 | + "nbformat_minor": 5 |
| 130 | +} |
0 commit comments