- class swh.lister.pattern.ListerStats(pages: 'int' = 0, origins: 'int' = 0)[source]#
- class swh.lister.pattern.Lister(scheduler: SchedulerInterface, url: str, instance: Optional[str] = None, credentials: Optional[Dict[str, Dict[str, List[Dict[str, str]]]]] = None, max_origins_per_page: Optional[int] = None, max_pages: Optional[int] = None, enable_origins: bool = True, with_github_session: bool = False)[source]#
The base class for a Software Heritage lister.
A lister scrapes a page by page list of origins from an upstream (a forge, the API of a package manager, …), and massages the results of that scrape into a list of origins that are recorded by the scheduler backend.
The main loop of the lister,
run(), basically revolves around the
get_pages()iterator, which sets up the lister state, then yields the scrape results page by page. The
get_origins_from_page()method converts the pages into a list of
model.ListedOrigin, sent to the scheduler at every page. The
commit_page()method can be used to update the lister state after a page of origins has been recorded in the scheduler backend.
finalize()method is called at lister teardown (whether the run has been successful or not) to update the local
stateobject before it’s sent to the database. This method must set the
updatedattribute if an updated state needs to be sent to the scheduler backend. This method can call
get_state_from_scheduler()to refresh and merge the lister state from the scheduler before it’s finalized (and potentially minimize the risk of race conditions between concurrent runs of the lister).
The state of the lister is serialized and deserialized from the dict stored in the scheduler backend, using the
scheduler – the instance of the Scheduler being used to register the origins listed by this lister
url – a URL representing this lister, e.g. the API’s base URL
instance – the instance name, to uniquely identify this lister instance, if not provided the URL network location will be used
credentials – dictionary of credentials for all listers. The first level identifies the
LISTER_NAME, the second level the lister
instance. The final level is a list of dicts containing the expected credentials for the given instance of that lister.
max_pages – the maximum number of pages listed in a full listing operation
max_origins_per_page – the maximum number of origins processed per page
enable_origins – whether the created origins should be enabled or not
- Generic types:
StateType: concrete lister type; should usually be a
dataclassfor stricter typing
PageType: type of scrape results; can usually be a
requests.Response, or a
- github_session: Optional[GitHubSession] = None#
- run() ListerStats [source]#
Run the lister.
A counter with the number of pages and origins seen for this run of the lister.
- get_state_from_scheduler() StateType [source]#
Update the state in the current instance from the state in the scheduler backend.
lister_obj, and returns its (deserialized) current state, to allow for comparison with the local state.
the state retrieved from the scheduler backend
- set_state_in_scheduler() None [source]#
Update the state in the scheduler backend from the state of the current instance.
swh.scheduler.exc.StaleData – in case of a race condition between concurrent listers (from
- state_from_dict(d: Dict[str, Any]) StateType [source]#
Convert the state stored in the scheduler backend (as a dict), to the concrete StateType for this lister.
- state_to_dict(state: StateType) Dict[str, Any] [source]#
Convert the StateType for this lister to its serialization as dict for storage in the scheduler.
Values must be JSON-compatible as that’s what the backend database expects.
- finalize() None [source]#
Custom hook to finalize the lister state before returning from the main loop.
This method must set
updatedif the lister has done some work.
If relevant, this method can use :meth`get_state_from_scheduler` to merge the current lister state with the one from the scheduler backend, reducing the risk of race conditions if we’re running concurrent listings.
This method is called in a finally block, which means it will also run when the lister fails.
- get_pages() Iterator[PageType] [source]#
Retrieve a list of pages of listed results. This is the main loop of the lister.
an iterator of raw pages fetched from the platform currently being listed.
- get_origins_from_page(page: PageType) Iterator[ListedOrigin] [source]#
Extract a list of
model.ListedOriginfrom a raw page of results.
page – a single page of results
an iterator for the origins present on the given page of results
- commit_page(page: PageType) None [source]#
Custom hook called after the current page has been committed in the scheduler backend.
This method can be used to update the state after a page of origins has been successfully recorded in the scheduler backend. If the new state should be recorded at the point the lister completes, the
updatedattribute must be set.
- send_origins(origins: Iterable[ListedOrigin]) List[str] [source]#
Record a list of
model.ListedOriginin the scheduler.
the list of origin URLs recorded in scheduler database
- classmethod from_config(scheduler: Dict[str, Any], **config: Any)[source]#
Instantiate a lister from a configuration dict.
This is basically a backwards-compatibility shim for the CLI.
scheduler – instantiation config for the scheduler
config – the configuration dict for the lister, with the following keys: - credentials (optional): credentials list for the scheduler - any other kwargs passed to the lister.
the instantiated lister
- class swh.lister.pattern.StatelessLister(scheduler: SchedulerInterface, url: str, instance: Optional[str] = None, credentials: Optional[Dict[str, Dict[str, List[Dict[str, str]]]]] = None, max_origins_per_page: Optional[int] = None, max_pages: Optional[int] = None, enable_origins: bool = True, with_github_session: bool = False)[source]#