wip(22w8a): add wip db and gcs client

This commit is contained in:
Cloud Shell 2022-02-25 01:04:00 +00:00
parent 9071d6d9fe
commit edc3b6fb9d
17 changed files with 154 additions and 0 deletions

4
.env.example Normal file
View file

@ -0,0 +1,4 @@
SOURCE_FOLDER=
TARGET_BUCKET=
GOOGLE_APPLICATION_CREDENTIALS=

50
.gitignore vendored Normal file
View file

@ -0,0 +1,50 @@
# Bootstrapping #
#################
/node_modules
/public/hot
/public/storage
/storage/*.key
/vendor
.env
.env.backup
.phpunit.result.cache
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
public/robots.txt
# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
Icon?
ehthumbs.db
Thumbs.db
.directory
# Tool specific files #
#######################
# vim
*~
*.swp
*.swo
# sublime text & textmate
*.sublime-*
*.stTheme.cache
*.tmlanguage.cache
*.tmPreferences.cache
# Eclipse
.settings/*
# JetBrains, aka PHPStorm, IntelliJ IDEA
.idea/*
# NetBeans
nbproject/*
# Visual Studio Code
.vscode
# Sass preprocessor
.sass-cache/

14
.theia/launch.json Normal file
View file

@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}

4
backup.py Normal file
View file

@ -0,0 +1,4 @@
from src import SQLite, StorageClient
client = SQLite()
print("OK")

2
requirements.txt Normal file
View file

@ -0,0 +1,2 @@
python-dotenv
google-cloud-storage

10
src/__init__.py Normal file
View file

@ -0,0 +1,10 @@
from dotenv import load_dotenv
from .glob import file_exists
from .db import SQLite
from .gcs import StorageClient
if not file_exists(".env"):
raise FileNotFoundError("Environment variable file does not exist. Copy '.env.example' to '.env'")
load_dotenv()

Binary file not shown.

Binary file not shown.

1
src/db/__init__.py Normal file
View file

@ -0,0 +1 @@
from .sqlite import SQLite

Binary file not shown.

Binary file not shown.

43
src/db/sqlite.py Normal file
View file

@ -0,0 +1,43 @@
import os
import sqlite3 as sqlite
from ..glob import file_exists
class SQLite():
def __init__(self):
self.db = sqlite.connect(self.get_db_path())
self.cursor = self.db.cursor()
self.init()
def query(self, sql: str):
result = self.cursor.execute(sql)
if result.rowcount < 1:
return False
return result
def get_db_path(self) -> str:
name = ".gcsarchive.db"
path = os.getenv("SOURCE_FOLDER")
# Append db file name if absent
if not path.endswith(name):
# Append tailing slash if absent
if path[-1] != "/":
path += "/"
path += name
return path
def configure_db(self):
metadata_sql = "CREATE TABLE metadata (key text, value text)"
metadata = self.query(metadata_sql)
def init(self):
# Set up db if it's fresh
hasmeta_sql = "SELECT name FROM sqlite_master WHERE type='table' AND name='metadata'"
if not self.query(hasmeta_sql):
self.configure_db()
return True

1
src/gcs/__init__.py Normal file
View file

@ -0,0 +1 @@
from .client import StorageClient

Binary file not shown.

Binary file not shown.

21
src/gcs/client.py Normal file
View file

@ -0,0 +1,21 @@
import os
from os.path import exists
from google.cloud import storage
class StorageClient(storage.Client):
def __init__(self, bucket: str = None):
if not bucket:
bucket = os.getenv("TARGET_BUCKET")
if not self.gcloud_key_exists():
raise Exception("GOOGLE_APPLICATION_CREDENTIALS has to point to a key file")
super().__init__()
# Check if env var is set to a key file
def gcloud_key_exists(self) -> bool:
keyfile = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
if not keyfile or not exists(keyfile):
return False
return True

4
src/glob.py Normal file
View file

@ -0,0 +1,4 @@
import os.path
def file_exists(file: str) -> bool:
return os.path.isfile(file)