first commit
This commit is contained in:
110
FoldersToSQL.py
Normal file
110
FoldersToSQL.py
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import datetime
|
||||||
|
import mysql.connector
|
||||||
|
|
||||||
|
# Requirements:
|
||||||
|
# Install python 3.x from https://www.python.org/downloads/
|
||||||
|
# pip install mysql-connector-python
|
||||||
|
|
||||||
|
# Usage:
|
||||||
|
# python foldersToSQL.py
|
||||||
|
|
||||||
|
# ================== CONFIGURATION ==================
|
||||||
|
SCAN_PATH = r"C:\Users\sebastien.plante\Downloads\test folders"
|
||||||
|
DB_CONFIG = {
|
||||||
|
"host": "localhost",
|
||||||
|
"user": "root",
|
||||||
|
"password": "qwerty1234",
|
||||||
|
"database": "testdb",
|
||||||
|
}
|
||||||
|
# ===================================================
|
||||||
|
|
||||||
|
TABLE_SQL = """
|
||||||
|
CREATE TABLE IF NOT EXISTS folders (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
name VARCHAR(255) NOT NULL UNIQUE,
|
||||||
|
last_modified DATETIME NOT NULL,
|
||||||
|
active TINYINT(1) NOT NULL DEFAULT 1
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
|
"""
|
||||||
|
|
||||||
|
UPSERT_SQL = """
|
||||||
|
INSERT INTO folders (name, last_modified, active)
|
||||||
|
VALUES (%s, %s, 1)
|
||||||
|
ON DUPLICATE KEY UPDATE
|
||||||
|
last_modified = VALUES(last_modified);
|
||||||
|
"""
|
||||||
|
|
||||||
|
MARK_INACTIVE_SQL_TEMPLATE = "UPDATE folders SET active = 0 WHERE name NOT IN ({placeholders});"
|
||||||
|
MARK_ALL_INACTIVE_SQL = "UPDATE folders SET active = 0;"
|
||||||
|
|
||||||
|
|
||||||
|
def get_connection():
|
||||||
|
return mysql.connector.connect(**DB_CONFIG)
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_table(cursor):
|
||||||
|
cursor.execute(TABLE_SQL)
|
||||||
|
|
||||||
|
|
||||||
|
def list_directories(base_path):
|
||||||
|
try:
|
||||||
|
entries = os.listdir(base_path)
|
||||||
|
except OSError as e:
|
||||||
|
print(f"Erreur lecture dossier: {e}")
|
||||||
|
sys.exit(1)
|
||||||
|
dirs = []
|
||||||
|
for name in entries:
|
||||||
|
full = os.path.join(base_path, name)
|
||||||
|
if os.path.isdir(full):
|
||||||
|
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(full))
|
||||||
|
dirs.append((name, mtime))
|
||||||
|
return dirs
|
||||||
|
|
||||||
|
|
||||||
|
def sync(base_path):
|
||||||
|
conn = get_connection()
|
||||||
|
try:
|
||||||
|
cur = conn.cursor()
|
||||||
|
ensure_table(cur)
|
||||||
|
|
||||||
|
current_dirs = list_directories(base_path)
|
||||||
|
current_names = {name for name, _ in current_dirs}
|
||||||
|
|
||||||
|
for name, mtime in current_dirs:
|
||||||
|
cur.execute(UPSERT_SQL, (name, mtime))
|
||||||
|
|
||||||
|
if current_names:
|
||||||
|
placeholders = ",".join(["%s"] * len(current_names))
|
||||||
|
sql = MARK_INACTIVE_SQL_TEMPLATE.format(placeholders=placeholders)
|
||||||
|
cur.execute(sql, list(current_names))
|
||||||
|
else:
|
||||||
|
cur.execute(MARK_ALL_INACTIVE_SQL)
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
print(
|
||||||
|
f"Synchronisation terminée. Dossiers actifs: {len(current_dirs)}")
|
||||||
|
except mysql.connector.Error as err:
|
||||||
|
print(f"Erreur MySQL: {err}")
|
||||||
|
finally:
|
||||||
|
cur.close()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if len(sys.argv) >= 2:
|
||||||
|
base_path = sys.argv[1]
|
||||||
|
else:
|
||||||
|
base_path = SCAN_PATH
|
||||||
|
|
||||||
|
if not os.path.isdir(base_path):
|
||||||
|
print(f"Chemin invalide: {base_path}")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
print(f"Scan dossier: {base_path}")
|
||||||
|
sync(base_path)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user