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
|
# yt-local Makefile
# Automated tasks for development, translations, and maintenance
.PHONY: help install dev clean test i18n-extract i18n-init i18n-update \
i18n-compile i18n-stats i18n-clean setup-dev lint format backup \
restore distclean info check-deps run test-cov i18n-workflow \
ensure-venv docker docker-run docker-stop docker-logs docker-clean
# Variables
SYSTEM_PYTHON := python3
PIP_COMPILE ?= pip-compile
PIP_SYNC ?= pip-sync
LANG_CODE ?= es
PROJECT_NAME := yt-local
# Prefer venv python, fall back to system
VENV_PYTHON := venv/bin/python
PYTHON = $(if $(wildcard $(VENV_PYTHON)),$(VENV_PYTHON),$(SYSTEM_PYTHON))
# Patterns for release artefacts (generate_release.py)
RELEASE_DIR := yt-local
RELEASE_GLOBS := yt-local-*.zip python-dist-*.zip
RELEASE_DOWNLOADS := get-pip.py vc15_*.7z
PYTHON_DIST_DIR := python
# Validate LANG_CODE: only allow letters and underscore, 2-5 chars (e.g. es, pt_BR)
# Guards against shell injection via make i18n-init LANG_CODE="$(malicious)"
LANG_CODE_SAFE := $(shell echo '$(LANG_CODE)' | grep -xE '[a-zA-Z_]{2,5}' || echo '')
## Help -----------------------------------------------------------------------
help: ## Show this help message
@echo "$(PROJECT_NAME) - Available tasks:"
@echo ""
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | \
awk 'BEGIN {FS = ":.*?## "}; {printf " %-20s %s\n", $$1, $$2}'
@echo ""
@echo "Examples:"
@echo " make install # Install dependencies"
@echo " make dev # Run development server"
@echo " make i18n-extract # Extract strings for translation"
@echo " make i18n-init LANG_CODE=fr # Initialize French"
@echo " make lint # Check code style"
## Venv bootstrap (internal) --------------------------------------------------
PIPTOOLS_COMPILE = $(VENV_PYTHON) -m piptools compile --generate-hashes
ensure-venv:
@if [ ! -f "$(VENV_PYTHON)" ]; then \
echo "[INFO] Creating virtual environment..."; \
$(SYSTEM_PYTHON) -m venv venv; \
echo "[INFO] Installing pip-tools..."; \
$(VENV_PYTHON) -m pip install --upgrade pip setuptools wheel pip-tools; \
fi
## Lock files -----------------------------------------------------------------
lock: ensure-venv ## Compile pinned lock files from pyproject.toml
@echo "[INFO] Compiling requirements.lock..."
$(PIPTOOLS_COMPILE) pyproject.toml -o requirements.lock
@echo "[INFO] Compiling requirements-dev.lock..."
$(PIPTOOLS_COMPILE) --extra dev pyproject.toml -o requirements-dev.lock
@echo "[SUCCESS] Lock files updated"
## Installation and Setup -----------------------------------------------------
install: ensure-venv lock ## Install/update project dependencies
@echo "[INFO] Installing dependencies..."
$(VENV_PYTHON) -m piptools sync requirements.lock
@echo "[SUCCESS] Dependencies installed"
setup-dev: ensure-venv lock ## Install dev dependencies (tests, linting)
@echo "[INFO] Installing dev dependencies..."
$(VENV_PYTHON) -m piptools sync requirements-dev.lock
@echo "[SUCCESS] Dev dependencies installed"
## Development ----------------------------------------------------------------
dev: ensure-venv ## Run development server
@echo "[INFO] Starting development server..."
@echo "[INFO] Server available at: http://localhost:9010"
$(PYTHON) server.py
run: dev ## Alias for dev
## Testing --------------------------------------------------------------------
test: ensure-venv ## Run tests
@echo "[INFO] Running tests..."
@if [ -d "tests" ]; then \
$(PYTHON) -m pytest -v; \
else \
echo "[WARN] No tests directory found"; \
fi
test-cov: ensure-venv ## Run tests with coverage
@echo "[INFO] Running tests with coverage..."
@$(PYTHON) -c "import pytest_cov" 2>/dev/null && \
$(PYTHON) -m pytest -v --cov=youtube --cov-report=html || \
echo "[WARN] pytest-cov not installed. Run: make setup-dev"
## Internationalization (i18n) ------------------------------------------------
i18n-extract: ensure-venv ## Extract strings for translation
@echo "[INFO] Extracting strings for translation..."
$(PYTHON) manage_translations.py extract
@echo "[SUCCESS] Strings extracted to translations/messages.pot"
i18n-init: ensure-venv ## Initialize new language (use LANG_CODE=xx)
@if [ -z "$(LANG_CODE_SAFE)" ]; then \
echo "[ERROR] Invalid LANG_CODE='$(LANG_CODE)'. Use 2-5 letters (e.g. es, pt_BR)."; \
exit 1; \
fi
@echo "[INFO] Initializing language: $(LANG_CODE_SAFE)"
$(PYTHON) manage_translations.py init $(LANG_CODE_SAFE)
@echo "[SUCCESS] Language $(LANG_CODE_SAFE) initialized"
@echo "[INFO] Edit: translations/$(LANG_CODE_SAFE)/LC_MESSAGES/messages.po"
i18n-update: ensure-venv ## Update existing translations
@echo "[INFO] Updating existing translations..."
$(PYTHON) manage_translations.py update
@echo "[SUCCESS] Translations updated"
i18n-compile: ensure-venv ## Compile translations to binary .mo files
@echo "[INFO] Compiling translations..."
$(PYTHON) manage_translations.py compile
@echo "[SUCCESS] Translations compiled"
i18n-stats: ## Show translation statistics
@echo "[INFO] Translation statistics:"
@echo ""
@for lang_dir in translations/*/; do \
if [ -d "$$lang_dir" ] && [ "$$lang_dir" != "translations/*/" ]; then \
lang=$$(basename "$$lang_dir"); \
po_file="$$lang_dir/LC_MESSAGES/messages.po"; \
if [ -f "$$po_file" ]; then \
total=$$(grep -c "^msgid " "$$po_file" 2>/dev/null || echo "0"); \
translated=$$(grep -c '^msgstr "[^"]' "$$po_file" 2>/dev/null || echo "0"); \
fuzzy=$$(grep -c "^#, fuzzy" "$$po_file" 2>/dev/null || echo "0"); \
if [ "$$total" -gt 0 ]; then \
percent=$$((translated * 100 / total)); \
echo " [STAT] $$lang: $$translated/$$total ($$percent%) - Fuzzy: $$fuzzy"; \
else \
echo " [STAT] $$lang: No translations yet"; \
fi; \
fi; \
fi; \
done
@echo ""
i18n-clean: ## Clean compiled translation files (.mo only)
@echo "[INFO] Cleaning compiled .mo files..."
find translations/ -name "*.mo" -delete 2>/dev/null || true
@echo "[SUCCESS] .mo files removed"
i18n-workflow: i18n-extract i18n-update i18n-compile i18n-stats ## Complete workflow: extract → update → compile
@echo "[SUCCESS] Translation workflow completed"
## Code Quality ---------------------------------------------------------------
lint: ensure-venv ## Check code with flake8
@echo "[INFO] Checking code style..."
@$(PYTHON) -c "import flake8" 2>/dev/null && \
$(PYTHON) -m flake8 youtube/ --max-line-length=120 --ignore=E501,W503,E402 \
--exclude=youtube/ytdlp_service.py,youtube/ytdlp_integration.py,youtube/ytdlp_proxy.py && \
echo "[SUCCESS] Code style check passed" || \
echo "[WARN] flake8 not installed. Run: make setup-dev"
format: ensure-venv ## Format code with black (if available)
@echo "[INFO] Formatting code..."
@$(PYTHON) -c "import black" 2>/dev/null && \
$(PYTHON) -m black youtube/ --line-length=120 --exclude='ytdlp_.*\.py' && \
echo "[SUCCESS] Code formatted" || \
echo "[WARN] black not installed. Run: make setup-dev"
check-deps: ensure-venv ## Check installed dependencies
@echo "[INFO] Checking dependencies..."
@$(PYTHON) -c "import flask_babel; print('[OK] Flask-Babel:', flask_babel.__version__)" 2>/dev/null || echo "[ERROR] Flask-Babel not installed"
@$(PYTHON) -c "import flask; print('[OK] Flask:', flask.__version__)" 2>/dev/null || echo "[ERROR] Flask not installed"
@$(PYTHON) -c "import yt_dlp; print('[OK] yt-dlp:', yt_dlp.__version__)" 2>/dev/null || echo "[ERROR] yt-dlp not installed"
## Maintenance ----------------------------------------------------------------
backup: ## Create translations backup
@echo "[INFO] Creating translations backup..."
@timestamp=$$(date +%Y%m%d_%H%M%S); \
tar -czf "translations_backup_$$timestamp.tar.gz" translations/ 2>/dev/null || echo "[WARN] No translations to backup"; \
if [ -f "translations_backup_$$timestamp.tar.gz" ]; then \
echo "[SUCCESS] Backup created: translations_backup_$$timestamp.tar.gz"; \
fi
restore: ## Restore translations from latest backup
@echo "[INFO] Restoring translations from backup..."
@latest_backup=$$(find . -maxdepth 1 -name 'translations_backup_*.tar.gz' -print0 | \
xargs -0 ls -t 2>/dev/null | head -1); \
if [ -n "$$latest_backup" ]; then \
tar -xzf "$$latest_backup"; \
echo "[SUCCESS] Restored from: $$latest_backup"; \
else \
echo "[ERROR] No backup files found"; \
fi
## Docker ---------------------------------------------------------------------
docker: ## Build Docker image
@echo "[INFO] Building Docker image..."
docker compose build
@echo "[SUCCESS] Image built"
docker-run: ## Start container (use ENABLE_TOR=1 for Tor)
@echo "[INFO] Starting container..."
ENABLE_TOR=$(or $(ENABLE_TOR),0) docker compose up -d
@echo "[SUCCESS] Container running at http://localhost:9010"
docker-stop: ## Stop container
@echo "[INFO] Stopping container..."
docker compose down
@echo "[SUCCESS] Container stopped"
docker-logs: ## Show container logs
docker compose logs -f
docker-clean: docker-stop ## Remove container, image, and volume
@echo "[INFO] Removing Docker artefacts..."
docker compose down -v --rmi local
@echo "[SUCCESS] Docker artefacts removed"
## Cleanup --------------------------------------------------------------------
clean: ## Clean temporary files, caches, and release artefacts
@echo "[INFO] Cleaning temporary files..."
# Python byte-code and caches
find . -type f -name "*.pyc" -delete
find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
# Compiled translation files (project-wide)
find . -type f -name "*.mo" -delete
# Pytest cache and coverage data
find . -type d -name ".pytest_cache" -exec rm -rf {} + 2>/dev/null || true
find . -type d -name "htmlcov" -exec rm -rf {} + 2>/dev/null || true
find . -type f -name ".coverage" -delete
# Misc temporary / backup artefacts
find . -type f \( -name "*.tmp" -o -name "*.bak" -o -name "*.log" \) -delete
find . -type d -name "*.cache" -exec rm -rf {} + 2>/dev/null || true
# Checksum files
find . -type f \( -name "*.sha512sum" -o -name "*.sha256sum" -o -name "*.sha1sum" \
-o -name "*.md5sum" -o -name "*.b2sum" \) -delete
# Release artefacts (generate_release.py)
rm -rf $(RELEASE_DIR)/ $(PYTHON_DIST_DIR)/
rm -f $(RELEASE_GLOBS) $(RELEASE_DOWNLOADS)
@echo "[SUCCESS] Temporary files removed"
distclean: clean ## Clean everything including venv
@echo "[INFO] Cleaning everything..."
rm -rf venv requirements.lock requirements-dev.lock
@echo "[SUCCESS] Complete cleanup done"
## Project Information --------------------------------------------------------
info: ensure-venv ## Show project information
@echo "[INFO] $(PROJECT_NAME) - Project information:"
@echo ""
@echo " [INFO] Directory: $$(pwd)"
@echo " [INFO] Python: $$($(PYTHON) --version 2>&1)"
@echo " [INFO] pip: $$($(PYTHON) -m pip --version 2>&1)"
@echo ""
@echo " [INFO] Configured languages:"
@for lang_dir in translations/*/; do \
if [ -d "$$lang_dir" ] && [ "$$lang_dir" != "translations/*/" ]; then \
echo " - $$(basename $$lang_dir)"; \
fi; \
done
@echo ""
@echo " [INFO] Main files:"
@echo " - babel.cfg (i18n configuration)"
@echo " - manage_translations.py (i18n CLI)"
@echo " - youtube/i18n_strings.py (centralized strings)"
@echo " - youtube/ytdlp_service.py (yt-dlp integration)"
@echo ""
# Default target
.DEFAULT_GOAL := help
|