Пример #1
0
 def log_rating(self, filename, rating):
     if filename:
         query = 'UPDATE music SET rating=%i WHERE \
                  path = "%s" and filename = "%s"' % (int(rating),
                  util.escape(os.path.dirname(filename)),
                  util.escape(os.path.basename(filename)) )
         self.runquery(query)
Пример #2
0
 def log_track(self, filename):
     if filename:
         query = 'UPDATE music SET play_count=play_count+1,last_play=%f WHERE \
                  path = "%s" and filename = "%s"' % (time.time(),
                  util.escape(os.path.dirname(filename)),
                  util.escape(os.path.basename(filename)) )
         self.runquery(query)
Пример #3
0
    def send_room_message(self, message, formatted=False):
        """formatted means that it is an html message"""
        if message is None:
            return

        try:
            if formatted:
                html_message = "<span>" + message + "</span>"
                self.send_message(mto=self.room,
                                  mbody=message,
                                  mhtml=html_message,
                                  mtype="groupchat")
            else:
                # send every line seperately
                lines = message.split("\n")
                for line in lines:
                    self.send_message(mto=self.room,
                                      mbody=line,
                                      mtype="groupchat")
        except Exception as e:
            print("ERROR sending message: " + message)
            message = "Error. Tried to send message: <br/>\n" + escape(message)
            message += "<br/>\n<br/>\nException was: <br/>\n" + escape(str(e))
            html_message = "<span>" + message + "</span>"
            self.send_message(mto=self.room,
                              mbody=message,
                              mhtml=html_message,
                              mtype="groupchat")
Пример #4
0
def setLinkEmulation(host, dev, bandwidth=None, **kwargs):
	assert ifaceutil.interfaceExists(host, dev)
	netem_ref = "dev %s root handle 1:0" % util.escape(dev)
	if not bandwidth is None:
		netem_ref = "dev %s parent 1:1 handle 10:" % util.escape(dev)
		_tc_mod(host, "qdisc", "dev %s root handle 1:" % util.escape(dev), _buildTbf(bandwidth))
	_tc_mod(host, "qdisc", netem_ref, _buildNetem(bandwidth=bandwidth, **kwargs))
Пример #5
0
    def generate_history(self, pagename):
        """
        Extract the complete history for a given page and stores it in the hdf.
        This information is used to present a changelog/history for a given page
        """
        cursor = self.db.cursor ()
        cursor.execute ('SELECT version, time, author, comment, ipnr FROM wiki '
                        'WHERE name=%s ORDER BY version DESC', pagename)
        i = 0
        while 1:
            row = cursor.fetchone()
            if not row: break
            elif i == 0:
                self.req.hdf.setValue('wiki.history', '1')

            time_str = time.strftime('%x %X', time.localtime(int(row[1])))

            n = 'wiki.history.%d' % i
            self.req.hdf.setValue(n, str(i))
            self.req.hdf.setValue(n+'.url',
                                  escape(self.env.href.wiki(pagename, str(row[0]))))
            self.req.hdf.setValue(n+'.diff_url',
                                  escape(self.env.href.wiki(pagename, str(row[0]), 1)))
            self.req.hdf.setValue(n+'.version', str(row[0]))
            self.req.hdf.setValue(n+'.time', time_str)
            self.req.hdf.setValue(n+'.author', str(row[2]))
            self.req.hdf.setValue(n+'.comment', wiki_to_oneliner(row[3] or '', self.req.hdf, self.env, self.db))
            self.req.hdf.setValue(n+'.ipaddr', str(row[4]))
            i = i + 1
Пример #6
0
def fetchWeekly(cur):
    cur.execute('select d_id,count(*) from response where replyDate > (select subtime(now(), \'7 00:00:00\')) group by d_id order by count(*) desc limit 5')
    MAX_RESPONSES = 5
    convos = []
    for r in cur.fetchall():
        d_id = r[0]
        cur.execute('select category.name,user_id,title,content from conversation inner join category using (cat_id) where d_id=%s',(d_id,))
        test = cur.fetchone()
        name = util.formatCategoryName(test[0]);
        title = util.escape(test[2].encode('utf-8'))
        poster = fetchUser(cur,test[1])
        
        item = {'user':poster,'content':util.escape(test[3].encode('utf-8')).replace('\n','<br />')}
        
        cur.execute('select user_id,content from response where d_id=%s order by replyDate desc limit '+str(MAX_RESPONSES),(d_id,))
        responses = []
        for r2 in cur.fetchall():
            responses.insert(0,{'user':fetchUser(cur,r2[0]),'content':util.escape(r2[1].encode('utf-8')).replace('\n','<br />')})
        if (responses < MAX_RESPONSES):
            responses.insert(0,item)
            
        # count replies
        numReplies = r[1]

        # count participants
        cur.execute('select DISTINCT user_id from response where d_id=%s',(d_id,))
        uids = set()
        for u in cur.fetchall():
            uids.add(u[0])
        uids.add(poster['user_id'])
        convos.append({'title':title,'category_name':name,'responses':responses,'numReplies':numReplies,'numUsers':len(uids)})
    return convos
Пример #7
0
    def log_receiver (self, changed_paths, rev, author, date, log, pool):
        if not changed_paths: return

        # Store the copyfrom-information so we can follow the file/dir
        # through tags/banches/copy/renames.
        for newpath in changed_paths.keys():
            change = changed_paths[newpath]
            if change.copyfrom_path:
                self.branch_info.setdefault(rev, []).append((change.copyfrom_path, newpath))

        shortlog = util.shorten_line(util.wiki_escape_newline(log))
        t = svn.util.svn_time_from_cstring(date, pool) / 1000000
        gmt = time.gmtime(t)
        item = {
            'rev'      : rev,
            'author'   : author and util.escape(author) or 'None',
            'date'     : util.svn_date_to_string (date, pool),
            'gmt'      : time.strftime('%a, %d %b %Y %H:%M:%S GMT', gmt),
            'log.raw'  : util.escape(log),
            'log'      : wiki_to_oneliner(util.shorten_line(util.wiki_escape_newline(log)), self.req.hdf, self.env,self.db),
            'shortlog' : util.escape(shortlog),
            'file_href': self.env.href.browser(self.path, rev),
            'changeset_href': self.env.href.changeset(rev)
            }
        self.log_info.insert (0, item)
Пример #8
0
def unpackdir(host, archive, dir, args=""):
    assert existsFile(host, archive), "Archive does not exist"
    if archive.endswith(".gz") and not "-z" in args:
        args = args + " -z"
    if archive.endswith(".bz2") and not "-j" in args:
        args = args + " -j"
    return host.execute("tar -xf %s -C %s %s" % (util.escape(archive), util.escape(dir), args))
Пример #9
0
 def log_rating(self, filename, rating):
     if filename:
         query = 'UPDATE music SET rating=%i WHERE \
                  path = "%s" and filename = "%s"' % (
             int(rating), util.escape(os.path.dirname(filename)),
             util.escape(os.path.basename(filename)))
         self.runquery(query)
Пример #10
0
def get_astropod(api_key):
    url = "https://api.nasa.gov/planetary/apod?api_key=" + api_key

    result = requests.get(url)

    if result.status_code != 200:
        return (
            "SPAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACE isn't working right now. Please check your sky, and try again.",
            False)

    data = result.json()

    url = data['url']
    title = util.escape(data['title'])
    explanation = util.escape(data['explanation'])
    date = data['date']

    if 'youtube' in url:
        code = url.split("embed/")[1].split("?")[0]
        url = "https://www.youtube.com/watch?v=" + code
        return ("**" + title + "**" + "\n" + explanation + "\n" + url, False)

    img_url = url

    e = discord.Embed().set_image(url=img_url)
    e.title = title
    e.description = explanation
    e.url = "https://apod.nasa.gov/apod/astropix.html"
    return (e, True)
Пример #11
0
def ping(host, ip, samples=10, maxWait=5, iface=None):
	assert samples >= 2
	iface = "" if iface is None else "-I %s" % util.escape(iface)
	cmd = "ping" if isIpv4(ip) else "ping6" 
	res = host.execute("%s -A -c %d -n -q -w %d %s %s" % (cmd, samples, maxWait, iface, util.escape(ip)))
	if not res:
		return
	import re
	spattern = re.compile("(\d+) packets transmitted, (\d+) received(, \+(\d+) errors)?, (\d+)% packet loss, time (\d+)(m?s)")
	dpattern = re.compile("rtt min/avg/max/mdev = (\d+\.\d+)/(\d+\.\d+)/(\d+\.\d+)/(\d+\.\d+) (m?s)(, pipe \d+)?, ipg/ewma (\d+\.\d+)/(\d+\.\d+) (m?s)")
	summary = False
	details = False
	for line in res.splitlines():
		if spattern.match(line):
			(transmitted, received, dummy, errors, loss, total, unit) = spattern.match(line).groups()
			(transmitted, received, errors, loss, total) = (int(transmitted), int(received), int(errors) if errors else None, float(loss)/100.0, float(total))
			summary = True
		if dpattern.match(line):
			(rttmin, rttavg, rttmax, rttstddev, rttunit, dummy, ipg, ewma, ipg_ewma_unit) = dpattern.match(line).groups()
			(rttmin, rttavg, rttmax, rttstddev, ipg, ewma) = (float(rttmin), float(rttavg), float(rttmax), float(rttstddev), float(ipg), float(ewma))
			details = True
	if not summary or not details or errors:
		return
	import math
	loss = 1.0 - math.sqrt(1.0 - loss)
	avg = rttavg / 2.0
	stddev = rttstddev / 2.0
	if rttunit == "s":
		avg = avg * 1000.0
		stddev = stddev * 1000.0
	return (loss, avg, stddev)
Пример #12
0
 def log_track(self, filename):
     if filename:
         query = 'UPDATE music SET play_count=play_count+1,last_play=%f WHERE \
                  path = "%s" and filename = "%s"' % (
             time.time(), util.escape(os.path.dirname(filename)),
             util.escape(os.path.basename(filename)))
         self.runquery(query)
Пример #13
0
Файл: repy.py Проект: m3z/ToMaTo
def setProfile(host, vmid, cpus, ram, bandwidth, **other):
	assert getState(host, vmid) == generic.State.PREPARED
	res = {"diskused": 1000000, "lograte": 10000, "events": 10000, "random": 10000}
	res.update({"cpu": float(cpus), "memory": int(ram)*1000000, "netrecv": int(bandwidth), "netsend": int(bandwidth)})
	resConf = "\n".join(["resource %s %s" % (key, value) for (key, value) in res.iteritems()])
	host.execute("echo -e %s > %s" % (util.escape(resConf), util.escape(_profileFile(vmid))) )
	
Пример #14
0
def populate_hdf(hdf, env, db, req):
    sql_to_hdf(db, "SELECT name FROM enum WHERE type='priority' "
               "ORDER BY value", hdf, 'enums.priority')
    sql_to_hdf(db, "SELECT name FROM enum WHERE type='severity' "
               "ORDER BY value", hdf, 'enums.severity')

    htdocs_location = env.get_config('trac', 'htdocs_location')
    if htdocs_location[-1] != '/':
        htdocs_location += '/'
    hdf.setValue('htdocs_location', htdocs_location)
    hdf.setValue('project.name', env.get_config('project', 'name'))
    # Kludges for RSS, etc
    hdf.setValue('project.name.encoded',
                 util.escape(env.get_config('project', 'name')))
    hdf.setValue('project.descr', env.get_config('project', 'descr'))
    hdf.setValue(
        'project.footer',
        env.get_config(
            'project', 'footer', ' Visit the Trac open source project at<br />'
            '<a href="http://trac.edgewall.com/">http://trac.edgewall.com/</a>'
        ))
    hdf.setValue('project.url', env.get_config('project', 'url'))
    hdf.setValue('trac.href.wiki', env.href.wiki())
    hdf.setValue('trac.href.browser', env.href.browser('/'))
    hdf.setValue('trac.href.timeline', env.href.timeline())
    hdf.setValue('trac.href.roadmap', env.href.roadmap())
    hdf.setValue('trac.href.report', env.href.report())
    hdf.setValue('trac.href.query', env.href.query())
    hdf.setValue('trac.href.newticket', env.href.newticket())
    hdf.setValue('trac.href.search', env.href.search())
    hdf.setValue('trac.href.about', env.href.about())
    hdf.setValue('trac.href.about_config', env.href.about('config'))
    hdf.setValue('trac.href.login', env.href.login())
    hdf.setValue('trac.href.logout', env.href.logout())
    hdf.setValue('trac.href.settings', env.href.settings())
    hdf.setValue('trac.href.homepage', 'http://trac.edgewall.com/')
    hdf.setValue('trac.version', __version__)
    hdf.setValue('trac.time', time.strftime('%c', time.localtime()))
    hdf.setValue('trac.time.gmt',
                 time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime()))

    hdf.setValue('header_logo.link', env.get_config('header_logo', 'link'))
    hdf.setValue('header_logo.alt', env.get_config('header_logo', 'alt'))
    src = env.get_config('header_logo', 'src')
    src_abs = re.match(r'https?://', src) != None
    if not src[0] == '/' and not src_abs:
        src = htdocs_location + src
    hdf.setValue('header_logo.src', src)
    hdf.setValue('header_logo.src_abs', str(src_abs))
    hdf.setValue('header_logo.width', env.get_config('header_logo', 'width'))
    hdf.setValue('header_logo.height', env.get_config('header_logo', 'height'))
    hdf.setValue('trac.href.logout', env.href.logout())
    if req:
        hdf.setValue('cgi_location', req.cgi_location)
        hdf.setValue('trac.authname', util.escape(req.authname))

    templates_dir = env.get_config('trac', 'templates_dir')
    hdf.setValue('hdf.loadpaths.0', env.get_templates_dir())
    hdf.setValue('hdf.loadpaths.1', templates_dir)
Пример #15
0
def merge_command(folder, movie, subtitle):
    base = os.path.splitext(movie)[0]
    if not os.path.isdir(os.path.join(folder, "Merged")):
        os.mkdir(os.path.join(folder, "Merged"))
    util.run_command('mkvmerge -o "{}.mkv" "{}" "{}.{}"'.format(
        util.escape(os.path.join(folder, "Merged", base)),
        util.escape(os.path.join(folder, movie)),
        util.escape(os.path.join(folder, base)), subtitle))
Пример #16
0
def bridgeDisconnect(host, bridge, iface, deleteBridgeIfLast=False):
	assert bridgeExists(host, bridge), "Bridge does not exist: %s" % bridge
	if not iface in bridgeInterfaces(host, bridge):
		return
	host.execute("brctl delif %s %s" % (util.escape(bridge), util.escape(iface)))
	assert not iface in bridgeInterfaces(host, bridge), "Interface %s could not be removed from bridge %s" % (iface, bridge)
	if deleteBridgeIfLast and not bridgeInterfaces(host, bridge):
		bridgeRemove(host, bridge)
Пример #17
0
 def add_link(self, rel, href, title=None, type=None, className=None):
     if not self.links.has_key(rel):
         self.links[rel] = []
     link = { 'href': escape(href) }
     if title: link['title'] = escape(title)
     if type: link['type'] = type
     if className: link['class'] = className
     self.links[rel].append(link)
Пример #18
0
def packdir(host, archive, dir, args=""):
    assert existsDir(host, dir), "Directory does not exist"
    if archive.endswith(".gz") and not "-z" in args:
        args = args + " -z"
    if archive.endswith(".bz2") and not "-j" in args:
        args = args + " -j"
    res = host.execute("tar -cf %s -C %s %s ." % (util.escape(archive), util.escape(dir), args))
    assert existsFile(host, archive), "Failed to pack directory: %s" % res
    return res
Пример #19
0
def setIncomingRedirect(host, srcDev, dstDev):
	assert ifaceutil.interfaceExists(host, srcDev)
	assert ifaceutil.interfaceExists(host, dstDev)
	_tc_mod(host, "qdisc", "dev %s ingress" % util.escape(srcDev))
	""" 
	Protocol all would forward all traffic but that results
	in ARP traffic being multiplied and causing lots of traffic
	""" 
	_tc_mod(host, "filter", "dev %s parent ffff:" % util.escape(srcDev), \
	 "protocol all prio 49152 u32 match u32 0 0 flowid 1:1 action mirred egress redirect dev %s" % util.escape(dstDev))
Пример #20
0
Файл: tc.py Проект: m3z/ToMaTo
def setLinkEmulation(host, dev, bandwidth=None, keepBandwidth=False, **kwargs):
	assert ifaceutil.interfaceExists(host, dev)
	netem_ref = "dev %s root handle 1:0" % util.escape(dev)
	cmd = ""
	if not bandwidth is None:
		netem_ref = "dev %s parent 1:1 handle 10:" % util.escape(dev)
		if not keepBandwidth:
			cmd = _tc_cmd("qdisc", "replace", "dev %s root handle 1:" % util.escape(dev), _buildTbf(bandwidth))
			cmd += ";"
	cmd += _tc_cmd("qdisc", "replace", netem_ref, _buildNetem(bandwidth=bandwidth, **kwargs))
	host.execute(cmd)
Пример #21
0
def bridgeConnect(host, bridge, iface):
	if iface in bridgeInterfaces(host, bridge):
		return
	assert interfaceExists(host, iface), "Interface does not exist: %s" % iface
	if not bridgeExists(host, bridge):
		bridgeCreate(host, bridge)
	oldbridge = interfaceBridge(host, iface)
	if oldbridge:
		bridgeDisconnect(host, oldbridge, iface)
	host.execute("brctl addif %s %s" % (util.escape(bridge), util.escape(iface)))
	assert iface in bridgeInterfaces(host, bridge), "Interface %s could not be connected to bridge %s" % (iface, bridge)
Пример #22
0
 def _expand_module_link(self, text):
     sep = text.find(':')
     if sep == -1:
         return None, None
     module = text[:sep]
     args = text[sep+1:]
     if module in ['bug', 'ticket']:
         cursor = self.db.cursor ()
         cursor.execute('SELECT summary,status FROM ticket WHERE id=%s', args)
         row = cursor.fetchone ()
         if row:
             summary = util.escape(util.shorten_line(row[0]))
             if row[1] == 'new':
                 return self._href.ticket(args), '%s:%s*' % (module, args), 0, 'NEW: ' +  summary
             elif row[1] == 'closed':
                 return self._href.ticket(args), '<del>%s:%s</del>' % (module, args), 0, 'CLOSED: ' + summary
             else:
                 return self._href.ticket(args), '%s:%s' % (module, args), 0, summary
         else:
             return self._href.ticket(args), '%s:%s' % (module, args), 1, ''
     elif module == 'wiki':
         if not self.env._wiki_pages.has_key(args):
             return self._href.wiki(args), '%s:%s' % (module, args), 1, None
         else:
             return self._href.wiki(args), '%s:%s' % (module, args), 0, None
     elif module == 'report':
         return self._href.report(args), '%s:%s' % (module, args), 0, None
     elif module == 'changeset':
         cursor = self.db.cursor ()
         cursor.execute('SELECT message FROM revision WHERE rev=%s', args)
         row = cursor.fetchone ()
         if row:
             return self._href.changeset(args), '%s:%s' % (module,args), 0, util.escape(util.shorten_line(row[0]))
         else:
             return self._href.changeset(args), '%s:%s' % (module,args), 1, ''
     elif module == 'milestone':
         return self._href.milestone(args), '%s:%s' % (module, args), 0, None
     elif module == 'search':
         return self._href.search(args), '%s:%s' % (module, args), 0, None
     elif module in ['source', 'repos', 'browser']:
         rev = None
         match = re.search('([^#]+)#(.+)', args)
         if match:
             args = match.group(1)
             rev = match.group(2)
         if rev:
             return self._href.browser(args, rev), \
                    '%s:%s#%s' % (module, args, rev), 0, None
         else:
             return self._href.browser(args), '%s:%s' % (module, args), 0, None
     else:
         return None, None, 0, None
Пример #23
0
def disconnectInterfaces(host, if1, if2, id):
	#calculate table ids
	table1 = 1000 + id * 2
	table2 = 1000 + id * 2 + 1
	try:
		#remove rules
		host.execute ( "ip rule del iif %s" % util.escape(if1) )
		host.execute ( "ip rule del iif %s" % util.escape(if2) )
		#remove routes
		host.execute ( "ip route del table %s default" % util.escape(table1) )
		host.execute ( "ip route del table %s default" % util.escape(table2) )
	except exceptions.CommandError, exc:
		if exc.errorCode != 2: #Rule does not exist
			raise
Пример #24
0
 def _crawl_once(page, depth_):
     self.wait()
     echo("Trying", escape(page.title), "at", depth_, flush = True)
     if match_function(page):
         echo("Taking", escape(page.title))
         return page
     elif depth_ >= self.depth:
         return None
     else:
         state = LinkState(page, self.depth - depth_, self.depth)
         link = self.selector.select_link(state)
         if link is None:
             return None
         new_page = wikipedia.page(link)
         return _crawl_once(new_page, depth_ + 1)
Пример #25
0
def send_pretty_error(e, env, req=None):
    import util
    import Href
    import os.path
    import traceback
    import StringIO
    tb = StringIO.StringIO()
    traceback.print_exc(file=tb)
    if not req:
        req = CGIRequest()
        req.authname = ''
        req.init_request()
    try:
        if not env:
            env = open_environment()
        env.href = Href.Href(req.cgi_location)
        cnx = env.get_db_cnx()
        populate_hdf(req.hdf, env, cnx, req)

        if isinstance(e, util.TracError):
            req.hdf.setValue('title', e.title or 'Error')
            req.hdf.setValue('error.title', e.title or 'Error')
            req.hdf.setValue('error.type', 'TracError')
            req.hdf.setValue('error.message', e.message)
            if e.show_traceback:
                req.hdf.setValue('error.traceback', util.escape(tb.getvalue()))
        elif isinstance(e, perm.PermissionError):
            req.hdf.setValue('title', 'Permission Denied')
            req.hdf.setValue('error.type', 'permission')
            req.hdf.setValue('error.action', e.action)
            req.hdf.setValue('error.message', str(e))
        else:
            req.hdf.setValue('title', 'Oops')
            req.hdf.setValue('error.type', 'internal')
            req.hdf.setValue('error.message', util.escape(str(e)))
            req.hdf.setValue('error.traceback', util.escape(tb.getvalue()))
        req.display('error.cs', response=500)
    except Exception:
        req.send_response(500)
        req.send_header('Content-Type', 'text/plain')
        req.end_headers()
        req.write('Oops...\n\nTrac detected an internal error:\n\n')
        req.write(str(e))
        req.write('\n')
        req.write(tb.getvalue())
    if env and env.log != None:
        env.log.error(str(e))
        env.log.error(tb.getvalue())
Пример #26
0
Файл: tc.py Проект: m3z/ToMaTo
def clearLinkEmulation(host, dev):
	assert ifaceutil.interfaceExists(host, dev)
	try:
		host.execute(_tc_cmd("qdisc", "del", "root dev %s" % util.escape(dev)))
	except exceptions.CommandError, exc:
		if not "No such file or directory" in exc.errorMessage:
			raise
Пример #27
0
def wrap_file_into_smth(filename: str, escape: bool, format: str):
    re_nt = re.compile("\n[\t ]+", re.MULTILINE)
    re_lf = re.compile("\n\n(\n)+", re.MULTILINE)

    _lfre = re.compile('(\r\n|\n\r)', re.M)
    _slfre = re.compile('( |\t)+\n', re.M)

    def strip0(s):
        crlfr = s.replace("\r\n", "\n").replace("\r", "\n")
        crlfr = re_nt.sub("\n", crlfr)
        crlfr = re_lf.sub("\n\n", crlfr)
        return crlfr

    def strip(s):
        s = _slfre.sub('\n', s)
        return s

    with open(filename, 'r', encoding='utf-8') as fh:
        plaintext = fh.read()

    if escape:
        escaped = util.escape(strip(plaintext))
    else:
        escaped = strip(plaintext)
    wrapped = format % (escaped, )

    with open(filename, 'wb') as fh:
        fh.write(wrapped.encode('utf-8'))
Пример #28
0
 def to_text(self):
     text = super(Predicate, self).to_text()
     if self.neg:
         text = 'not//' + text
     if self.prt != '':
         text += '//' + escape(self.prt)
     return text
Пример #29
0
def insertComment(conn, comment_data):
    author = comment_data['author']
    url = comment_data['url']
    comment = util.escape(comment_data['comment'])
    email = comment_data['email']
    ip = comment_data['ip']
    name= comment_data['name']
    dt = time.time()

    logging.info('Inserting comment by '+author)

    cursor = conn.cursor()

    insert = """
        insert into blog_item_comments
        (item_name, comment, name, website, email, ip, date)
        values (?,?,?,?,?,?,?)
        """

    getItemName = """
        select item_name
        from blog_items
        where item_title = ?
        """

    cursor.execute(getItemName, [name])
    data = cursor.fetchall()[0]
    item_name = data[0]
    logging.debug("item_name " + item_name)

    insert_data = [item_name, comment, author, url, email, ip, dt]
    cursor.execute(insert, insert_data)
    conn.commit()
Пример #30
0
def _expr_test_function_def(test_name, expr_string, expr_kind,
                            type, expected_result):
    template = "    def test_%s(self):\n" + \
               "        self._check_expr('%s','%s','%s','%s')\n\n"
    return template % tuple(escape(t) for t in (test_name, expr_string,
                                                expr_kind, type,
                                                expected_result))
Пример #31
0
def compile_level(n):
    level = "Level" + (str(n) if n < 5 else "Omega")
    compiled_files = {}

    for fname in os.listdir(level):
        if not (fname.endswith(".py")):
            continue
        if fname == 'test.py' or fname == '__init__.py':
            continue
        compile_file(level, fname, compiled_files)

    try:
        os.mkdir(level + "/compiled")
    except Exception:
        pass

    for fname in compiled_files.keys():
        with open(level + "/compiled/" + fname, "w") as fp:
            fp.write("# " + fname + "\n")
            fp.write(compiled_files[fname])
        with open(level + "/standalone/" + fname, "w") as fp:
            fp.write("# " + fname + "\n")
            fp.write("# This version of the file is completely standalone.\n")
            fp.write(
                "# You can actually run it in python, completely on its own,\n"
            )
            fp.write(
                "# and you can run its outputs, and their outputs, etc.\n")
            fp.write("PREAMBLE = \"\"\"\n")
            fp.write(escape(STANDALONE_PREAMBLE).strip() + "\n")
            fp.write("\"\"\"\n")
            fp.write("exec(PREAMBLE)\n\n")
            fp.write(compiled_files[fname])
Пример #32
0
def bridgeCreate(host, bridge):
	try:
		host.execute("brctl addbr %s" % util.escape(bridge))
	except exceptions.CommandError:
		if not bridgeExists(host, bridge):
			raise
	assert bridgeExists(host, bridge), "Bridge cannot be created: %s" % bridge
Пример #33
0
 def get_results(self, sql):
     cursor = self.db.cursor()
     cursor.execute(sql)
     results = []
     while 1:
         row = cursor.fetchone()
         if not row:
             break
         id = int(row['id'])
         results.append({
             'id':
             id,
             'href':
             self.env.href.ticket(id),
             'summary':
             util.escape(row['summary'] or '(no summary)'),
             'status':
             row['status'] or '',
             'component':
             row['component'] or '',
             'owner':
             row['owner'] or '',
             'priority':
             row['priority'] or ''
         })
     cursor.close()
     return results
Пример #34
0
def startDhcp(host, iface):
	for cmd in ["/sbin/dhclient", "/sbin/dhcpcd"]:
		try:
			return host.execute("[ -e %s ] && %s %s" % (cmd, cmd, util.escape(iface)))
		except exceptions.CommandError, err:
			if err.errorCode != 8:
				raise			
Пример #35
0
def fileTransfer(src_host,
                 src_path,
                 dst_host,
                 dst_path,
                 direct=False,
                 compressed=False):
    if compressed:
        src = src_host.getHostServer().randomFilename()
        dst = dst_host.getHostServer().randomFilename()
        compress(src_host, src_path, src)
    else:
        if direct:
            src = src_path
            dst = dst_path
            mode = src_host.execute(
                "stat -c %%a %s" % util.escape(src)).strip()
        else:
            dst = dst_host.getHostServer().randomFilename()
            src = src_host.getHostServer().randomFilename()
            copy(src_host, src_path, src)
    chmod(src_host, src, 644)
    url = src_host.getHostServer().downloadGrant(src, "file")
    res = fetch(dst_host, url, dst)
    assert existsFile(dst_host, dst), "Failure to transfer file: %s" % res
    if compressed:
        uncompress(dst_host, dst, dst_path)
        delete(dst_host, dst)
        delete(src_host, src)
    else:
        if not direct:
            copy(dst_host, dst, dst_path)
            delete(dst_host, dst)
            delete(src_host, src)
        else:
            chmod(src_host, src_path, mode)
Пример #36
0
def compile_file(level, fname, compiled_files):
    if fname in compiled_files:
        return

    start_token, end_token = "<INSERT_FILE ", ">"

    with open(level + "/" + fname) as fp:
        raw = fp.read()

    raw = strip_comments(raw)

    while True:
        try:
            get_file_index = raw.index(start_token)
        except Exception:
            compiled_files[fname] = raw
            return

        end = raw[get_file_index + len(start_token):].index(end_token)
        sub_file = raw[get_file_index + len(start_token):][:end]
        compile_file(level, sub_file, compiled_files)

        sub_contents = compiled_files[sub_file]

        left = raw[:get_file_index]
        right = raw[get_file_index + end + len(start_token) + len(end_token):]
        raw = left + '"""\n' + escape(sub_contents) + '\n"""' + right
Пример #37
0
def checkImage(host, path):
	assert host
	assert fileutil.existsFile(host, path)
	try:
		res = host.execute("tar -tzvf %s ./sbin/init" % util.escape(path))
		fault.check("0/0" in res, "Image contents not owned by root")
	except exceptions.CommandError, err:
		return err.errorMessage
Пример #38
0
def avaliable_admin(admin_id, password):
    admin = None
    con, cur = sqlrelay_cursor()
    cur.execute("""
SELECT 
   COUNT(0) 
FROM
   ms_admin 
WHERE
   admin_id = '%s' AND PASSWORD = PASSWORD('%s')
""" % (escape(from_utf8(admin_id)), escape(from_utf8(password))))

    if int(cur.fetchone()[0]) == 1:
        admin = admin_id

    sqlrelay_close(cur, con)
    return admin
Пример #39
0
 def _do_append_issue(self, issue, local):
     if local:
         message = issue.message
         filename = escape(issue.file.basename)
     else:
         message = "<span color='%s'>%s</span>" % (self._preferences.get("light-foreground-color"), issue.message)
         filename = "<span color='%s'>%s</span>" % (self._preferences.get("light-foreground-color"), issue.file.basename)
     self._store.append([self._icons[issue.severity], message, filename, issue])
Пример #40
0
    def html_processor(hdf, text, env):
        if Formatter._htmlproc_disallow_rule.search(text):
            err = """\
<div class="system-message">
 <strong>Error: HTML block contains disallowed tags.</strong>
 <pre>%s</pre>
</div>\n""" % util.escape(text)
            env.log.error(err)
            return err
        if Formatter._htmlproc_disallow_attribute.search(text):
            err = """\
<div class="system-message">
 <strong>Error: HTML block contains disallowed attributes.</strong>
 <pre>%s</pre>
</div>\n""" % util.escape(text)
            env.log.error(err)
            return err
        return text
Пример #41
0
    def att_string(self, args=None):
        args = args if args else self.__args__

        atts = []
        for k, v in args.iteritems():
            v = v if isinstance(v, list) else [v]
            atts.append(u"%s='%s'" % (k, u" ".join([util.escape(val) for val in v])))

        return u" ".join(atts)
Пример #42
0
 def crawl_times(self, base, match_function):
     """
     Performs self.crawl_once until a single match is found or self.max_tries attempts have
     been made.
     """
     if type(base) is str:
         echo("Basis:", escape(base))
         base = self.safely_call(lambda: wikipedia.page(base))
         if not base:
             return None
     else:
         echo("Basis:", escape(base.title))
     for i in range(0, self.max_tries):
         res = self.crawl_once(base, match_function)
         if res:
             return res
         else:
             echo("  Rejected")
Пример #43
0
 def _changesethref_formatter(self, match, fullmatch):
     number = int(match[1:-1])
     cursor = self.db.cursor ()
     cursor.execute('SELECT message FROM revision WHERE rev=%d', number)
     row = cursor.fetchone ()
     if not row:
         return '[<a class="missing" href="%s">%d</a>]' % (self._href.changeset(number), number)
     else:
         return '[<a title="%s" href="%s">%d</a>]' % ( util.escape(util.shorten_line(row[0])),self._href.changeset(number), number)
Пример #44
0
 def _crawl_once(page, depth_):
     self.wait()
     echo(" Trying:",
          escape(page.title),
          "(" + str(depth_) + ")",
          flush=True)
     if match_function(page):
         echo("  Accepted:", escape(page.title))
         return page
     elif depth_ >= self.depth:
         return None
     else:
         state = LinkState(page, self.depth - depth_, self.depth)
         link = self.selector.select_link(state)
         if link is None:
             return None
         new_page = wikipedia.page(link)
         return _crawl_once(new_page, depth_ + 1)
Пример #45
0
    def debug_str(self, i: StackIndex) -> str:
        s = self.str()

        dat2 = ['{{"text":" >>> {: <5}"}}'.format(escape(str(s[0])))]
        for d in s[1:]:
            if type(d) is TOS:
                dat2.append('{{"text":"[{}]"}}'.format(i.index))
            else:
                dat2.append('{{"text":"{}"}}'.format(escape(str(d))))

        tellraw = 'execute if entity @a[scores={{__DEBUG__=1..}}] run tellraw @a ["",{}]'
        yield tellraw.format(',{"text":"    "},'.join(dat2))

        if self.print_tag:
            yield tellraw.format(",".join([
                '{"text":" >>> STACK: "},{"nbt":"ArmorItems[0].tag","entity":"@s"}',
                '{"text":"TAGS: "},{"nbt":"Tags","entity":"@s"}'
            ]))
Пример #46
0
def make_query(filename, dirtitle):
    logger.log( 9, 'make_query(filename=%r, dirtitle=%r)', filename, dirtitle)
    if not os.path.exists(filename):
        print "File %s does not exist" % (filename)
        return None

    a = mediainfo.get(filename)
    t = tracknum(a['trackno'])
    ext = os.path.splitext(filename)[1]

    values = {}
    values['id'] = 'null'
    values['dirtitle'] = util.escape(dirtitle)
    values['path'] = util.escape(os.path.dirname(filename))
    values['filename'] = util.escape(os.path.basename(filename))
    values['type'] = ext
    values['artist'] = util.escape(a['artist'])
    values['title'] = util.escape(a['title'])
    values['album'] = util.escape(a['album'])
    values['genre'] = util.escape(a['genre'])
    values['year'] = inti(a['userdate'])
    values['track'] = int(t[0])
    values['track_total'] = int(t[1])
    values['bpm'] = a['bitrate'] and int(a['bitrate']) or -1
    values['last_play'] = float(0)
    values['play_count'] = int(0)
    values['start_time'] = inti(a['0'])
    values['end_time'] = a['length'] and inti(a['length']) or -1
    values['rating'] = int(0)
    values['eq'] = 'null'

    # if there is an error in one of the values it will show in this block
    "%(id)s" % values
    "'%(dirtitle)s'" % values
    "'%(path)s'" % values
    "'%(filename)s'" % values
    "'%(type)s'" % values
    "'%(artist)s'" % values
    "'%(title)s'" % values
    "'%(album)s'" % values
    "'%(genre)s'" % values
    "'%(year)s'" % values
    "%(track)i" % values
    "%(track_total)i" % values
    "%(bpm)i" % values
    "%(last_play)f" % values
    "%(play_count)i" % values
    "%(start_time)i" % values
    "%(end_time)i" % values
    "%(rating)i" % values
    "'%(eq)s'" % (values)

    # Assign the values
    VALUES = """%(id)s, '%(dirtitle)s', '%(path)s', '%(filename)s', '%(type)s', '%(artist)s', '%(title)s',
        '%(album)s', '%(genre)s', '%(year)s', %(track)i, %(track_total)i, %(bpm)i, %(last_play)f,
        %(play_count)i, %(start_time)i, %(end_time)i, %(rating)i, '%(eq)s'""" % (values)

    SQL = 'INSERT OR IGNORE INTO music VALUES (%s)' % VALUES
    return SQL
Пример #47
0
    def format(self, text, out):
        self.out = out
        self._open_tags = []
        self._list_stack = []

        self.in_code_block = 0
        self.in_table = 0
        self.in_table_row = 0
        self.in_table_cell = 0
        self.indent_level = 0
        self.paragraph_open = 0

        rules = self._compiled_rules

        for line in text.splitlines():
            # Handle code block
            if self.in_code_block or line.strip() == '{{{':
                self.handle_code_block(line)
                continue
            # Handle Horizontal ruler
            elif line[0:4] == '----':
                self.close_paragraph()
                self.close_indentation()
                self.close_list()
                self.close_table()
                self.out.write('<hr />' + os.linesep)
                continue
            # Handle new paragraph
            elif line == '':
                self.close_paragraph()
                self.close_indentation()
                self.close_list()
                continue

            line = util.escape(line)
            self.in_list_item = 0
            # Throw a bunch of regexps on the problem
            result = re.sub(rules, self.replace, line)
            # Close all open 'one line'-tags
            result += self.close_tag(None)

            if not self.in_list_item:
                self.close_list()

            if self.in_table and line[0:2] != '||':
                self.close_table()

            if len(result) and not self.in_list_item and not self.in_table:
                self.open_paragraph()
            out.write(result + os.linesep)
            self.close_table_row()

        self.close_table()
        self.close_paragraph()
        self.close_indentation()
        self.close_list()
Пример #48
0
 def display_rss(self):
     item = self.req.hdf.getObj('report.items')
     if item:
         item = item.child()
         while item:
             nodename = 'report.items.%s.summary' % item.name()
             summary = self.req.hdf.getValue(nodename, '')
             self.req.hdf.setValue(nodename, util.escape(summary))
             item = item.next()
     self.req.display(self.template_rss_name, 'text/xml')
Пример #49
0
def start(host, vmid, ifaces, args=[]):
	assert getState(host, vmid) == generic.State.PREPARED, "VM already running"
	ilist = ["-i repy%d.%s,alias=%s" % (vmid, util.identifier(i), util.identifier(i)) for i in ifaces]
	if isinstance(args, basestring):
		args = [args]
	alist = [util.escape(a) for a in args] 
	host.execute("tomato-repy -p %s -v %s %s > %s 2>&1 & echo $! > %s" % (_imagePath(vmid), " ".join(ilist), " ".join(alist), _logFile(vmid), _pidFile(vmid) ))
	assert getState(host, vmid) == generic.State.STARTED, "Repy device failed to start"
	for i in ifaces:
		waitForInterface(host, vmid, i)
Пример #50
0
    def command_line(self, command, warn_fail):
        pre = "execute store success score pass __asm__ run " if warn_fail else ""

        ls = [pre + command]
        if pre:
            fail = 'tellraw @a [{{"text": " !!!!!!!! FAIL: {}"}}]'.format(
                escape(command))
            ls.append(
                " " * 200 +
                'execute if score pass __asm__ matches 0 run {}'.format(fail))
        return ls
Пример #51
0
    def post(self):
        name = util.escape(self.request.get("user_first") + " " + self.request.get("user_last"))
        password = util.escape(self.request.get("user_pass"))
        email = util.escape(self.request.get("user_email"))


        user = databases.User.register(name, password, email)

        user.put()

        data = db.Blob(urlfetch.Fetch("http://i.imgur.com/efHNR.gif").content)
        filetype = 'gif'
        name = 'blank_profile.gif'

        image = databases.userImage(name = name, data = data, filetype = filetype, user = user)
        image.put()

        self.set_cookie("user", str(user.key().id()))

        self.redirect('/')
Пример #52
0
def bridgeRemove(host, bridge, disconnectAll=False, setIfdown=True):
	if not bridgeExists(host, bridge):
		return
	if disconnectAll:
		for iface in bridgeInterfaces(host, bridge):
			bridgeDisconnect(host, bridge, iface)
	assert not bridgeInterfaces(host, bridge)
	if setIfdown:
		ifdown(host, bridge)
	host.execute("brctl delbr %s" % util.escape(bridge))
	assert not bridgeExists(host, bridge), "Bridge cannot be removed: %s" % bridge
Пример #53
0
    def send_room_message(self, message, formatted=False):
        """formatted means that it is an html message"""
        if message is None:
            return

        try:
            if formatted:
                html_message = "<span>" + message + "</span>"
                self.send_message(mto=self.room, mbody=message, mhtml=html_message, mtype="groupchat")
            else:
                # send every line seperately
                lines = message.split("\n")
                for line in lines:
                    self.send_message(mto=self.room, mbody=line, mtype="groupchat")
        except Exception as e:
            print("ERROR sending message: " + message)
            message = "Error. Tried to send message: <br/>\n" + escape(message)
            message += "<br/>\n<br/>\nException was: <br/>\n" + escape(str(e))
            html_message = "<span>" + message + "</span>"
            self.send_message(mto=self.room, mbody=message, mhtml=html_message, mtype="groupchat")
Пример #54
0
    def generate_diff(self, pagename, version):
        import Diff
        Diff.get_options(self.env, self.req, self.args)
        if self.args.has_key('update'):
            self.req.redirect(self.env.href.wiki(pagename, version, 1))

        cursor = self.db.cursor()
        cursor.execute(
            'SELECT text,author,comment,time FROM wiki '
            'WHERE name=%s AND (version=%s or version=%s)'
            'ORDER BY version ASC', pagename, version - 1, version)
        res = cursor.fetchall()
        if not res:
            raise TracError(
                'Version %d of page "%s" not found.' % (version, pagename),
                'Page Not Found')

        if len(res) == 1:
            old = ''
        else:
            old = res[0][0].splitlines()
        new = res[-1][0].splitlines()
        author = res[-1][1] or ''
        comment = res[-1][2] or ''
        time_str = time.strftime('%c', time.localtime(int(res[-1][3])))
        self.req.hdf.setValue('wiki.version', str(version))
        self.req.hdf.setValue('wiki.diff.time', time_str)
        self.req.hdf.setValue('wiki.diff.author', escape(author))
        self.req.hdf.setValue('wiki.diff.comment', escape(comment))

        builder = Diff.HDFBuilder(self.req.hdf, 'wiki.diff')
        builder.writeline('header %s %d | %s %d redaeh' %
                          (pagename, version - 1, pagename, version))
        builder.writeline('@@ -1,%d +1,%d @@' % (len(old), len(new)))
        try:
            for line in difflib.Differ().compare(old, new):
                if line != '  ':
                    builder.writeline(line)
        except AttributeError:
            raise TracError('Python >= 2.2 is required for diff support.')
        builder.close()
Пример #55
0
    def format(self, text, out):
        if not text:
            return
        self.out = out
        self._open_tags = []

        rules = self._compiled_rules

        result = re.sub(rules, self.replace, util.escape(text.strip()))
        # Close all open 'one line'-tags
        result += self.close_tag(None)
        out.write(result)
Пример #56
0
 def post(self):
     if(self.request.cookies.get('user') and self.check_secure_val(self.request.cookies.get('user'))):
         user = databases.User.get_by_id(int(self.request.cookies.get('user').split('|')[0]))
         content = util.htmlify(util.escape(self.request.POST['content']))
         logging.debug(content)
         postid = self.request.get('id')
         post = databases.Post.get_by_id(int(postid))
         if post:
             comment = databases.Comment.addComment(post, user, content)
             self.render('comments.html', comment = comment, post = post, user = user)       
         else:
             self.write('ERROR')