|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | + |
| 13 | +import random |
| 14 | + |
| 15 | +import click |
| 16 | + |
| 17 | +from warehouse.cli import warehouse |
| 18 | + |
| 19 | + |
| 20 | +@warehouse.group() |
| 21 | +def observations(): |
| 22 | + """ |
| 23 | + Group for observation commands. |
| 24 | + """ |
| 25 | + |
| 26 | + |
| 27 | +@observations.command() |
| 28 | +@click.pass_obj |
| 29 | +def generate_random_observations(config): # pragma: no cover # dev-only tool |
| 30 | + """ |
| 31 | + Generate random Observations, DEVELOPMENT ONLY |
| 32 | +
|
| 33 | + Generates random observations for 10 projects in the database, |
| 34 | + along with any associated Observer models. |
| 35 | + It is only available in development mode, useful for |
| 36 | + """ |
| 37 | + # Imported here because we don't want to trigger an import from anything |
| 38 | + # but warehouse.cli at the module scope. |
| 39 | + from sqlalchemy import select |
| 40 | + |
| 41 | + from warehouse.accounts.models import User |
| 42 | + from warehouse.config import Environment |
| 43 | + from warehouse.db import Session |
| 44 | + from warehouse.observations.models import ObservationKind |
| 45 | + from warehouse.packaging.models import Project |
| 46 | + |
| 47 | + # bail early if not in development |
| 48 | + if not config.registry.settings.get("warehouse.env") == Environment.development: |
| 49 | + raise click.ClickException( |
| 50 | + "This command is only available in development mode." |
| 51 | + ) |
| 52 | + |
| 53 | + session = Session(bind=config.registry["sqlalchemy.engine"]) |
| 54 | + |
| 55 | + # A `request`-like object that implements the `db` method |
| 56 | + class MockRequest: |
| 57 | + def __init__(self): |
| 58 | + self.db = session |
| 59 | + |
| 60 | + request = MockRequest() |
| 61 | + |
| 62 | + # Get 10 users and project |
| 63 | + users = session.scalars(select(User).limit(10)).unique().all() |
| 64 | + projects = session.scalars(select(Project).limit(10)).all() |
| 65 | + |
| 66 | + # generate 10 random observations for each project |
| 67 | + for project in projects: |
| 68 | + [ |
| 69 | + project.record_observation( |
| 70 | + request=request, |
| 71 | + kind=random.choice(list(ObservationKind)), |
| 72 | + actor=random.choice(users), |
| 73 | + summary="CLI Generated", |
| 74 | + payload={"origin": "CLI"}, |
| 75 | + ) |
| 76 | + for _ in range(10) |
| 77 | + ] |
| 78 | + session.commit() # Finally, commit the transaction |
| 79 | + |
| 80 | + click.echo("Generated random observations for 10 projects.") |
0 commit comments