-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy-oc-up
More file actions
executable file
·230 lines (197 loc) · 8.29 KB
/
py-oc-up
File metadata and controls
executable file
·230 lines (197 loc) · 8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import configparser
import owncloud # pip install pyocclient
from urllib.error import HTTPError
import requests.exceptions
import importlib
import sys
import os
from pathlib import Path
importlib.reload(sys)
# Class for parsing config .cfg file
class Settings(object):
def __init__(self, configfile):
self.config = configparser.ConfigParser(interpolation=None)
self.config.read(configfile)
def load(self, section, opts):
try:
for key in opts:
try:
setattr(self, key, self.config.get(section, key))
except configparser.NoOptionError:
setattr(self, key, self.config.get('default', key))
except configparser.NoSectionError:
print(f'The section "{section}" does not exist')
except configparser.NoOptionError:
print(f'The value for "{key}" is missing')
else:
return True
return False
cfg_forum = 'dctrad'
cfg_opts = ['host',
'username',
'password'
]
owncloud_cfg = '.owncloud.cfg'
home = str(Path.home())
owncloud_cfg_home = os.path.join(home, owncloud_cfg)
if os.path.isfile(owncloud_cfg):
print(f"./{owncloud_cfg} found")
use_cfg = owncloud_cfg
elif os.path.isfile(owncloud_cfg_home):
print(f"{owncloud_cfg_home} found")
use_cfg = owncloud_cfg_home
else:
print("not found, either in local folder, or in home folder")
print(f"Rename {owncloud_cfg}.exemple in {owncloud_cfg}")
print("and put it in your HOME or your working folder")
cfg = Settings(use_cfg)
if cfg.load(cfg_forum, cfg_opts):
oc = owncloud.Client(cfg.host) # pylint: disable=no-member
print('--------------------------------------------------------------')
print(' Loged in Owncloud ')
print('--------------------------------------------------------------')
print("Le script vous permet d'uploader le CONTENU d'un dossier local")
print("vers un dossier du Owncloud existant, pour le mettre à jour")
print("Par exemple, vous pouvez uploader le CONTENU "
"(avec toute l'arborescence) d'un dossier :")
print('"Groupe Superman (nouvelle version)"')
print('vers le dossier :')
print('Owncloud "Groupe Superman"')
print('--------------------------------------------------------------')
print(' Choix du dossier LOCAL dont le contenu sera uploadé')
print(' Exemple : "./TODO/Groupe Superman Perso"')
print('--------------------------------------------------------------')
dir = os.getcwd()
dir = os.path.normpath(dir)
if os.path.exists(dir):
print("yay")
print(dir)
while True:
dir = os.path.normpath(dir)
local_dirs = [d for d in os.listdir(dir)]
i = 1
for ld in local_dirs:
# print(str(i) + "-\t" + ld)
print(f'{i:.<5d}{ld:<s}')
i += 1
choice = input("tapez un chiffre pour naviguer dans "
"le dossier, ou 'y' pour valider.\n")
if choice != 'y':
try:
dir_name = local_dirs[int(choice)-1]
dir = os.path.join(dir, dir_name)
if os.path.exists(dir):
print("yay")
print(dir)
else:
print(dir + " doesn't exist")
dir = os.getcwd()
except IndexError:
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("Mauvaise valeur")
else:
break
local_dir = dir
print(f"Dossier local : {local_dir}")
try:
oc.login(cfg.username, cfg.password) # pylint: disable=no-member
except requests.exceptions.MissingSchema:
print("Erreur.")
print(f"Veuillez configurer {owncloud_cfg} avec une url correcte")
except owncloud.owncloud.HTTPResponseError:
print("Erreur.")
print(f" Veuillez configurer {owncloud_cfg} "
"avec utilisateur et mot de passe valide")
# print(e)
sys.exit(1)
print('--------------------------------------------------------------')
print(' Choix du dossier Owncloud de destination: ')
print(' Exemple : DC Comics/New 52/Groupe Superman')
print('--------------------------------------------------------------')
try:
folder_path = '/'
folder_name = ''
while True:
list_dir = oc.list(folder_path, depth=1)
print("==========================================================")
print(f"Contenu de : {folder_path}")
print("Dossiers")
print("..........................................................")
i = 1
folder_list = []
for file in list_dir:
if file.is_dir():
name = file.get_name()
# full_path = file.get_path() + '/' + newName
full_path = file.get_path()
folder_list.append({'path': full_path, 'name': name})
# print(newName)
# print(str(i) + "-\t" + full_path)
print(f'{i:.<5d}{full_path:<s}')
i += 1
print("==========================================================")
print("Fichiers")
print("..........................................................")
for file in list_dir:
if not file.is_dir():
newName = file.get_name()
full_path = file.get_path() + '/' + newName
# full_path = file.get_path()
print(newName)
print("==========================================================")
choice = input("tapez un chiffre pour naviguer dans "
"le dossier, ou 'y' pour valider.\n")
if choice != 'y':
try:
folder_path = folder_list[int(choice)-1]['path']
folder_name = folder_list[int(choice)-1]['name']
except IndexError:
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("Mauvaise valeur")
else:
break
print(f"Dossier distant : {folder_path}")
print('-------------------------------------------------------------')
print('Vous allez uploader le contenu de :')
print(f'\t{local_dir}')
print('dans le dossier Owncloud :')
print(f'\t{folder_path}')
print('-------------------------------------------------------------')
choice = input("Voulez-vous continuer (y/n) ?\n")
if choice != 'y':
sys.exit(1)
# UPLOAD recap
print('--------------------------------------------------------------')
print('RECAPITULATIF')
print('--------------------------------------------------------------')
for root, dirs, files in os.walk(local_dir, topdown=True):
for name in files:
file_l_path = os.path.join(root, name)
file_rel_path = os.path.relpath(file_l_path, start=local_dir)
norm_path = os.path.normpath(file_rel_path)
remote_path = \
os.path.join(folder_path, norm_path).replace('\\', '/')
print(f"up : {file_l_path} in \t\t\t {remote_path}")
choice = input("Voulez-vous continuer (y/n) ?\n")
if choice != 'y':
sys.exit(1)
# UPLOAD real deal
for root, dirs, files in os.walk(local_dir, topdown=True):
for name in files:
file_l_path = os.path.join(root, name)
file_rel_path = os.path.relpath(file_l_path, start=local_dir)
norm_path = os.path.normpath(file_rel_path)
remote_path = \
os.path.join(folder_path, norm_path).replace('\\', '/')
print(f"up : {file_l_path} in \t\t\t {remote_path}")
try:
oc.put_file(remote_path, file_l_path)
except Exception as e:
print(e)
except HTTPError as e:
print(e.code)
except owncloud.owncloud.HTTPResponseError:
print("Dossier non valide")
input("Pressez une touche pour quitter")