log2ml/3-2-combine-code-data-documentation-project-manage/GitHub-Release-and-Secrets.ipynb

271 lines
10 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"id": "fa74a82c3dc6db1a",
"metadata": {},
"source": [
"# GitHub for data releases "
]
},
{
"cell_type": "markdown",
"id": "38c6a6b67bd16e42",
"metadata": {},
"source": [
"# Introduction"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "b8b55c9e96c43bc3",
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-07T11:57:54.752360Z",
"start_time": "2024-05-07T11:57:51.973091Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: python-dotenv in /home/marius/miniconda3/envs/llm_langchain/lib/python3.11/site-packages (1.0.1)\r\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install python-dotenv"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "b68da291930fbf4",
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-07T16:43:17.166579Z",
"start_time": "2024-05-07T16:43:12.783177Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting PyGithub\r\n",
" Downloading PyGithub-2.3.0-py3-none-any.whl.metadata (3.8 kB)\r\n",
"Collecting pynacl>=1.4.0 (from PyGithub)\r\n",
" Using cached PyNaCl-1.5.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl.metadata (8.6 kB)\r\n",
"Requirement already satisfied: requests>=2.14.0 in /home/marius/miniconda3/envs/llm_langchain/lib/python3.11/site-packages (from PyGithub) (2.31.0)\r\n",
"Collecting pyjwt>=2.4.0 (from pyjwt[crypto]>=2.4.0->PyGithub)\r\n",
" Downloading PyJWT-2.8.0-py3-none-any.whl.metadata (4.2 kB)\r\n",
"Requirement already satisfied: typing-extensions>=4.0.0 in /home/marius/miniconda3/envs/llm_langchain/lib/python3.11/site-packages (from PyGithub) (4.10.0)\r\n",
"Requirement already satisfied: urllib3>=1.26.0 in /home/marius/miniconda3/envs/llm_langchain/lib/python3.11/site-packages (from PyGithub) (2.2.1)\r\n",
"Requirement already satisfied: Deprecated in /home/marius/miniconda3/envs/llm_langchain/lib/python3.11/site-packages (from PyGithub) (1.2.14)\r\n",
"Requirement already satisfied: cryptography>=3.4.0 in /home/marius/miniconda3/envs/llm_langchain/lib/python3.11/site-packages (from pyjwt[crypto]>=2.4.0->PyGithub) (42.0.5)\r\n",
"Requirement already satisfied: cffi>=1.4.1 in /home/marius/miniconda3/envs/llm_langchain/lib/python3.11/site-packages (from pynacl>=1.4.0->PyGithub) (1.16.0)\r\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /home/marius/miniconda3/envs/llm_langchain/lib/python3.11/site-packages (from requests>=2.14.0->PyGithub) (3.3.2)\r\n",
"Requirement already satisfied: idna<4,>=2.5 in /home/marius/miniconda3/envs/llm_langchain/lib/python3.11/site-packages (from requests>=2.14.0->PyGithub) (3.6)\r\n",
"Requirement already satisfied: certifi>=2017.4.17 in /home/marius/miniconda3/envs/llm_langchain/lib/python3.11/site-packages (from requests>=2.14.0->PyGithub) (2024.2.2)\r\n",
"Requirement already satisfied: wrapt<2,>=1.10 in /home/marius/miniconda3/envs/llm_langchain/lib/python3.11/site-packages (from Deprecated->PyGithub) (1.16.0)\r\n",
"Requirement already satisfied: pycparser in /home/marius/miniconda3/envs/llm_langchain/lib/python3.11/site-packages (from cffi>=1.4.1->pynacl>=1.4.0->PyGithub) (2.21)\r\n",
"Downloading PyGithub-2.3.0-py3-none-any.whl (354 kB)\r\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m354.4/354.4 kB\u001b[0m \u001b[31m3.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m\r\n",
"\u001b[?25hDownloading PyJWT-2.8.0-py3-none-any.whl (22 kB)\r\n",
"Downloading PyNaCl-1.5.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (856 kB)\r\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m856.7/856.7 kB\u001b[0m \u001b[31m11.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m\r\n",
"\u001b[?25hInstalling collected packages: pyjwt, pynacl, PyGithub\r\n",
"Successfully installed PyGithub-2.3.0 pyjwt-2.8.0 pynacl-1.5.0\r\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install PyGithub"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "68cdc5d497e208e3",
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-11T16:18:46.000392Z",
"start_time": "2024-05-11T16:18:45.992401Z"
}
},
"outputs": [],
"source": [
"from dotenv import load_dotenv\n",
"import os\n",
"\n",
"load_dotenv(\"thesis_env_ro\", verbose=True) # take environment variables from the file\n",
"token = os.getenv('GITHUB_PERSONAL_ACCESS_TOKEN')"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "1069e0bfa4686f67",
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-16T17:44:25.085423Z",
"start_time": "2024-05-16T17:44:21.570849Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"https://api.github.com/repos/norandom/log2ml/releases/assets/168114916\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5f0622d443bc48728f0e7ed72ebd7fab",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0.00/6.28M [00:00<?, ?iB/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"File downloaded successfully and saved as lab_logs_normal_activity_may_15_2024.csv\n"
]
}
],
"source": [
"from github import Github\n",
"import requests\n",
"from tqdm.notebook import tqdm\n",
"\n",
"\n",
"def get_specific_file_from_tagged_release(token, repo_name, tag_name, filename):\n",
" g = Github(token)\n",
" repo = g.get_repo(repo_name)\n",
" releases = repo.get_releases()\n",
"\n",
" for release in releases:\n",
" if release.tag_name == tag_name:\n",
" for asset in release.get_assets():\n",
" if asset.name == filename:\n",
" return asset.url\n",
" print(\"File not found. Try get_specific_file_from_latest_release() instead.\")\n",
" return None\n",
"\n",
"def get_specific_file_from_latest_release(token, repo_name, filename):\n",
" g = Github(token)\n",
" repo = g.get_repo(repo_name)\n",
" release = repo.get_latest_release()\n",
"\n",
" for asset in release.get_assets():\n",
" if asset.name == filename:\n",
" return asset.url # Use asset.url which points to API URL needing headers\n",
"\n",
"def download_file(url, token, save_path):\n",
" headers = {'Authorization': f'token {token}', 'Accept': 'application/octet-stream'}\n",
" # First request to handle GitHub's redirection and authentication properly\n",
" with requests.get(url, headers=headers, stream=True) as initial_response:\n",
" initial_response.raise_for_status() # Ensure the initial request is successful\n",
" # Follow redirection if necessary, maintaining headers\n",
" if initial_response.history:\n",
" url = initial_response.url # Updated URL after redirection\n",
"\n",
" # Now, proceed with downloading the file\n",
" with requests.get(url, headers=headers, stream=True) as response:\n",
" response.raise_for_status()\n",
" total_size_in_bytes = int(response.headers.get('content-length', 0))\n",
" block_size = 1024\n",
" \n",
" progress_bar = tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True)\n",
" with open(save_path, 'wb') as file:\n",
" for data in response.iter_content(block_size):\n",
" progress_bar.update(len(data))\n",
" file.write(data)\n",
" progress_bar.close()\n",
"\n",
" if total_size_in_bytes != 0 and progress_bar.n != total_size_in_bytes:\n",
" print(\"ERROR, something went wrong\")\n",
" else:\n",
" print(f\"File downloaded successfully and saved as {save_path}\")\n",
"\n",
"# Your GitHub token\n",
"github_token = token\n",
"\n",
"# Repository name\n",
"repository_name = \"norandom/log2ml\"\n",
"\n",
"# File name to search for\n",
"file_name = \"lab_logs_normal_activity_may_15_2024.json\"\n",
"\n",
"# Get the download URL of the specific file\n",
"# download_url = get_specific_file_from_latest_release(github_token, repository_name, file_name)\n",
"download_url = get_specific_file_from_tagged_release(github_token, repository_name, \"foundations\", file_name)\n",
"print(download_url)\n",
"\n",
"if download_url:\n",
" local_file_path = \"lab_logs_normal_activity_may_15_2024.csv\"\n",
" download_file(download_url, github_token, local_file_path)\n",
"else:\n",
" print(\"File not found.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "393703bd6e7a693f",
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-16T17:44:33.762701Z",
"start_time": "2024-05-16T17:44:33.641528Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8000 lab_logs_normal_activity_may_15_2024.csv\r\n"
]
}
],
"source": [
"!wc -l lab_logs_normal_activity_may_15_2024.csv"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}