1+ import os
2+ import tempfile
3+ import zipfile
4+ import shutil
5+ from urllib .request import urlretrieve
6+
7+ import click
8+
9+
10+ @click .group ()
11+ def aikit ():
12+ """AI toolkit commands for DevHub."""
13+ pass
14+
15+
16+ @aikit .command ()
17+ def init (toolkit_url = 'https://github.com/devhub/devhub-cli-ai-toolkit/archive/refs/heads/main.zip' ):
18+ """Initialize AI toolkit by downloading and extracting toolkit files."""
19+
20+ working_dir = os .environ .get ('WORKING_DIR' , os .getcwd ())
21+
22+ click .echo (f'Downloading AI toolkit... { toolkit_url } ' )
23+
24+ # Create temporary file for download
25+ with tempfile .NamedTemporaryFile (suffix = '.zip' , delete = False ) as temp_file :
26+ temp_filename = temp_file .name
27+
28+ try :
29+ # Download the zip file
30+ urlretrieve (toolkit_url , temp_filename )
31+ click .echo ('Download completed.' )
32+
33+ # Extract the zip file
34+ click .echo ('Extracting toolkit files...' )
35+
36+ with zipfile .ZipFile (temp_filename , 'r' ) as zip_ref :
37+ # Get all file names in the zip
38+ all_files = zip_ref .namelist ()
39+
40+ # Filter files that are in the devhub-cli-ai-toolkit-main directory
41+ toolkit_files = [f for f in all_files if f .startswith ('devhub-cli-ai-toolkit-main/' ) and f != 'devhub-cli-ai-toolkit-main/' ]
42+
43+ extracted_count = 0
44+ skipped_count = 0
45+
46+ for file_path in toolkit_files :
47+ # Remove the root directory prefix
48+ relative_path = file_path [len ('devhub-cli-ai-toolkit-main/' ):]
49+
50+ # Skip empty relative paths or directories
51+ if not relative_path or file_path .endswith ('/' ):
52+ continue
53+
54+ target_path = os .path .join (working_dir , relative_path )
55+
56+ # Check if file already exists
57+ if os .path .exists (target_path ):
58+ click .echo (f'Skipping existing file: { relative_path } ' )
59+ skipped_count += 1
60+ continue
61+
62+ # Create directory if it doesn't exist
63+ target_dir = os .path .dirname (target_path )
64+ if target_dir :
65+ os .makedirs (target_dir , exist_ok = True )
66+
67+ # Extract file
68+ with zip_ref .open (file_path ) as source , open (target_path , 'wb' ) as target :
69+ shutil .copyfileobj (source , target )
70+
71+ click .echo (f'Extracted: { relative_path } ' )
72+ extracted_count += 1
73+
74+ click .echo (click .style (f'AI toolkit initialization completed!' , fg = 'green' ))
75+ click .echo (f'Extracted { extracted_count } files, skipped { skipped_count } existing files.' )
76+
77+ except Exception as e :
78+ click .echo (click .style (f'Error during AI toolkit initialization: { str (e )} ' , fg = 'red' ))
79+ finally :
80+ # Clean up temporary file
81+ if os .path .exists (temp_filename ):
82+ os .unlink (temp_filename )
0 commit comments