diff 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
line wrap: on
line diff
--- a/profilemanager/manager.py	Thu May 06 18:08:08 2010 -0700
+++ b/profilemanager/manager.py	Thu May 06 18:53:24 2010 -0700
@@ -50,23 +50,11 @@
         path = os.path.join(directory, dirname)
 
         # create directory
-        # TODO: (optionally) pre-populate the directory a la FF
+        # TODO: (optionally) pre-populate the directory a la Firefox
         os.mkdir(path)
 
         # update profiles.ini
-        parser = self.parser()
-        ctr = 0
-        section = 'Profile%d' % ctr # unsure of this naming convention
-        while section in parser.sections():
-            ctr += 1
-            section = 'Profile%d' % ctr
-        parser.add_section(section)
-        parser.set(section, 'Name', name)
-        parser.set(section, 'IsRelative', '%d' % int(relative))
-        parser.set(section, 'Path', relative and dirname or path)
-        if len(parser.sections()) == 1:
-            parser.set(section, 'Default', '1')
-        parser.write(file(self.profiles, 'w'))
+        self.add(name, relative and dirname or path, relative)
         
         # return the directory name
         return path
@@ -95,24 +83,37 @@
             return sorted(profiles.keys())
         return dict([(name, self.path(name)) for name in profiles.keys()])
 
-    def clone(self, source, dest):
+    def clone(self, source, dest, add=True):
         """
         clones the profile `source` and output to `dest`
+        - add: add the profile to the profiles.ini file
         """
         source_path = self.path(source)    # fs path of the `from` profile
 
         # dest: fs path to back up to
         relative = False
-        if not os.path.isabs(dest):
+        if os.path.sep in dest:
+            if not os.path.isabs(dest):
+                dest = os.path.abspath(dest)
+            dirname = dest
+            name = os.path.basename(dest)
+        else:
+            dirname = name = dest
             relative = True
             if not os.path.dirname(dest):
                 dest = '%s.%s' % (self.hash(), dest)
             dest = os.path.join(self.profile_dir, dest)
 
+        # ensure name is not already present
+        assert name not in self.profiles_dict(), 'Profile "%s" already in %s' % (name, self.profiles)
+
+        # update profiles.ini
+        if add:
+            self.add(name, dirname, relative)
+
+        # copy the files
         shutil.copytree(source_path, dest, symlinks=False)
-        
-        # TODO: update profiles.ini
-        
+            
 
     def backup(self, profile, dest=None):
         """
@@ -166,6 +167,33 @@
 
     ### internal functions
 
+    def add(self, profile, path, relative=True):
+        """
+        add a profile entry to profiles.ini
+        """
+        assert name not in self.profiles_dict(), 'Profile "%s" already in %s' % (name, self.profiles)
+        parser = self.parser()
+        ctr = 0
+        section = 'Profile%d' % ctr # unsure of this naming convention
+        while section in parser.sections():
+            ctr += 1
+            section = 'Profile%d' % ctr
+        parser.add_section(section)
+        parser.set(section, 'Name', profile)
+        parser.set(section, 'IsRelative', '%d' % int(relative))
+        parser.set(section, 'Path', relative and dirname or path)
+        if not ctr:
+            parser.set(section, 'Default', '1')
+        parser.write(file(self.profiles, 'w'))
+
+
+    def path(self, profile):
+        """returns the path to the profile"""
+        profile = self.profile_dict(profile)
+        if profile.get('isrelative', None) == '1':
+            return os.path.join(self.profile_dir, profile['path'])
+        return profile['path']
+
     def parser(self):
         """
         return a ConfigParser instance appropriate to profiles.ini
@@ -174,12 +202,6 @@
         parser.read(self.profiles)
         return parser
 
-    def path(self, profile):
-        """returns the path to the profile"""
-        profile = self.profile_dict(profile)
-        if profile.get('isrelative', None) == '1':
-            return os.path.join(self.profile_dir, profile['path'])
-        return profile['path']
 
     def section(self, profile, parser=None):
         """