i0110 commited on
Commit
16d59fe
·
verified ·
1 Parent(s): a8de1a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -11
app.py CHANGED
@@ -1,14 +1,12 @@
1
  import os
2
  import tempfile
3
  from flask import Flask, render_template, request, send_file, flash
4
- import requests
5
  from git import Repo
6
  import shutil
7
  import zipfile
8
  import re
9
 
10
  app = Flask(__name__)
11
- # app.secret_key = 'your_secret_key' # 移除 secret_key
12
 
13
  def download_github_files(repo_url, file_paths, github_token=None):
14
  """从GitHub仓库下载指定路径的文件,并打包成zip。"""
@@ -17,7 +15,6 @@ def download_github_files(repo_url, file_paths, github_token=None):
17
  repo_name = repo_url.split('/')[-1].replace(".git", "")
18
  local_repo_path = os.path.join(temp_dir, repo_name)
19
 
20
- print(f"Cloning repo: {repo_url} to {local_repo_path}")
21
  # 克隆仓库
22
  if github_token:
23
  repo_url_with_token = repo_url.replace("https://", f"https://{github_token}@")
@@ -25,27 +22,24 @@ def download_github_files(repo_url, file_paths, github_token=None):
25
  else:
26
  Repo.clone_from(repo_url, local_repo_path)
27
 
28
-
29
  zip_file_path = os.path.join(temp_dir, f"{repo_name}_files.zip")
30
  with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zf:
31
  for file_path in file_paths:
32
  abs_file_path = os.path.join(local_repo_path, file_path)
33
- print(f"Checking file path: {abs_file_path}")
34
  if os.path.exists(abs_file_path) and os.path.isfile(abs_file_path):
35
  zf.write(abs_file_path, os.path.basename(abs_file_path))
36
  elif os.path.exists(abs_file_path) and os.path.isdir(abs_file_path):
37
- for root, dirs, files in os.walk(abs_file_path):
38
  for file in files:
39
  file_abs_path = os.path.join(root, file)
40
  zf.write(file_abs_path, os.path.relpath(file_abs_path, local_repo_path))
 
41
  shutil.rmtree(local_repo_path)
42
  if os.path.exists(zip_file_path) and os.path.getsize(zip_file_path) > 0:
43
  return zip_file_path, f"{repo_name}_files.zip"
44
  else:
45
- print(f"Zip file is empty or not created.")
46
  return None, None
47
- except Exception as e:
48
- print(f"Error during download: {e}")
49
  return None, None
50
 
51
  def parse_github_url(url):
@@ -55,10 +49,8 @@ def parse_github_url(url):
55
  owner, repo, branch, path = match.groups()
56
  repo_url = f'https://github.com/{owner}/{repo}.git'
57
  file_paths = [path.lstrip('/')] if path else []
58
- print(f"Parsed repo_url: {repo_url}, file_paths: {file_paths}")
59
  return repo_url, file_paths
60
  else:
61
- print(f"Invalid URL: {url}")
62
  return None, None
63
 
64
  @app.route('/', methods=['GET', 'POST'])
 
1
  import os
2
  import tempfile
3
  from flask import Flask, render_template, request, send_file, flash
 
4
  from git import Repo
5
  import shutil
6
  import zipfile
7
  import re
8
 
9
  app = Flask(__name__)
 
10
 
11
  def download_github_files(repo_url, file_paths, github_token=None):
12
  """从GitHub仓库下载指定路径的文件,并打包成zip。"""
 
15
  repo_name = repo_url.split('/')[-1].replace(".git", "")
16
  local_repo_path = os.path.join(temp_dir, repo_name)
17
 
 
18
  # 克隆仓库
19
  if github_token:
20
  repo_url_with_token = repo_url.replace("https://", f"https://{github_token}@")
 
22
  else:
23
  Repo.clone_from(repo_url, local_repo_path)
24
 
 
25
  zip_file_path = os.path.join(temp_dir, f"{repo_name}_files.zip")
26
  with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zf:
27
  for file_path in file_paths:
28
  abs_file_path = os.path.join(local_repo_path, file_path)
 
29
  if os.path.exists(abs_file_path) and os.path.isfile(abs_file_path):
30
  zf.write(abs_file_path, os.path.basename(abs_file_path))
31
  elif os.path.exists(abs_file_path) and os.path.isdir(abs_file_path):
32
+ for root, dirs, files in os.walk(abs_file_path):
33
  for file in files:
34
  file_abs_path = os.path.join(root, file)
35
  zf.write(file_abs_path, os.path.relpath(file_abs_path, local_repo_path))
36
+
37
  shutil.rmtree(local_repo_path)
38
  if os.path.exists(zip_file_path) and os.path.getsize(zip_file_path) > 0:
39
  return zip_file_path, f"{repo_name}_files.zip"
40
  else:
 
41
  return None, None
42
+ except Exception:
 
43
  return None, None
44
 
45
  def parse_github_url(url):
 
49
  owner, repo, branch, path = match.groups()
50
  repo_url = f'https://github.com/{owner}/{repo}.git'
51
  file_paths = [path.lstrip('/')] if path else []
 
52
  return repo_url, file_paths
53
  else:
 
54
  return None, None
55
 
56
  @app.route('/', methods=['GET', 'POST'])