-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_env.py
More file actions
393 lines (326 loc) · 15.3 KB
/
sync_env.py
File metadata and controls
393 lines (326 loc) · 15.3 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import os
import shutil
from pathlib import Path
import sys
from datetime import datetime
import hashlib
import json
def calculate_file_hash(file_path):
"""Calculate MD5 hash of a file to check content"""
hash_md5 = hashlib.md5()
try:
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
except Exception:
return None
def find_env_files_in_repo(repo_path):
"""Find all .env files in a repository"""
env_files = []
repo_path = Path(repo_path).resolve()
if not repo_path.exists():
return env_files
# Common folders to ignore
ignore_dirs = {'.git', 'node_modules', '__pycache__', '.venv', 'venv', 'env', '.idea', '.vscode', 'dist', 'build', 'target', 'logs'}
for root, dirs, files in os.walk(repo_path):
# Filter out ignored directories
dirs[:] = [d for d in dirs if d not in ignore_dirs]
for file in files:
# Look for .env files - more comprehensive pattern
# Matches: .env, .env.*, *.env
if (file == '.env' or
file.startswith('.env.') or
file.endswith('.env') or
'.env' in file and file.endswith('.env')):
full_path = Path(root) / file
rel_path = full_path.relative_to(repo_path)
env_files.append({
'path': str(full_path),
'rel_path': str(rel_path),
'name': file,
'hash': calculate_file_hash(full_path),
'size': full_path.stat().st_size if full_path.exists() else 0
})
return env_files
def find_env_files_in_backup(repo_backup_folder):
"""Find all .env backup files for a specific repository"""
backup_files = []
repo_backup_folder = Path(repo_backup_folder)
if not repo_backup_folder.exists():
return backup_files
for backup_file in repo_backup_folder.iterdir():
if backup_file.is_file():
filename = backup_file.name
# Include only backup files:
# 1. Exact .env file
# 2. Files ending with .env (config.env, production.env, etc.)
# 3. Safe backup files (env_*.txt)
if (filename == '.env' or
filename.endswith('.env') or
(filename.endswith('.txt') and filename.startswith('env_'))):
backup_files.append({
'path': str(backup_file),
'name': filename,
'hash': calculate_file_hash(backup_file),
'size': backup_file.stat().st_size if backup_file.exists() else 0,
'original_name': filename
})
return backup_files
def sync_repo_to_backup(repo_env_files, repo_backup_folder, repo_name):
"""Sync .env files from repository to backup folder"""
copied_files = []
repo_backup_path = Path(repo_backup_folder)
for repo_file in repo_env_files:
source_file_path = Path(repo_file['path'])
# Create backup filename - TWO COPIES for .env files:
# 1. A renamed version for safety
# 2. The original .env file name
if repo_file['name'] == '.env':
# For files without extension, create descriptive name
rel_path_str = repo_file['rel_path']
parent_dir = os.path.dirname(rel_path_str)
# Create safe backup name
if parent_dir and parent_dir != '.':
folder_name = parent_dir.replace(os.sep, '_')
safe_backup_name = f"env_{repo_name}_{folder_name}.txt"
else:
safe_backup_name = f"env_{repo_name}_root.txt"
# Also keep the original .env name
original_backup_name = ".env"
# Copy with safe name
safe_backup_path = repo_backup_path / safe_backup_name
shutil.copy2(source_file_path, safe_backup_path)
print(f" COPIED: {repo_file['rel_path']} -> {safe_backup_name} (safe copy)")
copied_files.append(('new', repo_file['rel_path'], safe_backup_name))
# Copy with original name if it doesn't exist or is different
original_backup_path = repo_backup_path / original_backup_name
# Check if original .env already exists in backup
existing_original = None
backup_files = find_env_files_in_backup(repo_backup_path)
for bf in backup_files:
if bf['name'] == original_backup_name:
existing_original = bf
break
if existing_original:
# Compare hashes
if repo_file['hash'] != existing_original['hash']:
shutil.copy2(source_file_path, original_backup_path)
print(f" UPDATED: {repo_file['rel_path']} -> {original_backup_name} (original name)")
copied_files.append(('updated', repo_file['rel_path'], original_backup_name))
else:
print(f" SYNCED: {repo_file['rel_path']} as {original_backup_name} (already synchronized)")
else:
shutil.copy2(source_file_path, original_backup_path)
print(f" COPIED: {repo_file['rel_path']} -> {original_backup_name} (original name)")
copied_files.append(('new', repo_file['rel_path'], original_backup_name))
else:
# For other .env files, use repo prefix
backup_name = f"{repo_name}_{repo_file['name']}"
backup_file_path = repo_backup_path / backup_name
# Check if file already exists in backup
existing_backup = None
backup_files = find_env_files_in_backup(repo_backup_path)
for bf in backup_files:
if bf['name'] == backup_name:
existing_backup = bf
break
if existing_backup:
# Compare hashes
if repo_file['hash'] != existing_backup['hash']:
shutil.copy2(source_file_path, backup_file_path)
print(f" UPDATED: {repo_file['rel_path']} -> {backup_name}")
copied_files.append(('updated', repo_file['rel_path'], backup_name))
else:
print(f" SYNCED: {repo_file['rel_path']} (already synchronized)")
else:
shutil.copy2(source_file_path, backup_file_path)
print(f" NEW: {repo_file['rel_path']} -> {backup_name}")
copied_files.append(('new', repo_file['rel_path'], backup_name))
return copied_files
def sync_backup_to_repo(backup_files, repo_env_files, repo_path, repo_name):
"""Sync .env files from backup folder to repository"""
copied_files = []
repo_path_obj = Path(repo_path)
for backup_file in backup_files:
backup_name = backup_file['name']
source_backup_path = Path(backup_file['path'])
# Skip the safe backup copies when restoring to repo
# We only want to restore files that look like original .env files
should_restore = False
if backup_name == ".env":
# This is an original .env file
should_restore = True
repo_dest_name = ".env"
elif backup_name.startswith(f"{repo_name}_") and backup_name.endswith('.env'):
# This is a prefixed .env file from this repo
should_restore = True
# Remove the repo prefix when restoring
repo_dest_name = backup_name[len(repo_name)+1:] # +1 for the underscore
elif not (backup_name.startswith("env_") and backup_name.endswith('.txt')):
# Not a safe backup copy, might be worth restoring
should_restore = True
repo_dest_name = backup_name
if should_restore:
repo_dest_path = repo_path_obj / repo_dest_name
# Check if file already exists in repo
existing_repo_file = None
for repo_file in repo_env_files:
if repo_file['name'] == repo_dest_name:
existing_repo_file = repo_file
break
if existing_repo_file:
# Compare hashes
if backup_file['hash'] != existing_repo_file['hash']:
# Create conflict version
conflict_time = datetime.now().strftime('%Y%m%d_%H%M%S')
conflict_name = f"{repo_dest_name}.conflict_{conflict_time}"
conflict_path = repo_path_obj / conflict_name
shutil.copy2(source_backup_path, conflict_path)
print(f" CONFLICT: {backup_name} differs from repo {repo_dest_name}")
print(f" Created: {conflict_name}")
copied_files.append(('conflict', backup_name, conflict_name))
else:
print(f" EXISTS: {repo_dest_name} already in repo (identical)")
else:
# File doesn't exist in repo - copy it
print(f" RESTORED: {backup_name} -> {repo_dest_name}")
shutil.copy2(source_backup_path, repo_dest_path)
copied_files.append(('restored', backup_name, repo_dest_name))
return copied_files
def sync_env_files(repos_folder, backup_folder, mode='two-way'):
"""
Synchronize .env files between GitHub repos and backup folder
Args:
repos_folder: Folder containing GitHub repositories
backup_folder: Central backup folder for .env files
mode: 'two-way', 'repo-to-backup', or 'backup-to-repo'
"""
repos_path = Path(repos_folder).resolve()
backup_path = Path(backup_folder).resolve()
# Create backup folder if it doesn't exist
backup_path.mkdir(parents=True, exist_ok=True)
# Validate folders
if not repos_path.exists():
print(f"ERROR: Repository folder '{repos_folder}' not found.")
return
print("=" * 80)
print("GITHUB REPOSITORY .env FILE SYNCHRONIZER")
print("=" * 80)
print(f"Repository folder: {repos_path}")
print(f"Backup folder: {backup_path}")
print(f"Mode: {mode}")
print("-" * 80)
# Find all repositories (non-hidden folders)
repos = []
for item in repos_path.iterdir():
if item.is_dir() and not item.name.startswith('.'):
repos.append(item)
print(f"Found {len(repos)} repositories\n")
# Create synchronization report
sync_report = {
'timestamp': datetime.now().isoformat(),
'repos_folder': str(repos_path),
'backup_folder': str(backup_path),
'mode': mode,
'repos_processed': 0,
'files_copied_to_backup': 0,
'files_copied_to_repos': 0,
'files_skipped': 0,
'conflicts': [],
'repositories': []
}
# Process each repository
for repo in repos:
repo_name = repo.name
print(f"\nRepository: {repo_name}")
print("-" * 40)
# Find .env files in repository
repo_env_files = find_env_files_in_repo(repo)
# Create backup folder for this repository
repo_backup_folder = backup_path / repo_name
repo_backup_folder.mkdir(exist_ok=True)
print(f"Found {len(repo_env_files)} .env files in repository")
repo_report = {
'name': repo_name,
'repo_path': str(repo),
'env_files_found': len(repo_env_files),
'actions': []
}
# Synchronize based on mode
if mode in ['two-way', 'repo-to-backup']:
# Sync from repo to backup
copied = sync_repo_to_backup(repo_env_files, repo_backup_folder, repo_name)
repo_report['actions'].extend([{
'type': 'repo_to_backup',
'files': copied
}])
sync_report['files_copied_to_backup'] += len(copied)
if mode in ['two-way', 'backup-to-repo']:
# Sync from backup to repo
backup_files = find_env_files_in_backup(repo_backup_folder)
print(f"Found {len(backup_files)} backup files")
copied = sync_backup_to_repo(backup_files, repo_env_files, repo, repo_name)
repo_report['actions'].extend([{
'type': 'backup_to_repo',
'files': copied
}])
# Count only restored files, not conflicts
restored_files = len([f for f in copied if f[0] == 'restored'])
sync_report['files_copied_to_repos'] += restored_files
# Add conflicts to report
conflicts = [f for f in copied if f[0] == 'conflict']
for conflict in conflicts:
sync_report['conflicts'].append({
'repository': repo_name,
'backup_file': conflict[1],
'conflict_file': conflict[2]
})
sync_report['repos_processed'] += 1
sync_report['repositories'].append(repo_report)
# Save detailed report
save_sync_report(sync_report, backup_path)
print("\n" + "=" * 80)
print("SYNCHRONIZATION COMPLETE")
print("-" * 80)
print(f"Repositories processed: {sync_report['repos_processed']}")
print(f"Files copied to backup: {sync_report['files_copied_to_backup']}")
print(f"Files copied to repositories: {sync_report['files_copied_to_repos']}")
print(f"Conflicts detected: {len(sync_report['conflicts'])}")
print(f"Report saved to: {backup_path / 'sync_report.json'}")
print("=" * 80)
return sync_report
def save_sync_report(report, backup_path):
"""Save synchronization report to JSON file"""
report_file = backup_path / "sync_report.json"
with open(report_file, 'w', encoding='utf-8') as f:
json.dump(report, f, indent=2, ensure_ascii=False)
def main():
# Command line arguments
if len(sys.argv) < 2:
print("Usage: python sync_env.py <repos_folder> [backup_folder] [mode]")
print("\nModes:")
print(" two-way : Synchronize both directions (default)")
print(" repo-to-backup : Only copy from repos to backup")
print(" backup-to-repo : Only copy from backup to repos")
print("\nExamples:")
print(" python sync_env.py C:\\path\\to\\repos")
print(" python sync_env.py C:\\path\\to\\repos C:\\backup\\env two-way")
return
repos_folder = sys.argv[1]
if len(sys.argv) > 2:
backup_folder = sys.argv[2]
else:
# Default backup folder in user's home directory
backup_folder = Path.home() / ".env_backups"
if len(sys.argv) > 3:
mode = sys.argv[3]
if mode not in ['two-way', 'repo-to-backup', 'backup-to-repo']:
print(f"Warning: Unknown mode '{mode}', using 'two-way'")
mode = 'two-way'
else:
mode = 'two-way'
# Run synchronization
sync_env_files(repos_folder, backup_folder, mode)
if __name__ == "__main__":
main()