22
33import logging
44import re
5- from collections import namedtuple
65from pathlib import (
76 Path ,
87)
2019from exasol .nb_connector .ai_lab_config import AILabConfig as CKey
2120from exasol .nb_connector .secret_store import Secrets
2221from exasol .nb_connector .slc import constants
22+ from exasol .nb_connector .slc .git_access import GitAccess
23+ from exasol .nb_connector .slc .package_file_editor import append_packages
24+ from exasol .nb_connector .slc .package_types import (
25+ CondaPackageDefinition ,
26+ PipPackageDefinition ,
27+ )
2328from exasol .nb_connector .slc .slc_compression_strategy import SlcCompressionStrategy
2429from exasol .nb_connector .slc .slc_flavor import (
2530 SlcError ,
3136)
3237from exasol .nb_connector .utils import optional_str_to_bool
3338
34- PipPackageDefinition = namedtuple ("PipPackageDefinition" , ["pkg" , "version" ])
35- CondaPackageDefinition = namedtuple ("CondaPackageDefinition" , ["pkg" , "version" ])
36-
3739NAME_PATTERN = re .compile (r"^[A-Z][A-Z0-9_]*$" , flags = re .IGNORECASE )
3840
3941
@@ -45,65 +47,6 @@ def _verify_name(slc_name: str) -> None:
4547 )
4648
4749
48- def _read_packages (file_path : Path , package_definition : type ) -> list :
49- packages = []
50- with file_path .open ("r" , encoding = "utf-8" ) as f :
51- for line in f :
52- line = line .strip ()
53- if not line or line .startswith ("#" ):
54- continue # skip empty or commented lines
55- # Take everything before the '|' as package name
56- package , version = line .split ("|" , 1 )
57- packages .append (package_definition (package , version ))
58- return packages
59-
60-
61- def _ends_with_newline (file_path : Path ) -> bool :
62- content = file_path .read_text ()
63- return content .endswith ("\n " )
64-
65-
66- def _filter_packages (
67- original_packages : list [PipPackageDefinition ] | list [CondaPackageDefinition ],
68- new_packages : list [PipPackageDefinition ] | list [CondaPackageDefinition ],
69- ) -> list [PipPackageDefinition | CondaPackageDefinition ]:
70- filtered_packages = []
71- for package in new_packages :
72- add_package = True
73- for original_package in original_packages :
74- if package .pkg == original_package .pkg :
75- add_package = False
76- if package .version == original_package .version :
77- logging .warning ("Package already exists: %s" , original_package )
78- else :
79- raise SlcError (
80- "Package already exists: %s but with different version" ,
81- original_package ,
82- )
83- if add_package :
84- filtered_packages .append (package )
85- return filtered_packages
86-
87-
88- def _append_packages (
89- file_path : Path ,
90- package_definition : type ,
91- packages : list [PipPackageDefinition ] | list [CondaPackageDefinition ],
92- ):
93- """
94- Appends packages to the custom packages file.
95- """
96- original_packages = _read_packages (file_path , package_definition )
97- filtered_packages = _filter_packages (original_packages , packages )
98- ends_with_newline = _ends_with_newline (file_path )
99- if filtered_packages :
100- with open (file_path , "a" ) as f :
101- if not ends_with_newline :
102- f .write ("\n " )
103- for p in filtered_packages :
104- print (f"{ p .pkg } |{ p .version } " , file = f )
105-
106-
10750class ScriptLanguageContainer :
10851 """
10952 Support building different flavors of Exasol Script Language
@@ -242,6 +185,24 @@ def custom_conda_file(self) -> Path:
242185 """
243186 return self .custom_packages_dir / "conda_packages"
244187
188+ def restore_custom_pip_file (self ):
189+ """
190+ Restores the custom pip packages file from Git. All changes will be overwritten.
191+ """
192+ GitAccess .checkout_file (
193+ self .checkout_dir / "script-languages" ,
194+ self .custom_pip_file .relative_to (self .checkout_dir ),
195+ )
196+
197+ def restore_custom_conda_file (self ):
198+ """
199+ Restores the custom conda packages file from Git. All changes will be overwritten.
200+ """
201+ GitAccess .checkout_file (
202+ self .checkout_dir / "script-languages" ,
203+ self .custom_conda_file .relative_to (self .checkout_dir ),
204+ )
205+
245206 def export (self ) -> None :
246207 """
247208 Exports the current SLC to the export directory.
@@ -333,7 +294,7 @@ def append_custom_pip_packages(self, pip_packages: list[PipPackageDefinition]):
333294 Note: This method is not idempotent: Multiple calls with the same
334295 package definitions will result in duplicated entries.
335296 """
336- _append_packages (self .custom_pip_file , PipPackageDefinition , pip_packages )
297+ append_packages (self .custom_pip_file , PipPackageDefinition , pip_packages )
337298
338299 def append_custom_conda_packages (
339300 self , conda_packages : list [CondaPackageDefinition ]
@@ -343,7 +304,7 @@ def append_custom_conda_packages(
343304 Note: This method is not idempotent: Multiple calls with the same
344305 package definitions will result in duplicated entries.
345306 """
346- _append_packages (self .custom_conda_file , CondaPackageDefinition , conda_packages )
307+ append_packages (self .custom_conda_file , CondaPackageDefinition , conda_packages )
347308
348309 @property
349310 def docker_image_tags (self ) -> list [str ]:
0 commit comments