def get_branch(repo, relpath, format=None): """Return existing branch in destination repo. Create new if don't exist. @param format: force create new branch in specified format. """ repo_trans = repo.bzrdir.root_transport try: br_dir = BzrDir.open(repo_trans.abspath(relpath)) branch = br_dir.open_branch() except errors.NotBranchError: # create destination branch directory, creating parents as needed. needed = [relpath] while needed: try: repo_trans.mkdir(needed[-1]) needed.pop() except errors.NoSuchFile: parent = urlutils.dirname(needed[-1]) if parent == '': raise errors.BzrCommandError('Could not create branch dir') needed.append(parent) br_dir = BzrDir.create(repo_trans.abspath(relpath)) if format is None: format = BranchFormat.get_default_format() branch = format.initialize(br_dir) note('Created destination branch %s' % relpath) if branch.repository.bzrdir.root_transport.base != repo_trans.base: raise errors.BzrCommandError('Branch %s does not use repository %s' % (relpath, repo_trans.base)) # XXX: hack to make sure the branch is using the same repository # instance, for locking purposes branch.repository = repo return branch
def load_view_modules(): curr_dir = os.path.abspath(dirname(__file__)) view_dir = joinpath(curr_dir, 'view') py_files = [ filename for filename in os.listdir(view_dir) if filename.endswith('.py') and not filename.startswith('__')] for filename in py_files: __import__('wikkid.view.%s' % filename[:-3])
def _add_file(self, path, content, author, commit_message): """Add a new file at the specified path with the content. Then commit this new file with the specified commit_message. """ content = normalize_content(content) t = self.tree.bzrdir.root_transport # Get a transport for the path we want. self._ensure_directory_or_nonexistant(dirname(path)) t = t.clone(dirname(path)) t.create_prefix() # Put the file there. # TODO: UTF-8 encode text files? t.put_bytes(basename(path), content) self.tree.smart_add([t.local_abspath('.')]) self.tree.commit( message=commit_message, authors=[author])
def symlink(self, source, link_name): """See Transport.symlink.""" abs_link_dirpath = urlutils.dirname(self.abspath(link_name)) source_rel = urlutils.file_relpath( urlutils.strip_trailing_slash(abs_link_dirpath), urlutils.strip_trailing_slash(self.abspath(source)) ) try: os.symlink(source_rel, self._abspath(link_name)) except (IOError, OSError), e: self._translate_error(e, source_rel)
def run(self, from_location, to_location=None, lightweight=False, **kw): if lightweight: import errno import os from bzrlib.branch import Branch from bzrlib.errors import BzrError from bzrlib.urlutils import basename, dirname, join from bzrlib.workingtree import WorkingTree br_from = Branch.open(from_location) repo = br_from.repository if not repo.is_shared(): raise BzrError('branch --lightweight supported ' 'only for shared repository') wt_from = WorkingTree.open(from_location) working_path = wt_from.bzrdir.root_transport.base from_branch_path = br_from.bzrdir.root_transport.base if working_path == from_branch_path: raise BzrError('source branch is not lightweight checkout') if to_location is None: raise BzrError('you should specify name for new branch') from_basename = basename(from_branch_path) to_basename = basename(to_location) if from_basename == to_basename: raise BzrError('basename of source and destination is equal') to_branch_path = join(dirname(from_branch_path), to_basename) # make branch print >> self.outf, 'Create branch: %s => %s' % (from_branch_path, to_branch_path) builtins.cmd_branch.run(self, from_branch_path, to_branch_path, **kw) # make lightweight chekout source = Branch.open(to_branch_path) revision_id = source.last_revision() try: os.mkdir(to_location) except OSError, e: if e.errno == errno.EEXIST: raise errors.BzrCommandError( 'Target directory "%s" already' ' exists.' % to_location) if e.errno == errno.ENOENT: raise errors.BzrCommandError( 'Parent of "%s" does not exist.' % to_location) else: raise source.create_checkout(to_location, revision_id, lightweight)
def _ensure_dir(self, path, user): # If we are at the start, we are done. if not path: return # If the directory exists, we are done. if path in self.path_map: # Check to make sure that it is a directory. if self.path_map[path].file_type != FileType.DIRECTORY: raise FileExists( "Found a file at '%s' where a directory is needed" % path) return # Check to make sure the parent is in there too. self._ensure_dir(dirname(path), user) new_dir = File(path, None, self._integer.next(), user) self.file_id_map[new_dir.file_id] = new_dir self.path_map[new_dir.path] = new_dir
def _ensure_directory_or_nonexistant(self, dir_path): """Ensure the dir_path defines a directory or doesn't exist. Walk up the dir_path and make sure that the path either doesn't exist at all, or is a directory. The purpose of this is to make sure we don't try to add a file in a directory where the directory has the same name as an existing file. """ check = [] while dir_path: check.append(dir_path) dir_path = dirname(dir_path) while len(check): f = self.get_file(check.pop()) if f is not None: if not f.is_directory: raise FileExists( '%s exists and is not a directory' % f.path)
def get_preferred_path(self, path): """Get the preferred path for the path passed in. If the path ends with '.txt' and doesn't have any other '.'s in the basename, then we prefer to access that file without the '.txt'. If the resulting path is the default path, then the preferred path should be '/Home' providing Home is the default path.. """ filename = basename(path) if filename.endswith('.txt'): filename = filename[:-4] if path == '/': return '/' + self.DEFAULT_PATH elif '.' in filename: return path else: return joinpath(dirname(path), filename)
def __init__(self, skin_name=None): """Load the required templates.""" # Need to load the initial templates for the skin. if skin_name is None: skin_name = 'default' self.logger = logging.getLogger('wikkid') # TODO: if we are using a user defined directory for the skin, here is # where we'd use a different loader. loader = PackageLoader('wikkid.skin', skin_name) self.env = Environment(loader=loader) self.templates = { 'view_page': self.env.get_template('page.html'), 'edit_page': self.env.get_template('edit.html'), 'view_directory': self.env.get_template('directory_listing.html'), 'missing': self.env.get_template('missing-page.html'), 'missing-dir' : self.env.get_template('missing-directory.html') } module_location = urlutils.dirname(__file__) self.dir_name = urlutils.joinpath(module_location, skin_name)
def list_directory(self, directory_path): """Return a list of File objects for in the directory path. If the path doesn't exist, returns None. If the path exists but is empty, an empty list is returned. Otherwise a list of File objects in that directory. """ if directory_path is None: directory_path = '' else: directory = self.get_file(directory_path) if directory is None or not directory._is_directory: return None listing = [] for path, value in self.path_map.iteritems(): path_dir = dirname(path) if path_dir == directory_path: listing.append(value) return listing
def _build_generic_push_suggestion(self, master_url): master_parent = urlutils.dirname(master_url) branch_name = urlutils.basename(self.branch.base) return urlutils.join(master_parent, branch_name)
def dir_name(self): return urlutils.dirname(self.path)
def _add_file(self, path, content, user): self._ensure_dir(dirname(path), user) new_file = File(path, content, self._integer.next(), user) self.file_id_map[new_file.file_id] = new_file self.path_map[new_file.path] = new_file