Hi there,
I’m trying to create a graphical interface to mount some SMB share when few applications aren’t able to access them (snap, flatpak, etc.).
While Dolphin managed shares easily, some applications like ansel (a darktable fork) is only available as a snap file, so it’s unable to open collection of photos on the server.
However, I’ve tried to create something with Claude 3.5 AI which seems right, but it cannot create the folder in /mnt. Could someone help me?
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import tkinter as tk
from tkinter import ttk, messagebox
import subprocess
import os
class SMBMounter:
def __init__(self, root):
self.root = root
self.root.title("Montage des partages Lagrange")
# Configuration
self.server = "lagrange"
self.mount_base = "/mnt/lagrange"
self.shares = {
"photo": {"path": "photo", "mounted": False},
"music": {"path": "music", "mounted": False},
"video": {"path": "video", "mounted": False},
"commun": {"path": "commun", "mounted": False}
}
# Création des points de montage au démarrage
self.create_mount_points()
# Création de l'interface
self.create_widgets()
# Vérification initiale des montages
self.check_mounted_shares()
def create_mount_points(self):
"""Crée les points de montage nécessaires"""
try:
# Création du répertoire de base
if not os.path.exists(self.mount_base):
subprocess.run(['sudo', 'mkdir', '-p', self.mount_base])
# Création des sous-répertoires pour chaque partage
for share in self.shares:
mount_point = f"{self.mount_base}/{share}"
if not os.path.exists(mount_point):
subprocess.run(['sudo', 'mkdir', '-p', mount_point])
subprocess.run(['sudo', 'chmod', '777', mount_point])
except Exception as e:
messagebox.showerror("Erreur", f"Erreur lors de la création des points de montage: {str(e)}")
def create_widgets(self):
# Frame principal avec padding
main_frame = ttk.Frame(self.root, padding="10")
main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
# Style pour les widgets
style = ttk.Style()
style.configure('TCheckbutton', padding=5)
style.configure('TButton', padding=5)
# Variables pour les checkboxes
self.check_vars = {}
# Création des checkboxes pour chaque partage
for idx, share in enumerate(self.shares):
self.check_vars[share] = tk.BooleanVar()
cb = ttk.Checkbutton(
main_frame,
text=f"Partage \\{share}",
variable=self.check_vars[share]
)
cb.grid(row=idx, column=0, sticky=tk.W, pady=2)
# Boutons avec plus d'espace
ttk.Button(
main_frame,
text="Monter la sélection",
command=self.mount_selected
).grid(row=len(self.shares), column=0, pady=10)
ttk.Button(
main_frame,
text="Tout démonter",
command=self.unmount_all
).grid(row=len(self.shares) + 1, column=0, pady=5)
def mount_selected(self):
"""Monte les partages sélectionnés"""
for share, var in self.check_vars.items():
if var.get():
mount_point = f"{self.mount_base}/{share}"
# Commande de montage avec options étendues
cmd = [
'sudo', 'mount', '-t', 'cifs',
f'//{self.server}/{share}',
mount_point,
'-o', 'guest,uid=$(id -u),gid=$(id -g)'
]
try:
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
messagebox.showinfo("Succès", f"Partage {share} monté avec succès")
else:
messagebox.showerror("Erreur",
f"Erreur lors du montage de {share}\n{result.stderr}")
except Exception as e:
messagebox.showerror("Erreur", str(e))
self.check_mounted_shares()
def unmount_all(self):
"""Démonte tous les partages"""
for share in self.shares:
mount_point = f"{self.mount_base}/{share}"
try:
subprocess.run(['sudo', 'umount', mount_point], capture_output=True)
except Exception as e:
messagebox.showerror("Erreur", f"Erreur lors du démontage de {share}: {str(e)}")
self.check_mounted_shares()
def check_mounted_shares(self):
"""Vérifie quels partages sont montés"""
with open('/proc/mounts', 'r') as f:
mounts = f.read()
for share in self.shares:
mount_point = f"{self.mount_base}/{share}"
self.shares[share]["mounted"] = mount_point in mounts
if __name__ == "__main__":
root = tk.Tk()
app = SMBMounter(root)
root.mainloop()