wuhp commited on
Commit
4d4462c
Β·
verified Β·
1 Parent(s): e8735bb

Update fetch_code.py

Browse files
Files changed (1) hide show
  1. fetch_code.py +35 -31
fetch_code.py CHANGED
@@ -3,50 +3,54 @@ import sys
3
  from pathlib import Path
4
  from huggingface_hub import snapshot_download
5
 
6
- # Get Space 2 URL from environment
7
- SPACE2_URL = os.getenv("SPACE2_URL") # e.g., "username/space2-fetcher"
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
 
10
- if not SPACE2_URL or not HF_TOKEN:
11
- print("ERROR: SPACE2_URL and HF_TOKEN must be set as environment variables")
 
12
  sys.exit(1)
13
 
14
- # Download files from Space 2's repository
15
- # Space 2 has already fetched the code from Space 1
16
  try:
17
- print(f"Downloading code from Space 2: {SPACE2_URL}")
18
 
19
- local_dir = snapshot_download(
20
- repo_id=SPACE2_URL,
 
 
 
 
 
21
  repo_type="space",
22
  token=HF_TOKEN,
23
- local_dir="/app/code",
24
- local_dir_use_symlinks=False,
25
- allow_patterns=["fetched_code/**/*"] # Only get the fetched_code directory
26
  )
27
 
28
- # Move files from fetched_code subdirectory to /app/code
29
- fetched_dir = Path("/app/code/fetched_code")
30
- if fetched_dir.exists():
31
- import shutil
32
- for item in fetched_dir.iterdir():
33
- dest = Path("/app/code") / item.name
34
- if dest.exists():
35
- if dest.is_dir():
36
- shutil.rmtree(dest)
37
- else:
38
- dest.unlink()
39
- shutil.move(str(item), str(dest))
40
-
41
- # Remove the now-empty fetched_code directory
42
- fetched_dir.rmdir()
43
-
44
- print("βœ… Successfully downloaded and extracted code")
45
 
46
  # List files for verification
47
- files = list(Path("/app/code").rglob("*"))
48
- print(f"Total files: {len([f for f in files if f.is_file()])}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  except Exception as e:
51
  print(f"❌ Error downloading code: {e}")
 
 
52
  sys.exit(1)
 
3
  from pathlib import Path
4
  from huggingface_hub import snapshot_download
5
 
6
+ # Get PRIVATE Space 1 URL from environment (not Space 2!)
7
+ PRIVATE_SPACE_URL = os.getenv("PRIVATE_SPACE_URL") # e.g., "username/private-space"
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
 
10
+ if not PRIVATE_SPACE_URL or not HF_TOKEN:
11
+ print("ERROR: PRIVATE_SPACE_URL and HF_TOKEN must be set")
12
+ print("Add them as secrets in your Space settings")
13
  sys.exit(1)
14
 
15
+ # Download files directly from the private Space 1
 
16
  try:
17
+ print(f"Downloading code from private space: {PRIVATE_SPACE_URL}")
18
 
19
+ # Create /app/code directory
20
+ code_dir = Path("/app/code")
21
+ code_dir.mkdir(exist_ok=True, parents=True)
22
+
23
+ # Download all files from Space 1
24
+ snapshot_download(
25
+ repo_id=PRIVATE_SPACE_URL,
26
  repo_type="space",
27
  token=HF_TOKEN,
28
+ local_dir=str(code_dir),
29
+ local_dir_use_symlinks=False
 
30
  )
31
 
32
+ print("βœ… Successfully downloaded code")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  # List files for verification
35
+ files = [f for f in code_dir.rglob("*") if f.is_file()]
36
+ print(f"Total files downloaded: {len(files)}")
37
+
38
+ # Show first 20 files
39
+ for i, f in enumerate(files[:20]):
40
+ print(f" - {f.relative_to(code_dir)}")
41
+
42
+ if len(files) > 20:
43
+ print(f" ... and {len(files) - 20} more files")
44
+
45
+ # Check for package.json
46
+ package_json = code_dir / "package.json"
47
+ if package_json.exists():
48
+ print("βœ… Found package.json")
49
+ else:
50
+ print("⚠️ No package.json found - npm install will be skipped")
51
 
52
  except Exception as e:
53
  print(f"❌ Error downloading code: {e}")
54
+ import traceback
55
+ traceback.print_exc()
56
  sys.exit(1)