def check_working_directory (directory): dbgMsg("checking working directory %s" % directory) if not os.path.exists(directory): errMsg("specified working directory \'%s\' does not exist!\n" % directory) sys.exit(1) if not os.path.isdir(directory): errMsg("specified working directory \'%s\' is not a directory!\n" % directory) sys.exit(1) if not isWritable(directory): errMsg("specified working directory \'%s\' is not writable!\n" % directory) sys.exit() return True
def check_working_directory (directory): dbgMsg("checking working directory %s" % directory) if not os.path.exists(directory): errMsg("Specified working directory \'%s\' does not exist!\n\nPlease create the data base with the command line option '-n'\n" % directory) sys.exit(1) if not os.path.isdir(directory): errMsg("Specified working directory \'%s\' is not a directory!\n" % directory) sys.exit(1) if not isWritable(directory): errMsg("Specified working directory \'%s\' is not writable!\n" % directory) sys.exit() return True
def get_ip(): remote_address = ENVIRON.get("REMOTE_ADDR",'') if not remote_address: return "" dbgMsg("remote_address: %s" % str(remote_address)) matches = re.match('^([^.]+\.[^.]+\.[^.]+\.).*', remote_address) if matches: return matches.groups()[0] + "0" else: return ""
def send_request_to_google_analytics(utm_url): # Make a tracking request to Google Analytics from this server. # Copies the headers from the original request to the new one. http = Http() try: http.request(utm_url, "GET", headers = { 'User-Agent': ENVIRON.get('HTTP_USER_AGENT', 'Unknown'), 'Accepts-Language:': ENVIRON.get('HTTP_ACCEPT_LANGUAGE','') }) dbgMsg("success") except HttpLib2Error: raise Exception("Error opening: %s" % utm_url)
def get_utme(custom_var): # GA CustomVar if not custom_var: return "" utme_k = "" utme_v = "" aux = "" for k, v in custom_var.items(): utme_k += aux + sanitize(k) utme_v += aux + sanitize(v) aux = "*" dbgMsg("custom_var: " + utme_k + " - " + utme_v) return ("8(" + utme_k + ")9(" + utme_v + ")")
def isWritable(directory): try: tmp_prefix = "tmp_file_for_write_testing"; count = 0 filename = os.path.join(directory, tmp_prefix) while(os.path.exists(filename)): filename = "{}.{}".format(os.path.join(directory, tmp_prefix),count) count = count + 1 f = open(filename,"w") f.close() os.remove(filename) return True except Exception as e: dbgMsg("specified working directory \'%s\' is not writable!\n" % directory) return False
def send_request_to_google_analytics(utm_url, environ): """ // Make a tracking request to Google Analytics from this server. // Copies the headers from the original request to the new one. // If request containg utmdebug parameter, exceptions encountered // communicating with Google Analytics are thown. """ http = httplib2.Http() try: resp, content = http.request(utm_url, "GET", headers={'User-Agent': environ.get('HTTP_USER_AGENT', 'Unknown'), 'Accepts-Language:': environ.get("HTTP_ACCEPT_LANGUAGE",'')} ) dbgMsg("success") except HttpLib2Error, e: errMsg("fail: %s" % utm_url) if environ['GET'].get('utmdebug'): raise Exception("Error opening: %s" % utm_url) else: pass
def track_page_view(path, title, custom_var): # Track a page view and makes a server side request to Google Analytics and writes the transparent # gif byte data to the response. time_tup = time.localtime(time.time() + COOKIE_USER_PERSISTENCE) document_referer = "-" document_path = path account = ACCOUNT domain = DOMAIN visitor_id = get_visitor_id() # // Always try and add the cookie to the response. cookie = SimpleCookie() cookie[COOKIE_NAME] = visitor_id morsel = cookie[COOKIE_NAME] morsel['expires'] = time.strftime('%a, %d-%b-%Y %H:%M:%S %Z', time_tup) morsel['path'] = COOKIE_PATH utm_gif_location = "http://www.google-analytics.com/__utm.gif" utm_url = utm_gif_location + "?" + \ "utmwv=" + VERSION + \ "&utmn=" + get_random_number() + \ "&utmhn=" + quote(domain) + \ "&utme=" + get_utme(custom_var) + \ "&utmdt=" + quote(title) + \ "&utmr=" + quote(document_referer) + \ "&utmp=" + quote(document_path) + \ "&utmac=" + account + \ "&utmcc=__utma%3D999.999.999.999.999.1%3B" + \ "&utmvid=" + visitor_id + \ "&utmip=" + get_ip() send_request_to_google_analytics(utm_url) dbgMsg("utm_url: " + utm_url)
def foo(): dbgMsg("this is a debug message in", "foo")
return errStr = getCallString(1)+" : "+string.join(map(str, args), " ") for handler in _messageHandlers: handler.handleDbgMsg(errStr) registerMessageHandler(defaultMessageHandler()) #end of messaging.py #test.py #here is a simple use case for the above module from messaging import stdMsg, dbgMsg, errMsg, setDebugging setDebugging(0) dbgMsg("this won't be printed") stdMsg("but this will") setDebugging(1) def foo(): dbgMsg("this is a debug message in", "foo") class bar: def baz(self): errMsg("this is an error message in bar") foo() b = bar() b.baz() #end of test.py
def track_page_view(environ): """ // Track a page view, updates all the cookies and campaign tracker, // makes a server side request to Google Analytics and writes the transparent // gif byte data to the response. """ time_tup = time.localtime(time.time() + COOKIE_USER_PERSISTENCE) # set some useful items in environ: environ['COOKIES'] = parse_cookie(environ.get('HTTP_COOKIE', '')) environ['GET'] = {} for key, value in parse_qsl(environ.get('QUERY_STRING', ''), True): environ['GET'][key] = value # we only have one value per key name, right? :) x_utmac = environ['GET'].get('x_utmac', None) domain = environ.get('HTTP_HOST', '') # Get the referrer from the utmr parameter, this is the referrer to the # page that contains the tracking pixel, not the referrer for tracking # pixel. document_referer = environ['GET'].get("utmr", "") if not document_referer or document_referer == "0": document_referer = "-" else: document_referer = unquote(document_referer) document_path = environ['GET'].get('utmp', "") if document_path: document_path = unquote(document_path) account = environ['GET'].get('utmac', '') user_agent = environ.get("HTTP_USER_AGENT", '') # // Try and get visitor cookie from the request. cookie = environ['COOKIES'].get(COOKIE_NAME) visitor_id = get_visitor_id(environ.get("HTTP_X_DCMGUID", ''), account, user_agent, cookie) # // Always try and add the cookie to the response. cookie = SimpleCookie() cookie[COOKIE_NAME] = visitor_id morsel = cookie[COOKIE_NAME] morsel['expires'] = time.strftime('%a, %d-%b-%Y %H:%M:%S %Z', time_tup) morsel['path'] = COOKIE_PATH utm_gif_location = "http://www.google-analytics.com/__utm.gif" for utmac in [account, x_utmac]: if not utmac: continue # ignore empty utmacs # // Construct the gif hit url. utm_url = utm_gif_location + "?" + \ "utmwv=" + VERSION + \ "&utmn=" + get_random_number() + \ "&utmhn=" + quote(domain) + \ "&utmsr=" + environ['GET'].get('utmsr', '') + \ "&utme=" + environ['GET'].get('utme', '') + \ "&utmr=" + quote(document_referer) + \ "&utmp=" + quote(document_path) + \ "&utmac=" + utmac + \ "&utmcc=__utma%3D999.999.999.999.999.1%3B" + \ "&utmvid=" + visitor_id + \ "&utmip=" + get_ip(environ.get("REMOTE_ADDR",'')) dbgMsg("utm_url: " + utm_url) send_request_to_google_analytics(utm_url, environ) # // If the debug parameter is on, add a header to the response that contains # // the url that was used to contact Google Analytics. headers = [('Set-Cookie', str(cookie).split(': ')[1])] if environ['GET'].get('utmdebug', False): headers.append(('X-GA-MOBILE-URL', utm_url)) # Finally write the gif data to the response response = write_gif_data() response_headers = response['response_headers'] response_headers.extend(headers) return response
def track_page_view(environ): """ // Track a page view, updates all the cookies and campaign tracker, // makes a server side request to Google Analytics and writes the transparent // gif byte data to the response. """ time_tup = time.localtime(time.time() + COOKIE_USER_PERSISTENCE) # set some useful items in environ: environ['COOKIES'] = parse_cookie(environ.get('HTTP_COOKIE', '')) environ['GET'] = {} for key, value in parse_qsl(environ.get('QUERY_STRING', ''), True): environ['GET'][key] = value # we only have one value per key name, right? :) x_utmac = environ['GET'].get('x_utmac', None) domain = environ.get('HTTP_X_FORWARDED_HOST', environ.get('HTTP_HOST', '')) # Get the referrer from the utmr parameter, this is the referrer to the # page that contains the tracking pixel, not the referrer for tracking # pixel. document_referer = environ['GET'].get("utmr", "") if not document_referer or document_referer == "0": document_referer = "-" else: document_referer = unquote(document_referer) document_path = environ['GET'].get('utmp', "") if document_path: document_path = unquote(document_path) account = environ['GET'].get('utmac', '') user_agent = environ.get("HTTP_USER_AGENT", '') # // Try and get visitor cookie from the request. cookie = environ['COOKIES'].get(COOKIE_NAME) visitor_id = get_visitor_id(environ.get("HTTP_X_DCMGUID", ''), account, user_agent, cookie) # // Always try and add the cookie to the response. cookie = SimpleCookie() cookie[COOKIE_NAME] = visitor_id morsel = cookie[COOKIE_NAME] morsel['expires'] = time.strftime('%a, %d-%b-%Y %H:%M:%S %Z', time_tup) morsel['path'] = COOKIE_PATH utm_gif_location = "http://www.google-analytics.com/__utm.gif" client_ip = environ.get("HTTP_X_FORWARDED_FOR", environ.get("REMOTE_ADDR", "")) for utmac in [account, x_utmac]: if not utmac: continue # ignore empty utmacs # // Construct the gif hit url. utm_url = utm_gif_location + "?" + \ "utmwv=" + VERSION + \ "&utmn=" + get_random_number() + \ "&utmhn=" + quote(domain) + \ "&utmsr=" + environ['GET'].get('utmsr', '') + \ "&utme=" + environ['GET'].get('utme', '') + \ "&utmr=" + quote(document_referer) + \ "&utmp=" + quote(document_path) + \ "&utmac=" + utmac + \ "&utmcc=__utma%3D999.999.999.999.999.1%3B" + \ "&utmvid=" + visitor_id + \ "&utmip=" + get_ip(client_ip) dbgMsg("utm_url: " + utm_url) send_request_to_google_analytics(utm_url, environ) # // If the debug parameter is on, add a header to the response that contains # // the url that was used to contact Google Analytics. headers = [('Set-Cookie', str(cookie).split(': ')[1])] if environ['GET'].get('utmdebug', False): headers.append(('X-GA-MOBILE-URL', utm_url)) # Finally write the gif data to the response response = write_gif_data() response_headers = response['response_headers'] response_headers.extend(headers) return response