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