-
Notifications
You must be signed in to change notification settings - Fork 0
80 lines (69 loc) · 2.48 KB
/
Copy pathupdate-reqs.yml
File metadata and controls
80 lines (69 loc) · 2.48 KB
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
name: Update requirements.txt
on:
push:
branches:
- main
jobs:
update-reqs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Backup current requirements
run: |
if [ -f requirements.txt ]; then
mv requirements.txt requirements.old
else
touch requirements.old
fi
- name: Install pipreqs
run: pip install pipreqs
- name: Generate new requirements
run: pipreqs . --force --ignore .venv --mode gt
- name: Restore old versions if they exist
shell: python
run: |
import os
def parse_reqs(filename):
data = {}
if not os.path.exists(filename):
return data
with open(filename, 'r') as f:
for line in f:
line = line.strip()
if not line or line.startswith('#'): continue
parts = line.split('>=') if '>=' in line else line.split('==')
if len(parts) > 1:
data[parts[0].lower()] = line
return data
old_versions = parse_reqs('requirements.old')
new_lines = []
if os.path.exists('requirements.txt'):
with open('requirements.txt', 'r') as f:
for line in f:
line = line.strip()
if not line: continue
pkg_name = line.split('>=')[0].split('==')[0].strip().lower()
if pkg_name in old_versions:
new_lines.append(old_versions[pkg_name])
else:
new_lines.append(line)
with open('requirements.txt', 'w') as f:
f.write('\n'.join(new_lines) + '\n')
if os.path.exists('requirements.old'):
os.remove('requirements.old')
- name: Commit and push if changed
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add requirements.txt
if git diff --cached --quiet; then
echo "No changes in requirements.txt"
else
git commit -m "Updated requirements.txt"
git push
fi