comparison profilemanager/manager.py @ 40:34c740d1962d

abstract out adding an entry to profiles.ini
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 06 May 2010 18:53:24 -0700
parents 30fc269a15c8
children 769447f8cd08
comparison
equal deleted inserted replaced
39:30fc269a15c8 40:34c740d1962d
48 directory = self.profile_dir 48 directory = self.profile_dir
49 relative = True 49 relative = True
50 path = os.path.join(directory, dirname) 50 path = os.path.join(directory, dirname)
51 51
52 # create directory 52 # create directory
53 # TODO: (optionally) pre-populate the directory a la FF 53 # TODO: (optionally) pre-populate the directory a la Firefox
54 os.mkdir(path) 54 os.mkdir(path)
55 55
56 # update profiles.ini 56 # update profiles.ini
57 parser = self.parser() 57 self.add(name, relative and dirname or path, relative)
58 ctr = 0
59 section = 'Profile%d' % ctr # unsure of this naming convention
60 while section in parser.sections():
61 ctr += 1
62 section = 'Profile%d' % ctr
63 parser.add_section(section)
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')
69 parser.write(file(self.profiles, 'w'))
70 58
71 # return the directory name 59 # return the directory name
72 return path 60 return path
73 61
74 def remove(self, name, delete=True): 62 def remove(self, name, delete=True):
93 profiles = self.profiles_dict() 81 profiles = self.profiles_dict()
94 if not directories: 82 if not directories:
95 return sorted(profiles.keys()) 83 return sorted(profiles.keys())
96 return dict([(name, self.path(name)) for name in profiles.keys()]) 84 return dict([(name, self.path(name)) for name in profiles.keys()])
97 85
98 def clone(self, source, dest): 86 def clone(self, source, dest, add=True):
99 """ 87 """
100 clones the profile `source` and output to `dest` 88 clones the profile `source` and output to `dest`
89 - add: add the profile to the profiles.ini file
101 """ 90 """
102 source_path = self.path(source) # fs path of the `from` profile 91 source_path = self.path(source) # fs path of the `from` profile
103 92
104 # dest: fs path to back up to 93 # dest: fs path to back up to
105 relative = False 94 relative = False
106 if not os.path.isabs(dest): 95 if os.path.sep in dest:
96 if not os.path.isabs(dest):
97 dest = os.path.abspath(dest)
98 dirname = dest
99 name = os.path.basename(dest)
100 else:
101 dirname = name = dest
107 relative = True 102 relative = True
108 if not os.path.dirname(dest): 103 if not os.path.dirname(dest):
109 dest = '%s.%s' % (self.hash(), dest) 104 dest = '%s.%s' % (self.hash(), dest)
110 dest = os.path.join(self.profile_dir, dest) 105 dest = os.path.join(self.profile_dir, dest)
111 106
107 # ensure name is not already present
108 assert name not in self.profiles_dict(), 'Profile "%s" already in %s' % (name, self.profiles)
109
110 # update profiles.ini
111 if add:
112 self.add(name, dirname, relative)
113
114 # copy the files
112 shutil.copytree(source_path, dest, symlinks=False) 115 shutil.copytree(source_path, dest, symlinks=False)
113 116
114 # TODO: update profiles.ini
115
116 117
117 def backup(self, profile, dest=None): 118 def backup(self, profile, dest=None):
118 """ 119 """
119 backup the profile 120 backup the profile
120 - profile: name of the profile to be backed up 121 - profile: name of the profile to be backed up
164 """merge a set of profiles (not trivial!)""" 165 """merge a set of profiles (not trivial!)"""
165 raise NotImplementedError 166 raise NotImplementedError
166 167
167 ### internal functions 168 ### internal functions
168 169
169 def parser(self): 170 def add(self, profile, path, relative=True):
170 """ 171 """
171 return a ConfigParser instance appropriate to profiles.ini 172 add a profile entry to profiles.ini
172 """ 173 """
173 parser = ConfigParser() 174 assert name not in self.profiles_dict(), 'Profile "%s" already in %s' % (name, self.profiles)
174 parser.read(self.profiles) 175 parser = self.parser()
175 return parser 176 ctr = 0
177 section = 'Profile%d' % ctr # unsure of this naming convention
178 while section in parser.sections():
179 ctr += 1
180 section = 'Profile%d' % ctr
181 parser.add_section(section)
182 parser.set(section, 'Name', profile)
183 parser.set(section, 'IsRelative', '%d' % int(relative))
184 parser.set(section, 'Path', relative and dirname or path)
185 if not ctr:
186 parser.set(section, 'Default', '1')
187 parser.write(file(self.profiles, 'w'))
188
176 189
177 def path(self, profile): 190 def path(self, profile):
178 """returns the path to the profile""" 191 """returns the path to the profile"""
179 profile = self.profile_dict(profile) 192 profile = self.profile_dict(profile)
180 if profile.get('isrelative', None) == '1': 193 if profile.get('isrelative', None) == '1':
181 return os.path.join(self.profile_dir, profile['path']) 194 return os.path.join(self.profile_dir, profile['path'])
182 return profile['path'] 195 return profile['path']
196
197 def parser(self):
198 """
199 return a ConfigParser instance appropriate to profiles.ini
200 """
201 parser = ConfigParser()
202 parser.read(self.profiles)
203 return parser
204
183 205
184 def section(self, profile, parser=None): 206 def section(self, profile, parser=None):
185 """ 207 """
186 returns the name of the section that a profile is in or None 208 returns the name of the section that a profile is in or None
187 if not found 209 if not found