Пример #1
0
    def get_file(self, path, revision=HEAD):
        if not path or revision != HEAD:
            raise FileNotFoundError(path, revision)

        try:
            with open(self.repopath + '/' + path, 'rb') as f:
                return f.read()
        except IOError as e:
            raise FileNotFoundError(path, revision, detail=six.text_type(e))
Пример #2
0
    def cat_file(self, path, rev='tip', base_commit_id=None):
        # If the base commit id is provided it should override anything
        # that was parsed from the diffs.
        if rev != PRE_CREATION and base_commit_id is not None:
            rev = base_commit_id

        if rev == HEAD or rev == UNKNOWN:
            rev = "tip"
        elif rev == PRE_CREATION:
            rev = ""

        for rawpath in ["raw-file", "raw", "hg-history"]:
            try:
                url = self.FULL_FILE_URL % {
                    'url': self.path_stripped,
                    'rawpath': rawpath,
                    'revision': rev,
                    'quoted_path': urllib_quote(path.lstrip('/')),
                }

                return self.get_file_http(url, path, rev)
            except Exception:
                # It failed. Error was logged and we may try again.
                pass

        raise FileNotFoundError(path, rev)
Пример #3
0
    def cat_file(self, path, rev="tip"):
        if rev == HEAD or rev == UNKNOWN:
            rev = "tip"
        elif rev == PRE_CREATION:
            rev = ""

        for rawpath in ["raw-file", "raw", "hg-history"]:
            try:
                base_url = self.path.rstrip('/')

                if rawpath == 'hg-history':
                    base_url = self.path[:self.path.rfind('/')]

                url = self.FULL_FILE_URL % {
                    'url': base_url,
                    'rawpath': rawpath,
                    'revision': rev,
                    'quoted_path': urllib_quote(path.lstrip('/')),
                }

                return self.get_file_http(url, path, rev)
            except Exception:
                # It failed. Error was logged and we may try again.
                pass

        raise FileNotFoundError(path, rev)
Пример #4
0
    def _process_files(self,
                       file,
                       basedir,
                       check_existance=False,
                       limit_to=None):
        tool = self.repository.get_scmtool()

        for f in tool.get_parser(file.read()).parse():
            f2, revision = tool.parse_diff_revision(f.origFile, f.origInfo,
                                                    f.moved)

            if f2.startswith("/"):
                filename = f2
            else:
                filename = os.path.join(basedir, f2).replace("\\", "/")

            if limit_to is not None and filename not in limit_to:
                # This file isn't actually needed for the diff, so save
                # ourselves a remote file existence check and some storage.
                continue

            # FIXME: this would be a good place to find permissions errors
            if (revision != PRE_CREATION and revision != UNKNOWN
                    and not f.binary and not f.deleted and not f.moved and
                (check_existance and not self.repository.get_file_exists(
                    filename, revision, self.request))):
                raise FileNotFoundError(filename, revision)

            f.origFile = filename
            f.origInfo = revision

            yield f
Пример #5
0
    def _process_files(self, file, basedir, check_existance=False):
        tool = self.repository.get_scmtool()

        for f in tool.get_parser(file.read()).parse():
            f2, revision = tool.parse_diff_revision(f.origFile, f.origInfo,
                                                    f.moved)

            if f2.startswith("/"):
                filename = f2
            else:
                filename = os.path.join(basedir, f2).replace("\\", "/")

            # FIXME: this would be a good place to find permissions errors
            if (revision != PRE_CREATION and
                revision != UNKNOWN and
                not f.binary and
                not f.deleted and
                not f.moved and
                (check_existance and
                 not self.repository.get_file_exists(filename, revision))):
                raise FileNotFoundError(filename, revision)

            f.origFile = filename
            f.origInfo = revision

            yield f
Пример #6
0
    def cat_file(self, path, rev="tip"):
        if rev == HEAD:
            rev = "tip"
        elif rev == PRE_CREATION:
            rev = ""

        try:
            return self.repo.changectx(rev).filectx(path).data()
        except Exception, e:
            # LookupError moves from repo to revlog in hg v0.9.4, so we
            # catch the more general Exception to avoid the dependency.
            raise FileNotFoundError(path, rev, str(e))
Пример #7
0
    def cat_file(self, path, rev="tip"):
        if rev == HEAD:
            rev = "tip"
        elif rev == PRE_CREATION:
            rev = ""

        if path:
            p = self._run_hg(['cat', '--rev', rev, path])
            contents = p.stdout.read()
            failure = p.wait()

            if not failure:
                return contents

        raise FileNotFoundError(path, rev)
Пример #8
0
    def _process_files(self,
                       parser,
                       basedir,
                       repository,
                       base_commit_id,
                       request,
                       check_existence=False,
                       limit_to=None):
        tool = repository.get_scmtool()

        for f in parser.parse():
            dest_filename, dest_revision = tool.parse_diff_revision(
                f.newFile, f.newInfo, moved=f.moved, copied=f.copied)

            source_filename, source_revision = tool.parse_diff_revision(
                f.origFile, f.origInfo, moved=f.moved, copied=f.copied)

            dest_filename = self._normalize_filename(dest_filename, basedir)
            source_filename = self._normalize_filename(source_filename,
                                                       basedir)

            if limit_to is not None and dest_filename not in limit_to:
                # This file isn't actually needed for the diff, so save
                # ourselves a remote file existence check and some storage.
                continue

            # FIXME: this would be a good place to find permissions errors
            if (source_revision != PRE_CREATION and source_revision != UNKNOWN
                    and not f.binary and not f.deleted and not f.moved
                    and not f.copied and
                (check_existence and
                 not repository.get_file_exists(source_filename,
                                                source_revision,
                                                base_commit_id=base_commit_id,
                                                request=request))):
                raise FileNotFoundError(source_filename, source_revision,
                                        base_commit_id)

            f.origFile = source_filename
            f.origInfo = source_revision
            f.newFile = dest_filename
            f.newInfo = dest_revision

            yield f
Пример #9
0
    def cat_file(self, path, rev='tip', base_commit_id=None):
        # If the base commit id is provided it should override anything
        # that was parsed from the diffs.
        if rev != PRE_CREATION and base_commit_id is not None:
            rev = base_commit_id

        if rev == HEAD:
            rev = "tip"
        elif rev == PRE_CREATION:
            rev = ""

        if path:
            p = self._run_hg(['cat', '--rev', rev, path])
            contents = p.stdout.read()
            failure = p.wait()

            if not failure:
                return contents

        raise FileNotFoundError(path, rev)
Пример #10
0
                return f.read()

            except urllib2.HTTPError, e:

                if e.code != 404:
                    logging.error(
                        "%s: HTTP error code %d when fetching "
                        "file from %s: %s", self.__class__.__name__, e.code,
                        full_url, e)

            except Exception:
                logging.exception('%s: Non-HTTP error when fetching %r: ',
                                  self.__class__.__name__, full_url)

        if not found:
            raise FileNotFoundError(path, rev, str(e))

    def get_filenames(self, rev):
        raise NotImplemented


class HgClient(object):
    def __init__(self, repoPath, local_site):
        from mercurial import hg, ui
        from mercurial.__version__ import version

        version_parts = [int(x) for x in version.split(".")]

        if version_parts[0] == 1 and version_parts[1] <= 2:
            hg_ui = ui.ui(interactive=False)
        else:
Пример #11
0
def _process_files(parser, basedir, repository, base_commit_id,
                   request, get_file_exists=None, check_existence=False,
                   limit_to=None):
    """Collect metadata about files in the parser.

    Args:
        parser (reviewboard.diffviewer.parser.DiffParser):
            A DiffParser instance for the diff.

        basedir (unicode):
            The base directory to prepend to all file paths in the diff.

        repository (reviewboard.scmtools.models.Repository):
            The repository that the diff was created against.

        base_commit_id (unicode):
            The ID of the commit that the diff is based upon. This is
            needed by some SCMs or hosting services to properly look up
            files, if the diffs represent blob IDs instead of commit IDs
            and the service doesn't support those lookups.

        request (django.http.HttpRequest):
            The current HTTP request.

        check_existence (bool, optional):
            Whether or not existence checks should be performed against
            the upstream repository.

        get_file_exists (callable, optional):
            A callable to use to determine if a given file exists in the
            repository.

            If ``check_existence`` is ``True`` this argument must be
            provided.

        limit_to (list of unicode, optional):
            A list of filenames to limit the results to.

    Yields:
       reviewboard.diffviewer.parser.ParsedDiffFile:
       The files present in the diff.

    Raises:
        ValueError:
            ``check_existence`` was ``True`` but ``get_file_exists`` was not
            provided.
    """
    if check_existence and get_file_exists is None:
        raise ValueError('Must provide get_file_exists when check_existence '
                         'is True')

    tool = repository.get_scmtool()

    for f in parser.parse():
        source_filename, source_revision = tool.parse_diff_revision(
            f.origFile,
            f.origInfo,
            moved=f.moved,
            copied=f.copied)

        dest_filename = _normalize_filename(f.newFile, basedir)
        source_filename = _normalize_filename(source_filename,
                                              basedir)

        if limit_to is not None and dest_filename not in limit_to:
            # This file isn't actually needed for the diff, so save
            # ourselves a remote file existence check and some storage.
            continue

        # FIXME: this would be a good place to find permissions errors
        if (source_revision != PRE_CREATION and
            source_revision != UNKNOWN and
            not f.binary and
            not f.deleted and
            not f.moved and
            not f.copied and
            (check_existence and
             not get_file_exists(source_filename,
                                 source_revision,
                                 base_commit_id=base_commit_id,
                                 request=request))):
            raise FileNotFoundError(source_filename, source_revision,
                                    base_commit_id)

        f.origFile = source_filename
        f.origInfo = source_revision
        f.newFile = dest_filename

        yield f
Пример #12
0
def _process_files(parsed_diff,
                   basedir,
                   repository,
                   base_commit_id,
                   request,
                   get_file_exists=None,
                   check_existence=False,
                   limit_to=None):
    """Collect metadata about files in the parser.

    Args:
        parsed_diff (reviewboard.diffviewer.parser.ParsedDiff):
            The parsed diff to process.

        basedir (unicode):
            The base directory to prepend to all file paths in the diff.

        repository (reviewboard.scmtools.models.Repository):
            The repository that the diff was created against.

        base_commit_id (unicode):
            The ID of the commit that the diff is based upon. This is
            needed by some SCMs or hosting services to properly look up
            files, if the diffs represent blob IDs instead of commit IDs
            and the service doesn't support those lookups.

        request (django.http.HttpRequest):
            The current HTTP request.

        check_existence (bool, optional):
            Whether or not existence checks should be performed against
            the upstream repository.

        get_file_exists (callable, optional):
            A callable to use to determine if a given file exists in the
            repository.

            If ``check_existence`` is ``True`` this argument must be
            provided.

        limit_to (list of unicode, optional):
            A list of filenames to limit the results to.

    Yields:
       reviewboard.diffviewer.parser.ParsedDiffFile:
       Each file present in the diff.

    Raises:
        ValueError:
            ``check_existence`` was ``True`` but ``get_file_exists`` was not
            provided.
    """
    if check_existence and get_file_exists is None:
        raise ValueError('Must provide get_file_exists when check_existence '
                         'is True')

    tool = repository.get_scmtool()
    basedir = force_bytes(basedir)

    parsed_change = parsed_diff.changes[0]

    for f in parsed_change.files:
        # This will either be a Revision or bytes. Either way, convert it
        # bytes now.
        orig_revision = force_bytes(f.orig_file_details)

        source_filename, source_revision = tool.parse_diff_revision(
            f.orig_filename, orig_revision, moved=f.moved, copied=f.copied)

        assert isinstance(source_filename, bytes), (
            '%s.parse_diff_revision() must return a bytes filename, not %r' %
            (type(tool).__name__, type(source_filename)))
        assert isinstance(source_revision, (bytes, Revision)), (
            '%s.parse_diff_revision() must return a revision which is either '
            'bytes or reviewboard.scmtools.core.Revision, not %r' %
            (type(tool).__name__, type(source_revision)))

        dest_filename = _normalize_filename(f.modified_filename, basedir)

        if limit_to is not None and dest_filename not in limit_to:
            # This file isn't actually needed for the diff, so save
            # ourselves a remote file existence check and some storage.
            continue

        source_filename = _normalize_filename(source_filename, basedir)

        # FIXME: this would be a good place to find permissions errors
        if (source_revision != PRE_CREATION and source_revision != UNKNOWN
                and not f.binary and not f.deleted and not f.moved
                and not f.copied
                and (check_existence
                     and not get_file_exists(force_text(source_filename),
                                             force_text(source_revision),
                                             base_commit_id=base_commit_id,
                                             request=request))):
            raise FileNotFoundError(force_text(source_filename),
                                    force_text(source_revision),
                                    base_commit_id)

        f.orig_filename = source_filename
        f.orig_file_details = source_revision
        f.modified_filename = dest_filename

        yield f