Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,14 +6,10 @@ import shutil
|
|
6 |
from urllib.parse import urlparse
|
7 |
|
8 |
def validate_repo_url(url):
|
9 |
-
"""Validate if the input is a valid repository URL or GitHub shorthand."""
|
10 |
if not url:
|
11 |
return False
|
12 |
-
|
13 |
-
# Check if it's a GitHub shorthand (user/repo)
|
14 |
if '/' in url and len(url.split('/')) == 2:
|
15 |
return True
|
16 |
-
|
17 |
try:
|
18 |
result = urlparse(url)
|
19 |
return all([result.scheme, result.netloc])
|
@@ -21,12 +17,9 @@ def validate_repo_url(url):
|
|
21 |
return False
|
22 |
|
23 |
def create_download_file(content):
|
24 |
-
"""Create a temporary file for download and return its path."""
|
25 |
if not content:
|
26 |
return None
|
27 |
-
|
28 |
try:
|
29 |
-
# Create a temporary file
|
30 |
temp_file = tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt')
|
31 |
temp_file.write(content)
|
32 |
temp_file.close()
|
@@ -35,8 +28,7 @@ def create_download_file(content):
|
|
35 |
print(f"Error creating download file: {str(e)}")
|
36 |
return None
|
37 |
|
38 |
-
def pack_repository(repo_url, branch, output_style, remove_comments, remove_empty_lines, security_check):
|
39 |
-
"""Pack a repository using Repomix and return the output."""
|
40 |
if not repo_url:
|
41 |
return "Error: Please provide a repository URL", None
|
42 |
|
@@ -45,22 +37,18 @@ def pack_repository(repo_url, branch, output_style, remove_comments, remove_empt
|
|
45 |
|
46 |
temp_dir = None
|
47 |
try:
|
48 |
-
# Create temporary directory that persists until we're done
|
49 |
temp_dir = tempfile.mkdtemp()
|
50 |
-
|
51 |
-
# Prepare command
|
52 |
cmd = ["npx", "repomix"]
|
53 |
-
|
54 |
-
# Add remote repository options
|
55 |
cmd.extend(["--remote", repo_url])
|
56 |
|
57 |
if branch:
|
58 |
cmd.extend(["--remote-branch", branch])
|
59 |
|
60 |
-
|
|
|
|
|
61 |
cmd.extend(["--style", output_style])
|
62 |
|
63 |
-
# Add other options
|
64 |
if remove_comments:
|
65 |
cmd.append("--remove-comments")
|
66 |
if remove_empty_lines:
|
@@ -68,11 +56,9 @@ def pack_repository(repo_url, branch, output_style, remove_comments, remove_empt
|
|
68 |
if not security_check:
|
69 |
cmd.append("--no-security-check")
|
70 |
|
71 |
-
# Set output path
|
72 |
output_file = os.path.join(temp_dir, "repomix-output.txt")
|
73 |
cmd.extend(["-o", output_file])
|
74 |
|
75 |
-
# Execute Repomix
|
76 |
result = subprocess.run(
|
77 |
cmd,
|
78 |
capture_output=True,
|
@@ -83,16 +69,13 @@ def pack_repository(repo_url, branch, output_style, remove_comments, remove_empt
|
|
83 |
if result.returncode != 0:
|
84 |
return f"Error running Repomix: {result.stderr}", None
|
85 |
|
86 |
-
# Check if the file exists
|
87 |
if not os.path.exists(output_file):
|
88 |
return f"Error: Output file was not created. Repomix output: {result.stdout}\n{result.stderr}", None
|
89 |
|
90 |
-
# Read the output file
|
91 |
try:
|
92 |
with open(output_file, 'r', encoding='utf-8') as f:
|
93 |
content = f.read()
|
94 |
|
95 |
-
# Create a download file and return its path
|
96 |
download_path = create_download_file(content)
|
97 |
return content, download_path
|
98 |
|
@@ -103,16 +86,13 @@ def pack_repository(repo_url, branch, output_style, remove_comments, remove_empt
|
|
103 |
return f"Error: {str(e)}", None
|
104 |
|
105 |
finally:
|
106 |
-
# Clean up temporary directory
|
107 |
if temp_dir and os.path.exists(temp_dir):
|
108 |
try:
|
109 |
shutil.rmtree(temp_dir)
|
110 |
except Exception as e:
|
111 |
print(f"Warning: Could not remove temporary directory: {str(e)}")
|
112 |
|
113 |
-
# Create the Gradio interface
|
114 |
with gr.Blocks(title="Repo to TXT", theme=gr.themes.Soft()) as demo:
|
115 |
-
|
116 |
with gr.Row():
|
117 |
with gr.Column():
|
118 |
repo_url = gr.Textbox(
|
@@ -123,6 +103,11 @@ with gr.Blocks(title="Repo to TXT", theme=gr.themes.Soft()) as demo:
|
|
123 |
label="Branch/Tag/Commit (optional)",
|
124 |
placeholder="e.g., main, master, v1.0.0"
|
125 |
)
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
with gr.Row():
|
128 |
output_style = gr.Radio(
|
@@ -163,10 +148,9 @@ with gr.Blocks(title="Repo to TXT", theme=gr.themes.Soft()) as demo:
|
|
163 |
interactive=False
|
164 |
)
|
165 |
|
166 |
-
# Handle the pack button click
|
167 |
pack_button.click(
|
168 |
fn=pack_repository,
|
169 |
-
inputs=[repo_url, branch, output_style, remove_comments, remove_empty_lines, security_check],
|
170 |
outputs=[output_text, download_button]
|
171 |
)
|
172 |
|
|
|
6 |
from urllib.parse import urlparse
|
7 |
|
8 |
def validate_repo_url(url):
|
|
|
9 |
if not url:
|
10 |
return False
|
|
|
|
|
11 |
if '/' in url and len(url.split('/')) == 2:
|
12 |
return True
|
|
|
13 |
try:
|
14 |
result = urlparse(url)
|
15 |
return all([result.scheme, result.netloc])
|
|
|
17 |
return False
|
18 |
|
19 |
def create_download_file(content):
|
|
|
20 |
if not content:
|
21 |
return None
|
|
|
22 |
try:
|
|
|
23 |
temp_file = tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt')
|
24 |
temp_file.write(content)
|
25 |
temp_file.close()
|
|
|
28 |
print(f"Error creating download file: {str(e)}")
|
29 |
return None
|
30 |
|
31 |
+
def pack_repository(repo_url, branch, gh_token, output_style, remove_comments, remove_empty_lines, security_check):
|
|
|
32 |
if not repo_url:
|
33 |
return "Error: Please provide a repository URL", None
|
34 |
|
|
|
37 |
|
38 |
temp_dir = None
|
39 |
try:
|
|
|
40 |
temp_dir = tempfile.mkdtemp()
|
|
|
|
|
41 |
cmd = ["npx", "repomix"]
|
|
|
|
|
42 |
cmd.extend(["--remote", repo_url])
|
43 |
|
44 |
if branch:
|
45 |
cmd.extend(["--remote-branch", branch])
|
46 |
|
47 |
+
if gh_token:
|
48 |
+
cmd.extend(["--gh-token", gh_token])
|
49 |
+
|
50 |
cmd.extend(["--style", output_style])
|
51 |
|
|
|
52 |
if remove_comments:
|
53 |
cmd.append("--remove-comments")
|
54 |
if remove_empty_lines:
|
|
|
56 |
if not security_check:
|
57 |
cmd.append("--no-security-check")
|
58 |
|
|
|
59 |
output_file = os.path.join(temp_dir, "repomix-output.txt")
|
60 |
cmd.extend(["-o", output_file])
|
61 |
|
|
|
62 |
result = subprocess.run(
|
63 |
cmd,
|
64 |
capture_output=True,
|
|
|
69 |
if result.returncode != 0:
|
70 |
return f"Error running Repomix: {result.stderr}", None
|
71 |
|
|
|
72 |
if not os.path.exists(output_file):
|
73 |
return f"Error: Output file was not created. Repomix output: {result.stdout}\n{result.stderr}", None
|
74 |
|
|
|
75 |
try:
|
76 |
with open(output_file, 'r', encoding='utf-8') as f:
|
77 |
content = f.read()
|
78 |
|
|
|
79 |
download_path = create_download_file(content)
|
80 |
return content, download_path
|
81 |
|
|
|
86 |
return f"Error: {str(e)}", None
|
87 |
|
88 |
finally:
|
|
|
89 |
if temp_dir and os.path.exists(temp_dir):
|
90 |
try:
|
91 |
shutil.rmtree(temp_dir)
|
92 |
except Exception as e:
|
93 |
print(f"Warning: Could not remove temporary directory: {str(e)}")
|
94 |
|
|
|
95 |
with gr.Blocks(title="Repo to TXT", theme=gr.themes.Soft()) as demo:
|
|
|
96 |
with gr.Row():
|
97 |
with gr.Column():
|
98 |
repo_url = gr.Textbox(
|
|
|
103 |
label="Branch/Tag/Commit (optional)",
|
104 |
placeholder="e.g., main, master, v1.0.0"
|
105 |
)
|
106 |
+
gh_token = gr.Textbox(
|
107 |
+
label="GitHub Token (for private repos)",
|
108 |
+
placeholder="ghp_xxxxxxxxxxxxxx",
|
109 |
+
type="password"
|
110 |
+
)
|
111 |
|
112 |
with gr.Row():
|
113 |
output_style = gr.Radio(
|
|
|
148 |
interactive=False
|
149 |
)
|
150 |
|
|
|
151 |
pack_button.click(
|
152 |
fn=pack_repository,
|
153 |
+
inputs=[repo_url, branch, gh_token, output_style, remove_comments, remove_empty_lines, security_check],
|
154 |
outputs=[output_text, download_button]
|
155 |
)
|
156 |
|