Commits

Anonymous committed d275a46

refactored obtain() methods to prepare for #41 fix

  • Participants
  • Parent commits 70eb022
  • Branches trunk

Comments (0)

Files changed (1)

         """
         raise NotImplementedError
 
+    def switch(self, dest, url, rev_options):
+        """
+        Switch the repo at ``dest`` to point to ``URL``.
+        """
+        raise NotImplemented
+
+    def update(self, dest, rev_options):
+        """
+        Update an already-existing repo to the given ``rev_options``.
+        """
+        raise NotImplementedError
+    
+    def check_destination(self, dest, url, rev_options, rev_display):
+        """
+        Prepare a location to receive a checkout/clone.
+
+        Return True if the location is ready for (and requires) a
+        checkout/clone, False otherwise.
+        """
+        checkout = True
+        if os.path.exists(os.path.join(dest, self.dirname)):
+            existing_url = self.get_url(dest)
+            checkout = False
+            if self.compare_urls(existing_url, url):
+                logger.info('%s in %s exists, and has correct URL (%s)'
+                            % (self.repo_name.title(), display_path(dest), url))
+                logger.notify('Updating %s %s%s'
+                              % (display_path(dest), self.repo_name, rev_display))
+                self.update(dest, rev_options)
+            else:
+                logger.warn('%s %s in %s exists with URL %s'
+                            % (self.name, self.repo_name, display_path(dest), existing_url))
+                logger.warn('The plan is to install the %s repository %s'
+                            % (self.name, url))
+                response = ask('What to do?  (s)witch, (i)gnore, (w)ipe, (b)ackup ', ('s', 'i', 'w', 'b'))
+                if response == 's':
+                    logger.notify('Switching %s %s to %s%s'
+                                  % (self.repo_name, display_path(dest), url, rev_display))
+                    self.switch(dest, url, rev_options)
+                elif response == 'i':
+                    # do nothing
+                    pass
+                elif response == 'w':
+                    logger.warn('Deleting %s' % display_path(dest))
+                    shutil.rmtree(dest)
+                    checkout = True
+                elif response == 'b':
+                    dest_dir = backup_dir(dest)
+                    logger.warn('Backing up %s to %s'
+                                % (display_path(dest), dest_dir))
+                    shutil.move(dest, dest_dir)
+                    checkout = True
+        return checkout
+    
     def unpack(self, location):
         raise NotImplementedError
 
 class Subversion(VersionControl):
     name = 'svn'
     dirname = '.svn'
+    repo_name = 'checkout'
     schemes = ('svn', 'svn+ssh', 'svn+http', 'svn+https')
     bundle_file = 'svn-checkout.txt'
     guide = ('# This was an svn checkout; to make it a checkout again run:\n'
         if not match:
             logger.warn('Cannot determine URL of svn checkout %s' % display_path(location))
             logger.info('Output that cannot be parsed: \n%s' % output)
-            return 'unknown', 'unknown'
+            return None, None
         url = match.group(1).strip()
         match = _svn_revision_re.search(output)
         if not match:
             logger.warn('Cannot determine revision of svn checkout %s' % display_path(location))
             logger.info('Output that cannot be parsed: \n%s' % output)
-            return url, 'unknown'
+            return url, None
         return url, match.group(1)
 
+    def get_url(self, location):
+        return self.get_info(location)[0]
+
+    def get_revision(self, location):
+        return self.get_info(location)[1]
+    
     def parse_vcs_bundle_file(self, content):
         for line in content.splitlines():
             if not line.strip() or line.strip().startswith('#'):
         finally:
             logger.indent -= 2
 
+    def switch(self, dest, url, rev_options):
+        call_subprocess(
+            ['svn', 'switch'] + rev_options + [url, dest])
+            
+    def update(self, dest, rev_options):
+        call_subprocess(
+            ['svn', 'update'] + rev_options + [dest])
+
     def obtain(self, dest):
         url, rev = self.get_url_rev()
         if rev:
         else:
             rev_options = []
             rev_display = ''
-        checkout = True
-        if os.path.exists(os.path.join(dest, self.dirname)):
-            existing_url = self.get_info(dest)[0]
-            checkout = False
-            if self.compare_urls(existing_url, url):
-                logger.info('Checkout in %s exists, and has correct URL (%s)'
-                            % (display_path(dest), url))
-                logger.notify('Updating checkout %s%s'
-                              % (display_path(dest), rev_display))
-                call_subprocess(
-                    ['svn', 'update'] + rev_options + [dest])
-            else:
-                logger.warn('svn checkout in %s exists with URL %s'
-                            % (display_path(dest), existing_url))
-                logger.warn('The plan is to install the svn repository %s'
-                            % url)
-                response = ask('What to do?  (s)witch, (i)gnore, (w)ipe, (b)ackup ', ('s', 'i', 'w', 'b'))
-                if response == 's':
-                    logger.notify('Switching checkout %s to %s%s'
-                                  % (display_path(dest), url, rev_display))
-                    call_subprocess(
-                        ['svn', 'switch'] + rev_options + [url, dest])
-                elif response == 'i':
-                    # do nothing
-                    pass
-                elif response == 'w':
-                    logger.warn('Deleting %s' % display_path(dest))
-                    shutil.rmtree(dest)
-                    checkout = True
-                elif response == 'b':
-                    dest_dir = backup_dir(dest)
-                    logger.warn('Backing up %s to %s'
-                                % (display_path(dest), dest_dir))
-                    shutil.move(dest, dest_dir)
-                    checkout = True
-        if checkout:
+        if self.check_destination(dest, url, rev_options, rev_display):
             logger.notify('Checking out %s%s to %s'
                           % (url, rev_display, display_path(dest)))
             call_subprocess(
 class Git(VersionControl):
     name = 'git'
     dirname = '.git'
+    repo_name = 'clone'
     schemes = ('git', 'git+http', 'git+ssh', 'git+git')
     bundle_file = 'git-clone.txt'
     guide = ('# This was a Git repo; to make it a repo again run:\n'
                                         % (rev, display_path(dest)))
         return [rev]
 
+    def switch(self, dest, url, rev_options):
+        call_subprocess(
+            [GIT_CMD, 'config', 'remote.origin.url', url], cwd=dest)
+        call_subprocess(
+            [GIT_CMD, 'checkout', '-q'] + rev_options, cwd=dest)
+
+    def update(self, dest, rev_options):
+        call_subprocess([GIT_CMD, 'fetch', '-q'], cwd=dest)
+        call_subprocess(
+            [GIT_CMD, 'checkout', '-q', '-f'] + rev_options, cwd=dest)
+
     def obtain(self, dest):
         url, rev = self.get_url_rev()
         if rev:
         else:
             rev_options = ['origin/master']
             rev_display = ''
-        clone = True
-        if os.path.exists(os.path.join(dest, self.dirname)):
-            existing_url = self.get_url(dest)
-            rev_options = self.check_rev_options(rev, dest, rev_options)
-            clone = False
-            if self.compare_urls(existing_url, url):
-                logger.info('Clone in %s exists, and has correct URL (%s)'
-                            % (display_path(dest), url))
-                logger.notify('Updating clone %s%s'
-                              % (display_path(dest), rev_display))
-                call_subprocess([GIT_CMD, 'fetch', '-q'], cwd=dest)
-                call_subprocess(
-                    [GIT_CMD, 'checkout', '-q', '-f'] + rev_options, cwd=dest)
-            else:
-                logger.warn('Git clone in %s exists with URL %s'
-                            % (display_path(dest), existing_url))
-                logger.warn('The plan is to install the Git repository %s'
-                            % url)
-                response = ask('What to do?  (s)witch, (i)gnore, (w)ipe, (b)ackup ', ('s', 'i', 'w', 'b'))
-                if response == 's':
-                    logger.notify('Switching clone %s to %s%s'
-                                  % (display_path(dest), url, rev_display))
-                    call_subprocess(
-                        [GIT_CMD, 'config', 'remote.origin.url', url], cwd=dest)
-                    call_subprocess(
-                        [GIT_CMD, 'checkout', '-q'] + rev_options, cwd=dest)
-                elif response == 'i':
-                    # do nothing
-                    pass
-                elif response == 'w':
-                    logger.warn('Deleting %s' % display_path(dest))
-                    shutil.rmtree(dest)
-                    clone = True
-                elif response == 'b':
-                    dest_dir = backup_dir(dest)
-                    logger.warn('Backing up %s to %s' % (display_path(dest), dest_dir))
-                    shutil.move(dest, dest_dir)
-                    clone = True
-        if clone:
+        if self.check_destination(dest, url, rev_options, rev_display):
             logger.notify('Cloning %s%s to %s' % (url, rev_display, display_path(dest)))
             call_subprocess(
                 [GIT_CMD, 'clone', '-q', url, dest])
 class Mercurial(VersionControl):
     name = 'hg'
     dirname = '.hg'
+    repo_name = 'clone'
     schemes = ('hg', 'hg+http', 'hg+ssh')
     bundle_file = 'hg-clone.txt'
     guide = ('# This was a Mercurial repo; to make it a repo again run:\n'
         finally:
             shutil.rmtree(temp_dir)
 
+    def switch(self, dest, url, rev_options):
+        repo_config = os.path.join(dest, self.dirname, 'hgrc')
+        config = ConfigParser.SafeConfigParser()
+        try:
+            config.read(repo_config)
+            config.set('paths', 'default', url)
+            config_file = open(repo_config, 'w')
+            config.write(config_file)
+            config_file.close()
+        except (OSError, ConfigParser.NoSectionError), e:
+            logger.warn(
+                'Could not switch Mercurial repository to %s: %s'
+                % (url, e))
+        else:
+            call_subprocess(['hg', 'update', '-q'] + rev_options, cwd=dest)
+
+    def update(self, dest, rev_options):
+        call_subprocess(['hg', 'pull', '-q'], cwd=dest)
+        call_subprocess(
+            ['hg', 'update', '-q'] + rev_options, cwd=dest)
+        
     def obtain(self, dest):
         url, rev = self.get_url_rev()
         if rev:
         else:
             rev_options = []
             rev_display = ''
-        clone = True
-        if os.path.exists(os.path.join(dest, '.hg')):
-            existing_url = self.get_url(dest)
-            clone = False
-            if self.compare_urls(existing_url, url):
-                logger.info('Clone in %s exists, and has correct URL (%s)'
-                            % (display_path(dest), url))
-                logger.notify('Updating clone %s%s'
-                              % (display_path(dest), rev_display))
-                call_subprocess(['hg', 'pull', '-q'], cwd=dest)
-                call_subprocess(
-                    ['hg', 'update', '-q'] + rev_options, cwd=dest)
-            else:
-                logger.warn('Mercurial clone in %s exists with URL %s'
-                            % (display_path(dest), existing_url))
-                logger.warn('The plan is to install the Mercurial repository %s'
-                            % url)
-                response = ask('What to do?  (s)witch, (i)gnore, (w)ipe, (b)ackup ', ('s', 'i', 'w', 'b'))
-                if response == 's':
-                    logger.notify('Switching clone %s to %s%s'
-                                  % (display_path(dest), url, rev_display))
-                    repo_config = os.path.join(dest, '.hg/hgrc')
-                    config = ConfigParser.SafeConfigParser()
-                    try:
-                        config_file = open(repo_config, 'wb')
-                        config.readfp(config_file)
-                        config.set('paths', ''.join(rev_options), url)
-                        config.write(config_file)
-                    except (OSError, ConfigParser.NoSectionError):
-                        logger.warn(
-                            'Could not switch Mercurial repository to %s: %s'
-                                % (url, e))
-                    else:
-                        call_subprocess(
-                            ['hg', 'update', '-q'] + rev_options, cwd=dest)
-                elif response == 'i':
-                    # do nothing
-                    pass
-                elif response == 'w':
-                    logger.warn('Deleting %s' % display_path(dest))
-                    shutil.rmtree(dest)
-                    clone = True
-                elif response == 'b':
-                    dest_dir = backup_dir(dest)
-                    logger.warn('Backing up %s to %s' % (display_path(dest), dest_dir))
-                    shutil.move(dest, dest_dir)
-                    clone = True
-        if clone:
+        if self.check_destination(dest, url, rev_options, rev_display):
             logger.notify('Cloning hg %s%s to %s'
                           % (url, rev_display, display_path(dest)))
             call_subprocess(['hg', 'clone', '-q', url, dest])
 class Bazaar(VersionControl):
     name = 'bzr'
     dirname = '.bzr'
+    repo_name = 'branch'
     bundle_file = 'bzr-branch.txt'
     schemes = ('bzr', 'bzr+http', 'bzr+https', 'bzr+ssh', 'bzr+sftp')
     guide = ('# This was a Bazaar branch; to make it a branch again run:\n'
         finally:
             shutil.rmtree(temp_dir)
 
+    def switch(self, dest, url, rev_options):
+        call_subprocess([BZR_CMD, 'switch', url], cwd=dest)
+
+    def update(self, dest, rev_options):
+        call_subprocess(
+            [BZR_CMD, 'pull', '-q'] + rev_options, cwd=dest)
+            
     def obtain(self, dest):
         url, rev = self.get_url_rev()
         if rev:
         else:
             rev_options = []
             rev_display = ''
-        branch = True
-        update = False
-        if os.path.exists(os.path.join(dest, '.bzr')):
-            existing_url = self.get_url(dest)
-            branch = False
-            if self.compare_urls(existing_url, url):
-                logger.info('Checkout in %s exists, and has correct URL (%s)'
-                            % (display_path(dest), url))
-                logger.notify('Updating branch %s%s'
-                              % (display_path(dest), rev_display))
-                branch = update = True
-            else:
-                logger.warn('Bazaar branch in %s exists with URL %s'
-                            % (display_path(dest), existing_url))
-                logger.warn('The plan is to install the Bazaar repository %s'
-                            % url)
-                response = ask('What to do?  (s)witch, (i)gnore, (w)ipe, (b)ackup ', ('s', 'i', 'w', 'b'))
-                if response == 's':
-                    logger.notify('Switching branch %s to %s%s'
-                                  % (display_path(dest), url, rev_display))
-                    call_subprocess([BZR_CMD, 'switch', url], cwd=dest)
-                elif response == 'i':
-                    # do nothing
-                    pass
-                elif response == 'w':
-                    logger.warn('Deleting %s' % display_path(dest))
-                    shutil.rmtree(dest)
-                    branch = True
-                elif response == 'b':
-                    dest_dir = backup_dir(dest)
-                    logger.warn('Backing up %s to %s' % (display_path(dest), dest_dir))
-                    shutil.move(dest, dest_dir)
-                    branch = True
-        if branch:
+        if self.check_destination(dest, url, rev_options, rev_display):
             logger.notify('Checking out %s%s to %s'
                           % (url, rev_display, display_path(dest)))
-            if update:
-                call_subprocess(
-                    [BZR_CMD, 'pull', '-q'] + rev_options + [url], cwd=dest)
-            else:
-                call_subprocess(
-                    [BZR_CMD, 'branch', '-q'] + rev_options + [url, dest])
+            call_subprocess(
+                [BZR_CMD, 'branch', '-q'] + rev_options + [url, dest])
 
     def get_url_rev(self):
         # hotfix the URL scheme after removing bzr+ from bzr+ssh:// readd it