Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[issue-441] replace print() with logging #494

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 20 additions & 16 deletions src/spdx/clitools/pyspdxtools.py
Expand Up @@ -10,6 +10,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import sys
from typing import List

Expand All @@ -26,8 +27,11 @@

@click.command()
@click.option("--infile", "-i", help="The file containing the document to be validated or converted.")
@click.option("--outfile", "-o", help="The file to write the converted document to (write a dash for output to stdout or omit for no conversion).")
@click.option("--version", help='The SPDX version to be used during parsing and validation ("SPDX-2.2" or "SPDX-2.3"). Will be read from the document if not provided.', default=None)
@click.option("--outfile", "-o",
help="The file to write the converted document to (write a dash for output to stdout or omit for no conversion).")
@click.option("--version",
help='The SPDX version to be used during parsing and validation ("SPDX-2.2" or "SPDX-2.3"). Will be read from the document if not provided.',
default=None)
@click.option("--novalidation", is_flag=True, help="Don't validate the provided document.")
def main(infile: str, outfile: str, version: str, novalidation: bool):
"""
Expand All @@ -46,34 +50,34 @@ def main(infile: str, outfile: str, version: str, novalidation: bool):
version = document.creation_info.spdx_version

if not version in ["SPDX-2.2", "SPDX-2.3"]:
print(f"This tool only supports SPDX versions SPDX-2.2 and SPDX-2.3, but got: {version}",
file=sys.stderr)
logging.error(f"This tool only supports SPDX versions SPDX-2.2 and SPDX-2.3, but got: {version}")
sys.exit(1)

validation_messages: List[ValidationMessage] = validate_full_spdx_document(document, version)
if validation_messages:
print("The document is invalid. The following issues have been found:", file=sys.stderr)
for message in validation_messages:
print(message.validation_message, file=sys.stderr)
log_string = "\n".join(
["The document is invalid. The following issues have been found:"] +
[message.validation_message for message in validation_messages])
logging.error(log_string)
sys.exit(1)
else:
print("The document is valid.", file=sys.stderr)
logging.info("The document is valid.")

if outfile and outfile != "-":
write_file(document, outfile, validate=False)

except NotImplementedError as err:
print(err.args[0], file=sys.stderr)
print("Please note that this project is currently undergoing a major refactoring and therefore missing "
"a few features which will be added in time (refer to https://github.com/spdx/tools-python/issues "
"for insights into the current status).\n"
"In the meantime, please use the PyPI release version 0.7.0.", file=sys.stderr)
logging.error(err.args[0] +
"\nPlease note that this project is currently undergoing a major refactoring and therefore missing "
"a few features which will be added in time (refer to https://github.com/spdx/tools-python/issues "
"for insights into the current status).\n"
"In the meantime, please use the current PyPI release version.")
sys.exit(1)

except SPDXParsingError as err:
print("There have been issues while parsing the provided document:", file=sys.stderr)
for message in err.get_messages():
print(message, file=sys.stderr)
log_string = "\n".join(["There have been issues while parsing the provided document:"] +
[message for message in err.get_messages()])
logging.error(log_string)
sys.exit(1)


Expand Down
5 changes: 2 additions & 3 deletions src/spdx/writer/rdf/writer_utils.py
Expand Up @@ -8,7 +8,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import logging
from datetime import datetime
from typing import Any, Optional, Dict

Expand Down Expand Up @@ -59,8 +59,7 @@ def add_namespace_to_spdx_id(spdx_id: str, doc_namespace: str, external_doc_name
if ":" in spdx_id:
external_doc_ref_id = spdx_id.split(":")[0]
if external_doc_ref_id not in external_doc_namespaces.keys():
print(f"No namespace for external document reference with id {external_doc_ref_id} provided.",
file=sys.stderr)
logging.warning(f"No namespace for external document reference with id {external_doc_ref_id} provided.")
return spdx_id
return f"{external_doc_namespaces[external_doc_ref_id]}#{spdx_id.split(':')[1]}"

Expand Down