ec4d5d60731296db6c6f5022a8073f63cad1c9b7
[time-slider.git] / usr / share / time-slider / lib / time_slider / tmp2.py
1 #!/usr/bin/python2
2 #
3 # CDDL HEADER START
4 #
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
8 #
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
13 #
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
19 #
20 # CDDL HEADER END
21 #
22
23 import threading
24 import sys
25 import os
26 import time
27 import getopt
28 import locale
29 import shutil
30 import fcntl
31 from bisect import insort
32
33 try:
34     import pygtk
35     pygtk.require("2.4")
36 except:
37     pass
38 try:
39     import gtk
40     import gtk.glade
41     gtk.gdk.threads_init()
42 except:
43     sys.exit(1)
44 try:
45     import glib
46     import gobject
47 except:
48     sys.exit(1)
49
50 from os.path import abspath, dirname, join, pardir
51 sys.path.insert(0, join(dirname(__file__), pardir, "plugin"))
52 import plugin
53 sys.path.insert(0, join(dirname(__file__), pardir, "plugin", "rsync"))
54 import rsyncsmf
55
56
57 # here we define the path constants so that other modules can use it.
58 # this allows us to get access to the shared files without having to
59 # know the actual location, we just use the location of the current
60 # file and use paths relative to that.
61 SHARED_FILES = os.path.abspath(os.path.join(os.path.dirname(__file__),
62                                os.path.pardir,
63                                os.path.pardir))
64 LOCALE_PATH = os.path.join('/usr', 'share', 'locale')
65 RESOURCE_PATH = os.path.join(SHARED_FILES, 'res')
66
67 # the name of the gettext domain. because we have our translation files
68 # not in a global folder this doesn't really matter, setting it to the
69 # application name is a good idea tough.
70 GETTEXT_DOMAIN = 'time-slider'
71
72 # set up the glade gettext system and locales
73 gtk.glade.bindtextdomain(GETTEXT_DOMAIN, LOCALE_PATH)
74 gtk.glade.textdomain(GETTEXT_DOMAIN)
75
76 from . import zfs
77 from .rbac import RBACprofile
78
79 class RsyncBackup:
80
81     def __init__(self, mountpoint, rsync_dir = None,  fsname= None, snaplabel= None, creationtime= None):
82
83         if rsync_dir == None:
84           self.__init_from_mp (mountpoint)
85         else:
86           self.rsync_dir = rsync_dir
87           self.mountpoint = mountpoint
88           self.fsname = fsname
89           self.snaplabel = snaplabel
90   
91           self.creationtime = creationtime
92           try:
93               tm = time.localtime(self.creationtime)
94               self.creationtime_str = str(time.strftime ("%c", tm),
95                          locale.getpreferredencoding()).encode('utf-8')
96           except:
97               self.creationtime_str = time.ctime(self.creationtime)
98     
99     def __init_from_mp (self, mountpoint):
100         self.rsyncsmf = rsyncsmf.RsyncSMF("%s:rsync" %(plugin.PLUGINBASEFMRI))
101         rsyncBaseDir = self.rsyncsmf.get_target_dir()
102         sys,nodeName,rel,ver,arch = os.uname()
103         self.rsync_dir = os.path.join(rsyncBaseDir,
104                                      rsyncsmf.RSYNCDIRPREFIX,
105                                      nodeName)
106         self.mountpoint = mountpoint
107         
108         s1 = mountpoint.split ("%s/" % self.rsync_dir, 1)
109         s2 = s1[1].split ("/%s" % rsyncsmf.RSYNCDIRSUFFIX, 1)
110         s3 = s2[1].split ('/',2)
111         self.fsname = s2[0]
112         self.snaplabel =  s3[1]
113         self.creationtime = os.stat(mountpoint).st_mtime
114
115     def __str__(self):
116         ret = "self.rsync_dir = %s\n \
117                self.mountpoint = %s\n \
118                self.fsname = %s\n \
119                self.snaplabel = %s\n" % (self.rsync_dir, 
120                                          self.mountpoint, self.fsname,
121                                          self.snaplabel)
122         return ret                                       
123
124
125     def exists(self):
126         return os.path.exists(self.mountpoint)
127
128     def destroy(self):
129         lockFileDir = os.path.join(self.rsync_dir,
130                              self.fsname,
131                              rsyncsmf.RSYNCLOCKSUFFIX)
132
133         if not os.path.exists(lockFileDir):
134           os.makedirs(lockFileDir, 0o755)
135         
136         lockFile = os.path.join(lockFileDir, self.snaplabel + ".lock")
137         try:
138           lockFp = open(lockFile, 'w')
139           fcntl.flock(lockFp, fcntl.LOCK_EX | fcntl.LOCK_NB)
140         except IOError:
141           raise RuntimeError("couldn't delete %s, already used by another process" % self.mountpoint)
142           return 
143
144         trashDir = os.path.join(self.rsync_dir,
145                           self.fsname,
146                           rsyncsmf.RSYNCTRASHSUFFIX)
147         if not os.path.exists(trashDir):
148           os.makedirs(trashDir, 0o755)
149
150         backupTrashDir = os.path.join (self.rsync_dir,
151                                  self.fsname,
152                                  rsyncsmf.RSYNCTRASHSUFFIX,
153                                  self.snaplabel)
154
155         # move then delete
156         os.rename (self.mountpoint, backupTrashDir)
157         shutil.rmtree (backupTrashDir)
158
159         log = "%s/%s/%s/%s/%s.log" % (self.rsync_dir,
160                                    self.fsname,
161                                    rsyncsmf.RSYNCDIRSUFFIX,
162                                    ".partial",
163                                    self.snaplabel)
164         if os.path.exists (log):
165             os.unlink (log)
166
167         lockFp.close()
168         os.unlink(lockFile)
169
170
171 backupDirs = []
172 for root, dirs, files in os.walk(rsyncsmf.RsyncSMF("%s:rsync" %(plugin.PLUGINBASEFMRI)).get_target_dir ()):
173             if '.time-slider' in dirs:
174                 dirs.remove('.time-slider')
175                 backupDir = os.path.join(root, rsyncsmf.RSYNCDIRSUFFIX)
176                 if os.path.exists(backupDir):
177                     insort(backupDirs, os.path.abspath(backupDir))
178
179
180 print(backupDirs)
181
182