annotate profilemanager/manager.py @ 19:4a1815f8d146

* stub adding new profile to configuration * stub tests; have one mysterious failure with doctest :(
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 06 May 2010 12:42:27 -0700
parents 8e651dd8e9ad
children 49d523a33c89
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
15
543d08958b67 actually sample 8 random lowercase letters and numbers for the profile hash
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
7 import string
9
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
8 import ConfigParser
15
543d08958b67 actually sample 8 random lowercase letters and numbers for the profile hash
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
9 from random import Random
5
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
10 from utils import format_tabular
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
11 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
12
10
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
13 # Changes to profiles.ini:
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
14 # - add a ``backups`` field for each profile:
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
15 # Backups = /path/to/backup:1273104310 /path/to/other/backup:1273104355
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
16
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 class ProfileNotFound(Exception):
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 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
20 .ini file
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
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 class ProfileManager(object):
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 def __init__(self, profiles):
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 - 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
28 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 self.profiles = profiles
4
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
30 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
31
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 ### (public) API
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33
16
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
34 def new(self, name, directory=None, hash=True):
9
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
35 """
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
36 generate a new clean profile
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
37 - name: name of the profile to generate
16
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
38 - directory: where to create the profile [DEFAULT: relative to profiles.ini]
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
39 - hash: whether to generate a a random hash tag
9
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
40 """
16
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
41
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
42 # path to the profile directory
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
43 dirname = name
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
44 if hash:
18
8e651dd8e9ad fix two things about hash() usage
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
45 dirname = '%s.%s' % (self.hash(), dirname)
16
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
46 if not directory:
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
47 directory = self.profile_dir
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
48 dirname = os.path.join(directory, dirname)
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
49
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
50 # create directory
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
51 # TODO: (optionally) pre-populate the directory a la FF
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
52 os.mkdir(dirname)
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
53
19
4a1815f8d146 * stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
54 # update profiles.ini
4a1815f8d146 * stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
55 parser = ConfigParser()
4a1815f8d146 * stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
56 parser.read(self.profiles)
4a1815f8d146 * stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
57
16
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
58 # return the directory name
c80d44e5ca41 flush out new() function
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
59 return dirname
9
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
60
19
4a1815f8d146 * stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
61 def remove(self, name, delete=True):
4a1815f8d146 * stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
62 """
4a1815f8d146 * stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
63 remove a profile from profiles.ini
4a1815f8d146 * stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
64 - delete: delete the profile as well
4a1815f8d146 * stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
65 """
4a1815f8d146 * stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
66 raise NotImplementedError
4a1815f8d146 * stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
67
4
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
68 def list(self, directories=False):
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
69 """
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
70 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
71 - directories : display the directories
4
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
72 """
9
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
73 profiles = self.profiles_dict()
6
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
74 retval = []
9
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
75 for name in sorted(profiles.keys()):
6
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
76 values = [name]
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
77 if directories:
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
78 values.append(self.path(name))
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
79 retval.append(values)
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
80 return format_tabular(retval)
4
35dc297efa25 adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
81
8
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
82 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
83 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
84 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
85 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
86 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
87
8
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
88 # 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
89 relative = False
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
90 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
91 relative = True
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
92 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
93 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
94 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
95
8
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
96 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
97
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
98 # TODO: update profiles.ini
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
99
1
979315ed0816 mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
100
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
101 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
102 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
103 backup the profile
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
104 - 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
105 - 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
106 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
107 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
108 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
109 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
110 # TODO: add something like
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
111 # `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
112 # to self.profiles
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
113
10
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
114 def backups(self, profile=None):
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
115 """
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
116 list backups for a given profile, or all profiles if the
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
117 profile is not given; returns a list of backups if
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
118 profile is given or a dictionary of lists otherwise
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
119 """
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
120 if profile is None:
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
121 # all profiles
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
122 retval = {}
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
123 return retval
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
124 # TODO
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
125
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
126 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
127 """
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
128 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
129 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
130 - 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
131 - 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
132 """
8
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
133
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
134 # get the possible backups
10
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
135 backups = self.backups(profile)
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
136 # TODO: check to see if these all exist (print warnings if not)
8
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
137
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
138 # 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
139
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
140 if delete: # delete the backup
10
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
141 # delete the directory
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
142 # delete the entry from ``profiles.ini``
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
143 # if there are no backups, delete the ``backups`` line
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
144 pass #TODO
c77e9bef78d6 * update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
145
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
146
5
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
147 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
148 """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
149 raise NotImplementedError
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
150
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
151 ### internal functions
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
152
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
153 def path(self, profile):
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
154 """returns the path to the profile"""
6
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
155 profile = self.profile_dict(profile)
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
156 if profile.get('isrelative', None) == '1':
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
157 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
158 return profile['path']
0
7301d534bc6c initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
159
5
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
160 def profile_dict(self, profile):
9
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
161 """
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
162 return option dictionary for a single profile
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
163 """
5
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
164 parser = ConfigParser()
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
165 parser.read(self.profiles)
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
166 for section in parser.sections():
6
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
167 if not parser.has_option(section, 'name'):
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
168 continue # not a profile
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
169 if parser.get(section, 'name') == profile:
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
170 return dict(parser.items(section))
2a3f5cdfd60c flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
171 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
172
9
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
173 def profiles_dict(self):
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
174 """
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
175 return nested dict of all profiles
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
176 """
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
177 # XXX assumes profiles have unique names
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
178 parser = ConfigParser()
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
179 parser.read(self.profiles)
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
180 retval = {}
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
181 for section in parser.sections():
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
182 if section == 'General':
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
183 continue
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
184 try:
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
185 name = parser.get(section, 'name')
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
186 except ConfigParser.NoOptionError:
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
187 continue
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
188 retval[name] = self.profile_dict(name)
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
189 return retval
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
190
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
191
8
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
192 def hash(self):
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
193 """
7205cb6f5530 various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
194 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
195 """
15
543d08958b67 actually sample 8 random lowercase letters and numbers for the profile hash
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
196 population = string.lowercase + string.digits
18
8e651dd8e9ad fix two things about hash() usage
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
197 return ''.join(Random().sample(population, 8))
9
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
198
b1274abd1206 rework API a bit; the "public" methods are becoming a bit specialized to the command line, which was my fear, so I should probably inherit from ProfileManager to do the interface like I should
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
199