def put_bytes_non_atomic(self, relpath, bytes, mode=None, create_parent_dir=False, dir_mode=False): """See Transport.put_file_non_atomic""" abspath = self._remote_path(relpath) request = PUTRequest(abspath, bytes, accepted_errors=[200, 201, 204, 403, 404, 409]) def bare_put_file_non_atomic(): response = self._perform(request) code = response.code if code in (403, 404, 409): # Intermediate directories missing raise errors.NoSuchFile(abspath) if code not in (200, 201, 204): self._raise_curl_http_error(abspath, response, 'expected 200, 201 or 204.') try: bare_put_file_non_atomic() except errors.NoSuchFile: if not create_parent_dir: raise parent_dir = osutils.dirname(relpath) if parent_dir: self.mkdir(parent_dir, mode=dir_mode) return bare_put_file_non_atomic() else: # Don't forget to re-raise if the parent dir doesn't exist raise
def _get_final_delta(self): """Generate the final delta. Smart post-processing of changes, e.g. pruning of directories that would become empty, goes here. """ delta = list(self._delta_entries_by_fileid.values()) if self.prune_empty_dirs and self._dirs_that_might_become_empty: candidates = self._dirs_that_might_become_empty while candidates: never_born = set() parent_dirs_that_might_become_empty = set() for path, file_id in self._empty_after_delta(delta, candidates): newly_added = self._new_file_ids.get(path) if newly_added: never_born.add(newly_added) else: delta.append((path, None, file_id, None)) parent_dir = osutils.dirname(path) if parent_dir: parent_dirs_that_might_become_empty.add(parent_dir) candidates = parent_dirs_that_might_become_empty # Clean up entries that got deleted before they were ever added if never_born: delta = [de for de in delta if de[2] not in never_born] return delta
def _copy_one(self, fileid, suffix, other, pb): # TODO: Once the copy_to interface is improved to allow a source # and destination targets, then we can always do the copy # as long as other is a TextStore if not (isinstance(other, TextStore) and other._prefixed == self._prefixed): return super(TextStore, self)._copy_one(fileid, suffix, other, pb) mutter('_copy_one: %r, %r', fileid, suffix) path = other._get_name(fileid, suffix) if path is None: raise KeyError(fileid + '-' + str(suffix)) try: result = other._transport.copy_to([path], self._transport, mode=self._file_mode) except NoSuchFile: if not self._prefixed: raise try: self._transport.mkdir(osutils.dirname(path), mode=self._dir_mode) except FileExists: pass result = other._transport.copy_to([path], self._transport, mode=self._file_mode) if result != 1: raise BzrError('Unable to copy file: %r' % (path,))
def _copy_one(self, fileid, suffix, other, pb): # TODO: Once the copy_to interface is improved to allow a source # and destination targets, then we can always do the copy # as long as other is a TextStore if not (isinstance(other, TextStore) and other._prefixed == self._prefixed): return super(TextStore, self)._copy_one(fileid, suffix, other, pb) mutter('_copy_one: %r, %r', fileid, suffix) path = other._get_name(fileid, suffix) if path is None: raise KeyError(fileid + '-' + str(suffix)) try: result = other._transport.copy_to([path], self._transport, mode=self._file_mode) except NoSuchFile: if not self._prefixed: raise try: self._transport.mkdir(osutils.dirname(path), mode=self._dir_mode) except FileExists: pass result = other._transport.copy_to([path], self._transport, mode=self._file_mode) if result != 1: raise BzrError('Unable to copy file: %r' % (path, ))
def _make_new_versionedfile(self, file_id, transaction, known_missing=False, _filename=None): """Make a new versioned file. :param _filename: filename that would be returned from self.filename for file_id. This is used to reduce duplicate filename calculations when using 'get_weave_or_empty'. FOR INTERNAL USE ONLY. """ if not known_missing and self.has_id(file_id): self.delete(file_id, transaction) if _filename is None: _filename = self.filename(file_id) try: # we try without making the directory first because thats optimising # for the common case. weave = self._versionedfile_class(_filename, self._transport, self._file_mode, create=True, get_scope=self.get_scope, **self._versionedfile_kwargs) except errors.NoSuchFile: if not self._prefixed: # unexpected error - NoSuchFile is expected to be raised on a # missing dir only and that only occurs when we are prefixed. raise dirname = osutils.dirname(_filename) self._transport.mkdir(dirname, mode=self._dir_mode) weave = self._versionedfile_class(_filename, self._transport, self._file_mode, create=True, get_scope=self.get_scope, **self._versionedfile_kwargs) return weave
def _save(self): """Save the weave.""" self._check_write_ok() sio = StringIO() write_weave_v5(self, sio) sio.seek(0) bytes = sio.getvalue() path = self._weave_name + WeaveFile.WEAVE_SUFFIX try: self._transport.put_bytes(path, bytes, self._filemode) except errors.NoSuchFile: self._transport.mkdir(dirname(path)) self._transport.put_bytes(path, bytes, self._filemode)
def refresh_view(self): """Update the data in the view.""" plugins = mod_plugin.plugins() summary_data = [] locations_data = [] for name in sorted(plugins): plugin = plugins[name] version = format_plugin_version(plugin) description = format_plugin_description(plugin) directory = osutils.dirname(plugin.path()) summary_data.append((name, version, description)) locations_data.append((name, directory)) self._summary_viewer.setData(summary_data) self._locations_viewer.setData(locations_data)
def get_core_plugin_path(): core_path = None bzr_exe = bool(getattr(sys, 'frozen', None)) if bzr_exe: # expand path for bzr.exe # We need to use relative path to system-wide plugin # directory because bzrlib from standalone bzr.exe # could be imported by another standalone program # (e.g. bzr-config; or TortoiseBzr/Olive if/when they # will become standalone exe). [bialix 20071123] # __file__ typically is # C:\Program Files\Bazaar\lib\library.zip\bzrlib\plugin.pyc # then plugins directory is # C:\Program Files\Bazaar\plugins # so relative path is ../../../plugins core_path = osutils.abspath(osutils.pathjoin( osutils.dirname(__file__), '../../../plugins')) else: # don't look inside library.zip # search the plugin path before the bzrlib installed dir core_path = os.path.dirname(_mod_plugins.__file__) return core_path
def get_core_plugin_path(): core_path = None bzr_exe = bool(getattr(sys, 'frozen', None)) if bzr_exe: # expand path for bzr.exe # We need to use relative path to system-wide plugin # directory because bzrlib from standalone bzr.exe # could be imported by another standalone program # (e.g. bzr-config; or TortoiseBzr/Olive if/when they # will become standalone exe). [bialix 20071123] # __file__ typically is # C:\Program Files\Bazaar\lib\library.zip\bzrlib\plugin.pyc # then plugins directory is # C:\Program Files\Bazaar\plugins # so relative path is ../../../plugins core_path = osutils.abspath( osutils.pathjoin(osutils.dirname(__file__), '../../../plugins')) else: # don't look inside library.zip # search the plugin path before the bzrlib installed dir core_path = os.path.dirname(_mod_plugins.__file__) return core_path
def get_required_parents(self, matches): """Return a dict of all file parents that must be versioned. The keys are the required parents and the values are sets of their children. """ required_parents = {} for path in matches: while True: child = path path = osutils.dirname(path) if self.tree.path2id(path) is not None: break required_parents.setdefault(path, []).append(child) require_ids = {} for parent, children in required_parents.iteritems(): child_file_ids = set() for child in children: file_id = matches.get(child) if file_id is not None: child_file_ids.add(file_id) require_ids[parent] = child_file_ids return require_ids
# Copyright (C) 2007-2008 Canonical Ltd # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA """ssl_certs -- provides access to ssl keys and certificates needed by tests""" from bzrlib import ( osutils, ) # Directory containing all ssl files, keys or certificates base_dir = osutils.dirname(osutils.realpath(__file__)) def build_path(name): """Build and return a path in ssl_certs directory for name""" return osutils.pathjoin(base_dir, name)
def edit_commit_message_encoded(infotext, ignoreline=DEFAULT_IGNORE_LINE, start_message=None): """Let the user edit a commit message in a temp file. This is run if they don't give a message or message-containing file on the command line. :param infotext: Text to be displayed at bottom of message for the user's reference; currently similar to 'bzr status'. The string is already encoded :param ignoreline: The separator to use above the infotext. :param start_message: The text to place above the separator, if any. This will not be removed from the message after the user has edited it. The string is already encoded :return: commit message or None. """ msgfilename = None try: msgfilename, hasinfo = _create_temp_file_with_commit_template( infotext, ignoreline, start_message) if not msgfilename: return None basename = osutils.basename(msgfilename) msg_transport = transport.get_transport_from_path( osutils.dirname(msgfilename)) reference_content = msg_transport.get_bytes(basename) if not _run_editor(msgfilename): return None edited_content = msg_transport.get_bytes(basename) if edited_content == reference_content: if not ui.ui_factory.confirm_action( u"Commit message was not edited, use anyway", "bzrlib.msgeditor.unchanged", {}): # Returning "" makes cmd_commit raise 'empty commit message # specified' which is a reasonable error, given the user has # rejected using the unedited template. return "" started = False msg = [] lastline, nlines = 0, 0 # codecs.open() ALWAYS opens file in binary mode but we need text mode # 'rU' mode useful when bzr.exe used on Cygwin (bialix 20070430) f = file(msgfilename, 'rU') try: try: for line in codecs.getreader(osutils.get_user_encoding())(f): stripped_line = line.strip() # strip empty line before the log message starts if not started: if stripped_line != "": started = True else: continue # check for the ignore line only if there # is additional information at the end if hasinfo and stripped_line == ignoreline: break nlines += 1 # keep track of the last line that had some content if stripped_line != "": lastline = nlines msg.append(line) except UnicodeDecodeError: raise BadCommitMessageEncoding() finally: f.close() if len(msg) == 0: return "" # delete empty lines at the end del msg[lastline:] # add a newline at the end, if needed if not msg[-1].endswith("\n"): return "%s%s" % ("".join(msg), "\n") else: return "".join(msg) finally: # delete the msg file in any case if msgfilename is not None: try: os.unlink(msgfilename) except IOError, e: trace.warning("failed to unlink %s: %s; ignored", msgfilename, e)
def test_break_lock(self): self.run_bzr('break-lock --config %s' % osutils.dirname(self.config_file_name), stdin="y\n") self.assertRaises(errors.LockBroken, self.config.unlock)
def _add_entry(self, entry): # We need to combine the data if multiple entries have the same file-id. # For example, a rename followed by a modification looks like: # # (x, y, f, e) & (y, y, f, g) => (x, y, f, g) # # Likewise, a modification followed by a rename looks like: # # (x, x, f, e) & (x, y, f, g) => (x, y, f, g) # # Here's a rename followed by a delete and a modification followed by # a delete: # # (x, y, f, e) & (y, None, f, None) => (x, None, f, None) # (x, x, f, e) & (x, None, f, None) => (x, None, f, None) # # In summary, we use the original old-path, new new-path and new ie # when combining entries. old_path = entry[0] new_path = entry[1] file_id = entry[2] ie = entry[3] existing = self._delta_entries_by_fileid.get(file_id, None) if existing is not None: old_path = existing[0] entry = (old_path, new_path, file_id, ie) if new_path is None and old_path is None: # This is a delete cancelling a previous add del self._delta_entries_by_fileid[file_id] parent_dir = osutils.dirname(existing[1]) self.mutter("cancelling add of %s with parent %s" % (existing[1], parent_dir)) if parent_dir: self._dirs_that_might_become_empty.add(parent_dir) return else: self._delta_entries_by_fileid[file_id] = entry # Collect parent directories that might become empty if new_path is None: # delete parent_dir = osutils.dirname(old_path) # note: no need to check the root if parent_dir: self._dirs_that_might_become_empty.add(parent_dir) elif old_path is not None and old_path != new_path: # rename old_parent_dir = osutils.dirname(old_path) new_parent_dir = osutils.dirname(new_path) if old_parent_dir and old_parent_dir != new_parent_dir: self._dirs_that_might_become_empty.add(old_parent_dir) # Calculate the per-file parents, if not already done if file_id in self.per_file_parents_for_commit: return if old_path is None: # add # If this is a merge, the file was most likely added already. # The per-file parent(s) must therefore be calculated and # we can't assume there are none. per_file_parents, ie.revision = \ self.rev_store.get_parents_and_revision_for_entry(ie) self.per_file_parents_for_commit[file_id] = per_file_parents elif new_path is None: # delete pass elif old_path != new_path: # rename per_file_parents, _ = \ self.rev_store.get_parents_and_revision_for_entry(ie) self.per_file_parents_for_commit[file_id] = per_file_parents else: # modify per_file_parents, ie.revision = \ self.rev_store.get_parents_and_revision_for_entry(ie) self.per_file_parents_for_commit[file_id] = per_file_parents