Mercurial > mozilla > hg > ProfileManager
annotate profilemanager/manager.py @ 39:30fc269a15c8
* add additional tests
* remove unused hash argument from ProfileManager.clone
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 06 May 2010 18:08:08 -0700 |
parents | 81500171fda8 |
children | 34c740d1962d |
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 | 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 | 38 - directory: where to create the profile [DEFAULT: relative to profiles.ini] |
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 | 41 |
42 # path to the profile directory | |
20 | 43 dirname = name |
44 relative = False | |
16 | 45 if hash: |
18
8e651dd8e9ad
fix two things about hash() usage
Jeff Hammel <jhammel@mozilla.com>
parents:
16
diff
changeset
|
46 dirname = '%s.%s' % (self.hash(), dirname) |
16 | 47 if not directory: |
48 directory = self.profile_dir | |
20 | 49 relative = True |
50 path = os.path.join(directory, dirname) | |
16 | 51 |
52 # create directory | |
53 # TODO: (optionally) pre-populate the directory a la FF | |
20 | 54 os.mkdir(path) |
16 | 55 |
19
4a1815f8d146
* stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
56 # update profiles.ini |
22
956f5a4c589a
* restructure utility functions to do less more efficiently
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
57 parser = self.parser() |
20 | 58 ctr = 0 |
23 | 59 section = 'Profile%d' % ctr # unsure of this naming convention |
20 | 60 while section in parser.sections(): |
61 ctr += 1 | |
24
853a3f4b9659
correct another typo in manager (why cut+paste is evil)
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
62 section = 'Profile%d' % ctr |
26 | 63 parser.add_section(section) |
20 | 64 parser.set(section, 'Name', name) |
65 parser.set(section, 'IsRelative', '%d' % int(relative)) | |
66 parser.set(section, 'Path', relative and dirname or path) | |
67 if len(parser.sections()) == 1: | |
68 parser.set(section, 'Default', '1') | |
29 | 69 parser.write(file(self.profiles, 'w')) |
19
4a1815f8d146
* stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
70 |
16 | 71 # return the directory name |
36
a6222b71aab6
return the right thing, finally
Jeff Hammel <jhammel@mozilla.com>
parents:
31
diff
changeset
|
72 return path |
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 |
19
4a1815f8d146
* stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
74 def remove(self, name, delete=True): |
4a1815f8d146
* stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
75 """ |
4a1815f8d146
* stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
76 remove a profile from profiles.ini |
20 | 77 - delete: delete the profile directory as well |
19
4a1815f8d146
* stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
78 """ |
37 | 79 parser = self.parser() |
80 section = self.section(name, parser) | |
21
15484adb9758
note what section a a profile is in
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
81 if section is None: |
22
956f5a4c589a
* restructure utility functions to do less more efficiently
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
82 raise ProfileNotFound('Profile %s not found in %s' % (profile, self.profiles)) |
956f5a4c589a
* restructure utility functions to do less more efficiently
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
83 if delete: # remove the profile from disk |
956f5a4c589a
* restructure utility functions to do less more efficiently
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
84 shutil.rmtree(self.path(name)) |
956f5a4c589a
* restructure utility functions to do less more efficiently
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
85 parser.remove_section(section) |
29 | 86 parser.write(file(self.profiles, 'w')) |
19
4a1815f8d146
* stub adding new profile to configuration
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
87 |
4
35dc297efa25
adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
88 def list(self, directories=False): |
35dc297efa25
adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
89 """ |
35dc297efa25
adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
90 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
|
91 - directories : display the directories |
4
35dc297efa25
adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
92 """ |
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
|
93 profiles = self.profiles_dict() |
38 | 94 if not directories: |
95 return sorted(profiles.keys()) | |
96 return dict([(name, self.path(name)) for name in profiles.keys()]) | |
4
35dc297efa25
adding listing function and other cleanup
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
97 |
39 | 98 def clone(self, source, dest): |
0
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
99 """ |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
100 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
|
101 """ |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
102 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
|
103 |
8
7205cb6f5530
various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
104 # 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
|
105 relative = False |
7205cb6f5530
various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
106 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
|
107 relative = True |
7205cb6f5530
various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
108 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
|
109 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
|
110 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
|
111 |
8
7205cb6f5530
various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
112 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
|
113 |
7205cb6f5530
various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
114 # TODO: update profiles.ini |
7205cb6f5530
various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
115 |
1
979315ed0816
mucho cleanup on optionparser stuff
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
116 |
0
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
117 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
|
118 """ |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
119 backup the profile |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
120 - 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
|
121 - 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
|
122 """ |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
123 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
|
124 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
|
125 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
|
126 # TODO: add something like |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
127 # `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
|
128 # to self.profiles |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
129 |
10
c77e9bef78d6
* update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
130 def backups(self, profile=None): |
c77e9bef78d6
* update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
131 """ |
c77e9bef78d6
* update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
132 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
|
133 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
|
134 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
|
135 """ |
c77e9bef78d6
* update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
136 if profile is None: |
c77e9bef78d6
* update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
137 # all profiles |
c77e9bef78d6
* update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
138 retval = {} |
c77e9bef78d6
* update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
139 return retval |
c77e9bef78d6
* update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
140 # TODO |
c77e9bef78d6
* update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
141 |
0
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
142 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
|
143 """ |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
144 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
|
145 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
|
146 - 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
|
147 - 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
|
148 """ |
8
7205cb6f5530
various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
149 |
7205cb6f5530
various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
150 # get the possible backups |
10
c77e9bef78d6
* update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
151 backups = self.backups(profile) |
c77e9bef78d6
* update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
152 # 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
|
153 |
7205cb6f5530
various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
154 # 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
|
155 |
7205cb6f5530
various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
156 if delete: # delete the backup |
10
c77e9bef78d6
* update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
157 # delete the directory |
c77e9bef78d6
* update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
158 # delete the entry from ``profiles.ini`` |
c77e9bef78d6
* update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
159 # 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
|
160 pass #TODO |
c77e9bef78d6
* update list of public API functions
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
161 |
0
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
162 |
5
ca57920aa223
adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
163 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
|
164 """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
|
165 raise NotImplementedError |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
166 |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
167 ### internal functions |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
168 |
22
956f5a4c589a
* restructure utility functions to do less more efficiently
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
169 def parser(self): |
956f5a4c589a
* restructure utility functions to do less more efficiently
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
170 """ |
956f5a4c589a
* restructure utility functions to do less more efficiently
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
171 return a ConfigParser instance appropriate to profiles.ini |
956f5a4c589a
* restructure utility functions to do less more efficiently
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
172 """ |
25
fc31d3f26755
i seriously need to learn to code
Jeff Hammel <jhammel@mozilla.com>
parents:
24
diff
changeset
|
173 parser = ConfigParser() |
fc31d3f26755
i seriously need to learn to code
Jeff Hammel <jhammel@mozilla.com>
parents:
24
diff
changeset
|
174 parser.read(self.profiles) |
fc31d3f26755
i seriously need to learn to code
Jeff Hammel <jhammel@mozilla.com>
parents:
24
diff
changeset
|
175 return parser |
22
956f5a4c589a
* restructure utility functions to do less more efficiently
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
176 |
0
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
177 def path(self, profile): |
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
178 """returns the path to the profile""" |
6
2a3f5cdfd60c
flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
179 profile = self.profile_dict(profile) |
2a3f5cdfd60c
flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
180 if profile.get('isrelative', None) == '1': |
2a3f5cdfd60c
flush out helper functions and list directories
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
181 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
|
182 return profile['path'] |
0
7301d534bc6c
initial messy and incomplete strawman prototype for Mozilla (Firefox) profile management
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
183 |
22
956f5a4c589a
* restructure utility functions to do less more efficiently
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
184 def section(self, profile, parser=None): |
20 | 185 """ |
21
15484adb9758
note what section a a profile is in
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
186 returns the name of the section that a profile is in or None |
15484adb9758
note what section a a profile is in
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
187 if not found |
20 | 188 """ |
22
956f5a4c589a
* restructure utility functions to do less more efficiently
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
189 if parser is None: |
29 | 190 parser = self.parser() |
31 | 191 for section in parser.sections(): |
21
15484adb9758
note what section a a profile is in
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
192 if not parser.has_option(section, 'name'): |
15484adb9758
note what section a a profile is in
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
193 continue # not a profile |
15484adb9758
note what section a a profile is in
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
194 if parser.get(section, 'name') == profile: |
15484adb9758
note what section a a profile is in
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
195 return section |
20 | 196 |
27
5988a15da3b4
things are being awful; checking in anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
197 def profile_dict(self, profile): |
5988a15da3b4
things are being awful; checking in anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
198 """ |
5988a15da3b4
things are being awful; checking in anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
199 return option dictionary for a single profile |
5988a15da3b4
things are being awful; checking in anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
200 """ |
5988a15da3b4
things are being awful; checking in anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
201 parser = self.parser() |
5988a15da3b4
things are being awful; checking in anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
202 section = self.section(profile, parser) |
5988a15da3b4
things are being awful; checking in anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
203 if section is None: |
5988a15da3b4
things are being awful; checking in anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
204 raise ProfileNotFound('Profile %s not found in %s' % (profile, self.profiles)) |
5988a15da3b4
things are being awful; checking in anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
205 return dict(parser.items(section)) |
5988a15da3b4
things are being awful; checking in anyway
Jeff Hammel <jhammel@mozilla.com>
parents:
26
diff
changeset
|
206 |
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
|
207 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
|
208 """ |
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
|
209 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
|
210 """ |
22
956f5a4c589a
* restructure utility functions to do less more efficiently
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
211 # assumes profiles have unique names |
956f5a4c589a
* restructure utility functions to do less more efficiently
Jeff Hammel <jhammel@mozilla.com>
parents:
21
diff
changeset
|
212 parser = self.parser() |
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
|
213 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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 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
|
222 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
|
223 |
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
|
224 |
8
7205cb6f5530
various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
225 def hash(self): |
7205cb6f5530
various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
226 """ |
7205cb6f5530
various flushing out, deleting old TODOs and making new ones
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
227 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
|
228 """ |
15
543d08958b67
actually sample 8 random lowercase letters and numbers for the profile hash
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
229 population = string.lowercase + string.digits |
18
8e651dd8e9ad
fix two things about hash() usage
Jeff Hammel <jhammel@mozilla.com>
parents:
16
diff
changeset
|
230 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
|
231 |
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
|
232 |