annotate profilemanager/manager.py @ 8:7205cb6f5530

various flushing out, deleting old TODOs and making new ones
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 13 Apr 2010 19:24:52 -0700
parents d3b22d086934
children b1274abd1206
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 manage Mozilla/Firefox profiles
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 import os
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 import shutil
5
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
7 from utils import format_tabular
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
8 from ConfigParser import SafeConfigParser as ConfigParser
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 class ProfileNotFound(Exception):
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 exception when a profile is specified but is not present in a given
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 .ini file
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 class ProfileManager(object):
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 def __init__(self, profiles):
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 """
4
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
20 - profiles: filesystem path to profiles.ini file
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 self.profiles = profiles
4
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
23 self.profile_dir = os.path.abspath(os.path.dirname(profiles))
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 ### (public) API
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26
4
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
27 def list(self, directories=False):
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
28 """
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
29 lists the profiles available in the config file
6
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
30 - directories : display the directories
4
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
31 """
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
32 parser = ConfigParser()
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
33 parser.read(self.profiles)
6
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
34 retval = []
4
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
35 for section in parser.sections():
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
36 if section == 'General':
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
37 continue
6
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
38 name = parser.get(section, 'name')
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
39 values = [name]
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
40 if directories:
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
41 values.append(self.path(name))
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
42 retval.append(values)
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
43 return format_tabular(retval)
4
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
44
8
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
45 def backups(self, profile=None):
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
46 """
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
47 list backups for a given profile, or all profiles if the
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
48 profile is not given
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
49 """
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
50 # TODO
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
51
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
52 def clone(self, source, dest, hash=True):
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 clones the profile `source` and output to `dest`
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 source_path = self.path(source) # fs path of the `from` profile
7
d3b22d086934 comments for what to do next
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
57
8
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
58 # dest: fs path to back up to
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
59 relative = False
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
60 if not os.path.isabs(dest):
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
61 relative = True
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
62 if not os.path.dirname(dest):
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
63 dest = '%s.%s' % (self.hash(), dest)
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
64 dest = os.path.join(self.profile_dir, dest)
7
d3b22d086934 comments for what to do next
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
65
8
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
66 shutil.copytree(source_path, dest, symlinks=False)
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
67
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
68 # TODO: update profiles.ini
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
69
1
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
70
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71 def backup(self, profile, dest=None):
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73 backup the profile
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74 - profile: name of the profile to be backed up
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
75 - dest: name of the destination (optional)
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
76 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
77 if dest is None:
8
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
78 dest = '%s.%d.bak' % (profile, int(time.time()))
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
79 self.clone(profile, dest, hash=False)
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
80 # TODO: add something like
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
81 # `Backup=$(profile)s.$(datestamp)s.bak`
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
82 # to self.profiles
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
83
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
84 def restore(self, profile, date=None, delete=False):
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
85 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
86 restore the profile from a backup
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
87 the most recent backup is used unless `date` is given
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
88 - date : date to restore from
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
89 - delete : delete the backup after restoration
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
90 """
8
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
91
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
92 # get the possible backups
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
93 # TODO
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
94
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
95 # restore the backup over ``profile``
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
96
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
97 if delete: # delete the backup
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
98 pass
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
99
5
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
100 def merge(self, output, *profiles):
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
101 """merge a set of profiles (not trivial!)"""
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
102 raise NotImplementedError
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
103
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
104 ### internal functions
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
105
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
106 def path(self, profile):
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
107 """returns the path to the profile"""
6
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
108 profile = self.profile_dict(profile)
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
109 if profile.get('isrelative', None) == '1':
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
110 return os.path.join(self.profile_dir, profile['path'])
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
111 return profile['path']
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
112
5
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
113 def profile_dict(self, profile):
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
114 parser = ConfigParser()
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
115 parser.read(self.profiles)
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
116 for section in parser.sections():
6
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
117 if not parser.has_option(section, 'name'):
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
118 continue # not a profile
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
119 if parser.get(section, 'name') == profile:
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
120 return dict(parser.items(section))
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
121 raise ProfileNotFound('Profile %s not found in %s' % (profile, self.profiles))
8
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
122
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
123 def hash(self):
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
124 """
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
125 generate a random hash for a new profile
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
126 """
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
127 # XXX right now this is completely fake
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
128 return 'FOO'