def create_cookie_jar(cookie_file=None): """Return a cookie jar backed by cookie_file If cooie_file is not provided, we will default it. If the cookie_file does not exist, we will create it with the proper permissions. In the case where we default cookie_file, and it does not exist, we will attempt to copy the .post-review-cookies.txt file. """ home_path = get_home_path() if not cookie_file: cookie_file = os.path.join(home_path, RBTOOLS_COOKIE_FILE) post_review_cookies = os.path.join(home_path, '.post-review-cookies.txt') if (not os.path.isfile(cookie_file) and os.path.isfile(post_review_cookies)): try: shutil.copyfile(post_review_cookies, cookie_file) os.chmod(cookie_file, 0600) except IOError, e: logging.warning("There was an error while copying " "post-review's cookies: %s" % e)
def create_cookie_jar(cookie_file=None): """Return a cookie jar backed by cookie_file If cooie_file is not provided, we will default it. If the cookie_file does not exist, we will create it with the proper permissions. In the case where we default cookie_file, and it does not exist, we will attempt to copy the .post-review-cookies.txt file. """ home_path = get_home_path() if not cookie_file: cookie_file = os.path.join(home_path, RBTOOLS_COOKIE_FILE) post_review_cookies = os.path.join(home_path, '.post-review-cookies.txt') if (not os.path.isfile(cookie_file) and os.path.isfile(post_review_cookies)): try: shutil.copyfile(post_review_cookies, cookie_file) os.chmod(cookie_file, 0o600) except IOError as e: logging.warning("There was an error while copying " "post-review's cookies: %s", e) if not os.path.isfile(cookie_file): try: open(cookie_file, 'w').close() os.chmod(cookie_file, 0o600) except IOError as e: logging.warning('There was an error while creating a ' 'cookie file: %s', e) return MozillaCookieJar(cookie_file), cookie_file
def get_cookie(self): """Return a cookie file that is read-only.""" # If we end up creating a cookie file, make sure it's only # readable by the user. os.umask(0077) # Generate a path to the cookie file. return os.path.join(get_home_path(), ".post-review-cookies.txt")
def init_logging(self): """Initializes logging for the command. This will set up different log handlers based on the formatting we want for the given levels. The INFO log handler will just show the text, like a print statement. WARNING and higher will show the level name as a prefix, in the form of "LEVEL: message". If debugging is enabled, a debug log handler will be set up showing debug messages in the form of ">>> message", making it easier to distinguish between debugging and other messages. """ root = logging.getLogger() if self.options.debug: handler = logging.StreamHandler() handler.setFormatter(logging.Formatter('>>> %(message)s')) handler.setLevel(logging.DEBUG) handler.addFilter(LogLevelFilter(logging.DEBUG)) root.addHandler(handler) root.setLevel(logging.DEBUG) else: root.setLevel(logging.INFO) # Handler for info messages. We'll treat these like prints. handler = logging.StreamHandler() handler.setFormatter(logging.Formatter('%(message)s')) handler.setLevel(logging.INFO) handler.addFilter(LogLevelFilter(logging.INFO)) root.addHandler(handler) # Handler for warnings, errors, and criticals. They'll show the # level prefix and the message. handler = logging.StreamHandler() handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s')) handler.setLevel(logging.WARNING) root.addHandler(handler) logging.debug('RBTools %s', get_version_string()) logging.debug('Python %s', sys.version) logging.debug('Running on %s', platform.platform()) logging.debug('Home = %s', get_home_path()) logging.debug('Current directory = %s', os.getcwd())
def init_logging(self): """Initializes logging for the command. This will set up different log handlers based on the formatting we want for the given levels. The INFO log handler will just show the text, like a print statement. WARNING and higher will show the level name as a prefix, in the form of "LEVEL: message". If debugging is enabled, a debug log handler will be set up showing debug messages in the form of ">>> message", making it easier to distinguish between debugging and other messages. """ if sys.stdout.isatty(): # We only use colorized logging when writing to TTYs, so we don't # bother initializing it then. colorama.init() root = logging.getLogger() if self.options.debug: handler = logging.StreamHandler() handler.setFormatter(self._create_formatter( 'DEBUG', '{color}>>>{reset} %(message)s')) handler.setLevel(logging.DEBUG) handler.addFilter(LogLevelFilter(logging.DEBUG)) root.addHandler(handler) root.setLevel(logging.DEBUG) else: root.setLevel(logging.INFO) # Handler for info messages. We'll treat these like prints. handler = logging.StreamHandler() handler.setFormatter(self._create_formatter( 'INFO', '{color}%(message)s{reset}')) handler.setLevel(logging.INFO) handler.addFilter(LogLevelFilter(logging.INFO)) root.addHandler(handler) # Handlers for warnings, errors, and criticals. They'll show the # level prefix and the message. levels = ( ('WARNING', logging.WARNING), ('ERROR', logging.ERROR), ('CRITICAL', logging.CRITICAL), ) for level_name, level in levels: handler = logging.StreamHandler() handler.setFormatter(self._create_formatter( level_name, '{color}%(levelname)s:{reset} %(message)s')) handler.addFilter(LogLevelFilter(level)) handler.setLevel(level) root.addHandler(handler) logging.debug('RBTools %s', get_version_string()) logging.debug('Python %s', sys.version) logging.debug('Running on %s', platform.platform()) logging.debug('Home = %s', get_home_path()) logging.debug('Current directory = %s', os.getcwd())
def init_logging(self): """Initializes logging for the command. This will set up different log handlers based on the formatting we want for the given levels. The INFO log handler will just show the text, like a print statement. WARNING and higher will show the level name as a prefix, in the form of "LEVEL: message". If debugging is enabled, a debug log handler will be set up showing debug messages in the form of ">>> message", making it easier to distinguish between debugging and other messages. """ root = logging.getLogger() if self.options.debug: handler = logging.StreamHandler() handler.setFormatter(logging.Formatter('>>> %(message)s')) handler.setLevel(logging.DEBUG) handler.addFilter(LogLevelFilter(logging.DEBUG)) root.addHandler(handler) root.setLevel(logging.DEBUG) else: root.setLevel(logging.INFO) # Handler for info messages. We'll treat these like prints. handler = logging.StreamHandler() handler.setFormatter(logging.Formatter('%(message)s')) handler.setLevel(logging.INFO) handler.addFilter(LogLevelFilter(logging.INFO)) root.addHandler(handler) # Handler for warnings, errors, and criticals. They'll show the # level prefix and the message. handler = logging.StreamHandler() if (self.options.color == 'always' or (self.options.color == 'auto' and sys.stderr.isatty())): handler.setFormatter(ColoredFormatter( '%(log_color)s%(levelname)-8s%(reset)s' '%(message_log_color)s%(message)s', datefmt=None, reset=True, log_colors={ 'WARNING': self.options.warn_color, 'ERROR': self.options.error_color, 'CRITICAL': self.options.critical_color, }, secondary_log_colors={ 'message': { 'WARNING': self.options.warn_color, 'ERROR': self.options.error_color, 'CRITICAL': self.options.critical_color, } }, style='%' )) else: handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s')) handler.setLevel(logging.WARNING) root.addHandler(handler) logging.debug('RBTools %s', get_version_string()) logging.debug('Python %s', sys.version) logging.debug('Running on %s', platform.platform()) logging.debug('Home = %s', get_home_path()) logging.debug('Current directory = %s', os.getcwd())
def init_logging(self): """Initializes logging for the command. This will set up different log handlers based on the formatting we want for the given levels. The INFO log handler will just show the text, like a print statement. WARNING and higher will show the level name as a prefix, in the form of "LEVEL: message". If debugging is enabled, a debug log handler will be set up showing debug messages in the form of ">>> message", making it easier to distinguish between debugging and other messages. """ root = logging.getLogger() if self.options.debug: handler = logging.StreamHandler() handler.setFormatter(logging.Formatter('>>> %(message)s')) handler.setLevel(logging.DEBUG) handler.addFilter(LogLevelFilter(logging.DEBUG)) root.addHandler(handler) root.setLevel(logging.DEBUG) else: root.setLevel(logging.INFO) # Handler for info messages. We'll treat these like prints. handler = logging.StreamHandler() handler.setFormatter(logging.Formatter('%(message)s')) handler.setLevel(logging.INFO) handler.addFilter(LogLevelFilter(logging.INFO)) root.addHandler(handler) # Handler for warnings, errors, and criticals. They'll show the # level prefix and the message. handler = logging.StreamHandler() if (self.options.color == 'always' or (self.options.color == 'auto' and sys.stderr.isatty())): handler.setFormatter( ColoredFormatter( '%(log_color)s%(levelname)-8s%(reset)s' '%(message_log_color)s%(message)s', datefmt=None, reset=True, log_colors={ 'WARNING': self.options.warn_color, 'ERROR': self.options.error_color, 'CRITICAL': self.options.critical_color, }, secondary_log_colors={ 'message': { 'WARNING': self.options.warn_color, 'ERROR': self.options.error_color, 'CRITICAL': self.options.critical_color, } }, style='%')) else: handler.setFormatter( logging.Formatter('%(levelname)s: %(message)s')) handler.setLevel(logging.WARNING) root.addHandler(handler) logging.debug('RBTools %s', get_version_string()) logging.debug('Python %s', sys.version) logging.debug('Running on %s', platform.platform()) logging.debug('Home = %s', get_home_path()) logging.debug('Current directory = %s', os.getcwd())