def start(self, start_daemon=False): daemon_started = False while True: try: self.socket.connect(('', self.daemon_port)) threading.Thread.start(self) return True except socket.error: if not start_daemon: return False elif not daemon_started: print_stderr("Starting daemon [%s]" % self.config.get('server')) daemon_started = True pid = os.fork() if (pid == 0): # The first child. os.chdir("/") os.setsid() os.umask(0) pid2 = os.fork() if (pid2 == 0): # Second child server = NetworkServer(self.config) try: server.main_loop() except KeyboardInterrupt: print "Ctrl C - Stopping server" sys.exit(1) sys.exit(0) else: time.sleep(0.1)
def do_start_daemon(config): """Start a subprocess running this file.""" import subprocess args = [sys.executable, __file__, config.path] logfile = open(os.path.join(config.path, 'daemon.log'),'w') p = subprocess.Popen(args, stderr=logfile, stdout=logfile, close_fds=(os.name=="posix")) print_stderr("starting daemon (PID %d)"%p.pid)
def start(self, start_daemon=False): daemon_started = False while True: try: self.socket.connect(('', self.daemon_port)) threading.Thread.start(self) return True except socket.error: if not start_daemon: return False elif not daemon_started: print_stderr( "Starting daemon [%s]"%self.config.get('server')) daemon_started = True pid = os.fork() if (pid == 0): # The first child. os.chdir("/") os.setsid() os.umask(0) pid2 = os.fork() if (pid2 == 0): # Second child server = NetworkServer(self.config) try: server.main_loop() except KeyboardInterrupt: print "Ctrl C - Stopping server" sys.exit(1) sys.exit(0) else: time.sleep(0.1)
def do_start_daemon(config): import subprocess args = ["python", __file__, config.path] logfile = open(os.path.join(config.path, 'daemon.log'), 'w') if config.get('portable', False): args.append('--portable') p = subprocess.Popen(args, stderr=logfile, stdout=logfile, close_fds=True) print_stderr("starting daemon (PID %d)" % p.pid)
def do_start_daemon(config): import subprocess logfile = open(os.path.join(config.path, 'daemon.log'), 'w') p = subprocess.Popen([sys.executable, __file__], stderr=logfile, stdout=logfile, close_fds=(os.name == "posix")) print_stderr("starting daemon (PID %d)" % p.pid)
def do_start_daemon(config): import subprocess logfile = open(os.path.join(config.path, 'daemon.log'), 'w') p = subprocess.Popen(["python", __file__], stderr=logfile, stdout=logfile, close_fds=True) print_stderr("starting daemon (PID %d)" % p.pid)
def do_start_daemon(config): import subprocess args = ["python", __file__, config.path] logfile = open(os.path.join(config.path, 'daemon.log'),'w') if config.get('portable', False): args.append('--portable') p = subprocess.Popen(args, stderr=logfile, stdout=logfile, close_fds=True) print_stderr("starting daemon (PID %d)"%p.pid)
def do_start_daemon(config): """Start a subprocess running this file.""" import subprocess args = [sys.executable, __file__, config.path] logfile = open(os.path.join(config.path, 'daemon.log'), 'w') p = subprocess.Popen(args, stderr=logfile, stdout=logfile, close_fds=(os.name == "posix")) print_stderr("starting daemon (PID %d)" % p.pid)
def set_key(self, key, value, save = True): if not self.is_modifiable(key): print_stderr("Warning: not changing config key '%s' set on the command line" % key) return with self.lock: self.user_config[key] = value if save: self.save_user_config() return
def do_monkey_patching_of_python_ecdsa_internals_with_libsecp256k1(): if not _libsecp256k1: print_stderr( '[ecc] warning: libsecp256k1 library not available, falling back to python-ecdsa' ) return if not _patched_functions.prepared_to_patch: raise Exception("can't patch python-ecdsa without preparations") ecdsa.ecdsa.Private_key.sign = _patched_functions.fast_sign ecdsa.ecdsa.Public_key.verifies = _patched_functions.fast_verify ecdsa.ellipticcurve.Point.__mul__ = _patched_functions.fast_mul # ecdsa.ellipticcurve.Point.__add__ = ... # TODO?? _patched_functions.monkey_patching_active = True
def get_rpc_credentials(config): rpc_user = config.get('rpcuser', None) rpc_password = config.get('rpcpassword', None) if rpc_user is None or rpc_password is None: rpc_user = '******' import ecdsa, base64 bits = 128 nbytes = bits // 8 + (bits % 8 > 0) pw_int = ecdsa.util.randrange(pow(2, bits)) pw_b64 = base64.b64encode(int_to_bytes(pw_int, nbytes, 'big'), b'-_') rpc_password = to_string(pw_b64, 'ascii') config.set_key('rpcuser', rpc_user) config.set_key('rpcpassword', rpc_password, save=True) elif rpc_password == '': from .util import print_stderr print_stderr('WARNING: RPC authentication is disabled.') return rpc_user, rpc_password
def get_daemon(config, start_daemon=True): import socket daemon_port = config.get('daemon_port', DAEMON_PORT) daemon_started = False while True: try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('', daemon_port)) if not daemon_started: print_stderr("Connected to daemon on port %d" % daemon_port) return s except socket.error: if not start_daemon: return False elif not daemon_started: do_start_daemon(config) daemon_started = True else: time.sleep(0.1)
def get_daemon(config, start_daemon=True): import socket daemon_port = config.get('daemon_port', DAEMON_PORT) daemon_started = False while True: try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', daemon_port)) if not daemon_started: print_stderr("Connected to daemon on port %d"%daemon_port) return s except socket.error: if not start_daemon: return False elif not daemon_started: do_start_daemon(config) daemon_started = True else: time.sleep(0.1)
def print_log_msg(log_level, msg): import log ll = getattr(log, "LOG_%s" % log_level.upper()) file_called_from = inspect.stack()[2][1] line_called_from = inspect.stack()[2][2] method_called_from = inspect.stack()[2][3] debug_info = "%s:%s:%s" % (os.path.basename(file_called_from), method_called_from, line_called_from) try: for l in msg.split("\n"): if ll in log.LOG_TO_STDOUT: print "%s: %s" % (log_level, l) else: util.print_stderr("%s[%s]: %s\n" % (log_level, debug_info, l)) except Exception as e: # A logging mechanism should never cause a script to abort if # you can't expand all formatting markers util.print_stderr("Error while processing %s message:\n%s\n" % (log_level.upper(), e))
def get_daemon(config, start_daemon=True): import socket daemon_socket = os.path.join(config.path, DAEMON_SOCKET) daemon_started = False while True: try: s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.connect(daemon_socket) if not daemon_started: print_stderr("Connected to daemon") return s except socket.error: if not start_daemon: return False elif not daemon_started: do_start_daemon(config) daemon_started = True else: time.sleep(0.1) except: # do not use daemon if AF_UNIX is not available (windows) return False
def load_library(): if sys.platform == 'darwin': library_path = 'libsecp256k1.0.dylib' elif sys.platform in ('windows', 'win32'): library_path = 'libsecp256k1.dll' elif 'ANDROID_DATA' in os.environ: library_path = 'libsecp256k1.so' else: library_path = 'libsecp256k1.so.0' secp256k1 = ctypes.cdll.LoadLibrary(library_path) if not secp256k1: print_stderr('[ecc] warning: libsecp256k1 library failed to load') return None try: secp256k1.secp256k1_context_create.argtypes = [c_uint] secp256k1.secp256k1_context_create.restype = c_void_p secp256k1.secp256k1_context_randomize.argtypes = [c_void_p, c_char_p] secp256k1.secp256k1_context_randomize.restype = c_int secp256k1.secp256k1_ec_pubkey_create.argtypes = [ c_void_p, c_void_p, c_char_p ] secp256k1.secp256k1_ec_pubkey_create.restype = c_int secp256k1.secp256k1_ecdsa_sign.argtypes = [ c_void_p, c_char_p, c_char_p, c_char_p, c_void_p, c_void_p ] secp256k1.secp256k1_ecdsa_sign.restype = c_int secp256k1.secp256k1_ecdsa_verify.argtypes = [ c_void_p, c_char_p, c_char_p, c_char_p ] secp256k1.secp256k1_ecdsa_verify.restype = c_int secp256k1.secp256k1_ec_pubkey_parse.argtypes = [ c_void_p, c_char_p, c_char_p, c_size_t ] secp256k1.secp256k1_ec_pubkey_parse.restype = c_int secp256k1.secp256k1_ec_pubkey_serialize.argtypes = [ c_void_p, c_char_p, c_void_p, c_char_p, c_uint ] secp256k1.secp256k1_ec_pubkey_serialize.restype = c_int secp256k1.secp256k1_ecdsa_signature_parse_compact.argtypes = [ c_void_p, c_char_p, c_char_p ] secp256k1.secp256k1_ecdsa_signature_parse_compact.restype = c_int secp256k1.secp256k1_ecdsa_signature_serialize_compact.argtypes = [ c_void_p, c_char_p, c_char_p ] secp256k1.secp256k1_ecdsa_signature_serialize_compact.restype = c_int secp256k1.secp256k1_ec_pubkey_tweak_mul.argtypes = [ c_void_p, c_char_p, c_char_p ] secp256k1.secp256k1_ec_pubkey_tweak_mul.restype = c_int secp256k1.ctx = secp256k1.secp256k1_context_create( SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY) r = secp256k1.secp256k1_context_randomize(secp256k1.ctx, os.urandom(32)) if r: return secp256k1 else: print_stderr('[ecc] warning: secp256k1_context_randomize failed') return None except (OSError, AttributeError): #traceback.print_exc(file=sys.stderr) print_stderr( '[ecc] warning: libsecp256k1 library was found and loaded but there was an error when using it' ) return None
learnerOutput = [submission.main(x) for x in testCase["input"]] except Exception as e: send_feedback(0.0, "Your code returned this error: " + str(e)) return for i in range(0, numTestCases): if testCase["output"][i] != learnerOutput[i]: numTestCasesFailed += 1 # Calculate score and send feedback ---------------------------------------- totalPenalty = min(1.0, (testCasePenalty * numTestCasesFailed)) finalFractionalScore = 1.0 - totalPenalty if numTestCasesFailed > 0: feedback = "Your solution failed " + str( numTestCasesFailed) + " out of " + str( numTestCases) + " test cases. Please try again!" else: feedback = "Great job! You passed all test cases." send_feedback(finalFractionalScore, feedback) if __name__ == '__main__': try: partid = os.environ['partId'] except Exception as e: print_stderr("Please provide the partId.") send_feedback(0.0, "Please provide the partId.") else: main(partid)
def do_start_daemon(config): import subprocess logfile = open(os.path.join(config.path, 'daemon.log'),'w') p = subprocess.Popen([sys.executable,__file__], stderr=logfile, stdout=logfile, close_fds=(os.name=="posix")) print_stderr("starting daemon (PID %d)"%p.pid)
def do_start_daemon(config): import subprocess logfile = open(os.path.join(config.path, 'daemon.log'),'w') p = subprocess.Popen(["python",__file__], stderr=logfile, stdout=logfile, close_fds=True) print_stderr("starting daemon (PID %d)"%p.pid)
def main(partId): # Define # of test cases --------------------------------------------------- # Please ensure that the numTestCases is an integer >= 1. numTestCases = 5 if numTestCases > 1 and float(numTestCases).is_integer(): testCasePenalty = 1.0 / numTestCases else: print_stderr( "Please update your testCase value to be a whole integer greater than 0." ) send_feedback( 0.0, "Please reach out to course staff via discussion forums, to report a grader error." ) return # Find the learner's submission ---------------------------------------------- # The directory /shared/submission/ is the standard submission directory across all courses. # This is a readonly directory. If you'd like the students to submit a zip with multiple files, # please ensure that the grader first moves the files to a folder with the correct permissions to unzip. submission_location = "/shared/submission/" # Each partId is evaluated one at a time; thus, only one submission file will be stored # at a time in the /shared/submission/ directory. for file in os.listdir(submission_location): if file.endswith(".py"): learnerFile = file else: learnerFile = None if learnerFile is None: send_feedback(0.0, "Your file may not have the right extension.") return # Save the submission to /grader/ folder, which has executable permissions sub_source = submission_location + learnerFile sub_destination = '/grader/submission.py' shutil.copyfile(sub_source, sub_destination) import submission # Generate test cases ------------------------------------------------------ try: testCases = createTests(numTestCases) except Exception as e: print_stderr("createTests returned this error: " + str(e)) send_feedback( 0.0, "Please reach out to course staff via discussion forums, to report a grader error." ) return # Find matching part Id and corresponding test case ------------------------ testCase = match_partId(partId, testCases) if testCase is None: print_stderr( "Cannot find matching partId. Please double check your partId's") send_feedback( 0.0, "Please verify that you have submitted to the proper part of the assignment." ) return # Run the learner submission ----------------------------------------------- # Number of test cases failed. numTestCasesFailed = 0 try: learnerOutput = [submission.main(x) for x in testCase["input"]] except Exception as e: send_feedback(0.0, "Your code returned this error: " + str(e)) return for i in range(0, numTestCases): if testCase["output"][i] != learnerOutput[i]: numTestCasesFailed += 1 # Calculate score and send feedback ---------------------------------------- totalPenalty = min(1.0, (testCasePenalty * numTestCasesFailed)) finalFractionalScore = 1.0 - totalPenalty if numTestCasesFailed > 0: feedback = "Your solution failed " + str( numTestCasesFailed) + " out of " + str( numTestCases) + " test cases. Please try again!" else: feedback = "Great job! You passed all test cases." send_feedback(finalFractionalScore, feedback)
def main(): # Define # of test cases per code block and # of graded code blocks ------------------------------------- # Please ensure that the numTestCases and numGradedFuncs are integers >= 1. numTestCases = 5 numGradedFuncs = 2 if numTestCases >= 1 and float(numTestCases).is_integer( ) and numGradedFuncs >= 1 and float(numGradedFuncs).is_integer(): testCasePenalty = 1.0 / (numTestCases * numGradedFuncs) else: print_stderr( "Please check that numTestCases and numGradedFuncs are whole integers greater than 0." ) send_feedback( 0.0, "Please reach out to course staff via discussion forums, to report a grader error." ) return # Find the learner's submission and create appropriate file ---------------------------------------------- # The directory /shared/submission/ is the standard submission directory across all courses. # This is a readonly directory. If you'd like the students to submit a zip with multiple files, # please ensure that the grader first moves the files to a folder with the correct permissions to unzip. submission_location = "/shared/submission/" # Set identifier for output file and graded code graded_id = "# GRADED FUNCTION:" submission_destination = "/grader/" #"submissions/" for file in os.listdir(submission_location): if file.endswith(".json"): nb_output = file else: nb_output = None if nb_output is None: send_feedback( 0.0, "Please reach out to course staff via discussion forums, to report a grader error." ) return # Converts notebook output to submission.<extension> and saves in /grader/ folder, which has executable permissions try: make_submission(submission_location + nb_output, graded_id, submission_destination) except Exception as e: print_stderr("make_submission returned this error: " + str(e)) send_feedback( 0.0, "Please reach out to course staff via discussion forums, to report a grader error." ) return # Generate test cases ------------------------------------------------------ try: testCases = createTests(numTestCases) except Exception as e: print_stderr("createTests returned this error: " + str(e)) send_feedback( 0.0, "Please reach out to course staff via discussion forums, to report a grader error." ) return # Run the learner submission ----------------------------------------------- # import learner graded functions sys.path.append(submission_destination) try: from submission import factors1, factors2 except Exception as e: send_feedback(0.0, "Your code returned this error: " + str(e)) return # Number of test cases failed. numTestCasesFailed = 0 # Specific feedback flags factors1_feedback = 0 factors2_feedback = 0 try: #stdout_redirected prevents print statements from learner submission #from being stored in stdout with stdout_redirected(): learnerOutput = [ factors1(x) for x in testCases["factors1"]["input"] ] except Exception as e: send_feedback(0.0, "Your code returned this error: " + str(e)) return for j in range(0, numTestCases): if testCases["factors1"]["output"][j] != learnerOutput[j]: numTestCasesFailed += 1 factors1_feedback = 1 try: #stdout_redirected prevents print statements from learner submission #from being stored in stdout with stdout_redirected(): learnerOutput = [ factors2(x) for x in testCases["factors2"]["input"] ] except Exception as e: send_feedback(0.0, "Your code returned this error: " + str(e)) return for j in range(0, numTestCases): if testCases["factors2"]["output"][j] != learnerOutput[j]: numTestCasesFailed += 1 factors2_feedback = 1 # Calculate score and send feedback ---------------------------------------- totalPenalty = min(1.0, (testCasePenalty * numTestCasesFailed)) finalFractionalScore = 1.0 - totalPenalty if numTestCasesFailed > 0: feedback = "Your solution failed " + str( numTestCasesFailed) + " out of " + str( numGradedFuncs * numTestCases) + " test cases." if factors1_feedback: feedback += " Test case(s) failed in factors1." if factors2_feedback: feedback += " Test case(s) failed in factors2." feedback += " Please try again!" else: feedback = "Great job! You passed all test cases." send_feedback(finalFractionalScore, feedback)
import base64 import os import hashlib import hmac import pyaes from util import assert_bytes, InvalidPassword, to_bytes, to_string from util import print_stderr #from x11hash import getPoWHash try: from Cryptodome.Cipher import AES except: print_stderr('[crypto] warning: Cryptodome library not available,' ' falling back to pyaes') AES = None class InvalidPadding(Exception): pass def append_PKCS7_padding(data): assert_bytes(data) padlen = 16 - (len(data) % 16) return data + bytes([padlen]) * padlen def strip_PKCS7_padding(data): assert_bytes(data)