Skip to content

Commit bfac19c

Browse files
committed
feat: Add requirements importer for multi-language dependency files (#126)
Implements package import functionality for: - requirements.txt (Python/pip) - package.json (Node.js/npm) - Gemfile (Ruby/bundler) - Cargo.toml (Rust/cargo) - go.mod (Go modules) Features: - Auto-detection of requirements files - Dry-run mode for preview - Dev dependency support - Unified CLI interface - 50+ unit tests
1 parent da3e635 commit bfac19c

File tree

4 files changed

+1861
-0
lines changed

4 files changed

+1861
-0
lines changed

README_IMPORT.md

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
# Cortex Import - Package Requirements Importer
2+
3+
Import dependencies from various package manager requirement files.
4+
5+
## Quick Start
6+
7+
```bash
8+
# Import from a single file
9+
cortex import requirements.txt
10+
cortex import package.json
11+
12+
# Import from all detected files
13+
cortex import --all
14+
15+
# Preview what would be installed
16+
cortex import --dry-run requirements.txt
17+
18+
# Include dev dependencies
19+
cortex import --dev package.json
20+
```
21+
22+
## Supported File Formats
23+
24+
| File | Package Manager | Language |
25+
|------|-----------------|----------|
26+
| `requirements.txt` | pip | Python |
27+
| `package.json` | npm | Node.js |
28+
| `Gemfile` | bundler | Ruby |
29+
| `Cargo.toml` | cargo | Rust |
30+
| `go.mod` | go | Go |
31+
32+
## Commands
33+
34+
### Import Single File
35+
36+
```bash
37+
cortex import <file>
38+
39+
# Examples:
40+
cortex import requirements.txt
41+
cortex import package.json
42+
cortex import Gemfile
43+
cortex import Cargo.toml
44+
cortex import go.mod
45+
```
46+
47+
### Import All Detected Files
48+
49+
```bash
50+
cortex import --all
51+
52+
# From a specific directory
53+
cortex import --all --dir /path/to/project
54+
```
55+
56+
### Detect Files Only
57+
58+
```bash
59+
cortex import --detect
60+
61+
# Output:
62+
# Detected requirements files:
63+
# - requirements.txt
64+
# - package.json
65+
```
66+
67+
### Dry Run (Preview)
68+
69+
```bash
70+
cortex import --dry-run requirements.txt
71+
72+
# Output:
73+
# Parsing requirements.txt (pip)...
74+
# Dependencies (3):
75+
# - flask (2.0.1)
76+
# - requests (2.28.0)
77+
# - django
78+
# Installing...
79+
# Success: [DRY RUN] Would install 3 packages via pip
80+
```
81+
82+
### Include Dev Dependencies
83+
84+
```bash
85+
cortex import --dev package.json
86+
```
87+
88+
## Options
89+
90+
| Option | Short | Description |
91+
|--------|-------|-------------|
92+
| `--all` | `-a` | Import from all detected files |
93+
| `--detect` | `-d` | Detect files without importing |
94+
| `--dry-run` | `-n` | Preview without installing |
95+
| `--dev` | | Include dev dependencies |
96+
| `--verbose` | `-v` | Show detailed output |
97+
| `--dir` | | Directory to search (default: `.`) |
98+
99+
## File Format Examples
100+
101+
### requirements.txt (Python)
102+
103+
```txt
104+
flask==2.0.1
105+
requests>=2.28.0
106+
django~=4.0
107+
celery[redis]
108+
# Comments are ignored
109+
```
110+
111+
Supported specifiers:
112+
- Exact: `package==1.0.0`
113+
- Range: `package>=1.0,<2.0`
114+
- Compatible: `package~=1.0`
115+
- Extras: `package[extra1,extra2]`
116+
- Environment markers: `package; python_version >= "3.8"`
117+
118+
### package.json (Node.js)
119+
120+
```json
121+
{
122+
"dependencies": {
123+
"express": "^4.18.0",
124+
"lodash": "~4.17.21"
125+
},
126+
"devDependencies": {
127+
"jest": "^29.0.0",
128+
"eslint": "^8.0.0"
129+
}
130+
}
131+
```
132+
133+
### Gemfile (Ruby)
134+
135+
```ruby
136+
source 'https://rubygems.org'
137+
138+
gem 'rails', '7.0.0'
139+
gem 'pg', '>= 0.18', '< 2.0'
140+
141+
group :development do
142+
gem 'rubocop'
143+
end
144+
145+
group :test do
146+
gem 'rspec', '3.12.0'
147+
end
148+
```
149+
150+
### Cargo.toml (Rust)
151+
152+
```toml
153+
[package]
154+
name = "myproject"
155+
version = "0.1.0"
156+
157+
[dependencies]
158+
serde = "1.0"
159+
tokio = { version = "1.28", features = ["full"] }
160+
161+
[dev-dependencies]
162+
criterion = "0.5"
163+
```
164+
165+
### go.mod (Go)
166+
167+
```go
168+
module example.com/myproject
169+
170+
go 1.21
171+
172+
require (
173+
github.com/gin-gonic/gin v1.9.1
174+
github.com/spf13/cobra v1.7.0
175+
)
176+
```
177+
178+
Indirect dependencies (marked with `// indirect`) are skipped.
179+
180+
## Python API
181+
182+
```python
183+
from cortex.requirements_importer import (
184+
RequirementsImporter,
185+
RequirementsParser,
186+
PackageJsonParser,
187+
)
188+
189+
# Create importer
190+
importer = RequirementsImporter(dry_run=True, verbose=True)
191+
192+
# Detect files in current directory
193+
files = importer.detect_files('.')
194+
print(f"Found: {files}")
195+
196+
# Parse a single file
197+
result = importer.parse_file('requirements.txt')
198+
print(f"Dependencies: {len(result.dependencies)}")
199+
print(f"Package manager: {result.package_manager.value}")
200+
201+
# Install
202+
success, message = importer.install(result, include_dev=True)
203+
print(f"Install: {message}")
204+
205+
# Get summary
206+
print(importer.summary())
207+
```
208+
209+
## Exit Codes
210+
211+
| Code | Description |
212+
|------|-------------|
213+
| 0 | Success |
214+
| 1 | Error (file not found, parse error, install failed) |
215+
216+
## Requirements
217+
218+
- Python 3.8+
219+
- Package managers must be installed for installation:
220+
- pip (Python) - usually included with Python
221+
- npm (Node.js) - install Node.js
222+
- bundler (Ruby) - `gem install bundler`
223+
- cargo (Rust) - install Rust via rustup
224+
- go (Go) - install Go
225+
226+
## Files
227+
228+
- `cortex/requirements_importer.py` - Core implementation
229+
- `cortex/import_cli.py` - CLI commands
230+
- `tests/test_requirements_importer.py` - Unit tests (50+ tests)
231+
- `README_IMPORT.md` - This documentation
232+
233+
## Related Issue
234+
235+
- [#126 Package Import from Requirements Files](https://github.com/cortexlinux/cortex/issues/126)

0 commit comments

Comments
 (0)