#631 [frontend] linter for alembic revisions
Merged 4 years ago by msuchy. Opened 4 years ago by praiskup.

@@ -0,0 +1,71 @@

+ #! /usr/bin/python3

+ 

+ import os

+ import re

+ import sys

+ import glob

+ from runpy import run_path

+ 

+ script_dir = os.path.dirname(__file__)

+ 

+ sys.path.append(os.path.join(script_dir, '..', 'coprs_frontend'))

+ 

+ version_files  = os.path.join(

+     script_dir,

+     '..',

+     'coprs_frontend',

+     'alembic',

+     'schema',

+     'versions',

+     '*.py'

+ )

+ 

+ 

+ rev_map = {}

+ rev_db = {}

+ initial_revision = None

+ 

+ for py_file in glob.glob(version_files):

+     pattern = "^\s*{}\s*=\s*['\"](.*)['\"]\s*$"

+     revision = None

+     down_revision = None

+     for line in open(py_file):

+         match = re.match(pattern.format("revision"), line)

+         if match:

+             revision = match[1]

+         match = re.match(pattern.format("down_revision"), line)

+         if match:

+             down_revision = match[1]

+ 

+     if not down_revision:

+         initial_revision = revision

+     else:

+         rev_map[down_revision] = {

+             'next': revision,

+         }

+     rev_db[revision] = {

+         'revision': revision,

+         'file': py_file,

+     }

+ 

+ def print_revision(revision):

+     print(os.path.basename(revision['file']))

+ 

+ printed_rev = initial_revision

+ while True:

+     revision = rev_db[printed_rev]

+     revision['printed'] = True # mark as printed, for later linting

+     print_revision(revision)

+     if not printed_rev in rev_map:

+         break

+     printed_rev = rev_map[printed_rev]['next']

+ 

+ 

+ fail = False

+ for rev in rev_db:

+     if 'printed' not in rev_db[rev]:

+         sys.stderr.write("WARNING: dangling revision '{}'\n".format(rev))

+         fail = True

+ 

+ if fail:

+     sys.exit(1)

This is WIP, or more like a question whether you want this to live in copr.git.

I'll use this script to check that our alembic revisions are in order in future, and in future we could hook this into our future "linting CI" (together with pylint, etc.).

rebased onto e9de8437fb6996cc8169b58ac1abf140bc2afc7c

4 years ago

I am fine with this one. I would send it to Alembic upstream as this can be useful for the too.

rebased onto 84421838a7221fb87abd94d50f07316a30ae9a09

4 years ago

Metadata Update from @praiskup:
- Pull-request tagged with: needs-work

4 years ago

rebased onto 75da31cf606b1db2b19e6ea22938acc1930c7ed1

4 years ago

Moved to ./frontend/build_aux.

Metadata Update from @praiskup:
- Pull-request untagged with: needs-work

4 years ago

Metadata Update from @praiskup:
- Pull-request tagged with: review

4 years ago

rebased onto 048cf12

4 years ago

Pull-Request has been merged by msuchy

4 years ago
Metadata