Skip to content

Commit 007a009

Browse files
abstract zipapp to separate dedicated submodule (#215)
1 parent d99c96d commit 007a009

File tree

7 files changed

+64
-57
lines changed

7 files changed

+64
-57
lines changed

crates/djls-project/src/inspector.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod ipc;
22
pub mod pool;
33
pub mod queries;
4+
mod zipapp;
45

56
pub use queries::Query;
67
use serde::Deserialize;

crates/djls-project/src/inspector/ipc.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,25 @@ use std::process::Stdio;
99
use anyhow::Context;
1010
use anyhow::Result;
1111
use serde_json;
12-
use tempfile::NamedTempFile;
1312

13+
use super::zipapp::InspectorFile;
1414
use super::DjlsRequest;
1515
use super::DjlsResponse;
1616
use crate::python::PythonEnvironment;
1717

18-
const INSPECTOR_PYZ: &[u8] = include_bytes!(concat!(
19-
env!("CARGO_WORKSPACE_DIR"),
20-
"/python/dist/djls_inspector.pyz"
21-
));
22-
2318
pub struct InspectorProcess {
2419
child: Child,
2520
stdin: std::process::ChildStdin,
2621
stdout: BufReader<std::process::ChildStdout>,
27-
_zipapp_file: NamedTempFile,
22+
_zipapp_file: InspectorFile,
2823
}
2924

3025
impl InspectorProcess {
3126
pub fn new(python_env: &PythonEnvironment, project_path: &Path) -> Result<Self> {
32-
let mut zipapp_file = tempfile::Builder::new()
33-
.prefix("djls_inspector_")
34-
.suffix(".pyz")
35-
.tempfile()
36-
.context("Failed to create temp file for inspector")?;
37-
38-
zipapp_file
39-
.write_all(INSPECTOR_PYZ)
40-
.context("Failed to write inspector zipapp to temp file")?;
41-
zipapp_file
42-
.flush()
43-
.context("Failed to flush inspector zipapp")?;
44-
45-
let zipapp_path = zipapp_file.path();
27+
let zipapp_file = InspectorFile::create()?;
4628

4729
let mut cmd = Command::new(&python_env.python_path);
48-
cmd.arg(zipapp_path)
30+
cmd.arg(zipapp_file.path())
4931
.stdin(Stdio::piped())
5032
.stdout(Stdio::piped())
5133
.stderr(Stdio::inherit())

crates/djls-project/src/inspector/queries.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,6 @@ pub enum Query {
1212
DjangoInit,
1313
}
1414

15-
#[derive(Serialize, Deserialize)]
16-
#[serde(rename_all = "lowercase")]
17-
pub enum VersionReleaseLevel {
18-
Alpha,
19-
Beta,
20-
Candidate,
21-
Final,
22-
}
23-
2415
#[derive(Serialize, Deserialize)]
2516
pub struct PythonEnvironmentQueryData {
2617
pub sys_base_prefix: PathBuf,
@@ -31,6 +22,15 @@ pub struct PythonEnvironmentQueryData {
3122
pub sys_version_info: (u32, u32, u32, VersionReleaseLevel, u32),
3223
}
3324

25+
#[derive(Serialize, Deserialize)]
26+
#[serde(rename_all = "lowercase")]
27+
pub enum VersionReleaseLevel {
28+
Alpha,
29+
Beta,
30+
Candidate,
31+
Final,
32+
}
33+
3434
#[derive(Serialize, Deserialize)]
3535
pub struct TemplateTagQueryData {
3636
pub templatetags: Vec<TemplateTag>,
@@ -42,9 +42,3 @@ pub struct TemplateTag {
4242
pub module: String,
4343
pub doc: Option<String>,
4444
}
45-
46-
#[derive(Serialize, Deserialize)]
47-
pub struct DjangoInitQueryData {
48-
pub success: bool,
49-
pub message: Option<String>,
50-
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::io::Write;
2+
use std::path::Path;
3+
4+
use anyhow::Context;
5+
use anyhow::Result;
6+
use tempfile::NamedTempFile;
7+
8+
const INSPECTOR_PYZ: &[u8] = include_bytes!(concat!(
9+
env!("CARGO_WORKSPACE_DIR"),
10+
"/python/dist/djls_inspector.pyz"
11+
));
12+
13+
pub struct InspectorFile(NamedTempFile);
14+
15+
impl InspectorFile {
16+
pub fn create() -> Result<Self> {
17+
let mut zipapp_file = tempfile::Builder::new()
18+
.prefix("djls_inspector_")
19+
.suffix(".pyz")
20+
.tempfile()
21+
.context("Failed to create temp file for inspector")?;
22+
23+
zipapp_file
24+
.write_all(INSPECTOR_PYZ)
25+
.context("Failed to write inspector zipapp to temp file")?;
26+
zipapp_file
27+
.flush()
28+
.context("Failed to flush inspector zipapp")?;
29+
30+
Ok(Self(zipapp_file))
31+
}
32+
33+
pub fn path(&self) -> &Path {
34+
self.0.path()
35+
}
36+
}

crates/djls-project/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct DjangoProject {
2626
path: PathBuf,
2727
env: Option<PythonEnvironment>,
2828
template_tags: Option<TemplateTags>,
29-
inspector_pool: Arc<InspectorPool>,
29+
inspector: Arc<InspectorPool>,
3030
}
3131

3232
impl DjangoProject {
@@ -36,7 +36,7 @@ impl DjangoProject {
3636
path,
3737
env: None,
3838
template_tags: None,
39-
inspector_pool: Arc::new(InspectorPool::new()),
39+
inspector: Arc::new(InspectorPool::new()),
4040
}
4141
}
4242

@@ -52,7 +52,7 @@ impl DjangoProject {
5252
let request = DjlsRequest {
5353
query: Query::DjangoInit,
5454
};
55-
let response = self.inspector_pool.query(env, &self.path, &request)?;
55+
let response = self.inspector.query(env, &self.path, &request)?;
5656

5757
if !response.ok {
5858
anyhow::bail!("Failed to initialize Django: {:?}", response.error);
@@ -62,7 +62,7 @@ impl DjangoProject {
6262
let request = DjlsRequest {
6363
query: Query::Templatetags,
6464
};
65-
let response = self.inspector_pool.query(env, &self.path, &request)?;
65+
let response = self.inspector.query(env, &self.path, &request)?;
6666

6767
if let Some(data) = response.data {
6868
self.template_tags = Some(TemplateTags::from_json(&data)?);

python/src/djls_inspector/inspector.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ def handle_request(request: dict[str, Any]) -> DjlsResponse:
6969
return DjlsResponse(ok=True, data=get_installed_templatetags())
7070

7171
elif query == Query.DJANGO_INIT:
72-
return DjlsResponse(ok=True, data=initialize_django())
72+
success, error = initialize_django()
73+
return DjlsResponse(ok=success, data=None, error=error)
7374

7475
return DjlsResponse(ok=False, error=f"Unhandled query type: {query}")
7576

python/src/djls_inspector/queries.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,8 @@ def get_installed_templatetags() -> TemplateTagQueryData:
9090
return TemplateTagQueryData(templatetags=templatetags)
9191

9292

93-
@dataclass
94-
class DjangoInitQueryData:
95-
success: bool
96-
message: str | None = None
97-
98-
99-
def initialize_django() -> DjangoInitQueryData:
93+
def initialize_django() -> tuple[bool, str | None]:
94+
"""Initialize Django and return (success, error_message)."""
10095
import os
10196
import django
10297
from django.apps import apps
@@ -122,9 +117,9 @@ def initialize_django() -> DjangoInitQueryData:
122117
current_path = current_path.parent
123118

124119
if not manage_py:
125-
return DjangoInitQueryData(
126-
success=False,
127-
message="Could not find manage.py or DJANGO_SETTINGS_MODULE not set",
120+
return (
121+
False,
122+
"Could not find manage.py or DJANGO_SETTINGS_MODULE not set",
128123
)
129124

130125
# Add project directory to sys.path
@@ -163,12 +158,10 @@ def initialize_django() -> DjangoInitQueryData:
163158
if not apps.ready:
164159
django.setup()
165160

166-
return DjangoInitQueryData(
167-
success=True, message="Django initialized successfully"
168-
)
161+
return True, None
169162

170163
except Exception as e:
171-
return DjangoInitQueryData(success=False, message=str(e))
164+
return False, str(e)
172165

173166

174-
QueryData = PythonEnvironmentQueryData | TemplateTagQueryData | DjangoInitQueryData
167+
QueryData = PythonEnvironmentQueryData | TemplateTagQueryData

0 commit comments

Comments
 (0)