Skip to content

Commit 68c1822

Browse files
committed
feat: add Flake8 linting standard to Python scripts (closes #117)
Add .flake8 config (max-line-length=120), fix all PEP8 violations in bin/config_build.py and bin/package_build.py, and add GitHub Actions CI workflow to enforce linting on future PRs. Signed-off-by: vmuralictr <vmurali.ctr@gmail.com>
1 parent a2455de commit 68c1822

4 files changed

Lines changed: 200 additions & 132 deletions

File tree

.flake8

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[flake8]
2+
max-line-length = 120
3+
exclude =
4+
config/supported_distros.py,
5+
distro_data/,
6+
react-frontend/,
7+
.claude/,
8+
__pycache__,
9+
*.pyc
10+
per-file-ignores =
11+
bin/config_build.py:E501

.github/workflows/python-lint.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Python Lint (Flake8)
2+
3+
on:
4+
push:
5+
paths:
6+
- 'bin/**.py'
7+
- '.flake8'
8+
- '.github/workflows/python-lint.yml'
9+
pull_request:
10+
paths:
11+
- 'bin/**.py'
12+
- '.flake8'
13+
- '.github/workflows/python-lint.yml'
14+
15+
jobs:
16+
flake8:
17+
name: Flake8
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v4
21+
22+
- name: Set up Python
23+
uses: actions/setup-python@v5
24+
with:
25+
python-version: '3.x'
26+
27+
- name: Install Flake8
28+
run: pip install flake8 flake8-bugbear
29+
30+
- name: Run Flake8
31+
run: flake8 bin/

bin/config_build.py

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,78 +11,84 @@
1111
RHEL_reg = r'(x?RHEL)_(\d{1,2})_(\d{1,2}).*\.json'
1212
Ubuntu_reg = r'(x?Ubuntu)_(\d{1,2})_(\d{1,2}).*\.json'
1313
regexes = [
14-
re.compile(SLES_reg),
15-
re.compile(RHEL_reg),
16-
re.compile(Ubuntu_reg)
14+
re.compile(SLES_reg),
15+
re.compile(RHEL_reg),
16+
re.compile(Ubuntu_reg)
1717
]
1818

19+
1920
def scan():
20-
return os.listdir(DATA_FILE_LOCATION)
21+
return os.listdir(DATA_FILE_LOCATION)
22+
2123

2224
def packagetype(file):
23-
global SLES_reg, RHEL_reg, Ubuntu_reg, regexes
24-
for ind,reg in enumerate(regexes):
25-
if reg.match(file):
26-
addfile(file, ind)
25+
for ind, reg in enumerate(regexes):
26+
if reg.match(file):
27+
addfile(file, ind)
28+
2729

2830
def addfile(file, ind):
29-
global SLES_reg, RHEL_reg, Ubuntu_reg, regexes
30-
flag = False
31-
with open(SUPPORTED_DISTRO_FILE) as DATA:
32-
data = DATA.read()
33-
groups = regexes[ind].search(file)
34-
name = re.sub(r'^x', '', groups.group(1).replace('_', ' '))
35-
if not ind == 0:
36-
if name in data and not file in data:
37-
print(f"Found new file: {file}")
38-
start = data.index(name) + len(name)+7
39-
new_data = f"\t'{groups.group(1)} {groups.group(2)}.{groups.group(3)}': '{file}',\n"
40-
flag = True
41-
else:
42-
if name in data and not file in data:
43-
print(f"Found new file: {file}")
44-
start = data.index(name) + len(name)+7
45-
new_data = f"\t'SLES {groups.group(4)} {'' if groups.group(6)==None else groups.group(6)}': '{file}',\n"
46-
flag = True
47-
48-
if flag:
49-
with open(SUPPORTED_DISTRO_FILE, 'r+') as DATA:
50-
DATA.seek(start)
51-
next_data = DATA.read()
52-
DATA.seek(start)
53-
DATA.write(new_data)
54-
DATA.write(next_data)
31+
flag = False
32+
with open(SUPPORTED_DISTRO_FILE) as DATA:
33+
data = DATA.read()
34+
groups = regexes[ind].search(file)
35+
name = re.sub(r'^x', '', groups.group(1).replace('_', ' '))
36+
if ind != 0:
37+
if name in data and file not in data:
38+
print(f"Found new file: {file}")
39+
start = data.index(name) + len(name) + 7
40+
new_data = f"\t'{groups.group(1)} {groups.group(2)}.{groups.group(3)}': '{file}',\n"
41+
flag = True
42+
else:
43+
if name in data and file not in data:
44+
print(f"Found new file: {file}")
45+
start = data.index(name) + len(name) + 7
46+
new_data = (
47+
f"\t'SLES {groups.group(4)} "
48+
f"{'': if groups.group(6) is None else groups.group(6)}': '{file}',\n"
49+
)
50+
flag = True
5551

56-
def format_data():
57-
duplicate = []
52+
if flag:
5853
with open(SUPPORTED_DISTRO_FILE, 'r+') as DATA:
59-
old_format = DATA.read()
60-
DATA.truncate(0)
61-
new_format = re.sub(r"(json',\n})", "json'\n}", old_format)
62-
DATA.seek(0)
63-
DATA.write(new_format)
54+
DATA.seek(start)
55+
next_data = DATA.read()
56+
DATA.seek(start)
57+
DATA.write(new_data)
58+
DATA.write(next_data)
59+
60+
61+
def format_data():
62+
with open(SUPPORTED_DISTRO_FILE, 'r+') as DATA:
63+
old_format = DATA.read()
64+
DATA.truncate(0)
65+
new_format = re.sub(r"(json',\n})", "json'\n}", old_format)
66+
DATA.seek(0)
67+
DATA.write(new_format)
68+
6469

6570
def del_cache():
66-
try:
67-
print("Attempting to delete cached_data.json...")
68-
os.remove(f'{DATA_FILE_LOCATION}/cached_data.json')
69-
except:
70-
print("File not found in directory.")
71+
try:
72+
print("Attempting to delete cached_data.json...")
73+
os.remove(f'{DATA_FILE_LOCATION}/cached_data.json')
74+
except OSError:
75+
print("File not found in directory.")
76+
7177

7278
def pull_new(files):
73-
global regexes
74-
print("Attempting to update PDS data sources...")
75-
for file in files:
76-
if any(regex.match(file) for regex in regexes):
77-
print(f'Updating {file}...')
78-
subprocess.run([f'{SDT_BASE}/bin/package_build.py', file], stdout=subprocess.DEVNULL)
79+
print("Attempting to update PDS data sources...")
80+
for file in files:
81+
if any(regex.match(file) for regex in regexes):
82+
print(f'Updating {file}...')
83+
subprocess.run([f'{SDT_BASE}/bin/package_build.py', file], stdout=subprocess.DEVNULL)
84+
7985

8086
if __name__ == "__main__":
81-
print("Scanning distro_data directory...")
82-
files = scan()
83-
for file in files:
84-
packagetype(file)
85-
format_data()
86-
pull_new(files)
87-
del_cache()
88-
print('Done.')
87+
print("Scanning distro_data directory...")
88+
files = scan()
89+
for file in files:
90+
packagetype(file)
91+
format_data()
92+
pull_new(files)
93+
del_cache()
94+
print('Done.')

0 commit comments

Comments
 (0)