Make a complete, running app (a CRM, a shop, a blog) with one command, such as dbbasic crm.

0 downloads

dbbasic-cli/src/dbbasic/bootstrap.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-cli.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-cli.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-cli.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Version 0.1.2: Working instant apps! a36b93a 1 year, 2 months ago History
bootstrap.py · 5.8 KB · 197 lines
1"""
2Bootstrap DBBasic core for new installations
3"""
4
5import os
6import subprocess
7import sys
8from pathlib import Path
9from rich.console import Console
10from rich.progress import Progress, SpinnerColumn, TextColumn
11
12console = Console()
13
14def get_dbbasic_home():
15 """Get or create DBBasic home directory"""
16 dbbasic_home = Path.home() / ".dbbasic"
17 dbbasic_home.mkdir(exist_ok=True)
18 return dbbasic_home
19
20def is_dbbasic_installed():
21 """Check if DBBasic core is installed"""
22 dbbasic_home = get_dbbasic_home()
23 core_dir = dbbasic_home / "core"
24 return core_dir.exists() and (core_dir / "dbbasic_crud_engine.py").exists()
25
26def install_dbbasic():
27 """Install DBBasic core from GitHub"""
28 dbbasic_home = get_dbbasic_home()
29 core_dir = dbbasic_home / "core"
30
31 with Progress(
32 SpinnerColumn(),
33 TextColumn("[progress.description]{task.description}"),
34 console=console,
35 ) as progress:
36
37 # Clone repository
38 task = progress.add_task("Installing DBBasic core...", total=4)
39
40 if core_dir.exists():
41 # Update existing installation
42 progress.update(task, description="Updating DBBasic core...")
43 subprocess.run(
44 ["git", "pull"],
45 cwd=str(core_dir),
46 capture_output=True,
47 check=False
48 )
49 else:
50 # Fresh installation
51 progress.update(task, description="Downloading DBBasic core...")
52 result = subprocess.run(
53 ["git", "clone", "https://github.com/askrobots/dbbasic.git", str(core_dir)],
54 capture_output=True,
55 text=True
56 )
57 if result.returncode != 0:
58 console.print(f"[red]Failed to download DBBasic: {result.stderr}[/red]")
59 raise SystemExit(1)
60
61 progress.advance(task)
62
63 # Install dependencies
64 progress.update(task, description="Installing dependencies...")
65 requirements_file = core_dir / "requirements.txt"
66
67 # Create minimal requirements if file doesn't exist
68 if not requirements_file.exists():
69 requirements = """
70fastapi>=0.100.0
71uvicorn>=0.24.0
72duckdb>=0.9.0
73pyyaml>=6.0
74httpx>=0.25.0
75websockets>=11.0
76jinja2>=3.1.0
77python-multipart>=0.0.5
78pystructui>=0.1.0
79"""
80 requirements_file.write_text(requirements.strip())
81
82 # Install requirements
83 subprocess.run(
84 [sys.executable, "-m", "pip", "install", "-q", "-r", str(requirements_file)],
85 capture_output=True
86 )
87 progress.advance(task)
88
89 # Create templates directory
90 progress.update(task, description="Setting up templates...")
91 templates_dir = core_dir / "templates"
92 templates_dir.mkdir(exist_ok=True)
93
94 # Create a basic CRM template if it doesn't exist
95 crm_template_dir = templates_dir / "crm"
96 crm_template_dir.mkdir(exist_ok=True)
97
98 crm_config = crm_template_dir / "config.yaml"
99 if not crm_config.exists():
100 crm_config.write_text("""
101# CRM Template Configuration
102name: CRM
103description: Customer Relationship Management
104
105models:
106 contacts:
107 fields:
108 - {name: name, type: string, required: true}
109 - {name: email, type: email, unique: true}
110 - {name: phone, type: string}
111 - {name: company, type: string}
112 - {name: status, type: enum, values: [lead, prospect, customer]}
113
114 deals:
115 fields:
116 - {name: title, type: string, required: true}
117 - {name: contact_id, type: reference, model: contacts}
118 - {name: amount, type: decimal}
119 - {name: stage, type: enum, values: [discovery, proposal, negotiation, closed]}
120 - {name: close_date, type: date}
121
122views:
123 - {name: dashboard, type: dashboard}
124 - {name: contacts, type: table, model: contacts}
125 - {name: pipeline, type: kanban, model: deals, group_by: stage}
126""")
127 progress.advance(task)
128
129 # The data service is what we need
130 progress.update(task, description="Finalizing installation...")
131 data_service = core_dir / "dbbasic_crud_engine.py"
132 if not data_service.exists():
133 # Create a minimal working CRUD engine
134 crud_engine.write_text('''
135"""
136DBBasic CRUD Engine - Minimal working version
137"""
138
139from fastapi import FastAPI
140import uvicorn
141import yaml
142from pathlib import Path
143
144def create_app(config_file="dbbasic.yaml"):
145 """Create FastAPI application from config"""
146 app = FastAPI(title="DBBasic App")
147
148 # Load configuration
149 config_path = Path(config_file)
150 if config_path.exists():
151 with open(config_path) as f:
152 config = yaml.safe_load(f)
153 else:
154 config = {"project": {"name": "DBBasic App"}}
155
156 @app.get("/")
157 async def root():
158 return {
159 "message": f"Welcome to {config.get('project', {}).get('name', 'DBBasic')}",
160 "status": "running",
161 "config": config_file
162 }
163
164 @app.get("/health")
165 async def health():
166 return {"status": "healthy"}
167
168 return app
169
170if __name__ == "__main__":
171 app = create_app()
172 uvicorn.run(app, host="0.0.0.0", port=8000)
173''')
174
175 progress.advance(task)
176
177 console.print("\n[green]✅ DBBasic core installed successfully![/green]")
178 return core_dir
179
180def ensure_dbbasic():
181 """Ensure DBBasic is installed, install if not"""
182 if not is_dbbasic_installed():
183 console.print("[yellow]DBBasic core not found. Installing...[/yellow]")
184 return install_dbbasic()
185 else:
186 dbbasic_home = get_dbbasic_home()
187 return dbbasic_home / "core"
188
189def get_dbbasic_dir():
190 """Get the DBBasic directory, installing if necessary"""
191 # First check if we're in development mode (local dbbasic exists)
192 local_dbbasic = Path("/Users/danq/websheet/dbbasic")
193 if local_dbbasic.exists():
194 return local_dbbasic
195
196 # Otherwise ensure it's installed
197 return ensure_dbbasic()