-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.py
77 lines (63 loc) · 2.32 KB
/
storage.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# __author__ = 'maximus'
import os
from datetime import datetime
from shutil import copy2, make_archive, rmtree
import logging
logger = logging.getLogger('CCND')
class FileStorage(object):
def __init__(self, path):
# self.path = store_init['id']
# self.type = store_init['type']
self.dir_root = path
self.dir_session = None
self.path_session = None
logger.info('Init file storage')
def add_to_storage(self, fname, data):
with open(self.path_session + '/' + fname + '.cfg', 'w') as file:
file.write(data)
def add_to_storage_from_tftp(self, fname, path_to_tftp):
tftp = path_to_tftp + '/' + fname + '.cfg'
storage = self.path_session + '/' + fname + '.cfg'
try:
copy2(tftp, storage)
os.remove(tftp)
except FileNotFoundError as err:
print(err)
return False
return True
def new_session(self):
self.dir_session = 'backup-{}'.format(datetime.now().strftime("%Y-%m-%dT%H:%M"))
self.path_session = '{}/{}'.format(self.dir_root, self.dir_session)
os.makedirs(self.path_session, exist_ok=True)
def close_session(self):
self.archive()
self.path_session = None
def archive(self):
make_archive(self.path_session, 'gztar', self.dir_root, self.dir_session)
rmtree(self.path_session)
class GitStorage(object):
def __init__(self, path):
self.dir_root = path
self.dir_session = None
self.path_session = None
logger.info('Init file storage')
def add_to_storage(self, fname, data):
pass
def add_to_storage_from_tftp(self, fname, path_to_tftp):
tftp = path_to_tftp + '/' + fname + '.cfg'
storage = self.path_session + '/' + fname + '.cfg'
try:
copy2(tftp, storage)
os.remove(tftp)
except FileNotFoundError as err:
print(err)
return False
return True
def new_session(self):
self.dir_session = 'backup-{}'.format(datetime.now().strftime("%Y-%m-%dT%H:%M"))
self.path_session = '{}/{}'.format(self.dir_root, self.dir_session)
os.makedirs(self.path_session, exist_ok=True)
def close_session(self):
self.path_session = None