def get_files(module_dirs): modules = [] module_names = [] for module_dir in module_dirs: try: module_files = [ f for f in os.listdir(module_dir) if is_module(f, module_dir) ] except OSError as ex: log.debug("Error scanning directory {0}: {1}".format( module_dir, str(ex))) continue for module_file in module_files: full_path = os.path.join(module_dir, module_file) name = os.path.splitext(module_file)[0] if name in module_names: log.critical( "Plugin with file name {0} already loaded. Please rename {1}." .format(name, full_path)) raise HtExit(1) else: module_names.append(name) module = {'path': full_path, 'name': name} modules.append(module) return modules
def Start(SOURCE): ''' Detects which "Subject" the file is and sends it to the correct handler ''' log.info(' ') log.info('--------------------------------------------') log.info('[{0}]: Processing File'.format(SOURCE)) file = ProcessFile(SOURCE) if file.Subject() == 'Unknown': log.critical('[{0}]: Unable to determine subject!'.format(SOURCE)) sys.exit() elif file.Subject() == 'Facility Request': log.debug('[{0}]: Detected Facility Request...'.format(SOURCE)) FacilityRequest.Start(SOURCE, PATH_FAC_REQUEST) elif file.Subject() == 'Employee File': pass else: log.critical( '[{0}]: Unknown error has occured while processing file...'.format( SOURCE)) sys.exit()
def configureHdfsSiteXML(): if not os.path.exists(HDFS_SITE_XML_PATH): filename = os.path.basename(HDFS_SITE_XML_PATH) log.critical(f"File not found: {HDFS_SITE_XML_PATH}") raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), filename) restoreFile(HDFS_SITE_XML_PATH) filename = os.path.basename(HDFS_SITE_XML_PATH) log.info(f"Configuring {filename}") doc = minidom.parse(HDFS_SITE_XML_PATH) configurationElement = doc.getElementsByTagName('configuration')[0] propertyElement = doc.createElement('property') nameElement = doc.createElement("name") nameElementData = doc.createTextNode('dfs.replication') nameElement.appendChild(nameElementData) valueElement = doc.createElement("value") valueElementData = doc.createTextNode('1') valueElement.appendChild(valueElementData) propertyElement.appendChild(nameElement) propertyElement.appendChild(valueElement) configurationElement.appendChild(propertyElement) with open(HDFS_SITE_XML_PATH, 'w') as fp: fp.write(doc.toprettyxml()) log.info(f"{filename} configuration done.")
def process_replacements(self): for href, enclosure_replacement, pub_key, test_pub_key in self.replacements: log.debug( "href:`%s` enclosure_replacement:`%s` pub_key:`%s` test_pub_key:`%s`" % (href, enclosure_replacement, pub_key, test_pub_key)) errors = [] if not href: errors.append("href null or empty") continue if not enclosure_replacement: errors.append("enclosure_replacement null or empty") if not pub_key: errors.append("pub_key null or empty") if not test_pub_key: errors.append("test_pub_key null or empty") if errors: log.error("Skipping:%s for: href:%s enclosure_replacement:%s " "pub_key:%s test_pub_key:%s" % (",".join(errors), href, enclosure_replacement, pub_key, test_pub_key)) continue if self.max_errors_reached(): log.critical("Max errors as been reached for all urls %s" % pformat(error_tracker)) return while not self.max_errors_reached(): find = href good_url = self.get_first_test_result(test_pub_key, pub_key) if not good_url: log.error("Unable to get a good url for test_pub_key:%s " "pub_key:%s" % (test_pub_key, pub_key)) break log.debug("******* MADE IT good_url:%s" % good_url) hash_url = good_url.replace(test_pub_key, ":hash") replace = good_url.replace(test_pub_key, enclosure_replacement) log.debug("replace:%s" % replace) if hash_url not in error_tracker: error_tracker[hash_url] = 0 if hash_url not in reliablity_tracker: reliablity_tracker[hash_url] = 0 if self.test_download(replace): reliablity_tracker[hash_url] += 1 self.text = self.text.replace(find, replace) break error_tracker[hash_url] += 1
def alpha_batch(infile, name, action, alphas, grayed): nflippeds = {} for alpha in alphas: outfile = infile.replace( '.', '_{2}_{1}_p{0}.'.format(int(alpha * 10), 'cgc' if grayed else 'pbc', name)) # print alpha log.critical('---------------------\n' + outfile + '\n---------------------') nflipped = action(infile, outfile, alpha) nflippeds[(name, alpha)] = nflipped return nflippeds
def createstattable(self, version): """Creates table for storing process' runtime statistics""" tablename = self.gettablename(version, self.table[0]) self.table.append(tablename) # Create table try: self.c.execute('''CREATE TABLE {} \ (cpu REAL, mem INTEGER, thread INTEGER)'''.format(tablename)) except sqlite3.OperationalError: # Table already exists log.critical("Table {} already exists!".format(tablename)) raise
def banner(arg): try: reddit.subreddit(SUBREDDIT).banned.add(arg, duration=BAN_LENGTH, ban_reason=BAN_REASON, ban_message=BAN_MESSAGE, note='') except Exception as e: log.critical(e) log.critical("Missing permission to ban (Access)") else: log.info(f"Banned {arg} for {BAN_LENGTH}") reset(val)
def _connectServer(self): ip = '192.168.4.1' inPort = 9001 self.buffer_size = 1024 self.my_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: self.my_socket.bind((ip, inPort)) self.my_socket.setblocking(0) self.my_socket.settimeout(0.002) self.my_socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, self.buffer_size) log.info("OSCServer : IP = %s inPort = %d Buffer Size = %d", ip, inPort, self.buffer_size) except Exception as e: log.critical("Server connection error:\n" + str(e)) exit(0)
def measure_temp(): temp_c = os.popen('vcgencmd measure_temp').readline() temp_c = temp_c.replace("temp=", '') temp = temp_c.replace(".0'C", '') temp = int(temp) if temp > 80: return log.critical('Device - CPU OVER TEMPERATURE - %s' % temp_c) else: return log.info('Device - CPU Temperature - %s' % temp_c)
def backupFile(filePath): if not os.path.exists(filePath): log.critical("FILE NOT FOUND : {filePath}") return filename = os.path.basename(filePath) src = filePath if not os.path.exists(BACKUP_DIR): log.info os.mkdir(BACKUP_DIR) dst = filePath.replace(HADOOP_HOME, BACKUP_DIR) dstFolder = os.path.dirname(dst) if not os.path.exists(dst): log.info("Intializing Backup for {filename}") if not os.path.exists(dstFolder): log.debug(f"Creating Directory Tree {dstFolder}") os.makedirs(dstFolder) shutil.copy(src, dst) log.debug(f"Successfully Copied {src} to {dstFolder}.") log.info(f"Backup Created for {filename}.") return log.debug(f"Backup Already Found for {filename}.")
def main(): context = zmq.Context() socket = context.socket(zmq.REP) socket.bind(ADDRESS) log.info(f'listening on {ADDRESS}') try: while True: # Receive next request from client message = socket.recv_json() log.info(f'Received request: "{message}"') # Do some 'work' time.sleep(0.1) reply = {'name': message.get('name', 'unknown'), 'msg': 'World'} log.info(f'Completed work: "{reply}"') # Send reply back to client socket.send_json(reply) except KeyboardInterrupt: log.critical('KeyboardInterrupt detected. Exiting.') finally: socket.close() context.term()
def sanity_check(): "Sanity Check for script." config.count_application_runs += 1 _warnings = [] ### LOCAL CHECKS ### # Windows version check winver = sys.getwindowsversion() log.debug('Running iQuality v%s (r%d) on Windows %d.%d.%d %s' % (__version__, __rev__, winver.major, winver.minor, winver.build, winver.service_pack)) # Python version check if sys.version_info < (2, 6) or sys.version_info >= (3, 0): msg = "must use python 2.7" log.critical(msg) raise Exception(msg) log.debug('CPython version is %d.%d.%d' % (sys.version_info.major, sys.version_info.minor, sys.version_info.micro)) log.debug('PyQt version is %s, Qt version is %s' % (QtCore.PYQT_VERSION_STR, QtCore.QT_VERSION_STR)) # youtube-dl check try: import youtube_dl log.debug("youtube-dl version is %s" % youtube_dl.__version__) except ImportError: log.warning("Could not load the youtube-dl module") # Phonon version check try: from PyQt4.phonon import Phonon if Phonon.BackendCapabilities.isMimeTypeAvailable('video/x-flv'): log.debug('Phonon version is %s. video/x-flv is supported.' % Phonon.phononVersion()) else: log.warning('Phonon version is %s. video/x-flv is not supported.' % Phonon.phononVersion()) except ImportError: log.warning("Could not load the phonon module") # Free space check freespace = utils.get_free_space(config.temp_dir) if freespace < 200 * 1024**2: # 200 MB drive = os.path.splitdrive(config.temp_dir)[0] log.warning( "There are less than 200MB available in drive %s (%.2fMB left)." % (drive, freespace / 1024.0**2)) _warnings.append(NoSpaceWarning(drive, freespace)) # iTunes' availablity check itunesPath = r'%s\My Documents\My Music\iTunes\iTunes Media\Automatically Add to iTunes' % utils.get_home_dir( ) if not os.path.exists(itunesPath): config.is_itunes_installed = False if config.post_download_action == 'addItunes': config.post_download_action = 'ask' log.warning( "iTunes Media not found. setting is_itunes_installed to False") # Context Menu check try: # IMPROVE: REMOVE THE TRY-EXCEPT BLOCK if config.id3editor_in_context_menu and not utils.check_context_menu_status( ): log.debug("Registering Context Menu Object...") try: utils.register_with_context_menu() except WindowsError, e: if e.winerror == 5: # Access is denied log.debug( "Access is denied. Setting id3editor_in_context_menu to False." ) config.id3editor_in_context_menu = False else: raise if not config.id3editor_in_context_menu and utils.check_context_menu_status( ): log.debug("Unregistering Context Menu Object...") try: utils.unregister_with_context_menu() except WindowsError, e: if e.winerror == 5: # Access is denied log.debug( "Access is denied. Setting id3editor_in_context_menu to True." ) config.id3editor_in_context_menu = True else: raise
def handle_error(plugin, error=None): log.critical( "An error occurred during the execution of plugin '{name}'".format( **plugin.PLUGIN_INFO)) log.critical("Error is: %s" % error)
from logger import log log.info("Hellow world") log.debug('This is a debug message') log.info('This is an info message') log.warning('This is a warning message') log.error('This is an error message') log.critical('This is a critical message')
from login import (sql_user, sql_password, sql_host, sql_database) try: connection = psycopg2.connect(user=sql_user, password=sql_password, host=sql_host, port="5432", database=sql_database) cursor = connection.cursor() cursor.execute("SELECT version();") record = cursor.fetchone() log.info("You are connected to - " + str(record)) except (Exception, psycopg2.Error) as error: log.critical("Error while connecting to PostgreSQL " + str(error)) exit() def check_existence(val0): try: cursor = connection.cursor() postgreSQL_select_Query = "select * from public.ban" cursor.execute(postgreSQL_select_Query) db = cursor.fetchall() for row in db: if row[0] == val0: return True else: return False except (Exception, psycopg2.Error) as error:
def sanity_check(): "Sanity Check for script." config.count_application_runs += 1 _warnings = [] ### LOCAL CHECKS ### # Windows version check winver = sys.getwindowsversion() log.debug('Running iQuality v%s (r%d) on Windows %d.%d.%d %s' % (__version__, __rev__, winver.major, winver.minor, winver.build, winver.service_pack)) # Python version check if sys.version_info < (2, 6) or sys.version_info >= (3, 0): msg = "must use python 2.7" log.critical(msg) raise Exception(msg) log.debug('CPython version is %d.%d.%d' % (sys.version_info.major, sys.version_info.minor, sys.version_info.micro)) log.debug('PyQt version is %s, Qt version is %s' % (QtCore.PYQT_VERSION_STR, QtCore.QT_VERSION_STR)) # youtube-dl check try: import youtube_dl log.debug("youtube-dl version is %s" % youtube_dl.__version__) except ImportError: log.warning("Could not load the youtube-dl module") # Phonon version check try: from PyQt4.phonon import Phonon if Phonon.BackendCapabilities.isMimeTypeAvailable('video/x-flv'): log.debug('Phonon version is %s. video/x-flv is supported.' % Phonon.phononVersion()) else: log.warning('Phonon version is %s. video/x-flv is not supported.' % Phonon.phononVersion()) except ImportError: log.warning("Could not load the phonon module") # Free space check freespace = utils.get_free_space(config.temp_dir) if freespace < 200*1024**2: # 200 MB drive = os.path.splitdrive(config.temp_dir)[0] log.warning("There are less than 200MB available in drive %s (%.2fMB left)." % (drive, freespace/1024.0**2)) _warnings.append(NoSpaceWarning(drive, freespace)) # iTunes' availablity check itunesPath = r'%s\My Documents\My Music\iTunes\iTunes Media\Automatically Add to iTunes' % utils.get_home_dir() if not os.path.exists(itunesPath): config.is_itunes_installed = False if config.post_download_action == 'addItunes': config.post_download_action = 'ask' log.warning("iTunes Media not found. setting is_itunes_installed to False") # Context Menu check try: # IMPROVE: REMOVE THE TRY-EXCEPT BLOCK if config.id3editor_in_context_menu and not utils.check_context_menu_status(): log.debug("Registering Context Menu Object...") try: utils.register_with_context_menu() except WindowsError, e: if e.winerror == 5: # Access is denied log.debug("Access is denied. Setting id3editor_in_context_menu to False.") config.id3editor_in_context_menu = False else: raise if not config.id3editor_in_context_menu and utils.check_context_menu_status(): log.debug("Unregistering Context Menu Object...") try: utils.unregister_with_context_menu() except WindowsError, e: if e.winerror == 5: # Access is denied log.debug("Access is denied. Setting id3editor_in_context_menu to True.") config.id3editor_in_context_menu = True else: raise
from login import (reddit_client_id, reddit_client_secret, reddit_user_agent, reddit_username, reddit_password) # REDDIT LOGGING try: reddit = praw.Reddit(client_id=reddit_client_id, client_secret=reddit_client_secret, user_agent=reddit_user_agent, username=reddit_username, password=reddit_password) if reddit.read_only == False: log.info("Logged in as u/" + reddit.user.me().name) else: log.critical('Reddit is read only') log.critical("Login credentials are missing.") quit() except Exception as e: log.critical(e) log.critical("Login credentials are not correct.") quit() def banner(arg): try: reddit.subreddit(SUBREDDIT).banned.add(arg, duration=BAN_LENGTH, ban_reason=BAN_REASON, ban_message=BAN_MESSAGE,