Checkin of 0.2.98 upstream source
[time-slider.git] / usr / share / time-slider / lib / time_slider / tmp2.py
1 #!/usr/bin/python2.6
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 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 = unicode(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, 0755)
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, \
142           "couldn't delete %s, already used by another process" % self.mountpoint
143           return 
144
145         trashDir = os.path.join(self.rsync_dir,
146                           self.fsname,
147                           rsyncsmf.RSYNCTRASHSUFFIX)
148         if not os.path.exists(trashDir):
149           os.makedirs(trashDir, 0755)
150
151         backupTrashDir = os.path.join (self.rsync_dir,
152                                  self.fsname,
153                                  rsyncsmf.RSYNCTRASHSUFFIX,
154                                  self.snaplabel)
155
156         # move then delete
157         os.rename (self.mountpoint, backupTrashDir)
158         shutil.rmtree (backupTrashDir)
159
160         log = "%s/%s/%s/%s/%s.log" % (self.rsync_dir,
161                                    self.fsname,
162                                    rsyncsmf.RSYNCDIRSUFFIX,
163                                    ".partial",
164                                    self.snaplabel)
165         if os.path.exists (log):
166             os.unlink (log)
167
168         lockFp.close()
169         os.unlink(lockFile)
170
171
172 backupDirs = []
173 for root, dirs, files in os.walk(rsyncsmf.RsyncSMF("%s:rsync" %(plugin.PLUGINBASEFMRI)).get_target_dir ()):
174             if '.time-slider' in dirs:
175                 dirs.remove('.time-slider')
176                 backupDir = os.path.join(root, rsyncsmf.RSYNCDIRSUFFIX)
177                 if os.path.exists(backupDir):
178                     insort(backupDirs, os.path.abspath(backupDir))
179
180
181 print backupDirs
182
183