File size: 6,794 Bytes
b7a7f32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import os
import sys
from time import sleep
import click

# RCOUNT: 145


class CommandDefinition:
    def generator(self, name, det):  # noqa
        from utils import generator

        if det == "all":
            if not name:
                print("Enter at least one name!")
            for item in name:
                generator.create_model(item)
                generator.create_endpoint(item)
                generator.create_crud(item)
                generator.create_schema(item)

        elif det == "model":
            if not name:
                print("Enter at least one model name!")
            for item in name:
                generator.create_model(item)

        elif det == "schema":
            if not name:
                print("Enter at least one schema name!")
            for item in name:
                generator.create_schema(item)

        elif det == "endpoint":
            if not name:
                print("Enter at least one endpoint name!")
            for item in name:
                generator.create_endpoint(item)

        elif det == "crud":
            if not name:
                print("Enter at least one CRUD name!")
            for item in name:
                generator.create_crud(item)

    def start(self):
        from misc.scripts import launch

        launch.run()

    def mkmig(self):
        from alembic import command
        from alembic.config import Config

        alembic_cfg = Config("alembic.ini")

        msg = input("Enter a message: ")
        command.revision(config=alembic_cfg, autogenerate=True, message=msg)
        click.echo("Inside migrate")

    def mig(self):
        from alembic import command
        from alembic.config import Config

        alembic_cfg = Config("alembic.ini")
        command.upgrade(alembic_cfg, "head")

    def cleanmig(self):
        for file in os.listdir("migrations/versions/"):
            if file != ".keep":
                if os.path.isfile(f"migrations/versions/{file}"):
                    os.remove(f"migrations/versions/{file}")

    def cleanredis(self):
        from core.config import settings

        os.system(
            f"docker-compose exec redis redis-cli -a {settings.SECRET_KEY} FLUSHALL"
        )

    def logs(self):
        os.system(f"docker-compose logs -f -t")

    def remake(self):
        self.cleandb()
        self.populate()

    def remakeall(self):
        from alembic import command
        from alembic.config import Config

        try:
            os.system(f"docker-compose down -v -t 5")
            os.system(
                f"cd .. && docker-compose up -d postgres redis pgadmin mailhog file_server"
            )
        except Exception as e:
            print(e)

        self.cleanmig()

        alembic_cfg = Config("alembic.ini")

        rev_created = False

        while True:
            try:
                if not rev_created:
                    command.revision(
                        config=alembic_cfg, autogenerate=True, message="Remake All"
                    )
                    rev_created = True

                command.upgrade(alembic_cfg, "head")
                break
            except Exception as e:
                print(e)
                print("Waiting for containers to boot!")
                sleep(3)

        try:
            self.populate()
        except Exception as e:
            print(e)

    def dcstart(self):
        os.system(
            "cd .. && docker-compose up -d postgres file_server redis pgadmin mailhog"
        )

    def cleandb(self):
        try:
            from alembic import command
            from alembic.config import Config

            from core.db import engine

            self.cleanmig()
            engine.execute("DROP schema public CASCADE")
            engine.execute("CREATE schema public")
            alembic_cfg = Config("alembic.ini")
            command.revision(config=alembic_cfg, autogenerate=True, message="cleandb")
            command.upgrade(alembic_cfg, "head")
        except Exception as e:
            print(e)

    def populate(self):
        from utils import populate as db_populate

        sleep(5)
        db_populate.populate_all()

    def pytest(self):
        ec = int(
            os.system(
                "pytest --verbose --color=yes tests/api/api_v1/")
            / 256
        )
        sys.exit(ec)

    def build_push(self):
        ec = int(
            os.system(
                "docker build -t registry.gitlab.com/arpandaze/sikshyalaya/backend . && docker push registry.gitlab.com/arpandaze/sikshyalaya/backend")
            / 256
        )
        sys.exit(ec)


commands = CommandDefinition()


@click.group()
def main():
    pass


@click.group("create")
@click.pass_context
def create(context):
    if not context.invoked_subcommand:
        pass


@click.group("clean")
@click.pass_context
def clean(context):
    if not context.invoked_subcommand:
        pass


@click.command()
@click.argument("name", nargs=-1)
def all(name):  # noqa
    commands.generator(name, "all")


@click.command()
@click.argument("name", nargs=-1)
def model(name):
    commands.generator(name, "model")


@click.command()
@click.argument("name", nargs=-1)
def schema(name):
    commands.generator(name, "schema")


@click.command()
@click.argument("name", nargs=-1)
def endpoint(name):
    commands.generator(name, "endpoint")


@click.command()
@click.argument("name", nargs=-1)
def crud(name):
    commands.generator(name, "crud")


@click.command()
def start():
    commands.start()


@click.command()
def dcstart():
    commands.dcstart()


@click.command()
def pytest():
    commands.pytest()

@click.command()
def build_push():
    commands.build_push()


@click.command()
def mkmig():
    commands.mkmig()


@click.command()
def mig():
    commands.mig()


@click.command(name="mig")
def clean_mig():
    commands.cleanmig()


@click.command()
def redis():
    commands.cleanredis()


@click.command()
def logs():
    commands.logs()


@click.command()
def remakeall():
    commands.remakeall()


@click.command()
def remake():
    commands.remake()


@click.command()
def db():
    commands.cleandb()


@click.command()
def populate():
    commands.populate()


main.add_command(create)
main.add_command(clean)
main.add_command(start)
main.add_command(mkmig)
main.add_command(mig)
main.add_command(populate)
main.add_command(logs)
main.add_command(remakeall)
main.add_command(remake)
main.add_command(dcstart)
main.add_command(pytest)
main.add_command(build_push)
clean.add_command(db)
clean.add_command(clean_mig)
clean.add_command(redis)
create.add_command(model)
create.add_command(schema)
create.add_command(crud)
create.add_command(endpoint)
create.add_command(all)

if __name__ == "__main__":
    main()