i0110 commited on
Commit
4d5a12f
·
verified ·
1 Parent(s): db88506

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -18
app.py CHANGED
@@ -8,44 +8,46 @@ import zipfile
8
  import re
9
 
10
  app = Flask(__name__)
11
- app.secret_key = 'your_secret_key' # 更改为更安全的密钥
12
 
13
- def download_github_files(repo_url, file_paths):
14
  """从GitHub仓库下载指定路径的文件,并打包成zip。"""
15
  try:
16
  temp_dir = tempfile.mkdtemp()
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
- Repo.clone_from(repo_url, local_repo_path)
 
 
 
 
 
23
 
24
  zip_file_path = os.path.join(temp_dir, f"{repo_name}_files.zip")
25
  with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zf:
26
  for file_path in file_paths:
27
  abs_file_path = os.path.join(local_repo_path, file_path)
28
- print(f"Checking file path: {abs_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
-
38
- shutil.rmtree(local_repo_path) # 删除克隆的仓库
39
  if os.path.exists(zip_file_path) and os.path.getsize(zip_file_path) > 0:
40
  return zip_file_path, f"{repo_name}_files.zip"
41
  else:
42
- print(f"Zip file is empty or not created.")# 打印
43
- return None, None
44
  except Exception as e:
45
- print(f"Error during download: {e}") # 打印
46
  return None, None
47
 
48
-
49
  def parse_github_url(url):
50
  """解析 GitHub URL,提取仓库 URL 和文件路径。"""
51
  match = re.match(r'https://github\.com/([^/]+)/([^/]+)/tree/([^/]+)(.*)', url)
@@ -53,17 +55,17 @@ def parse_github_url(url):
53
  owner, repo, branch, path = match.groups()
54
  repo_url = f'https://github.com/{owner}/{repo}.git'
55
  file_paths = [path.lstrip('/')] if path else []
56
- print(f"Parsed repo_url: {repo_url}, file_paths: {file_paths}") # 打印
57
  return repo_url, file_paths
58
  else:
59
- print(f"Invalid URL: {url}") # 打印
60
  return None, None
61
 
62
-
63
  @app.route('/', methods=['GET', 'POST'])
64
  def index():
65
  if request.method == 'POST':
66
  repo_path = request.form.get('repo_path')
 
67
  if not repo_path:
68
  flash('请提供 GitHub 链接', 'error')
69
  return render_template('index.html')
@@ -73,7 +75,7 @@ def index():
73
  flash('无效的 GitHub 链接', 'error')
74
  return render_template('index.html')
75
 
76
- zip_path, zip_filename = download_github_files(repo_url, file_paths)
77
  if zip_path:
78
  return send_file(zip_path, as_attachment=True, download_name=zip_filename)
79
  else:
@@ -82,6 +84,5 @@ def index():
82
 
83
  return render_template('index.html')
84
 
85
-
86
  if __name__ == '__main__':
87
  app.run(debug=True, host="0.0.0.0")
 
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。"""
15
  try:
16
  temp_dir = tempfile.mkdtemp()
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}@")
24
+ Repo.clone_from(repo_url_with_token, local_repo_path)
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):
52
  """解析 GitHub URL,提取仓库 URL 和文件路径。"""
53
  match = re.match(r'https://github\.com/([^/]+)/([^/]+)/tree/([^/]+)(.*)', 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'])
65
  def index():
66
  if request.method == 'POST':
67
  repo_path = request.form.get('repo_path')
68
+ github_token = request.form.get('github_token')
69
  if not repo_path:
70
  flash('请提供 GitHub 链接', 'error')
71
  return render_template('index.html')
 
75
  flash('无效的 GitHub 链接', 'error')
76
  return render_template('index.html')
77
 
78
+ zip_path, zip_filename = download_github_files(repo_url, file_paths, github_token)
79
  if zip_path:
80
  return send_file(zip_path, as_attachment=True, download_name=zip_filename)
81
  else:
 
84
 
85
  return render_template('index.html')
86
 
 
87
  if __name__ == '__main__':
88
  app.run(debug=True, host="0.0.0.0")