-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage_dependencies.py
More file actions
501 lines (399 loc) · 14.7 KB
/
Copy pathmanage_dependencies.py
File metadata and controls
501 lines (399 loc) · 14.7 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
"""
Manage GOG game dependencies separately from game files.
This script handles downloading and managing redistributables (MSVC, DirectX, etc.)
in a separate location for archival purposes.
"""
import argparse
import json
import logging
import os
import sys
from pathlib import Path
# Add parent directory to path for imports
sys.path.insert(0, str(Path(__file__).parent.parent))
from galaxy_dl.api import GalaxyAPI
from galaxy_dl.auth import AuthManager
from galaxy_dl.dependencies import DependencyManager
# Global symbol variables (set by setup_symbols)
SYMBOL_CHECK = '[OK]'
SYMBOL_ERROR = '[ERROR]'
SYMBOL_INSTALLED = 'X'
SYMBOL_NOT_INSTALLED = ' '
def detect_unicode_support(force_ascii=False):
"""
Detect if the terminal supports Unicode output.
Args:
force_ascii: If True, force ASCII mode regardless of terminal support
Returns:
True if Unicode is supported, False otherwise
"""
# Check for explicit force ASCII flag
if force_ascii:
return False
# Check for environment variable to force ASCII mode
if os.environ.get('FORCE_ASCII', '').lower() in ('1', 'true', 'yes'):
return False
# Check if stdout encoding supports Unicode
try:
encoding = sys.stdout.encoding or ''
if encoding.lower() in ('utf-8', 'utf8'):
return True
# Try to encode a Unicode character
'✓'.encode(encoding)
return True
except (UnicodeEncodeError, AttributeError, LookupError):
return False
def setup_symbols(force_ascii=False):
"""
Set up symbol variables based on Unicode support.
Args:
force_ascii: If True, force ASCII mode
"""
global SYMBOL_CHECK, SYMBOL_ERROR, SYMBOL_INSTALLED, SYMBOL_NOT_INSTALLED
use_unicode = detect_unicode_support(force_ascii)
if use_unicode:
SYMBOL_CHECK = '✓'
SYMBOL_ERROR = '✗'
SYMBOL_INSTALLED = '✓'
SYMBOL_NOT_INSTALLED = ' '
else:
SYMBOL_CHECK = '[OK]'
SYMBOL_ERROR = '[ERROR]'
SYMBOL_INSTALLED = 'X'
SYMBOL_NOT_INSTALLED = ' '
def setup_logging(verbose: bool = False):
"""Configure logging."""
level = logging.DEBUG if verbose else logging.INFO
logging.basicConfig(
level=level,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
def cmd_init(args):
"""Initialize dependency system."""
# Setup authentication
auth_manager = AuthManager()
if not auth_manager.is_authenticated():
print("Error: Not authenticated. Run 'galaxy-dl login' first.")
return 1
api = GalaxyAPI(auth_manager)
# Initialize dependency manager
dep_manager = DependencyManager(api, base_path=args.path)
if dep_manager.initialize():
print(f"{SYMBOL_CHECK} Dependency system initialized in: {dep_manager.base_path}")
print(f"{SYMBOL_CHECK} Repository metadata saved")
print(f"{SYMBOL_CHECK} {len(dep_manager.repository.dependencies)} dependencies available")
return 0
else:
print(f"{SYMBOL_ERROR} Failed to initialize dependency system")
return 1
def cmd_list_game(args):
"""List dependencies for a specific game."""
# Setup authentication
auth_manager = AuthManager()
if not auth_manager.is_authenticated():
print("Error: Not authenticated. Run 'galaxy-dl login' first.")
return 1
api = GalaxyAPI(auth_manager)
# Initialize dependency manager
dep_manager = DependencyManager(api, base_path=args.path)
if not dep_manager.initialize():
print(f"{SYMBOL_ERROR} Failed to initialize dependency system")
return 1
# Load depot/repository file to get dependencies
file_path = Path(args.file_path)
if not file_path.exists():
print(f"Error: File not found: {file_path}")
return 1
with open(file_path, 'r') as f:
try:
data = json.load(f)
except:
# Try decompressing first
import zlib
f.seek(0)
content = f.read()
if isinstance(content, str):
content = content.encode()
decompressed = zlib.decompress(content)
data = json.loads(decompressed)
# Detect version and extract dependencies
version = data.get("version")
dependency_ids = []
if version == 1:
# V1: Extract from redist objects in product.depots array
print("Detected V1 format (repository.json)")
product = data.get("product", {})
depots = product.get("depots", [])
for depot in depots:
if "redist" in depot:
dependency_ids.append(depot["redist"])
elif version == 2:
# V2: Extract from dependencies array
print("Detected V2 format (depot.json)")
dependency_ids = data.get("dependencies", [])
else:
print(f"Error: Unknown or missing version field: {version}")
return 1
if not dependency_ids:
print("No dependencies found.")
return 0
dep_manager.list_dependencies(dependency_ids)
return 0
def cmd_download_game(args):
"""Download dependencies for a specific game."""
# Setup authentication
auth_manager = AuthManager()
if not auth_manager.is_authenticated():
print("Error: Not authenticated. Run 'galaxy-dl login' first.")
return 1
api = GalaxyAPI(auth_manager)
# Initialize dependency manager
dep_manager = DependencyManager(api, base_path=args.path)
if not dep_manager.initialize():
print(f"{SYMBOL_ERROR} Failed to initialize dependency system")
return 1
# Load depot/repository file
file_path = Path(args.file_path)
if not file_path.exists():
print(f"Error: File not found: {file_path}")
return 1
with open(file_path, 'r') as f:
try:
data = json.load(f)
except:
# Try decompressing first
import zlib
f.seek(0)
content = f.read()
if isinstance(content, str):
content = content.encode()
decompressed = zlib.decompress(content)
data = json.loads(decompressed)
# Detect version and extract dependencies
version = data.get("version")
dependency_ids = []
if version == 1:
# V1: Extract from redist objects in product.depots array
print("Detected V1 format (repository.json)")
product = data.get("product", {})
depots = product.get("depots", [])
for depot in depots:
if "redist" in depot:
dependency_ids.append(depot["redist"])
elif version == 2:
# V2: Extract from dependencies array
print("Detected V2 format (depot.json)")
dependency_ids = data.get("dependencies", [])
else:
print(f"Error: Unknown or missing version field: {version}")
return 1
if not dependency_ids:
print("No dependencies found.")
return 0
# Get all dependencies (include_redist=True to show everything)
deps = dep_manager.get_dependencies_for_game(
dependency_ids,
include_redist=True
)
if not deps:
print("No matching dependencies found in repository.")
return 0
print(f"\nDownloading {len(deps)} dependencies...")
print("=" * 80)
success_count = 0
for dep in deps:
if dep_manager.download_dependency(dep):
success_count += 1
print("=" * 80)
print(f"\n{SYMBOL_CHECK} Successfully downloaded {success_count}/{len(deps)} dependencies")
if success_count < len(deps):
return 1
return 0
def cmd_list_all(args):
"""List all available dependencies in repository."""
# Setup authentication
auth_manager = AuthManager()
if not auth_manager.is_authenticated():
print("Error: Not authenticated. Run 'galaxy-dl login' first.")
return 1
api = GalaxyAPI(auth_manager)
# Initialize dependency manager
dep_manager = DependencyManager(api, base_path=args.path)
if not dep_manager.initialize():
print(f"{SYMBOL_ERROR} Failed to initialize dependency system")
return 1
deps = sorted(dep_manager.repository.dependencies.values(), key=lambda d: d.id)
print(f"\nTotal dependencies in repository: {len(deps)}")
print("=" * 80)
for dep in deps:
installed = SYMBOL_INSTALLED if dep.id in dep_manager.installed else SYMBOL_NOT_INSTALLED
size_mb = dep.compressed_size / (1024 * 1024)
redist = "(redist)" if dep.is_redist else ""
print(f"[{installed}] {dep.id:25} {size_mb:8.2f} MB {redist}")
print("=" * 80)
return 0
def cmd_download_all(args):
"""Download all available dependencies."""
# Setup authentication
auth_manager = AuthManager()
if not auth_manager.is_authenticated():
print("Error: Not authenticated. Run 'galaxy-dl login' first.")
return 1
api = GalaxyAPI(auth_manager)
# Initialize dependency manager
dep_manager = DependencyManager(api, base_path=args.path)
if not dep_manager.initialize():
print(f"{SYMBOL_ERROR} Failed to initialize dependency system")
return 1
deps = sorted(dep_manager.repository.dependencies.values(), key=lambda d: d.id)
print(f"\nDownloading all {len(deps)} dependencies...")
print("=" * 80)
success_count = 0
for dep in deps:
if dep_manager.download_dependency(dep):
success_count += 1
print("=" * 80)
print(f"\n{SYMBOL_CHECK} Successfully downloaded {success_count}/{len(deps)} dependencies")
if success_count < len(deps):
return 1
return 0
def cmd_download(args):
"""Download a specific dependency by name."""
# Setup authentication
auth_manager = AuthManager()
if not auth_manager.is_authenticated():
print("Error: Not authenticated. Run 'galaxy-dl login' first.")
return 1
api = GalaxyAPI(auth_manager)
# Initialize dependency manager
dep_manager = DependencyManager(api, base_path=args.path)
if not dep_manager.initialize():
print(f"{SYMBOL_ERROR} Failed to initialize dependency system")
return 1
# Get the dependency
dep = dep_manager.repository.get_dependency(args.dependency_name)
if not dep:
print(f"Error: Dependency '{args.dependency_name}' not found.")
print(f"\nAvailable dependencies:")
for dep_id in sorted(dep_manager.repository.dependencies.keys()):
print(f" - {dep_id}")
return 1
# Download it
print(f"Downloading {dep.id}...")
size_mb = dep.compressed_size / (1024 * 1024)
print(f"Size: {size_mb:.2f} MB")
print(f"Path: {dep.executable_path}")
print("=" * 80)
if dep_manager.download_dependency(dep):
print("=" * 80)
print(f"\n{SYMBOL_CHECK} Successfully downloaded {dep.id}")
return 0
else:
print("=" * 80)
print(f"\n{SYMBOL_ERROR} Failed to download {dep.id}")
return 1
def cmd_update(args):
"""Update the dependency repository from GOG."""
# Setup authentication
auth_manager = AuthManager()
if not auth_manager.is_authenticated():
print("Error: Not authenticated. Run 'galaxy-dl login' first.")
return 1
api = GalaxyAPI(auth_manager)
# Initialize dependency manager (this will fetch latest repository)
print("Fetching latest dependency repository from GOG...")
dep_manager = DependencyManager(api, base_path=args.path)
if not dep_manager.initialize():
print(f"{SYMBOL_ERROR} Failed to update dependency repository")
return 1
print(f"\n{SYMBOL_CHECK} Repository updated successfully")
print(f"Build ID: {dep_manager.repository.repository.get('build_id', 'unknown')}")
print(f"Total dependencies: {len(dep_manager.repository.dependencies)}")
return 0
def main():
parser = argparse.ArgumentParser(
description="Manage GOG game dependencies separately from game files"
)
parser.add_argument(
'--path',
default='./dependencies',
help='Base path for dependency storage (default: ./dependencies)'
)
parser.add_argument(
'-v', '--verbose',
action='store_true',
help='Enable verbose logging'
)
parser.add_argument(
'--ascii',
action='store_true',
help='Force ASCII output (disable Unicode symbols)'
)
subparsers = parser.add_subparsers(dest='command', help='Command to execute')
# Init command
parser_init = subparsers.add_parser(
'init',
help='Initialize dependency system and download repository metadata'
)
# List game dependencies
parser_list_game = subparsers.add_parser(
'list-game',
help='List dependencies for a specific game'
)
parser_list_game.add_argument(
'file_path',
help='Path to depot/repository JSON file (V1: repository.json, V2: *_depot.json)'
)
# Download game dependencies
parser_download = subparsers.add_parser(
'download-game',
help='Download dependencies for a specific game'
)
parser_download.add_argument(
'file_path',
help='Path to depot/repository JSON file (V1: repository.json, V2: *_depot.json)'
)
# List all dependencies
parser_list_all = subparsers.add_parser(
'list-all',
help='List all available dependencies in repository'
)
# Download all dependencies
parser_download_all = subparsers.add_parser(
'download-all',
help='Download all available dependencies (66 total)'
)
# Download specific dependency
parser_download = subparsers.add_parser(
'download',
help='Download a specific dependency by name'
)
parser_download.add_argument(
'dependency_name',
help='Name of the dependency to download (e.g., MSVC2010_x64, DirectX)'
)
# Update dependency repository
parser_update = subparsers.add_parser(
'update',
help='Update the dependency repository from GOG'
)
args = parser.parse_args()
setup_logging(args.verbose)
# Setup symbol variables based on --ascii flag
setup_symbols(force_ascii=args.ascii)
if not args.command:
parser.print_help()
return 1
# Route to command
commands = {
'init': cmd_init,
'list-game': cmd_list_game,
'download-game': cmd_download_game,
'list-all': cmd_list_all,
'download-all': cmd_download_all,
'download': cmd_download,
'update': cmd_update,
}
return commands[args.command](args)
if __name__ == '__main__':
sys.exit(main())