voirs 0.1.0-alpha.2

Advanced voice synthesis and speech processing library for Rust
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
#!/usr/bin/env python3
"""
VoiRS Dependency Management System
==================================

Comprehensive dependency management tool for VoiRS examples and crates.
Manages Cargo.toml dependencies, detects version conflicts, suggests updates,
and ensures compatibility across the entire workspace.

Features:
- Workspace dependency analysis and management
- Version conflict detection and resolution
- Automated dependency updates with compatibility checking
- Security vulnerability scanning in dependencies
- License compliance checking
- Dependency graph visualization
- Unused dependency detection
- Dependency deduplication recommendations

Usage:
    python dependency_manager.py [OPTIONS]

Options:
    --workspace-dir PATH    Path to workspace root (default: ..)
    --examples-dir PATH     Path to examples directory (default: .)
    --config PATH          Path to dependency config file
    --report PATH          Generate detailed dependency report
    --update               Update dependencies to latest compatible versions
    --check-security       Check for security vulnerabilities
    --check-licenses       Check license compatibility
    --fix-conflicts        Automatically fix version conflicts
    --unused               Detect and report unused dependencies
    --verbose              Enable verbose output
"""

import argparse
import json
import logging
import os
import subprocess
import sys
import time
import re
import requests
from dataclasses import dataclass, asdict
from pathlib import Path
from typing import List, Dict, Optional, Set, Any, Tuple
from packaging import version
import tempfile

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    datefmt='%H:%M:%S'
)
logger = logging.getLogger(__name__)

@dataclass
class Dependency:
    """Represents a Rust dependency"""
    name: str
    version: str
    source: str = "crates.io"  # crates.io, git, path, etc.
    features: List[str] = None
    optional: bool = False
    workspace: bool = False
    
    def __post_init__(self):
        if self.features is None:
            self.features = []

@dataclass
class CrateInfo:
    """Information about a crate in the workspace"""
    name: str
    path: Path
    cargo_toml_path: Path
    dependencies: List[Dependency]
    dev_dependencies: List[Dependency]
    build_dependencies: List[Dependency]
    is_example: bool = False
    
    def __post_init__(self):
        if self.dependencies is None:
            self.dependencies = []
        if self.dev_dependencies is None:
            self.dev_dependencies = []
        if self.build_dependencies is None:
            self.build_dependencies = []

@dataclass
class VersionConflict:
    """Represents a version conflict between dependencies"""
    dependency_name: str
    conflicting_versions: List[Tuple[str, str]]  # (version, source_crate)
    recommended_version: Optional[str] = None
    severity: str = "warning"  # warning, error

@dataclass
class SecurityVulnerability:
    """Represents a security vulnerability in a dependency"""
    dependency_name: str
    affected_versions: str
    vulnerability_id: str
    severity: str
    description: str
    patched_versions: List[str]

@dataclass
class DependencyReport:
    """Comprehensive dependency analysis report"""
    timestamp: str
    workspace_crates: int
    total_dependencies: int
    unique_dependencies: int
    version_conflicts: List[VersionConflict]
    security_vulnerabilities: List[SecurityVulnerability]
    unused_dependencies: List[str]
    outdated_dependencies: List[Dict[str, Any]]
    license_issues: List[Dict[str, Any]]
    recommendations: List[str]

class VoiRSDependencyManager:
    """Comprehensive dependency manager for VoiRS workspace"""

    def __init__(self, workspace_dir: Path, examples_dir: Path, config_path: Optional[Path] = None):
        self.workspace_dir = workspace_dir
        self.examples_dir = examples_dir
        self.config_path = config_path
        self.config = self._load_config()
        self.reports_dir = examples_dir / "dependency_reports"
        self.reports_dir.mkdir(exist_ok=True)
        
        self.crates: List[CrateInfo] = []
        self.workspace_dependencies: Dict[str, Dependency] = {}
        self.crates_io_cache: Dict[str, Any] = {}

    def _load_config(self) -> Dict[str, Any]:
        """Load dependency manager configuration"""
        default_config = {
            "allowed_licenses": [
                "MIT", "Apache-2.0", "BSD-2-Clause", "BSD-3-Clause", 
                "ISC", "MPL-2.0", "CC0-1.0", "Unlicense"
            ],
            "blocked_dependencies": [],
            "preferred_versions": {},
            "security_check": {
                "enabled": True,
                "severity_threshold": "medium"
            },
            "update_policy": {
                "major_updates": False,
                "minor_updates": True,
                "patch_updates": True,
                "pre_release": False
            },
            "workspace_optimization": {
                "deduplicate_versions": True,
                "prefer_workspace_deps": True,
                "consolidate_features": True
            }
        }

        if self.config_path and self.config_path.exists():
            try:
                import toml
                with open(self.config_path, 'r') as f:
                    user_config = toml.load(f)
                    # Merge configurations
                    for key, value in user_config.items():
                        if isinstance(value, dict) and key in default_config:
                            default_config[key].update(value)
                        else:
                            default_config[key] = value
            except ImportError:
                logger.warning("toml package not found, using default config")
            except Exception as e:
                logger.warning(f"Failed to load config: {e}, using defaults")

        return default_config

    def discover_crates(self) -> None:
        """Discover all crates in the workspace"""
        logger.info("Discovering crates in workspace...")
        
        # Parse workspace Cargo.toml
        workspace_toml = self.workspace_dir / "Cargo.toml"
        if workspace_toml.exists():
            self._parse_workspace_dependencies(workspace_toml)
        
        # Find all Cargo.toml files in workspace
        cargo_files = list(self.workspace_dir.glob("**/Cargo.toml"))
        
        for cargo_file in cargo_files:
            # Skip target directories
            if "target" in cargo_file.parts:
                continue
                
            try:
                crate_info = self._parse_cargo_toml(cargo_file)
                if crate_info:
                    self.crates.append(crate_info)
                    logger.debug(f"Found crate: {crate_info.name}")
            except Exception as e:
                logger.warning(f"Failed to parse {cargo_file}: {e}")
        
        logger.info(f"Discovered {len(self.crates)} crates")

    def _parse_workspace_dependencies(self, workspace_toml: Path) -> None:
        """Parse workspace-level dependencies"""
        try:
            import toml
            with open(workspace_toml, 'r') as f:
                data = toml.load(f)
            
            workspace_deps = data.get("workspace", {}).get("dependencies", {})
            
            for name, spec in workspace_deps.items():
                if isinstance(spec, str):
                    dep = Dependency(name=name, version=spec, workspace=True)
                elif isinstance(spec, dict):
                    dep = Dependency(
                        name=name,
                        version=spec.get("version", ""),
                        source=self._determine_source(spec),
                        features=spec.get("features", []),
                        optional=spec.get("optional", False),
                        workspace=True
                    )
                else:
                    continue
                
                self.workspace_dependencies[name] = dep
                
        except ImportError:
            logger.error("toml package required for parsing Cargo.toml files")
            sys.exit(1)
        except Exception as e:
            logger.warning(f"Failed to parse workspace dependencies: {e}")

    def _parse_cargo_toml(self, cargo_file: Path) -> Optional[CrateInfo]:
        """Parse a Cargo.toml file and extract dependency information"""
        try:
            import toml
            with open(cargo_file, 'r') as f:
                data = toml.load(f)
            
            package = data.get("package", {})
            crate_name = package.get("name", cargo_file.parent.name)
            
            # Determine if this is an example
            is_example = "examples" in str(cargo_file) or "example" in package.get("keywords", [])
            
            # Parse dependencies
            dependencies = self._parse_dependencies(data.get("dependencies", {}))
            dev_dependencies = self._parse_dependencies(data.get("dev-dependencies", {}))
            build_dependencies = self._parse_dependencies(data.get("build-dependencies", {}))
            
            return CrateInfo(
                name=crate_name,
                path=cargo_file.parent,
                cargo_toml_path=cargo_file,
                dependencies=dependencies,
                dev_dependencies=dev_dependencies,
                build_dependencies=build_dependencies,
                is_example=is_example
            )
            
        except Exception as e:
            logger.warning(f"Failed to parse {cargo_file}: {e}")
            return None

    def _parse_dependencies(self, deps_dict: Dict[str, Any]) -> List[Dependency]:
        """Parse dependencies from Cargo.toml section"""
        dependencies = []
        
        for name, spec in deps_dict.items():
            if isinstance(spec, str):
                dep = Dependency(name=name, version=spec)
            elif isinstance(spec, dict):
                dep = Dependency(
                    name=name,
                    version=spec.get("version", ""),
                    source=self._determine_source(spec),
                    features=spec.get("features", []),
                    optional=spec.get("optional", False),
                    workspace=spec.get("workspace", False)
                )
            else:
                continue
            
            dependencies.append(dep)
        
        return dependencies

    def _determine_source(self, spec: Dict[str, Any]) -> str:
        """Determine the source of a dependency"""
        if "path" in spec:
            return "path"
        elif "git" in spec:
            return "git"
        elif "registry" in spec:
            return spec["registry"]
        else:
            return "crates.io"

    def analyze_version_conflicts(self) -> List[VersionConflict]:
        """Analyze version conflicts across the workspace"""
        logger.info("Analyzing version conflicts...")
        
        conflicts = []
        dependency_versions = {}
        
        # Collect all dependency versions
        for crate in self.crates:
            for dep_list in [crate.dependencies, crate.dev_dependencies, crate.build_dependencies]:
                for dep in dep_list:
                    if dep.workspace:
                        continue  # Skip workspace dependencies
                    
                    if dep.name not in dependency_versions:
                        dependency_versions[dep.name] = []
                    
                    dependency_versions[dep.name].append((dep.version, crate.name))
        
        # Find conflicts
        for dep_name, versions in dependency_versions.items():
            unique_versions = {}
            for version_spec, source_crate in versions:
                if version_spec not in unique_versions:
                    unique_versions[version_spec] = []
                unique_versions[version_spec].append(source_crate)
            
            if len(unique_versions) > 1:
                # Determine if this is a real conflict
                resolved_versions = []
                for version_spec in unique_versions.keys():
                    try:
                        # Simple version resolution (could be more sophisticated)
                        if version_spec.startswith("="):
                            resolved_versions.append(version_spec[1:])
                        elif version_spec.startswith("^") or version_spec.startswith("~"):
                            resolved_versions.append(version_spec[1:])
                        else:
                            resolved_versions.append(version_spec)
                    except:
                        resolved_versions.append(version_spec)
                
                # Check if versions are actually conflicting
                if len(set(resolved_versions)) > 1:
                    conflicting_versions = [(v, ", ".join(unique_versions[v])) for v in unique_versions.keys()]
                    
                    # Suggest recommended version
                    recommended = self._suggest_version_resolution(dep_name, resolved_versions)
                    
                    conflicts.append(VersionConflict(
                        dependency_name=dep_name,
                        conflicting_versions=conflicting_versions,
                        recommended_version=recommended,
                        severity="warning"
                    ))
        
        logger.info(f"Found {len(conflicts)} version conflicts")
        return conflicts

    def _suggest_version_resolution(self, dep_name: str, versions: List[str]) -> str:
        """Suggest a version to resolve conflicts"""
        try:
            # Get latest version from crates.io
            latest_version = self._get_latest_version(dep_name)
            if latest_version:
                return latest_version
        except:
            pass
        
        # Fallback: suggest the highest version
        try:
            sorted_versions = sorted(versions, key=lambda x: version.parse(x), reverse=True)
            return sorted_versions[0]
        except:
            return versions[0] if versions else "unknown"

    def _get_latest_version(self, crate_name: str) -> Optional[str]:
        """Get the latest version of a crate from crates.io"""
        if crate_name in self.crates_io_cache:
            return self.crates_io_cache[crate_name].get("max_version")
        
        try:
            response = requests.get(f"https://crates.io/api/v1/crates/{crate_name}", timeout=5)
            if response.status_code == 200:
                data = response.json()
                crate_info = data.get("crate", {})
                max_version = crate_info.get("max_version")
                self.crates_io_cache[crate_name] = crate_info
                return max_version
        except:
            pass
        
        return None

    def check_security_vulnerabilities(self) -> List[SecurityVulnerability]:
        """Check for security vulnerabilities using cargo audit"""
        logger.info("Checking for security vulnerabilities...")
        
        vulnerabilities = []
        
        try:
            # Run cargo audit
            result = subprocess.run(
                ["cargo", "audit", "--json"],
                cwd=self.workspace_dir,
                capture_output=True,
                text=True,
                timeout=60
            )
            
            if result.returncode == 0:
                # Parse JSON output
                try:
                    audit_data = json.loads(result.stdout)
                    
                    for vuln in audit_data.get("vulnerabilities", {}).get("list", []):
                        advisory = vuln.get("advisory", {})
                        package = vuln.get("package", {})
                        
                        vulnerability = SecurityVulnerability(
                            dependency_name=package.get("name", "unknown"),
                            affected_versions=package.get("version", "unknown"),
                            vulnerability_id=advisory.get("id", "unknown"),
                            severity=advisory.get("severity", "unknown"),
                            description=advisory.get("title", "No description"),
                            patched_versions=advisory.get("patched_versions", [])
                        )
                        
                        vulnerabilities.append(vulnerability)
                        
                except json.JSONDecodeError:
                    logger.warning("Failed to parse cargo audit JSON output")
                    
        except subprocess.TimeoutExpired:
            logger.warning("cargo audit timed out")
        except FileNotFoundError:
            logger.warning("cargo audit not found - install with 'cargo install cargo-audit'")
        except Exception as e:
            logger.warning(f"Failed to run cargo audit: {e}")
        
        logger.info(f"Found {len(vulnerabilities)} security vulnerabilities")
        return vulnerabilities

    def detect_unused_dependencies(self) -> List[str]:
        """Detect unused dependencies using cargo machete"""
        logger.info("Detecting unused dependencies...")
        
        unused = []
        
        try:
            # Run cargo machete
            result = subprocess.run(
                ["cargo", "machete"],
                cwd=self.workspace_dir,
                capture_output=True,
                text=True,
                timeout=120
            )
            
            if result.returncode == 0:
                # Parse output
                for line in result.stdout.split('\n'):
                    if line.strip() and not line.startswith('Analyzing'):
                        unused.append(line.strip())
                        
        except subprocess.TimeoutExpired:
            logger.warning("cargo machete timed out")
        except FileNotFoundError:
            logger.info("cargo machete not found - install with 'cargo install cargo-machete'")
        except Exception as e:
            logger.warning(f"Failed to run cargo machete: {e}")
        
        logger.info(f"Found {len(unused)} unused dependencies")
        return unused

    def check_outdated_dependencies(self) -> List[Dict[str, Any]]:
        """Check for outdated dependencies"""
        logger.info("Checking for outdated dependencies...")
        
        outdated = []
        
        for crate in self.crates:
            for dep in crate.dependencies:
                if dep.source == "crates.io" and not dep.workspace:
                    latest_version = self._get_latest_version(dep.name)
                    if latest_version and latest_version != dep.version:
                        try:
                            if version.parse(latest_version) > version.parse(dep.version.lstrip("^~=")):
                                outdated.append({
                                    "name": dep.name,
                                    "current": dep.version,
                                    "latest": latest_version,
                                    "crate": crate.name
                                })
                        except:
                            # Version parsing failed, still report as potentially outdated
                            outdated.append({
                                "name": dep.name,
                                "current": dep.version,
                                "latest": latest_version,
                                "crate": crate.name
                            })
        
        logger.info(f"Found {len(outdated)} outdated dependencies")
        return outdated

    def update_dependencies(self, dry_run: bool = True) -> Dict[str, Any]:
        """Update dependencies to latest compatible versions"""
        logger.info(f"{'Simulating' if dry_run else 'Performing'} dependency updates...")
        
        update_results = {
            "updated": [],
            "failed": [],
            "skipped": []
        }
        
        # Get outdated dependencies
        outdated = self.check_outdated_dependencies()
        
        for dep_info in outdated:
            dep_name = dep_info["name"]
            current_version = dep_info["current"]
            latest_version = dep_info["latest"]
            
            # Check update policy
            if not self._should_update(current_version, latest_version):
                update_results["skipped"].append({
                    "name": dep_name,
                    "reason": "Update policy restriction",
                    "current": current_version,
                    "available": latest_version
                })
                continue
            
            if dry_run:
                update_results["updated"].append({
                    "name": dep_name,
                    "from": current_version,
                    "to": latest_version,
                    "dry_run": True
                })
            else:
                # Perform actual update
                try:
                    success = self._update_dependency(dep_name, latest_version)
                    if success:
                        update_results["updated"].append({
                            "name": dep_name,
                            "from": current_version,
                            "to": latest_version
                        })
                    else:
                        update_results["failed"].append({
                            "name": dep_name,
                            "error": "Update failed"
                        })
                except Exception as e:
                    update_results["failed"].append({
                        "name": dep_name,
                        "error": str(e)
                    })
        
        logger.info(f"Update summary: {len(update_results['updated'])} updated, "
                   f"{len(update_results['failed'])} failed, {len(update_results['skipped'])} skipped")
        
        return update_results

    def _should_update(self, current: str, latest: str) -> bool:
        """Check if dependency should be updated based on policy"""
        try:
            current_ver = version.parse(current.lstrip("^~="))
            latest_ver = version.parse(latest)
            
            policy = self.config.get("update_policy", {})
            
            if latest_ver.major > current_ver.major:
                return policy.get("major_updates", False)
            elif latest_ver.minor > current_ver.minor:
                return policy.get("minor_updates", True)
            elif latest_ver.micro > current_ver.micro:
                return policy.get("patch_updates", True)
            else:
                return False
                
        except:
            return False

    def _update_dependency(self, dep_name: str, new_version: str) -> bool:
        """Update a specific dependency in relevant Cargo.toml files"""
        updated = False
        
        for crate in self.crates:
            cargo_toml = crate.cargo_toml_path
            
            try:
                import toml
                with open(cargo_toml, 'r') as f:
                    data = toml.load(f)
                
                # Update in different dependency sections
                sections = ["dependencies", "dev-dependencies", "build-dependencies"]
                
                for section in sections:
                    if section in data and dep_name in data[section]:
                        if isinstance(data[section][dep_name], str):
                            data[section][dep_name] = f"^{new_version}"
                        elif isinstance(data[section][dep_name], dict):
                            data[section][dep_name]["version"] = f"^{new_version}"
                        
                        updated = True
                
                if updated:
                    with open(cargo_toml, 'w') as f:
                        toml.dump(data, f)
                        
            except Exception as e:
                logger.error(f"Failed to update {dep_name} in {cargo_toml}: {e}")
                return False
        
        return updated

    def generate_dependency_graph(self) -> Dict[str, Any]:
        """Generate a dependency graph for visualization"""
        logger.info("Generating dependency graph...")
        
        graph = {
            "nodes": [],
            "edges": []
        }
        
        # Add nodes for each crate
        for crate in self.crates:
            graph["nodes"].append({
                "id": crate.name,
                "label": crate.name,
                "type": "example" if crate.is_example else "crate",
                "path": str(crate.path)
            })
        
        # Add edges for dependencies
        for crate in self.crates:
            for dep in crate.dependencies:
                # Only add edges for internal dependencies
                dep_crate = next((c for c in self.crates if c.name == dep.name), None)
                if dep_crate:
                    graph["edges"].append({
                        "from": crate.name,
                        "to": dep.name,
                        "type": "dependency",
                        "version": dep.version
                    })
        
        return graph

    def generate_report(self) -> DependencyReport:
        """Generate comprehensive dependency report"""
        logger.info("Generating dependency report...")
        
        # Analyze all aspects
        version_conflicts = self.analyze_version_conflicts()
        security_vulns = self.check_security_vulnerabilities()
        unused_deps = self.detect_unused_dependencies()
        outdated_deps = self.check_outdated_dependencies()
        
        # Count unique dependencies
        all_deps = set()
        total_deps = 0
        
        for crate in self.crates:
            for dep_list in [crate.dependencies, crate.dev_dependencies, crate.build_dependencies]:
                for dep in dep_list:
                    all_deps.add(dep.name)
                    total_deps += 1
        
        # Generate recommendations
        recommendations = []
        
        if version_conflicts:
            recommendations.append(f"Resolve {len(version_conflicts)} version conflicts")
        
        if security_vulns:
            high_severity = [v for v in security_vulns if v.severity in ["high", "critical"]]
            if high_severity:
                recommendations.append(f"Address {len(high_severity)} high/critical security vulnerabilities")
        
        if unused_deps:
            recommendations.append(f"Remove {len(unused_deps)} unused dependencies")
        
        if outdated_deps:
            recommendations.append(f"Update {len(outdated_deps)} outdated dependencies")
        
        return DependencyReport(
            timestamp=time.strftime('%Y-%m-%d %H:%M:%S'),
            workspace_crates=len(self.crates),
            total_dependencies=total_deps,
            unique_dependencies=len(all_deps),
            version_conflicts=version_conflicts,
            security_vulnerabilities=security_vulns,
            unused_dependencies=unused_deps,
            outdated_dependencies=outdated_deps,
            license_issues=[],  # TODO: Implement license checking
            recommendations=recommendations
        )

    def save_report(self, report: DependencyReport, output_path: Path) -> None:
        """Save dependency report to file"""
        with open(output_path, 'w', encoding='utf-8') as f:
            json.dump(asdict(report), f, indent=2, default=str)
        
        logger.info(f"Dependency report saved to: {output_path}")

    def print_summary(self, report: DependencyReport) -> None:
        """Print dependency report summary"""
        print("\n" + "="*60)
        print("📦 VoiRS Dependency Analysis Report")
        print("="*60)
        print(f"📊 Workspace Crates: {report.workspace_crates}")
        print(f"📦 Total Dependencies: {report.total_dependencies}")
        print(f"🔗 Unique Dependencies: {report.unique_dependencies}")
        
        if report.version_conflicts:
            print(f"\n⚠️  Version Conflicts: {len(report.version_conflicts)}")
            for conflict in report.version_conflicts[:5]:  # Show top 5
                print(f"{conflict.dependency_name}: {len(conflict.conflicting_versions)} versions")
                for version_spec, sources in conflict.conflicting_versions:
                    print(f"    - {version_spec} (used by: {sources})")
                if conflict.recommended_version:
                    print(f"    💡 Recommended: {conflict.recommended_version}")
        
        if report.security_vulnerabilities:
            print(f"\n🔒 Security Vulnerabilities: {len(report.security_vulnerabilities)}")
            high_severity = [v for v in report.security_vulnerabilities if v.severity in ["high", "critical"]]
            if high_severity:
                print(f"  ❌ High/Critical: {len(high_severity)}")
            for vuln in report.security_vulnerabilities[:3]:  # Show top 3
                print(f"{vuln.dependency_name}: {vuln.vulnerability_id} ({vuln.severity})")
                print(f"    {vuln.description}")
        
        if report.unused_dependencies:
            print(f"\n🗑️  Unused Dependencies: {len(report.unused_dependencies)}")
            for unused in report.unused_dependencies[:5]:  # Show first 5
                print(f"{unused}")
        
        if report.outdated_dependencies:
            print(f"\n📅 Outdated Dependencies: {len(report.outdated_dependencies)}")
            for outdated in report.outdated_dependencies[:5]:  # Show first 5
                print(f"{outdated['name']}: {outdated['current']}{outdated['latest']}")
        
        if report.recommendations:
            print(f"\n💡 Recommendations:")
            for rec in report.recommendations:
                print(f"{rec}")

    def run_dependency_analysis(self, 
                              check_security: bool = True,
                              check_unused: bool = True,
                              update_deps: bool = False) -> DependencyReport:
        """Run comprehensive dependency analysis"""
        logger.info("Starting VoiRS dependency analysis...")
        
        start_time = time.time()
        
        try:
            # Discover crates
            self.discover_crates()
            
            # Generate report
            report = self.generate_report()
            
            # Update dependencies if requested
            if update_deps:
                update_results = self.update_dependencies(dry_run=False)
                logger.info(f"Updated {len(update_results['updated'])} dependencies")
            
            duration = time.time() - start_time
            logger.info(f"Dependency analysis completed in {duration:.1f}s")
            
            return report
            
        except Exception as e:
            logger.error(f"Dependency analysis failed: {e}")
            raise

def main():
    """Main entry point"""
    parser = argparse.ArgumentParser(description="VoiRS Dependency Management System")
    parser.add_argument("--workspace-dir", "-w", type=Path, default=Path(".."),
                       help="Path to workspace root")
    parser.add_argument("--examples-dir", "-e", type=Path, default=Path("."),
                       help="Path to examples directory")
    parser.add_argument("--config", "-c", type=Path,
                       help="Path to dependency config file")
    parser.add_argument("--report", "-r", type=Path,
                       help="Generate detailed dependency report at path")
    parser.add_argument("--update", action="store_true",
                       help="Update dependencies to latest compatible versions")
    parser.add_argument("--check-security", action="store_true",
                       help="Check for security vulnerabilities")
    parser.add_argument("--fix-conflicts", action="store_true",
                       help="Automatically fix version conflicts")
    parser.add_argument("--unused", action="store_true",
                       help="Detect and report unused dependencies")
    parser.add_argument("--verbose", "-v", action="store_true",
                       help="Enable verbose output")
    
    args = parser.parse_args()
    
    if args.verbose:
        logging.getLogger().setLevel(logging.DEBUG)
    
    try:
        # Initialize dependency manager
        manager = VoiRSDependencyManager(args.workspace_dir, args.examples_dir, args.config)
        
        # Run dependency analysis
        report = manager.run_dependency_analysis(
            check_security=args.check_security,
            check_unused=args.unused,
            update_deps=args.update
        )
        
        # Print summary
        manager.print_summary(report)
        
        # Save detailed report if requested
        if args.report:
            manager.save_report(report, args.report)
        
        # Return exit code based on issues found
        critical_issues = len([v for v in report.security_vulnerabilities if v.severity in ["high", "critical"]])
        if critical_issues > 0:
            logger.error("Critical security vulnerabilities found")
            return 2
        elif report.version_conflicts or report.security_vulnerabilities:
            logger.warning("Dependency issues found")
            return 1
        else:
            logger.info("Dependency analysis passed")
            return 0
            
    except Exception as e:
        logger.error(f"Dependency analysis failed: {e}")
        if args.verbose:
            import traceback
            traceback.print_exc()
        return 3

if __name__ == "__main__":
    sys.exit(main())