Hi,

Please check the below code.

URL = "http://11.12.13.27:8080/cruisecontrol"

from urllib2 import urlopen
from HTMLParser import HTMLParser

import re

# Fetching links using HTMLParser
def get_links(url):
    parser = MyHTMLParser()
    parser.feed(urlopen(url).read())
    parser.close()
    return parser.links

# Build url for Deploy page
def get_deploy_url():
    url = URL + "/buildresults/xxx_%s_nightly_build" % branch
    print url
    for link in get_links(url):
        if link["href"].startswith("Deploy"):
            return "%s/%s" % (URL, link["href"])

# Build url for Destination page
def get_destination_url():
    url = get_deploy_url()
    print url
    destination_re = re.compile(r" %s ") % destination
    for link in get_links(url):
        if destination_re.search(link["href"]):
            return "http://11.12.13.27:8080/cruisecontrol/" + link["href"]

# Parsing HTML pages 
class MyHTMLParser(HTMLParser):
    def __init__(self, *args, **kwd):
        HTMLParser.__init__(self, *args, **kwd)
        self.links = []

    def handle_starttag(self, tag, attrs):
        if tag == "a":
            attrs = dict(attrs)
            if "href" in attrs:
                self.links.append(dict(attrs))

    def handle_endtag(self, tag):
        pass

if __name__ == "__main__":
    # Read the branch name and the test destination to deploy on
    lines = [x.split(':') for x in open("branch_dest.txt")]
    print lines
    branch = "%s" % lines[0][1].strip()
    print branch
    destination = "%s" % lines[1][1].strip()
    print destination
    
    final_url = get_destination_url()
    if final_url is None:
        print "Could not find a destination to deploy"
    else:
        print final_url

The code is basically reading the branch name and the test environment (from branch_dest.txt file) to deploy that particular branches build on.

I am getting the below error

Traceback (most recent call last):
  File "C:\deploy_input.py", line 62, in <module>
    final_url = get_destination_url()
  File "C:\deploy_input.py", line 31, in get_destination_url
    if link["href"].startswith("%s") % destination:
TypeError: unsupported operand type(s) for %: 'bool' and 'str'

Regular exp isnt taking %s. I am from QA have managed to do this with help from google and this forum. Please help me with this error too.

Recommended Answers

All 2 Replies

wherever you write :

something("%s") % one_variable

write

something("%s" % one_variable)

Hey, it worked fine! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.