ucalyptus commited on
Commit
5fdf54a
·
1 Parent(s): 4883a1d
Files changed (1) hide show
  1. app.py +58 -34
app.py CHANGED
@@ -22,50 +22,74 @@ def validate_repo_url(url):
22
 
23
  def pack_repository(repo_url, branch, output_style, remove_comments, remove_empty_lines, security_check):
24
  """Pack a repository using Repomix and return the output."""
 
 
 
 
 
 
 
25
  try:
26
- # Create temporary directory
27
- with tempfile.TemporaryDirectory() as temp_dir:
28
- # Prepare command
29
- cmd = ["npx", "repomix"]
30
-
31
- # Add remote repository options
32
- cmd.extend(["--remote", repo_url])
 
 
 
 
33
 
34
- if branch:
35
- cmd.extend(["--remote-branch", branch])
36
-
37
- # Add style option
38
- cmd.extend(["--style", output_style])
 
 
 
 
 
39
 
40
- # Add other options
41
- if remove_comments:
42
- cmd.append("--remove-comments")
43
- if remove_empty_lines:
44
- cmd.append("--remove-empty-lines")
45
- if not security_check:
46
- cmd.append("--no-security-check")
47
-
48
- # Set output path
49
- output_file = os.path.join(temp_dir, "repomix-output.txt")
50
- cmd.extend(["-o", output_file])
 
 
 
51
 
52
- # Execute Repomix
53
- result = subprocess.run(cmd,
54
- capture_output=True,
55
- text=True,
56
- cwd=temp_dir)
57
 
58
- if result.returncode != 0:
59
- return f"Error: {result.stderr}"
60
-
61
- # Read the output file
62
- with open(output_file, 'r') as f:
63
  content = f.read()
64
-
65
  return content
 
 
66
 
67
  except Exception as e:
68
  return f"Error: {str(e)}"
 
 
 
 
 
 
 
 
69
 
70
  # Create the Gradio interface
71
  with gr.Blocks(title="Repomix Web Interface", theme=gr.themes.Soft()) as demo:
 
22
 
23
  def pack_repository(repo_url, branch, output_style, remove_comments, remove_empty_lines, security_check):
24
  """Pack a repository using Repomix and return the output."""
25
+ if not repo_url:
26
+ return "Error: Please provide a repository URL"
27
+
28
+ if not validate_repo_url(repo_url):
29
+ return "Error: Invalid repository URL format"
30
+
31
+ temp_dir = None
32
  try:
33
+ # Create temporary directory that persists until we're done
34
+ temp_dir = tempfile.mkdtemp()
35
+
36
+ # Prepare command
37
+ cmd = ["npx", "repomix"]
38
+
39
+ # Add remote repository options
40
+ cmd.extend(["--remote", repo_url])
41
+
42
+ if branch:
43
+ cmd.extend(["--remote-branch", branch])
44
 
45
+ # Add style option
46
+ cmd.extend(["--style", output_style])
47
+
48
+ # Add other options
49
+ if remove_comments:
50
+ cmd.append("--remove-comments")
51
+ if remove_empty_lines:
52
+ cmd.append("--remove-empty-lines")
53
+ if not security_check:
54
+ cmd.append("--no-security-check")
55
 
56
+ # Set output path
57
+ output_file = os.path.join(temp_dir, "repomix-output.txt")
58
+ cmd.extend(["-o", output_file])
59
+
60
+ # Execute Repomix
61
+ result = subprocess.run(
62
+ cmd,
63
+ capture_output=True,
64
+ text=True,
65
+ cwd=temp_dir
66
+ )
67
+
68
+ if result.returncode != 0:
69
+ return f"Error running Repomix: {result.stderr}"
70
 
71
+ # Check if the file exists
72
+ if not os.path.exists(output_file):
73
+ return f"Error: Output file was not created. Repomix output: {result.stdout}\n{result.stderr}"
 
 
74
 
75
+ # Read the output file
76
+ try:
77
+ with open(output_file, 'r', encoding='utf-8') as f:
 
 
78
  content = f.read()
 
79
  return content
80
+ except Exception as e:
81
+ return f"Error reading output file: {str(e)}"
82
 
83
  except Exception as e:
84
  return f"Error: {str(e)}"
85
+
86
+ finally:
87
+ # Clean up temporary directory
88
+ if temp_dir and os.path.exists(temp_dir):
89
+ try:
90
+ shutil.rmtree(temp_dir)
91
+ except Exception as e:
92
+ print(f"Warning: Could not remove temporary directory: {str(e)}")
93
 
94
  # Create the Gradio interface
95
  with gr.Blocks(title="Repomix Web Interface", theme=gr.themes.Soft()) as demo: