Example #1
0
    def get_resource_at_path(self, path):
        """Get the resource from the filestore for the specified path.

        The path starts with a slash as proveded through the url traversal,
        the filestore does not expect nor want a leading slash.  It is the
        responsibility of this method to remove the leading slash.
        """
        assert path.startswith('/')
        file_path = path[1:]
        is_default = False
        if file_path == '' or file_path == self.DEFAULT_PATH:
            file_path = self.DEFAULT_PATH
            is_default = True

        dir_resource = None
        file_resource = self.filestore.get_file(file_path)
        # If the resource exists and is a file, we are done.
        if file_resource is not None:
            if file_resource.file_type != FileType.DIRECTORY:
                return self.get_resource(path, file_path, file_resource, None)
            else:
                dir_resource = file_resource
                file_resource = None

        if '.' not in basename(file_path):
            file_path += '.txt'
            file_resource = self.filestore.get_file(file_path)

        resource = self.get_resource(
            path, file_path, file_resource, dir_resource)
        if is_default:
            directlyProvides(resource, IDefaultPage)
        return resource
Example #2
0
def classify_filename(name):
    """Classify a file based on its name.
    
    :param name: File path.
    :return: One of code, documentation, translation or art. 
        None if determining the file type failed.
    """
    # FIXME: Use mime types? Ohcount? 
    extension = os.path.splitext(name)[1]
    if extension in (".c", ".h", ".py", ".cpp", ".rb", ".pm", ".pl", ".ac"):
        return "code"
    if extension in (".html", ".xml", ".txt", ".rst", ".TODO"):
        return "documentation"
    if extension in (".po",):
        return "translation"
    if extension in (".svg", ".png", ".jpg"):
        return "art"
    if not extension:
        basename = urlutils.basename(name)
        if basename in ("README", "NEWS", "TODO", 
                        "AUTHORS", "COPYING"):
            return "documentation"
        if basename in ("Makefile",):
            return "code"

    mutter("don't know how to classify %s", name)
    return None
Example #3
0
def classify_filename(name):
    """Classify a file based on its name.
    
    :param name: File path.
    :return: One of code, documentation, translation or art. 
        None if determining the file type failed.
    """
    # FIXME: Use mime types? Ohcount?
    extension = os.path.splitext(name)[1]
    if extension in (".c", ".h", ".py", ".cpp", ".rb", ".pm", ".pl", ".ac"):
        return "code"
    if extension in (".html", ".xml", ".txt", ".rst", ".TODO"):
        return "documentation"
    if extension in (".po", ):
        return "translation"
    if extension in (".svg", ".png", ".jpg"):
        return "art"
    if not extension:
        basename = urlutils.basename(name)
        if basename in ("README", "NEWS", "TODO", "AUTHORS", "COPYING"):
            return "documentation"
        if basename in ("Makefile", ):
            return "code"

    mutter("don't know how to classify %s", name)
    return None
Example #4
0
 def get_resource(self, path, file_path, file_resource, dir_resource):
     """Return the correct type of resource based on the params."""
     filename = basename(file_path)
     if path == '/':
         return RootResource(
             self, path, file_path, file_resource, None)
     elif file_resource is not None:
         # We are pointing at a file.
         file_type = file_resource.file_type
         if file_type == FileType.BINARY_FILE:
             # Binary resources have no associated directory.
             return BinaryResource(
                 self, path, file_path, file_resource, None)
         # This is known to be not entirely right.
         if (filename.endswith('.txt') or
             '.' not in file_resource.base_name):
             return WikiTextFile(
                 self, path, file_path, file_resource,
                 dir_resource)
         else:
             return SourceTextFile(
                 self, path, file_path, file_resource, None)
     elif dir_resource is not None:
         return DirectoryResource(
             self, path, file_path, None, dir_resource)
     else:
         return MissingResource(
             self, path, file_path, None, None)
Example #5
0
    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 rename(self, rel_from, rel_to):
     """See Transport.rename().
     """
     try:
         if self._decorated.has(rel_to):
             rel_to = urlutils.join(rel_to, urlutils.basename(rel_from))
         self._decorated.rename(rel_from, rel_to)
     except (errors.DirectoryNotEmpty, errors.FileExists), e:
         # absorb the error
         return
Example #7
0
 def rename(self, rel_from, rel_to):
     """See Transport.rename().
     """
     try:
         if self._decorated.has(rel_to):
             rel_to = urlutils.join(rel_to, urlutils.basename(rel_from))
         self._decorated.rename(rel_from, rel_to)
     except (errors.DirectoryNotEmpty, errors.FileExists), e:
         # absorb the error
         return
Example #8
0
    def get_filepath(self, params, tree):
        """Calculate the path to the file in a tree.

        This is overridden to return just the basename, rather than full path,
        so that e.g. if the config says ``changelog_merge_files = ChangeLog``,
        then all ChangeLog files in the tree will match (not just one in the
        root of the tree).
        
        :param params: A MergeHookParams describing the file to merge
        :param tree: a Tree, e.g. self.merger.this_tree.
        """
        return urlutils.basename(tree.id2path(params.file_id))
Example #9
0
 def test_create_anonymous_lightweight_checkout(self):
     """A lightweight checkout from a readonly branch should succeed."""
     tree_a = self.make_branch_and_tree("a")
     rev_id = tree_a.commit("put some content in the branch")
     # open the branch via a readonly transport
     url = self.get_readonly_url(urlutils.basename(tree_a.branch.base))
     t = transport.get_transport_from_url(url)
     if not tree_a.branch.bzrdir._format.supports_transport(t):
         raise tests.TestNotApplicable("format does not support transport")
     source_branch = _mod_branch.Branch.open(url)
     # sanity check that the test will be valid
     self.assertRaises((errors.LockError, errors.TransportNotPossible), source_branch.lock_write)
     checkout = source_branch.create_checkout("c", lightweight=True)
     self.assertEqual(rev_id, checkout.last_revision())
Example #10
0
File: pull.py Project: biji/qbzr
 def _build_lp_push_suggestion(self, master_url):
     try:
         from bzrlib.plugins.launchpad import account
     except ImportError:
         # yes, ImportError is possible with bzr.exe,
         # because user has option to not install launchpad plugin at all
         return ''
     from bzrlib.plugins.qbzr.lib.util import launchpad_project_from_url
     user_name = account.get_lp_login()
     project_name = launchpad_project_from_url(master_url)
     branch_name = urlutils.basename(self.branch.base)
     if user_name and project_name and branch_name:
         return "lp:~%s/%s/%s" % (user_name, project_name, branch_name)
     else:
         return ''
 def test_create_anonymous_lightweight_checkout(self):
     """A lightweight checkout from a readonly branch should succeed."""
     tree_a = self.make_branch_and_tree('a')
     rev_id = tree_a.commit('put some content in the branch')
     # open the branch via a readonly transport
     url = self.get_readonly_url(urlutils.basename(tree_a.branch.base))
     t = transport.get_transport_from_url(url)
     if not tree_a.branch.bzrdir._format.supports_transport(t):
         raise tests.TestNotApplicable("format does not support transport")
     source_branch = _mod_branch.Branch.open(url)
     # sanity check that the test will be valid
     self.assertRaises((errors.LockError, errors.TransportNotPossible),
                       source_branch.lock_write)
     checkout = source_branch.create_checkout('c', lightweight=True)
     self.assertEqual(rev_id, checkout.last_revision())
Example #12
0
File: app.py Project: jelmer/wikkid
def serve_file(filename):
    if os.path.exists(filename):
        basename = urlutils.basename(filename)
        content_type = mimetypes.guess_type(basename)[0]

        res = Response(content_type=content_type, conditional_response=True)
        res.app_iter = FileIterable(filename)
        res.content_length = os.path.getsize(filename)
        res.last_modified = os.path.getmtime(filename)
        # Todo: is this the best value for the etag?
        # perhaps md5 would be a better alternative
        res.etag = '%s-%s-%s' % (os.path.getmtime(filename),
            os.path.getsize(filename),
            hash(filename))
        return res

    else:
        return HTTPNotFound()
Example #13
0
File: bzr.py Project: jelmer/wikkid
    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])
Example #14
0
    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)
Example #15
0
File: pull.py Project: biji/qbzr
 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)
Example #16
0
 def base_name(self):
     return urlutils.basename(self.path)
Example #17
0
 def delete(self, relpath):
     if urlutils.basename(relpath).startswith('.nfs'):
         raise errors.ResourceBusy(self.abspath(relpath))
     return self._decorated.delete(relpath)
Example #18
0
 def readonly_repository(self, repo):
     relpath = urlutils.basename(repo.bzrdir.user_url.rstrip('/'))
     return ControlDir.open_from_transport(
         self.get_readonly_transport(relpath)).open_repository()