Comments (2)
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.
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)
- ngamsServer and ngamsDaemon use different PID files HOT 1
- NGAMS python client --server option bug HOT 1
- Checksum calculation mismatch failure on python 3 HOT 3
- Improve path computation fixed in #80
- Deprecated function calls in src/ngamsCore/ngamsLib HOT 4
- ngamsDapiStatus attribute __ioTime: Integer vs. Float HOT 1
- ngamsCore/ngamsLib/logutils.py inconsistent types of local variable HOT 1
- ngamsCore, function isoTime2Secs() mixes Integers and Floats HOT 1
- ngamsUDT/ngamsUDTSender.py not Python3 ready HOT 1
- There is no six.input() function HOT 1
- base64 decoding of DB password is incorrect Python3 code HOT 1
- Insufficient error checks in get_db_parameters() function HOT 2
- more Python3-related changes to ngamsUtils/ngamsUtils/ngasXSyncTool.py HOT 1
- Python3-related fixes to ngamsUtils/ngamsUtils/ngasVerifyCloning.py HOT 1
- Python3-related issues in ngamsUtils/ngamsUtils/ngasCheckFileCopies.py HOT 2
- Defunct (Janitor) process after OFFLINE,ONLINE (or INIT) HOT 2
- QUERY/files command does not support UseFileIgnore=false HOT 4
- Backward compatible checksum variant HOT 5
- XML response contains invalid link to NgamsStatus DTD HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ngas.