3
3
4
4
"""This module contains the functions used for generating build specs from the Macaron database."""
5
5
6
+ import json
6
7
import logging
7
8
import os
8
9
from collections .abc import Mapping
13
14
from sqlalchemy .orm import Session
14
15
15
16
from macaron .build_spec_generator .build_command_patcher import PatchCommandBuildTool , PatchValueType
17
+ from macaron .build_spec_generator .common_spec .core import gen_generic_build_spec
16
18
from macaron .build_spec_generator .reproducible_central .reproducible_central import gen_reproducible_central_build_spec
19
+ from macaron .errors import GenerateBuildSpecError
17
20
from macaron .path_utils .purl_based_path import get_purl_based_dir
18
21
19
22
logger : logging .Logger = logging .getLogger (__name__ )
@@ -24,6 +27,8 @@ class BuildSpecFormat(str, Enum):
24
27
25
28
REPRODUCIBLE_CENTRAL = "rc-buildspec"
26
29
30
+ DEFAULT = "default-buildspec"
31
+
27
32
28
33
CLI_COMMAND_PATCHES : dict [
29
34
PatchCommandBuildTool ,
@@ -95,49 +100,66 @@ def gen_build_spec_for_purl(
95
100
db_engine = create_engine (f"sqlite+pysqlite:///file:{ database_path } ?mode=ro&uri=true" , echo = False )
96
101
build_spec_content = None
97
102
103
+ build_spec_dir_path = os .path .join (
104
+ output_path ,
105
+ "buildspec" ,
106
+ get_purl_based_dir (
107
+ purl_name = purl .name ,
108
+ purl_namespace = purl .namespace ,
109
+ purl_type = purl .type ,
110
+ ),
111
+ )
112
+
98
113
with Session (db_engine ) as session , session .begin ():
114
+ try :
115
+ build_spec = gen_generic_build_spec (purl = purl , session = session , patches = CLI_COMMAND_PATCHES )
116
+ except GenerateBuildSpecError as error :
117
+ logger .error ("Error while generating the build spec: %s." , error )
118
+ return os .EX_DATAERR
99
119
match build_spec_format :
100
120
case BuildSpecFormat .REPRODUCIBLE_CENTRAL :
101
- build_spec_content = gen_reproducible_central_build_spec (
102
- purl = purl ,
103
- session = session ,
104
- patches = CLI_COMMAND_PATCHES ,
105
- )
121
+ try :
122
+ build_spec_content = gen_reproducible_central_build_spec (build_spec )
123
+ except GenerateBuildSpecError as error :
124
+ logger .error ("Error while generating the build spec: %s." , error )
125
+ return os .EX_DATAERR
126
+ build_spec_file_path = os .path .join (build_spec_dir_path , "reproducible_central.buildspec" )
127
+ # Default build spec.
128
+ case BuildSpecFormat .DEFAULT :
129
+ try :
130
+ build_spec_content = json .dumps (build_spec )
131
+ except ValueError as error :
132
+ logger .error ("Error while serializing the build spec: %s." , error )
133
+ return os .EX_DATAERR
134
+ build_spec_file_path = os .path .join (build_spec_dir_path , "macaron.buildspec" )
106
135
107
136
if not build_spec_content :
108
137
logger .error ("Error while generating the build spec." )
109
138
return os .EX_DATAERR
110
139
111
140
logger .debug ("Build spec content: \n %s" , build_spec_content )
112
141
113
- build_spec_filepath = os .path .join (
114
- output_path ,
115
- "buildspec" ,
116
- get_purl_based_dir (
117
- purl_name = purl .name ,
118
- purl_namespace = purl .namespace ,
119
- purl_type = purl .type ,
120
- ),
121
- "macaron.buildspec" ,
122
- )
123
-
124
- os .makedirs (
125
- name = os .path .dirname (build_spec_filepath ),
126
- exist_ok = True ,
127
- )
142
+ try :
143
+ os .makedirs (
144
+ name = build_spec_dir_path ,
145
+ exist_ok = True ,
146
+ )
147
+ except OSError as error :
148
+ logger .error ("Unable to create the output file: %s." , error )
149
+ return os .EX_OSERR
128
150
129
151
logger .info (
130
- "Generating the %s format build spec to %s. " ,
152
+ "Generating the %s format build spec to %s" ,
131
153
build_spec_format .value ,
132
- os .path .relpath (build_spec_filepath , os .getcwd ()),
154
+ os .path .relpath (build_spec_file_path , os .getcwd ()),
133
155
)
134
156
try :
135
- with open (build_spec_filepath , mode = "w" , encoding = "utf-8" ) as file :
157
+ with open (build_spec_file_path , mode = "w" , encoding = "utf-8" ) as file :
136
158
file .write (build_spec_content )
137
159
except OSError as error :
138
160
logger .error (
139
161
"Could not create the build spec at %s. Error: %s" ,
140
- os .path .relpath (build_spec_filepath , os .getcwd ()),
162
+ os .path .relpath (build_spec_file_path , os .getcwd ()),
141
163
error ,
142
164
)
143
165
return os .EX_OSERR
0 commit comments