-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_usage.py
More file actions
executable file
Β·202 lines (167 loc) Β· 7.06 KB
/
example_usage.py
File metadata and controls
executable file
Β·202 lines (167 loc) Β· 7.06 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
#!/usr/bin/env python3
"""
Example usage of the repository-based logic functionality.
This script demonstrates how to use the enhanced repository management features.
"""
from pathlib import Path
from app.models.repository import Repository, RepositoryStorage
def demonstrate_repository_management():
"""Demonstrate repository management functionality."""
print("π Repository-Based Logic Demo")
print("=" * 50)
# Initialize storage (in real usage, this would be in your working directory)
storage_path = Path("example_repository_metadata.json")
storage = RepositoryStorage(storage_path)
print("π Repository Management Operations:\n")
# 1. Add some example repositories
print("1οΈβ£ Adding example repositories...")
repos = [
Repository(
url="https://github.com/pytorch/pytorch.git",
commit_id="v1.13.0",
playground_path="/tmp/pytorch_v1.13.0",
kg_root_node_id=100,
kg_max_ast_depth=3,
kg_chunk_size=1000,
kg_chunk_overlap=200,
),
Repository(
url="https://github.com/pytorch/pytorch.git",
commit_id="v1.14.0",
playground_path="/tmp/pytorch_v1.14.0",
kg_root_node_id=101,
kg_max_ast_depth=3,
kg_chunk_size=1000,
kg_chunk_overlap=200,
),
Repository(
url="https://github.com/tensorflow/tensorflow.git",
commit_id=None, # Latest
playground_path="/tmp/tensorflow_latest",
kg_root_node_id=200,
kg_max_ast_depth=3,
kg_chunk_size=1000,
kg_chunk_overlap=200,
),
]
for repo in repos:
storage.save_repository(repo)
commit_display = repo.commit_id or "Latest"
print(f" β Added: {repo.url} ({commit_display})")
# 2. List all repositories
print("\n2οΈβ£ Listing all repositories...")
all_repos = storage._load_repositories()
print(f" Found {len(all_repos)} repositories:")
for i, repo in enumerate(all_repos, 1):
commit_display = repo.commit_id or "Latest"
print(f" {i}. {repo.url} ({commit_display}) - KG ID: {repo.kg_root_node_id}")
# 3. Find specific repository
print("\n3οΈβ£ Finding specific repository...")
specific_repo = storage.get_repository_by_url_and_commit_id(
"https://github.com/pytorch/pytorch.git", "v1.13.0"
)
if specific_repo:
print(f" β Found: {specific_repo.url} (v1.13.0) - KG ID: {specific_repo.kg_root_node_id}")
# 4. Simulate repository-based logic
print("\n4οΈβ£ Simulating repository-based logic...")
def simulate_get_or_create(url: str, commit_id: str = None):
"""Simulate the get_or_create_repository logic."""
existing = storage.get_repository_by_url_and_commit_id(url, commit_id)
if existing:
commit_display = commit_id or "Latest"
print(
f" π Reusing existing: {url} ({commit_display}) - KG ID: {existing.kg_root_node_id}"
)
return existing.playground_path, existing.kg_root_node_id, False
else:
commit_display = commit_id or "Latest"
print(f" π Would create new: {url} ({commit_display})")
return "/tmp/new_repo", 999, True
# Test cases
test_cases = [
("https://github.com/pytorch/pytorch.git", "v1.13.0"), # Exists
("https://github.com/pytorch/pytorch.git", "v1.15.0"), # New version
("https://github.com/tensorflow/tensorflow.git", None), # Exists (Latest)
("https://github.com/huggingface/transformers.git", None), # New repo
]
for url, commit_id in test_cases:
path, kg_id, is_new = simulate_get_or_create(url, commit_id)
# 5. Delete repository
print("\n5οΈβ£ Deleting repository...")
deleted = storage.delete_repository("https://github.com/pytorch/pytorch.git", "v1.14.0")
if deleted:
print(" β Successfully deleted PyTorch v1.14.0")
# Show remaining repositories
remaining = storage._load_repositories()
print(f" π Remaining repositories: {len(remaining)}")
for repo in remaining:
commit_display = repo.commit_id or "Latest"
print(f" - {repo.url} ({commit_display})")
# 6. Show benefits
print("\n6οΈβ£ Repository-based logic benefits:")
print(" πΎ Avoids re-cloning identical repositories")
print(" π Faster processing for repeated requests")
print(" π§ Reuses knowledge graphs from Neo4j")
print(" πΎ Saves disk space and bandwidth")
print(" π Consistent results for same repo versions")
# Clean up example file
if storage_path.exists():
storage_path.unlink()
print(f"\nπ§Ή Cleaned up example file: {storage_path}")
def show_command_examples():
"""Show command-line usage examples."""
print("\nπ Command-Line Usage Examples:")
print("=" * 50)
examples = [
("List all repositories", "python manage_repositories.py list"),
(
"Show repository details",
"python manage_repositories.py info 'https://github.com/user/repo.git'",
),
(
"Delete specific version",
"python manage_repositories.py delete 'https://github.com/user/repo.git' -c abc123",
),
(
"Delete all versions",
"python manage_repositories.py delete-all-commits 'https://github.com/user/repo.git'",
),
("Export as JSON", "python manage_repositories.py export --format json"),
("Export as table", "python manage_repositories.py export --format table"),
]
for description, command in examples:
print(f"π {description}:")
print(f" {command}\n")
def show_integration_example():
"""Show how to integrate with existing code."""
print("π§ Integration Example:")
print("=" * 50)
integration_code = """
# Before (without repository-based logic):
def old_reproduce_bug(github_url, github_token, commit_id=None):
# Always clone and build KG
repo_path = repository_service.clone_github_repo(github_token, github_url, commit_id)
kg_root_id = knowledge_graph_service.build_and_save_knowledge_graph(repo_path)
# ... rest of processing
# After (with repository-based logic):
def new_reproduce_bug(github_url, github_token, commit_id=None):
# Check for existing repo, reuse if available
repo_path, kg_root_id, is_new = repository_service.get_or_create_repository(
github_token, github_url, commit_id
)
if is_new:
print("π Created new repository")
else:
print("π Reusing existing repository")
# ... rest of processing (same as before)
# Smart cleanup: only remove if new
if is_new:
repository_service.clean_repository(github_url, commit_id)
else:
print("π Keeping repository for future use")
"""
print(integration_code)
if __name__ == "__main__":
demonstrate_repository_management()
show_command_examples()
show_integration_example()