ListerStats(pages: int = 0, origins: int = 0)¶
pages: int = 0¶
origins: int = 0¶
dict() → Dict[str, int]¶
Lister(scheduler: swh.scheduler.interface.SchedulerInterface, url: str, instance: str, credentials: Optional[Dict[str, Dict[str, List[Dict[str, str]]]]] = None)¶
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).
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 used, in conjunction with
LISTER_NAME, to uniquely identify this lister instance.
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.
- 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
LISTER_NAME: str = ''¶
run() → swh.lister.pattern.ListerStats¶
Run the lister.
A counter with the number of pages and origins seen for this run of the lister.
get_state_from_scheduler() → StateType¶
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¶
Update the state in the scheduler backend from the state of the current instance.
:raises concurrent listers (from
state_from_dict(d: Dict[str, Any]) → StateType¶
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]¶
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¶
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]¶
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[swh.scheduler.model.ListedOrigin]¶
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¶
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[swh.scheduler.model.ListedOrigin]) → int¶
Record a list of
model.ListedOriginin the scheduler.
the number of listed origins recorded in the scheduler
from_config(scheduler: Dict[str, Any], **config: Any)¶
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
Instantiate a lister from the configuration loaded from the SWH_CONFIG_FILENAME envvar, with potential extra keyword arguments if their value is not None.
kwargs – kwargs passed to the lister instantiation
StatelessLister(scheduler: swh.scheduler.interface.SchedulerInterface, url: str, instance: str, credentials: Optional[Dict[str, Dict[str, List[Dict[str, str]]]]] = None)¶
state_from_dict(d: Dict[str, Any]) → None¶
Always return empty state
state_to_dict(state: None) → Dict[str, Any]¶
Always set empty state