예제 #1
0
def lookup_origin_visit_latest(
    origin_url: str,
    require_snapshot: bool = False,
    type: Optional[str] = None,
    allowed_statuses: Optional[List[str]] = None,
) -> Optional[OriginVisitInfo]:
    """Return the origin's latest visit

    Args:
        origin_url: origin to list visits for
        type: Optional visit type to filter on (e.g git, tar, dsc, svn,
            hg, npm, pypi, ...)
        allowed_statuses: list of visit statuses considered
            to find the latest visit. For instance,
            ``allowed_statuses=['full']`` will only consider visits that
            have successfully run to completion.
        require_snapshot: filter out origins without a snapshot

    Returns:
       The origin visit info as dict if found

    """

    visit_status = origin_get_latest_visit_status(
        storage,
        origin_url,
        type=type,
        allowed_statuses=allowed_statuses,
        require_snapshot=require_snapshot,
    )
    return (converters.from_origin_visit(visit_status.to_dict())
            if visit_status else None)
예제 #2
0
 def origin_visit_get_by(self, origin_url: str,
                         visit_id: int) -> OriginVisitInfo:
     visit = self.storage.origin_visit_get_by(origin_url, visit_id)
     assert visit is not None
     visit_status = self.storage.origin_visit_status_get_latest(
         origin_url, visit_id)
     assert visit_status is not None
     return converters.from_origin_visit({
         **visit_status.to_dict(), "type":
         visit.type
     })
예제 #3
0
def lookup_origin_visits(origin_id, last_visit=None, per_page=10):
    """Yields the origin origin_ids' visits.

    Args:
        origin_id: origin to list visits for

    Yields:
       Dictionaries of origin_visit for that origin

    """
    lookup_origin({'id': origin_id})
    visits = _lookup_origin_visits(origin_id, last_visit=last_visit,
                                   limit=per_page)
    for visit in visits:
        yield converters.from_origin_visit(visit)
예제 #4
0
 def origin_visit_status_get_latest(
     self,
     origin_url,
     type: Optional[str] = None,
     allowed_statuses: Optional[List[str]] = None,
     require_snapshot: bool = False,
 ):
     visit_status = origin_get_latest_visit_status(
         self.storage,
         origin_url,
         type=type,
         allowed_statuses=allowed_statuses,
         require_snapshot=require_snapshot,
     )
     return (converters.from_origin_visit(visit_status.to_dict())
             if visit_status else None)
예제 #5
0
def lookup_origin_visit(origin_id, visit_id):
    """Return information about visit visit_id with origin origin_id.

    Args:
        origin_id: origin concerned by the visit
        visit_id: the visit identifier to lookup

    Yields:
       The dict origin_visit concerned

    """
    visit = storage.origin_visit_get_by(origin_id, visit_id)
    if not visit:
        raise NotFoundExc('Origin with id %s or its visit '
                          'with id %s not found!' % (origin_id, visit_id))
    return converters.from_origin_visit(visit)
예제 #6
0
    def origin_visit_get(self, origin_url):
        next_page_token = None
        visits = []
        while True:
            visit_page = self.storage.origin_visit_get(
                origin_url, page_token=next_page_token)
            next_page_token = visit_page.next_page_token

            for visit in visit_page.results:
                visit_status = self.storage.origin_visit_status_get_latest(
                    origin_url, visit.visit)
                visits.append(
                    converters.from_origin_visit({
                        **visit_status.to_dict(), "type":
                        visit.type
                    }))
            if not next_page_token:
                break
        return visits
예제 #7
0
def lookup_origin_visit(origin_url: str, visit_id: int) -> OriginVisitInfo:
    """Return information about visit visit_id with origin origin.

    Args:
        origin: origin concerned by the visit
        visit_id: the visit identifier to lookup

    Yields:
       The dict origin_visit concerned

    """
    visit = storage.origin_visit_get_by(origin_url, visit_id)
    visit_status = storage.origin_visit_status_get_latest(origin_url, visit_id)
    if not visit:
        raise NotFoundExc(
            f"Origin {origin_url} or its visit with id {visit_id} not found!")
    return converters.from_origin_visit({
        **visit_status.to_dict(), "type":
        visit.type
    })
예제 #8
0
def lookup_origin_visits(origin: str,
                         last_visit: Optional[int] = None,
                         per_page: int = 10) -> Iterator[OriginVisitInfo]:
    """Yields the origin origins' visits.

    Args:
        origin: origin to list visits for

    Yields:
       Dictionaries of origin_visit for that origin

    """
    for visit in _lookup_origin_visits(origin,
                                       last_visit=last_visit,
                                       limit=per_page):
        visit_status = storage.origin_visit_status_get_latest(
            origin, visit.visit)
        yield converters.from_origin_visit({
            **visit_status.to_dict(), "type":
            visit.type
        })
예제 #9
0
def test_from_origin_visit():
    snap_hash = "b5f0b7f716735ebffe38505c60145c4fd9da6ca3"

    for snap in [snap_hash, None]:
        visit = {
            "date": {
                "timestamp":
                datetime.datetime(2015,
                                  1,
                                  1,
                                  22,
                                  0,
                                  0,
                                  tzinfo=datetime.timezone.utc).timestamp(),
                "offset":
                0,
                "negative_utc":
                False,
            },
            "origin": 10,
            "visit": 100,
            "metadata": None,
            "status": "full",
            "snapshot": hashutil.hash_to_bytes(snap) if snap else snap,
        }

        expected_visit = {
            "date": "2015-01-01T22:00:00+00:00",
            "origin": 10,
            "visit": 100,
            "metadata": {},
            "status": "full",
            "snapshot": snap_hash if snap else snap,
        }

        actual_visit = converters.from_origin_visit(visit)

        assert actual_visit == expected_visit
예제 #10
0
    def test_from_origin_visit(self):
        snap_hash = 'b5f0b7f716735ebffe38505c60145c4fd9da6ca3'

        for snap in [snap_hash, None]:
            # given
            visit = {
                'date': {
                    'timestamp':
                    datetime.datetime(
                        2015, 1, 1, 22, 0, 0,
                        tzinfo=datetime.timezone.utc).timestamp(),
                    'offset':
                    0,
                    'negative_utc':
                    False,
                },
                'origin': 10,
                'visit': 100,
                'metadata': None,
                'status': 'full',
                'snapshot': hashutil.hash_to_bytes(snap) if snap else snap,
            }

            expected_visit = {
                'date': '2015-01-01T22:00:00+00:00',
                'origin': 10,
                'visit': 100,
                'metadata': {},
                'status': 'full',
                'snapshot': snap_hash if snap else snap
            }

            # when
            actual_visit = converters.from_origin_visit(visit)

            # then
            self.assertEqual(actual_visit, expected_visit)
예제 #11
0
 def origin_visit_get_by(cls, origin_id, visit_id):
     visit = cls.storage.origin_visit_get_by(origin_id, visit_id)
     return converters.from_origin_visit(visit)