Script 3fdedd58ac73_add_module_table_py
|
|
1 """add module table
2
3 Revision ID: 3fdedd58ac73
4 Revises: 412c2c8d9da
5 Create Date: 2016-10-26 22:01:09.361070
6
7 """
8
9
10 revision = '3fdedd58ac73'
11 down_revision = '412c2c8d9da'
12
13 from alembic import op
14 import sqlalchemy as sa
15
16 from coprs.models import Module, Action, Copr, User, Group
17 from sqlalchemy.orm import sessionmaker
18
19 import json
20 import base64
21 import modulemd
22 import yaml
23 from coprs.logic.coprs_logic import CoprsLogic
24 from coprs.logic.actions_logic import ActionsLogic
25 from coprs.helpers import ActionTypeEnum
26
27
29 bind = op.get_bind()
30 Session = sessionmaker()
31 session = Session(bind=bind)
32
33 op.create_table(
34 "module",
35 sa.Column("id", sa.Integer, primary_key=True),
36 sa.Column("name", sa.String(100), nullable=False),
37 sa.Column("stream", sa.String(100), nullable=False),
38 sa.Column("version", sa.Integer, nullable=False),
39 sa.Column("summary", sa.String(100), nullable=False),
40 sa.Column("description", sa.Text),
41 sa.Column("created_on", sa.Integer, nullable=True),
42 sa.Column("yaml_b64", sa.Text),
43 sa.Column("copr_id", sa.Integer, sa.ForeignKey("copr.id")),
44 )
45 op.create_unique_constraint("unique_name_stream_version_copr_id", "module",
46 ["name", "stream", "version", "copr_id"])
47 session.commit()
48
49
50 added_modules = set()
51 for action in ActionsLogic.get_many(ActionTypeEnum("build_module")).order_by(Action.id.desc()):
52 data = json.loads(action.data)
53 if not "ownername" in data:
54 continue
55 copr = get_copr(session, data["ownername"], data["projectname"])
56 yml_str = base64.b64decode(data["modulemd_b64"])
57 yml = yaml.safe_load(yml_str)
58
59 mmd = modulemd.ModuleMetadata()
60 mmd.name = yml["data"]["name"]
61 mmd.stream = ""
62 mmd.version = action.created_on
63 mmd.summary = yml["data"]["summary"]
64
65 if "filter" in yml["data"]["components"]["rpms"]:
66 for package in yml["data"]["components"]["rpms"]["filter"]:
67 mmd.filter.add_rpm(package)
68
69 for package in yml["data"]["components"]["rpms"]["api"]:
70 mmd.api.add_rpm(package)
71
72 for profile_name in yml["data"]["profiles"]:
73 mmd.profiles[profile_name] = modulemd.profile.ModuleProfile()
74 for package in yml["data"]["profiles"][profile_name]["rpms"]:
75 mmd.profiles[profile_name].add_rpm(package)
76
77 module_kwargs = {
78 "name": mmd.name,
79 "stream": mmd.stream,
80 "version": mmd.version,
81 "summary": mmd.summary,
82 "description": mmd.description,
83 "yaml_b64": base64.b64encode(mmd.dumps()),
84 "created_on": action.created_on,
85 "copr_id": copr.id,
86 }
87
88
89
90
91 if full_module_name(copr, mmd) in added_modules:
92 print("Skipping {}; Already exists".format(full_module_name(copr, mmd)))
93 continue
94 else:
95 print("Adding {}".format(full_module_name(copr, mmd)))
96
97 session.add(Module(**module_kwargs))
98 added_modules.add(full_module_name(copr, mmd))
99
100
102
103 op.drop_table('module')
104
105
106
111
112
113 -def get_copr(session, ownername, projectname):
119