Skip to content

Commit bfbaff7

Browse files
committed
chore: address PR feedback
Signed-off-by: behnazh-w <[email protected]>
1 parent 78356b5 commit bfbaff7

File tree

6 files changed

+70
-58
lines changed

6 files changed

+70
-58
lines changed

src/macaron/build_spec_generator/build_command_patcher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _patch_commands(
5959
cli_command = effective_cli_parser.parse(cmds)
6060
except CommandLineParseError as error:
6161
logger.error(
62-
"Failed to parse the cli command %s. Error %s.",
62+
"Failed to patch the cli command %s. Error %s.",
6363
" ".join(cmds),
6464
error,
6565
)
@@ -77,7 +77,7 @@ def _patch_commands(
7777
)
7878
except PatchBuildCommandError as error:
7979
logger.error(
80-
"Failed to patch the mvn command %s. Error %s.",
80+
"Failed to patch the build command %s. Error %s.",
8181
" ".join(cmds),
8282
error,
8383
)

src/macaron/build_spec_generator/build_spec_generator.py

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,12 @@ def gen_build_spec_for_purl(
7878
purl: PackageURL
7979
The package URL to generate build spec for.
8080
database_path: str
81-
The path to the Macaron database.
81+
The path to the Macaron SQLite database file. This database will be accessed in read-only mode,
82+
ensuring that no modifications can be made during operations.
8283
build_spec_format: BuildSpecFormat
8384
The format of the final build spec content.
85+
output_path: str
86+
The path to the output directory.
8487
8588
Returns
8689
-------
@@ -89,10 +92,10 @@ def gen_build_spec_for_purl(
8992
buildspec file cannot be created in the local filesystem, ``os.EX_DATAERR`` if there was an
9093
error generating the content for the buildspec file.
9194
"""
92-
db_engine = create_engine(f"sqlite+pysqlite:///{database_path}", echo=False)
95+
db_engine = create_engine(f"sqlite+pysqlite:///file:{database_path}?mode=ro&uri=true", echo=False)
96+
build_spec_content = None
9397

9498
with Session(db_engine) as session, session.begin():
95-
build_spec_content = None
9699
match build_spec_format:
97100
case BuildSpecFormat.REPRODUCIBLE_CENTRAL:
98101
build_spec_content = gen_reproducible_central_build_spec(
@@ -101,42 +104,42 @@ def gen_build_spec_for_purl(
101104
patches=CLI_COMMAND_PATCHES,
102105
)
103106

104-
if not build_spec_content:
105-
logger.error("Error while generating reproducible central build spec.")
106-
return os.EX_DATAERR
107-
108-
logger.debug("Build spec content: \n%s", build_spec_content)
109-
110-
build_spec_filepath = os.path.join(
111-
output_path,
112-
"buildspec",
113-
get_purl_based_dir(
114-
purl_name=purl.name,
115-
purl_namespace=purl.namespace,
116-
purl_type=purl.type,
117-
),
118-
"macaron.buildspec",
107+
if not build_spec_content:
108+
logger.error("Error while generating the build spec.")
109+
return os.EX_DATAERR
110+
111+
logger.debug("Build spec content: \n%s", build_spec_content)
112+
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+
)
128+
129+
logger.info(
130+
"Generating the %s format build spec to %s.",
131+
build_spec_format.value,
132+
os.path.relpath(build_spec_filepath, os.getcwd()),
133+
)
134+
try:
135+
with open(build_spec_filepath, mode="w", encoding="utf-8") as file:
136+
file.write(build_spec_content)
137+
except OSError as error:
138+
logger.error(
139+
"Could not create the build spec at %s. Error: %s",
140+
os.path.relpath(build_spec_filepath, os.getcwd()),
141+
error,
119142
)
143+
return os.EX_OSERR
120144

121-
os.makedirs(
122-
name=os.path.dirname(build_spec_filepath),
123-
exist_ok=True,
124-
)
125-
126-
try:
127-
with open(build_spec_filepath, mode="w", encoding="utf-8") as file:
128-
logger.info(
129-
"Generating the %s format build spec to %s.",
130-
build_spec_format.value,
131-
os.path.relpath(build_spec_filepath, os.getcwd()),
132-
)
133-
file.write(build_spec_content)
134-
except OSError as error:
135-
logger.error(
136-
"Could not create the build spec at %s. Error: %s",
137-
os.path.relpath(build_spec_filepath, os.getcwd()),
138-
error,
139-
)
140-
return os.EX_OSERR
141-
142-
return os.EX_OK
145+
return os.EX_OK

src/macaron/build_spec_generator/cli_command_parser/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ def patch_mapping(
6767

6868
@dataclass
6969
class OptionDef(Generic[P]):
70-
"""This class represent a definition of a CLI option for argparse.ArgumentParser.
70+
"""This class represents a definition of a CLI option for argparse.ArgumentParser.
7171
7272
This class also contains the information for validating a patch value.
73-
The generic type T is the patch expected type (if it's not None).
73+
The generic type P is the patch expected type (if it's not None).
7474
"""
7575

7676
# e.g. `--long-option-name`
@@ -83,7 +83,7 @@ def is_valid_patch_option(self, patch: Any) -> TypeGuard[P]:
8383
raise NotImplementedError()
8484

8585
@abstractmethod
86-
def add_itself_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
86+
def add_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
8787
"""Add a new argument to argparser.ArgumentParser representing this option."""
8888
raise NotImplementedError()
8989

src/macaron/build_spec_generator/cli_command_parser/gradle_cli_parser.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def is_valid_patch_option(self, patch: Any) -> TypeGuard[bool]:
5050
"""Return True if the provided patch value is compatible with the internal type of this option."""
5151
return isinstance(patch, bool)
5252

53-
def add_itself_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
53+
def add_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
5454
"""Add a new argument to argparser.ArgumentParser representing this option."""
5555
kwargs: dict[str, Any] = {}
5656

@@ -90,7 +90,7 @@ def get_negated_long_name(long_name: str) -> str:
9090
"""Return the negated version of a long option name."""
9191
return f"--no-{long_name.removeprefix('--')}"
9292

93-
def add_itself_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
93+
def add_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
9494
"""Add a new argument to argparser.ArgumentParser representing this option."""
9595
# We allow providing both the normal and negated form.
9696
negated_long_name = self.get_negated_long_name(self.long_name)
@@ -127,7 +127,7 @@ def is_valid_patch_option(self, patch: Any) -> TypeGuard[str]:
127127
"""Return True if the provided patch value is compatible with the internal type of this option."""
128128
return isinstance(patch, str)
129129

130-
def add_itself_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
130+
def add_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
131131
"""Add a new argument to argparser.ArgumentParser representing this option."""
132132
if self.short_name:
133133
arg_parse.add_argument(
@@ -161,7 +161,7 @@ def is_valid_patch_option(self, patch: Any) -> TypeGuard[dict[str, str | None]]:
161161
"""Return True if the provided patch value is compatible with the internal type of this option."""
162162
return is_dict_of_str_to_str_or_none(patch)
163163

164-
def add_itself_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
164+
def add_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
165165
"""Add a new argument to argparser.ArgumentParser representing this option."""
166166
arg_parse.add_argument(
167167
*(self.short_name, self.long_name),
@@ -184,7 +184,7 @@ def is_valid_patch_option(self, patch: Any) -> TypeGuard[list[str]]:
184184
"""Return True if the provided patch value is compatible with the internal type of this option."""
185185
return is_list_of_strs(patch)
186186

187-
def add_itself_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
187+
def add_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
188188
"""Add a new argument to argparser.ArgumentParser representing this option."""
189189
# Doesn't require to allow cases like "gradle --help".
190190
arg_parse.add_argument(
@@ -212,7 +212,7 @@ def is_valid_patch_option(self, patch: Any) -> TypeGuard[list[str]]:
212212
"""Return True if the provided patch value is compatible with the internal type of this option."""
213213
return is_list_of_strs(patch)
214214

215-
def add_itself_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
215+
def add_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
216216
"""Add a new argument to argparser.ArgumentParser representing this option."""
217217
arg_parse.add_argument(
218218
*(self.short_name, self.long_name),
@@ -464,7 +464,7 @@ def __init__(self) -> None:
464464
self.option_defs: dict[str, OptionDef] = {}
465465

466466
for opt_def in GRADLE_OPTION_DEF:
467-
opt_def.add_itself_to_arg_parser(self.arg_parser)
467+
opt_def.add_to_arg_parser(self.arg_parser)
468468

469469
self.option_defs[opt_def.long_name] = opt_def
470470

src/macaron/build_spec_generator/cli_command_parser/maven_cli_parser.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def is_valid_patch_option(self, patch: Any) -> TypeGuard[bool]:
4646
"""Return True if the provided patch value is compatible with the internal type of this option."""
4747
return isinstance(patch, bool)
4848

49-
def add_itself_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
49+
def add_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
5050
"""Add a new argument to argparser.ArgumentParser representing this option."""
5151
if self.dest:
5252
arg_parse.add_argument(
@@ -80,7 +80,7 @@ def is_valid_patch_option(self, patch: Any) -> TypeGuard[str]:
8080
"""Return True if the provided patch value is compatible with the internal type of this option."""
8181
return isinstance(patch, str)
8282

83-
def add_itself_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
83+
def add_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
8484
"""Add a new argument to argparser.ArgumentParser representing this option."""
8585
arg_parse.add_argument(
8686
*(self.short_name, self.long_name),
@@ -111,7 +111,7 @@ def is_valid_patch_option(self, patch: Any) -> TypeGuard[list[str]]:
111111
"""Return True if the provided patch value is compatible with the internal type of this option."""
112112
return is_list_of_strs(patch)
113113

114-
def add_itself_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
114+
def add_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
115115
"""Add a new argument to argparser.ArgumentParser representing this option."""
116116
arg_parse.add_argument(
117117
*(self.short_name, self.long_name),
@@ -141,7 +141,7 @@ def is_valid_patch_option(self, patch: Any) -> TypeGuard[dict[str, str | None]]:
141141
"""Return True if the provided patch value is compatible with the internal type of this option."""
142142
return is_dict_of_str_to_str_or_none(patch)
143143

144-
def add_itself_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
144+
def add_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
145145
"""Add a new argument to argparser.ArgumentParser representing this option."""
146146
arg_parse.add_argument(
147147
*(self.short_name, self.long_name),
@@ -164,7 +164,7 @@ def is_valid_patch_option(self, patch: Any) -> TypeGuard[list[str]]:
164164
"""Return True if the provided patch value is compatible with the internal type of this option."""
165165
return is_list_of_strs(patch)
166166

167-
def add_itself_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
167+
def add_to_arg_parser(self, arg_parse: argparse.ArgumentParser) -> None:
168168
"""Add a new argument to argparser.ArgumentParser representing this option."""
169169
# Doesn't require to allow cases like "mvn --help".
170170
arg_parse.add_argument(
@@ -364,7 +364,7 @@ def __init__(self) -> None:
364364
self.option_defs: dict[str, OptionDef] = {}
365365

366366
for opt_def in MAVEN_OPTION_DEF:
367-
opt_def.add_itself_to_arg_parser(self.arg_parser)
367+
opt_def.add_to_arg_parser(self.arg_parser)
368368

369369
self.option_defs[opt_def.long_name] = opt_def
370370

src/macaron/build_spec_generator/reproducible_central/reproducible_central.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,15 @@ def gen_reproducible_central_build_spec(
339339
for this PURL, 3. Failed to patch the build commands using the provided ``patches``, 4. The database from
340340
``session`` doesn't contain enough information.
341341
"""
342+
if purl.type != "maven":
343+
logger.error(
344+
"Reproducible Central build specification only supports PURLs of type 'maven'. "
345+
"Received PURL type: '%s' (%s). Please provide a valid Maven package URL.",
346+
purl.type,
347+
purl.to_string(),
348+
)
349+
return None
350+
342351
logger.debug(
343352
"Generating build spec for %s with command patches:\n%s",
344353
purl,

0 commit comments

Comments
 (0)