-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
407 lines (327 loc) · 11.5 KB
/
Copy pathsetup.py
File metadata and controls
407 lines (327 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/env python3
"""
JSBSim XML Generator - Initial Setup
This script helps you set up your environment for the first time.
It will:
1. Detect your operating system
2. Search for FlightGear installation
3. Verify Python dependencies
4. Create config.yaml with detected settings
5. Create required directories
Usage:
python setup.py
Options:
--reconfigure Force reconfiguration even if config.yaml exists
--minimal Create minimal config without FlightGear detection
--batch Non-interactive mode (use defaults for all prompts)
"""
import os
import sys
import platform
import shutil
import argparse
from pathlib import Path
from typing import Optional, Dict, Any
def print_header(text: str):
"""Print formatted header"""
print()
print("=" * 70)
print(text)
print("=" * 70)
def print_step(step_num: int, text: str):
"""Print step header"""
print()
print(f"[Step {step_num}] {text}")
print("-" * 70)
def detect_os() -> str:
"""Detect operating system
Returns:
OS name: 'windows', 'linux', 'darwin'
"""
return platform.system().lower()
def find_flightgear(os_name: str) -> Optional[Path]:
"""Find FlightGear executable
Args:
os_name: Operating system name
Returns:
Path to FlightGear executable, or None
"""
print("[INFO] Searching for FlightGear...")
# Try system PATH first
fg_cmd = 'fgfs.exe' if os_name == 'windows' else 'fgfs'
path_result = shutil.which(fg_cmd)
if path_result:
print(f"[OK] Found in PATH: {path_result}")
return Path(path_result)
# Check common installation locations
common_paths = []
if os_name == 'windows':
common_paths = [
Path("C:/Program Files/FlightGear/bin/fgfs.exe"),
Path("C:/FlightGear/bin/fgfs.exe"),
Path("D:/Program Files/FlightGear/bin/fgfs.exe"),
Path("D:/FlightGear/bin/fgfs.exe"),
]
elif os_name == 'linux':
common_paths = [
Path("/usr/bin/fgfs"),
Path("/usr/local/bin/fgfs"),
Path("/opt/FlightGear/bin/fgfs"),
]
elif os_name == 'darwin':
common_paths = [
Path("/Applications/FlightGear.app/Contents/MacOS/fgfs"),
Path("/usr/local/bin/fgfs"),
]
for path in common_paths:
if path.exists():
print(f"[OK] Found at: {path}")
return path
print("[WARN] FlightGear not found")
return None
def check_python_version() -> bool:
"""Check Python version
Returns:
True if Python >= 3.8
"""
version = sys.version_info
if version.major >= 3 and version.minor >= 8:
print(f"[OK] Python {version.major}.{version.minor}.{version.micro}")
return True
else:
print(f"[WARN] Python {version.major}.{version.minor}.{version.micro} detected")
print(" Python 3.8 or higher is recommended")
return False
def check_dependencies() -> Dict[str, bool]:
"""Check required Python packages
Returns:
Dictionary of package availability
"""
packages = {
'yaml': 'PyYAML',
'pandas': 'pandas',
'openpyxl': 'openpyxl',
'jsbsim': 'jsbsim',
'scipy': 'scipy',
'numpy': 'numpy',
}
results = {}
for module_name, package_name in packages.items():
try:
__import__(module_name)
results[package_name] = True
except ImportError:
results[package_name] = False
return results
def prompt_user(question: str, default: str = 'y', batch_mode: bool = False) -> str:
"""Prompt user for input
Args:
question: Question to ask
default: Default answer
batch_mode: If True, use default without prompting
Returns:
User response or default
"""
if batch_mode:
return default
response = input(f"{question} [{default}]: ").strip()
return response if response else default
def create_config_yaml(output_path: Path, os_name: str,
flightgear_path: Optional[Path],
batch_mode: bool = False):
"""Create config.yaml file
Args:
output_path: Path to output config.yaml
os_name: Operating system name
flightgear_path: Path to FlightGear (or None)
batch_mode: Non-interactive mode
"""
print()
print(f"Creating config.yaml at: {output_path}")
# Read default config template
template_path = output_path.parent / 'config' / 'config.default.yaml'
if not template_path.exists():
print(f"[ERROR] Default config template not found: {template_path}")
return False
with open(template_path, 'r', encoding='utf-8') as f:
config_content = f.read()
# Replace values
if flightgear_path:
# Use forward slashes for cross-platform compatibility
fg_path_str = str(flightgear_path).replace('\\', '/')
config_content = config_content.replace(
'flightgear_exe: null',
f'flightgear_exe: "{fg_path_str}"'
)
config_content = config_content.replace(
'os: null',
f'os: "{os_name}"'
)
# Write config.yaml
with open(output_path, 'w', encoding='utf-8') as f:
f.write(config_content)
print("[OK] config.yaml created successfully")
return True
def create_directories(project_root: Path):
"""Create required directories
Args:
project_root: Project root directory
"""
directories = [
'output',
'temp',
'logs',
'dataout',
]
print()
print("Creating required directories...")
for dir_name in directories:
dir_path = project_root / dir_name
# If a file exists with the same name, remove it
if dir_path.exists() and dir_path.is_file():
print(f" [WARN] {dir_name} exists as a file, removing...")
dir_path.unlink()
dir_path.mkdir(parents=True, exist_ok=True)
print(f" [OK] {dir_name}/")
def print_next_steps(flightgear_found: bool, missing_packages: list):
"""Print next steps after setup
Args:
flightgear_found: Whether FlightGear was found
missing_packages: List of missing Python packages
"""
print_header("SETUP COMPLETE")
print()
print("Configuration file created: config.yaml")
print()
if missing_packages:
print("NEXT STEPS:")
print()
print("1. Install missing Python packages:")
print(f" pip install {' '.join(missing_packages)}")
print()
print(" Or install all dependencies:")
print(" pip install -r requirements.txt")
print()
if not flightgear_found:
print("2. Install FlightGear (optional, for visualization):")
print()
os_name = detect_os()
if os_name == 'windows':
print(" Download from: https://www.flightgear.org/download/")
elif os_name == 'linux':
print(" Ubuntu/Debian: sudo apt-get install flightgear")
print(" Fedora/RHEL: sudo yum install flightgear")
elif os_name == 'darwin':
print(" Download from: https://www.flightgear.org/download/")
print()
print(" After installation, run 'python setup.py --reconfigure'")
print()
print("3. Test your installation:")
print(" python tests/test_jsbsim_load.py ExampleAircraft")
print()
print("4. Generate your first aircraft:")
print(" python src/generate_jsbsim_from_gsheet.py \\")
print(" -i templates/Aircraft_Input_Template.xlsx \\")
print(" -o output/MyAircraft")
print()
print("For more information:")
print(" - README.md - Project overview")
print(" - config/README.md - Configuration guide")
print(" - docs/user_guide/ - User documentation")
print()
def main():
"""Main setup function"""
parser = argparse.ArgumentParser(
description='JSBSim XML Generator - Initial Setup',
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument('--reconfigure', action='store_true',
help='Force reconfiguration even if config.yaml exists')
parser.add_argument('--minimal', action='store_true',
help='Create minimal config without FlightGear detection')
parser.add_argument('--batch', action='store_true',
help='Non-interactive mode (use defaults)')
args = parser.parse_args()
print_header("JSBSim XML Generator - Initial Setup")
# Determine project root
project_root = Path(__file__).resolve().parent
config_path = project_root / 'config.yaml'
# Check if config already exists
if config_path.exists() and not args.reconfigure:
print()
print(f"[INFO] Configuration file already exists: {config_path}")
print()
response = prompt_user(
"Do you want to reconfigure?",
default='n',
batch_mode=args.batch
)
if response.lower() not in ['y', 'yes']:
print()
print("Setup cancelled. Use --reconfigure to force reconfiguration.")
return 0
# Step 1: Detect OS
print_step(1, "Detecting Operating System")
os_name = detect_os()
print(f"[OK] Detected OS: {os_name}")
# Step 2: Check Python version
print_step(2, "Checking Python Version")
check_python_version()
# Step 3: Check dependencies
print_step(3, "Checking Python Dependencies")
deps = check_dependencies()
missing_packages = [pkg for pkg, available in deps.items() if not available]
if missing_packages:
print()
print("[WARN] Missing packages:")
for pkg in missing_packages:
print(f" - {pkg}")
print()
print("You can install them after setup with:")
print(" pip install -r requirements.txt")
else:
print("[OK] All required packages are installed")
# Step 4: Find FlightGear
flightgear_path = None
if not args.minimal:
print_step(4, "Finding FlightGear Installation")
flightgear_path = find_flightgear(os_name)
if not flightgear_path:
print()
print("[INFO] FlightGear is optional but recommended for visualization")
response = prompt_user(
"Do you want to specify FlightGear path manually?",
default='n',
batch_mode=args.batch
)
if response.lower() in ['y', 'yes']:
manual_path = input("Enter FlightGear executable path: ").strip()
if manual_path and Path(manual_path).exists():
flightgear_path = Path(manual_path)
print(f"[OK] Using: {flightgear_path}")
else:
print("[WARN] Invalid path, continuing without FlightGear")
# Step 5: Create config.yaml
print_step(5, "Creating Configuration File")
if not create_config_yaml(config_path, os_name, flightgear_path, args.batch):
return 1
# Step 6: Create directories
print_step(6, "Creating Required Directories")
create_directories(project_root)
# Print next steps
print_next_steps(flightgear_path is not None, missing_packages)
return 0
if __name__ == '__main__':
try:
sys.exit(main())
except KeyboardInterrupt:
print()
print()
print("Setup cancelled by user")
sys.exit(1)
except Exception as e:
print()
print(f"[ERROR] Setup failed: {e}")
import traceback
traceback.print_exc()
sys.exit(1)