migrate_to_uv.sh 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #!/bin/bash
  2. # =============================================================================
  3. # UV Migration Script
  4. # =============================================================================
  5. # Migrates from pip/venv to uv package manager
  6. # Includes cleanup, verification, and safety checks
  7. # =============================================================================
  8. set -e # Exit on any error
  9. # Colors for output
  10. RED='\033[0;31m'
  11. GREEN='\033[0;32m'
  12. YELLOW='\033[1;33m'
  13. BLUE='\033[0;34m'
  14. NC='\033[0m' # No Color
  15. # Script version
  16. VERSION="1.0.0"
  17. # Print header
  18. echo -e "${BLUE}=================================${NC}"
  19. echo -e "${BLUE} UV Migration Script v${VERSION}${NC}"
  20. echo -e "${BLUE}=================================${NC}"
  21. echo ""
  22. # Function to print colored output
  23. print_success() {
  24. echo -e "${GREEN}✅ $1${NC}"
  25. }
  26. print_warning() {
  27. echo -e "${YELLOW}⚠️ $1${NC}"
  28. }
  29. print_error() {
  30. echo -e "${RED}❌ $1${NC}"
  31. }
  32. print_info() {
  33. echo -e "${BLUE}ℹ️ $1${NC}"
  34. }
  35. # Function to check if command exists
  36. command_exists() {
  37. command -v "$1" >/dev/null 2>&1
  38. }
  39. # Function to get file size in human readable format
  40. get_size() {
  41. if [[ "$OSTYPE" == "darwin"* ]]; then
  42. # macOS
  43. du -h "$1" 2>/dev/null | cut -f1 || echo "0B"
  44. else
  45. # Linux
  46. du -sh "$1" 2>/dev/null | cut -f1 || echo "0B"
  47. fi
  48. }
  49. # Function to count files
  50. count_files() {
  51. find "$1" -type f 2>/dev/null | wc -l | tr -d ' '
  52. }
  53. # Check if we're in the project root
  54. if [[ ! -f "pyproject.toml" ]]; then
  55. print_error "pyproject.toml not found. Please run this script from the project root."
  56. exit 1
  57. fi
  58. print_info "Starting migration process..."
  59. echo ""
  60. # =============================================================================
  61. # Step 1: Pre-migration Analysis
  62. # =============================================================================
  63. echo -e "${BLUE}📊 Pre-migration Analysis${NC}"
  64. echo "----------------------------"
  65. # Count cache files before cleanup
  66. PYCACHE_DIRS=$(find . -path "./.venv" -prune -o -name "__pycache__" -type d -print | wc -l | tr -d ' ')
  67. PYC_FILES=$(find . -path "./.venv" -prune -o -name "*.pyc" -type f -print | wc -l | tr -d ' ')
  68. echo "📁 Cache directories found: $PYCACHE_DIRS"
  69. echo "📄 .pyc files found: $PYC_FILES"
  70. # Check if pytest cache exists
  71. if [[ -d ".pytest_cache" ]]; then
  72. PYTEST_SIZE=$(get_size ".pytest_cache")
  73. echo "🧪 Pytest cache size: $PYTEST_SIZE"
  74. else
  75. echo "🧪 No pytest cache found"
  76. fi
  77. # Check for old venv
  78. if [[ -d "venv" ]]; then
  79. print_warning "Old 'venv' directory still exists!"
  80. VENV_SIZE=$(get_size "venv")
  81. echo "📦 Old venv size: $VENV_SIZE"
  82. else
  83. print_success "No old 'venv' directory found"
  84. fi
  85. # Check for requirements.txt
  86. if [[ -f "requirements.txt" ]]; then
  87. print_warning "requirements.txt still exists (should be removed after migration)"
  88. else
  89. print_success "requirements.txt already removed"
  90. fi
  91. echo ""
  92. # =============================================================================
  93. # Step 2: UV Installation Check
  94. # =============================================================================
  95. echo -e "${BLUE}🔧 UV Installation Check${NC}"
  96. echo "----------------------------"
  97. if command_exists uv; then
  98. UV_VERSION=$(uv --version | head -1)
  99. print_success "UV installed: $UV_VERSION"
  100. else
  101. print_error "UV not found! Installing UV..."
  102. if command_exists curl; then
  103. curl -LsSf https://astral.sh/uv/install.sh | sh
  104. # Add to PATH for this session
  105. export PATH="$HOME/.local/bin:$PATH"
  106. if command_exists uv; then
  107. UV_VERSION=$(uv --version | head -1)
  108. print_success "UV installed successfully: $UV_VERSION"
  109. else
  110. print_error "UV installation failed!"
  111. exit 1
  112. fi
  113. else
  114. print_error "curl not found! Please install UV manually."
  115. exit 1
  116. fi
  117. fi
  118. echo ""
  119. # =============================================================================
  120. # Step 3: Cache Cleanup
  121. # =============================================================================
  122. echo -e "${BLUE}🧹 Cache Cleanup${NC}"
  123. echo "-------------------"
  124. # Ask for confirmation unless --force flag is provided
  125. if [[ "$1" != "--force" ]]; then
  126. echo "This will delete:"
  127. echo " - $PYCACHE_DIRS __pycache__ directories"
  128. echo " - $PYC_FILES .pyc files"
  129. echo " - .pytest_cache directory (if exists)"
  130. echo ""
  131. read -p "Continue with cleanup? (y/N): " -n 1 -r
  132. echo
  133. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  134. print_warning "Cleanup cancelled by user"
  135. exit 0
  136. fi
  137. fi
  138. print_info "Starting cleanup..."
  139. # Remove __pycache__ directories (excluding .venv)
  140. DELETED_DIRS=0
  141. while IFS= read -r -d '' dir; do
  142. if [[ "$dir" != *"/.venv/"* ]]; then
  143. rm -rf "$dir"
  144. ((DELETED_DIRS++))
  145. fi
  146. done < <(find . -name "__pycache__" -type d -print0)
  147. # Remove .pyc files (excluding .venv)
  148. DELETED_FILES=0
  149. while IFS= read -r -d '' file; do
  150. if [[ "$file" != *"/.venv/"* ]]; then
  151. rm -f "$file"
  152. ((DELETED_FILES++))
  153. fi
  154. done < <(find . -name "*.pyc" -type f -print0)
  155. # Remove pytest cache
  156. if [[ -d ".pytest_cache" ]]; then
  157. rm -rf ".pytest_cache"
  158. print_success "Removed .pytest_cache directory"
  159. fi
  160. # Remove .pyo files if any
  161. find . -path "./.venv" -prune -o -name "*.pyo" -type f -delete
  162. print_success "Cleanup completed!"
  163. echo " - Removed $DELETED_DIRS __pycache__ directories"
  164. echo " - Removed $DELETED_FILES .pyc files"
  165. echo ""
  166. # =============================================================================
  167. # Step 4: UV Environment Verification
  168. # =============================================================================
  169. echo -e "${BLUE}🔍 UV Environment Verification${NC}"
  170. echo "--------------------------------"
  171. # Check if .venv exists
  172. if [[ -d ".venv" ]]; then
  173. print_success "UV environment (.venv) exists"
  174. else
  175. print_warning "UV environment not found. Creating..."
  176. uv sync
  177. fi
  178. # Check if uv.lock exists
  179. if [[ -f "uv.lock" ]]; then
  180. print_success "uv.lock file exists"
  181. else
  182. print_warning "uv.lock not found. Running uv sync..."
  183. uv sync
  184. fi
  185. # Test imports
  186. print_info "Testing key imports..."
  187. if uv run python -c "import hyperliquid, pandas, requests; print('All imports successful!')" >/dev/null 2>&1; then
  188. print_success "All key dependencies working"
  189. else
  190. print_error "Import test failed!"
  191. exit 1
  192. fi
  193. echo ""
  194. # =============================================================================
  195. # Step 5: Old Files Cleanup (Optional)
  196. # =============================================================================
  197. echo -e "${BLUE}🗑️ Old Files Cleanup${NC}"
  198. echo "-----------------------"
  199. # Check for old venv directory
  200. if [[ -d "venv" ]]; then
  201. VENV_SIZE=$(get_size "venv")
  202. print_warning "Old 'venv' directory found (${VENV_SIZE})"
  203. if [[ "$1" != "--force" ]]; then
  204. read -p "Delete old 'venv' directory? (y/N): " -n 1 -r
  205. echo
  206. if [[ $REPLY =~ ^[Yy]$ ]]; then
  207. rm -rf "venv"
  208. print_success "Removed old 'venv' directory"
  209. else
  210. print_warning "Keeping old 'venv' directory"
  211. fi
  212. else
  213. rm -rf "venv"
  214. print_success "Removed old 'venv' directory"
  215. fi
  216. else
  217. print_success "No old 'venv' directory found"
  218. fi
  219. # Check for requirements.txt
  220. if [[ -f "requirements.txt" ]]; then
  221. print_warning "requirements.txt still exists"
  222. if [[ "$1" != "--force" ]]; then
  223. read -p "Delete requirements.txt? (y/N): " -n 1 -r
  224. echo
  225. if [[ $REPLY =~ ^[Yy]$ ]]; then
  226. rm -f "requirements.txt"
  227. print_success "Removed requirements.txt"
  228. else
  229. print_warning "Keeping requirements.txt"
  230. fi
  231. else
  232. rm -f "requirements.txt"
  233. print_success "Removed requirements.txt"
  234. fi
  235. else
  236. print_success "requirements.txt already removed"
  237. fi
  238. echo ""
  239. # =============================================================================
  240. # Step 6: Final Verification
  241. # =============================================================================
  242. echo -e "${BLUE}✅ Final Verification${NC}"
  243. echo "-----------------------"
  244. # Count remaining cache files
  245. REMAINING_PYCACHE=$(find . -path "./.venv" -prune -o -name "__pycache__" -type d -print | wc -l | tr -d ' ')
  246. REMAINING_PYC=$(find . -path "./.venv" -prune -o -name "*.pyc" -type f -print | wc -l | tr -d ' ')
  247. if [[ $REMAINING_PYCACHE -eq 0 && $REMAINING_PYC -eq 0 ]]; then
  248. print_success "All cache files cleaned"
  249. else
  250. print_warning "$REMAINING_PYCACHE cache dirs and $REMAINING_PYC .pyc files remaining"
  251. fi
  252. # Test UV commands
  253. print_info "Testing UV commands..."
  254. # Test uv run
  255. if uv run python --version >/dev/null 2>&1; then
  256. print_success "uv run working"
  257. else
  258. print_error "uv run failed"
  259. fi
  260. # Test project dependencies
  261. if uv run python -c "import src; print('Project imports working')" >/dev/null 2>&1; then
  262. print_success "Project imports working"
  263. else
  264. print_warning "Project imports need attention"
  265. fi
  266. echo ""
  267. # =============================================================================
  268. # Migration Summary
  269. # =============================================================================
  270. echo -e "${GREEN}🎉 Migration Summary${NC}"
  271. echo "====================="
  272. echo ""
  273. print_success "UV migration completed successfully!"
  274. echo ""
  275. echo "📋 What was done:"
  276. echo " ✅ Cleaned up $DELETED_DIRS cache directories"
  277. echo " ✅ Removed $DELETED_FILES .pyc files"
  278. echo " ✅ Verified UV installation and environment"
  279. echo " ✅ Tested key dependencies"
  280. echo ""
  281. echo "🚀 Next steps:"
  282. echo " • Use 'uv run python script.py' instead of 'python script.py'"
  283. echo " • Use 'uv sync' to install/update dependencies"
  284. echo " • Use 'uv add package_name' to add new packages"
  285. echo ""
  286. echo "📚 Quick reference:"
  287. echo " uv sync # Install dependencies"
  288. echo " uv run python trading_bot.py # Run your bot"
  289. echo " uv add requests # Add new package"
  290. echo " uv remove requests # Remove package"
  291. echo ""
  292. print_success "Happy coding with UV! 🚀"