GithubHelp home page GithubHelp logo

Comments (2)

ulange-eso-org avatar ulange-eso-org commented on July 4, 2024

further type annotations in src/ngamsUtils/ngamsUtils/ngasUtilsLib.py revealed more issues:

  • filter() in Python3 returns a generator whereas the code here expects a list
  • raw_input() doesn't exist in Python3
  • no/incorrect error reporting if values are not defined in RC file

Here is the full diff:

diff --git a/src/ngamsUtils/ngamsUtils/ngasUtilsLib.py b/src/ngamsUtils/ngamsUtils/ngasUtilsLib.py
index 41ea97ee..2774bcf5 100644
--- a/src/ngamsUtils/ngamsUtils/ngasUtilsLib.py
+++ b/src/ngamsUtils/ngamsUtils/ngasUtilsLib.py
@@ -31,6 +31,8 @@
 Utility functions used by the tool in the NGAS Utils module
 """

+from typing import Optional, List, Tuple, Dict, Any
+
 import base64
 import getpass
 import glob
@@ -69,7 +71,7 @@ def get_ngas_resource_file():
     return os.path.expanduser(NGAS_RC_FILE)


-def get_parameter_ngas_resource_file(parameter):
+def get_parameter_ngas_resource_file(parameter: str) -> Optional[str]:
     """
     Retrieve a parameter from the NGAS resource file.

@@ -120,48 +122,53 @@ def get_parameter_ngas_resource_file(parameter):
     return None


-def encrypt_access_code(access_code):
+def encrypt_access_code(access_code: str) -> str:
     """
     Encode an Access Code as used by the NGAS Utilities

     :param access_code: Access Code as typed by the user (string)
     :return: Encoded Access Code (string)
     """
-    return base64.encodestring(access_code)
+    return base64.encodebytes(access_code.encode()).decode('utf-8')


-def decrypt_access_code(encrypted_access_code):
+def decrypt_access_code(encrypted_access_code: str) -> str:
     """
     Decode an Access Code as used by the NGAS Utilities

     :param encrypted_access_code: Encrypted Access Code (string)
     :return: Decoded Access Code (string)
     """
-    return base64.decodestring(encrypted_access_code)
+    return base64.decodebytes(encrypted_access_code.encode()).decode('utf-8')


-def check_access_code(access_code):
+def check_access_code(access_code: str):
     """
     Check the given access code against the one defined in the NGAS resource file. In case of match, it returns 1
     otherwise 0.

     :param access_code: Access Code as given by the user (string)
-    """
-    decrypted_access_code = decrypt_access_code(get_parameter_ngas_resource_file(NGAS_RC_PAR_ACC_CODE))
+    """
+
+    rc_access_code = get_parameter_ngas_resource_file(NGAS_RC_PAR_ACC_CODE)
+    if rc_access_code is None: raise Exception("No access code defined in RC file!!")
+
+    decrypted_access_code = decrypt_access_code(rc_access_code)
     if decrypted_access_code == access_code:
         return
+
     else:
         raise Exception("Incorrect access code given!!")


-def console_input(message):
+def console_input(message: str) -> str:
     """
     Simple function to prompt the user for input. The string entered is stripped before returning it to the user.

     :param message: Message to print (string)
     :return: Information entered by the user (string)
     """
-    return raw_input("INPUT> " + message + " ").strip()
+    return input("INPUT> " + message + " ").strip()


 def get_db_parameters():
@@ -178,7 +185,7 @@ def get_db_parameters():
     return interface, server, db, user, password


-def send_email(subject, to, message, content_type=None, attachment_name=None):
+def send_email(subject: str, to: str, message: str, content_type: Optional[str]=None, attachment_name: Optional[str]=None):
     """
     Send an e-mail to the recipient with the given subject

@@ -189,6 +196,9 @@ def send_email(subject, to, message, content_type=None, attachment_name=None):
     :param attachment_name: Name of attachment in mail (string)
     """
     smtp_host = get_parameter_ngas_resource_file(NGAS_RC_PAR_SMTP_HOST)
+    if not smtp_host:
+        print("Error: No SMTP host defined in RC file")
+        return
     email_list = to.split(",")
     from_field = getpass.getuser() + "@" + os.uname()[1].split(".")[0]
     for emailAdr in email_list:
@@ -205,7 +215,7 @@ def send_email(subject, to, message, content_type=None, attachment_name=None):
             print("Error sending email to recipient: " + str(emailAdr) + ". Error: " + str(e))


-def dcc_message_to_file_list(dcc_message_file, target_file):
+def dcc_message_to_file_list(dcc_message_file: str, target_file: str):
     """
     Converts a DCC (Data Consistency Checking, Inconsistency Notification
     Message, e.g.:
@@ -287,7 +297,7 @@ def dcc_message_to_file_list(dcc_message_file, target_file):
                 break
             line_elements = dcc_message_lines[line_num][file_id_index:].split(" ")
             # element_list = cleanList(line_elements)
-            element_list = filter(None, line_elements)
+            element_list = list(filter(None, line_elements))
             file_id = element_list[0]
             file_version = element_list[1]
             disk_id = element_list[2].split(":")[1]
@@ -296,7 +306,7 @@ def dcc_message_to_file_list(dcc_message_file, target_file):
     fo.close()


-def dcc_report_to_file_list(dcc_report):
+def dcc_report_to_file_list(dcc_report: str) -> str:
     """
     Converts a DCC Report to a File List

@@ -311,7 +321,7 @@ def dcc_report_to_file_list(dcc_report):
         if line.find("ERROR: File in DB missing on disk") != -1 \
                 or line.find("ERROR: Inconsistent checksum found") != -1:
             # clean_list = cleanList(line_elements)
-            clean_list = filter(None, line.split(" "))
+            clean_list = list(filter(None, line.split(" ")))
             disk_id = clean_list[-1].split(":")[-1]
             file_id = clean_list[-3]
             file_version = clean_list[-2]
@@ -319,7 +329,7 @@ def dcc_report_to_file_list(dcc_report):
     return file_list_buffer


-def parse_file_list(file_list_file):
+def parse_file_list(file_list_file: str) -> List[List[str]]:
     """
     Function that parses the the given file list listing entries like this:

@@ -363,7 +373,7 @@ def parse_file_list(file_list_file):
     return file_reference_list


-def remove_recursively(path):
+def remove_recursively(path: str):
     """
     Remove files, links and directories recursively for a given path

@@ -379,7 +389,7 @@ def remove_recursively(path):
         shutil.rmtree(path)


-def check_delete_tmp_directories(directory_name_pattern, time_out=600):
+def check_delete_tmp_directories(directory_name_pattern: str, time_out: int=600):
     """
     Check if there are temporary directories files according to the given pattern, which are older than the time out
     specified. In case yes, remove these if possible.
@@ -396,7 +406,7 @@ def check_delete_tmp_directories(directory_name_pattern, time_out=600):
                 remove_recursively(glob_file)


-def check_server_running(host=None, port=8001):
+def check_server_running(host: Optional[str]=None, port: int=8001) -> str:
     """
     Function to check if server is running. It returns the status of the server, which is one of the following values:

@@ -434,7 +444,7 @@ NGAS_OPT_OPT = "OPTIONAL"
 NGAS_OPT_MAN = "MANDATORY"
 NGAS_OPT_INT = "INTERNAL"

-_stdOptions = [["help", [], 0, NGAS_OPT_OPT, "",
+_stdOptions: List[List[Any]] = [["help", [], 0, NGAS_OPT_OPT, "",
                 "Print out man-page and exit."],
                ["debug", [], 0, NGAS_OPT_OPT, "",
                 "Run in debug mode."],
@@ -453,7 +463,7 @@ _stdOptions = [["help", [], 0, NGAS_OPT_OPT, "",
                 "Access code to access the NGAS system with the NGAS Utilities."]]


-def generate_options_dictionary_and_document(tool_options):
+def generate_options_dictionary_and_document(tool_options: List[List[Any]]) -> Tuple[Dict[Any, Any], str]:
     """
     From the options defined, generate a dictionary with this info and generate a man-page

from ngas.

rtobar avatar rtobar commented on July 4, 2024

All fixed now and merged to the master branch.

Note that the usage of base64 doesn't really bring any security into the table as the function names encrypt_access_code and decrypt_access_code might suggest -- it only obfuscate secrets to the uninitiated.

from ngas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.