diff options
author | Carl Meyer <carl@dirtcircle.com> | 2009-10-03 23:04:38 -0400 |
---|---|---|
committer | Carl Meyer <carl@dirtcircle.com> | 2009-10-03 23:04:38 -0400 |
commit | afa1c59c81b8229c257399422ca14490a05f4500 (patch) | |
tree | 00b0790371548fb0ee35728826a4db56801e2d46 | |
parent | a1ffdfbaac54f2ed9bf597fb5fcc9da22d2df6b7 (diff) | |
download | pip-afa1c59c81b8229c257399422ca14490a05f4500.tar.gz |
refactored obtain() methods to prepare for #41 fix
-rw-r--r-- | pip.py | 284 |
1 files changed, 119 insertions, 165 deletions
@@ -2738,6 +2738,60 @@ class VersionControl(object): """ 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 @@ -2752,6 +2806,7 @@ _svn_revision_re = re.compile(r'Revision: (.+)') 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' @@ -2766,15 +2821,21 @@ class Subversion(VersionControl): 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('#'): @@ -2819,6 +2880,14 @@ class Subversion(VersionControl): 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: @@ -2827,42 +2896,7 @@ class Subversion(VersionControl): 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( @@ -3020,6 +3054,7 @@ vcs.register(Subversion) 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' @@ -3088,6 +3123,17 @@ class Git(VersionControl): % (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: @@ -3096,45 +3142,7 @@ class Git(VersionControl): 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]) @@ -3221,6 +3229,7 @@ vcs.register(Git) 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' @@ -3266,6 +3275,27 @@ class Mercurial(VersionControl): 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: @@ -3274,54 +3304,7 @@ class Mercurial(VersionControl): 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]) @@ -3398,6 +3381,7 @@ vcs.register(Mercurial) 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' @@ -3443,6 +3427,13 @@ class Bazaar(VersionControl): 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: @@ -3451,48 +3442,11 @@ class Bazaar(VersionControl): 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 |