@@ -73,8 +73,60 @@ def write_python_version(python_version):
73
73
f .write (tomlkit .dumps (doc ))
74
74
75
75
76
+ def _generate_pixi_dependencies_config (
77
+ packages , pip_only_packages , repo_name , module_name , python_version , description
78
+ ):
79
+ """Generate pixi dependencies configuration data.
80
+
81
+ Returns:
82
+ tuple: (conda_dependencies, pypi_dependencies, project_config)
83
+ """
84
+ # Project configuration
85
+ project_config = {
86
+ "name" : repo_name ,
87
+ "description" : description
88
+ or "A data science project created with cookiecutter-data-science" ,
89
+ "version" : "0.1.0" ,
90
+ "channels" : ["conda-forge" ],
91
+ "platforms" : ["linux-64" , "osx-64" , "osx-arm64" , "win-64" ],
92
+ }
93
+
94
+ # Conda dependencies
95
+ conda_dependencies = {"python" : f"~={ python_version } .0" }
96
+
97
+ # Filter out pip and pip-only packages from conda dependencies
98
+ conda_packages = [
99
+ p for p in sorted (packages ) if p not in pip_only_packages and p != "pip"
100
+ ]
101
+ for p in conda_packages :
102
+ conda_dependencies [p ] = "*"
103
+
104
+ # PyPI dependencies
105
+ has_pip_packages = any (p in pip_only_packages for p in packages )
106
+ pypi_dependencies = {}
107
+
108
+ if has_pip_packages :
109
+ # Add pip to conda dependencies when we have PyPI packages
110
+ conda_dependencies ["pip" ] = "*"
111
+ for p in sorted (packages ):
112
+ if p in pip_only_packages :
113
+ pypi_dependencies [p ] = "*"
114
+
115
+ # Always add the module as editable PyPI dependency
116
+ pypi_dependencies [module_name ] = {"path" : "." , "editable" : True }
117
+
118
+ return conda_dependencies , pypi_dependencies , project_config
119
+
120
+
76
121
def write_dependencies (
77
- dependencies , packages , pip_only_packages , repo_name , module_name , python_version
122
+ dependencies ,
123
+ packages ,
124
+ pip_only_packages ,
125
+ repo_name ,
126
+ module_name ,
127
+ python_version ,
128
+ environment_manager = None ,
129
+ description = None ,
78
130
):
79
131
if dependencies == "requirements.txt" :
80
132
with open (dependencies , "w" ) as f :
@@ -88,8 +140,43 @@ def write_dependencies(
88
140
elif dependencies == "pyproject.toml" :
89
141
with open (dependencies , "r" ) as f :
90
142
doc = tomlkit .parse (f .read ())
91
- doc ["project" ].add ("dependencies" , sorted (packages ))
92
- doc ["project" ]["dependencies" ].multiline (True )
143
+
144
+ # If using pixi, add pixi-specific configuration
145
+ if environment_manager == "pixi" :
146
+ # Add pixi project configuration
147
+ if "tool" not in doc :
148
+ doc ["tool" ] = tomlkit .table ()
149
+ if "pixi" not in doc ["tool" ]:
150
+ doc ["tool" ]["pixi" ] = tomlkit .table ()
151
+
152
+ # Generate pixi configuration using helper function
153
+ conda_deps , pypi_deps , project_config = _generate_pixi_dependencies_config (
154
+ packages ,
155
+ pip_only_packages ,
156
+ repo_name ,
157
+ module_name ,
158
+ python_version ,
159
+ description ,
160
+ )
161
+
162
+ # Add project configuration
163
+ doc ["tool" ]["pixi" ]["project" ] = tomlkit .table ()
164
+ for key , value in project_config .items ():
165
+ doc ["tool" ]["pixi" ]["project" ][key ] = value
166
+
167
+ # Add conda dependencies
168
+ doc ["tool" ]["pixi" ]["dependencies" ] = tomlkit .table ()
169
+ for dep , version in conda_deps .items ():
170
+ doc ["tool" ]["pixi" ]["dependencies" ][dep ] = version
171
+
172
+ # Add PyPI dependencies
173
+ doc ["tool" ]["pixi" ]["pypi-dependencies" ] = tomlkit .table ()
174
+ for dep , version in pypi_deps .items ():
175
+ doc ["tool" ]["pixi" ]["pypi-dependencies" ][dep ] = version
176
+ else :
177
+ # Standard pyproject.toml dependencies
178
+ doc ["project" ].add ("dependencies" , sorted (packages ))
179
+ doc ["project" ]["dependencies" ].multiline (True )
93
180
94
181
with open (dependencies , "w" ) as f :
95
182
f .write (tomlkit .dumps (doc ))
@@ -122,3 +209,47 @@ def write_dependencies(
122
209
lines += ["" , "[requires]" , f'python_version = "{ python_version } "' ]
123
210
124
211
f .write ("\n " .join (lines ))
212
+
213
+ elif dependencies == "pixi.toml" :
214
+ # Generate pixi configuration using helper function
215
+ conda_deps , pypi_deps , project_config = _generate_pixi_dependencies_config (
216
+ packages ,
217
+ pip_only_packages ,
218
+ repo_name ,
219
+ module_name ,
220
+ python_version ,
221
+ description ,
222
+ )
223
+
224
+ with open (dependencies , "w" ) as f :
225
+ lines = ["[project]" ]
226
+
227
+ # Add project configuration
228
+ for key , value in project_config .items ():
229
+ if isinstance (value , str ):
230
+ lines .append (f'{ key } = "{ value } "' )
231
+ elif isinstance (value , list ):
232
+ lines .append (f"{ key } = { value } " )
233
+ else :
234
+ lines .append (f'{ key } = "{ value } "' )
235
+
236
+ lines .append ("" )
237
+ lines .append ("[dependencies]" )
238
+
239
+ # Add conda dependencies
240
+ for dep , version in conda_deps .items ():
241
+ lines .append (f'{ dep } = "{ version } "' )
242
+
243
+ # Add PyPI dependencies if any
244
+ if pypi_deps :
245
+ lines .append ("" )
246
+ lines .append ("[pypi-dependencies]" )
247
+ for dep , config in pypi_deps .items ():
248
+ if isinstance (config , dict ):
249
+ # Handle editable local package
250
+ lines .append (f'{ dep } = {{ path = ".", editable = true }}' )
251
+ else :
252
+ lines .append (f'{ dep } = "{ config } "' )
253
+
254
+ f .write ("\n " .join (lines ))
255
+ f .write ("\n " )
0 commit comments