import sys import os import shutil import subprocess import codecs """ Doesn't handle non-stored svn passwords properly!!""" def svn_command(command_array): """ Invoke svn command """ popen = subprocess.Popen(["svn"] + command_array, stdout=None, stderr=None, stdin=None) popen.communicate() rcode = popen.returncode if rcode != 0: raise Exception("svn invocation errored with code %d" % rcode) def remove_dir(directory_path): """ Remove a directory and all of its contents """ shutil.rmtree(directory_path) #print "Pretend cleanup '%s'..." % directory_path def get_line_end(line): """ Get the line end characters for the line """ if len(line) >= 2 and line[len(line)-2] == '\r': return "\r\n" return "\n" def match_change_txt_dev_line(line): """ Find a line looking like this: ===== nnn-dev ===== """ index = line.find("=== ") if index == -1: return False end_index = line.find("-dev ===", index + 4) return end_index != -1 def make_change_txt_release_line(build_version, line_end): return "======================= Release %s =====================%s" % (build_version, line_end) def set_change_file_release_row(change_file_path, release_version): """ Convert the -dev row in a change file to a release row """ converted = False temp_file = "%s.tmp" % change_file_path fd = codecs.open(change_file_path, "r", "utf-8") try: out_fd = codecs.open(temp_file, "w", "utf-8") try: for line in fd: if converted: out_fd.write(line) else: if match_change_txt_dev_line(line): out_fd.write(make_change_txt_release_line(release_version, get_line_end(line))) converted = True else: out_fd.write(line) finally: out_fd.close() finally: fd.close() # Remove old file and rename temp file os.unlink(change_file_path) os.rename(temp_file, change_file_path) def match_change_txt_add_line(line): """ Look for a line with === in it """ return line.find("===") >= 0 def make_change_txt_dev_line(dev_version, line_end): return "======================= %s-dev =====================%s" % (dev_version, line_end); def add_change_file_new_dev_row(change_file_path, dev_version): """ Add a new -dev row to the start of a change file """ added_row = False temp_file = "%s.tmp" % change_file_path fd = codecs.open(change_file_path, "r", "utf-8") try: out_fd = codecs.open(temp_file, "w", "utf-8") try: for line in fd: if added_row: out_fd.write(line) else: if match_change_txt_add_line(line): line_end = get_line_end(line) out_fd.write(make_change_txt_dev_line(dev_version, line_end)) out_fd.write(line_end) out_fd.write(line_end) out_fd.write(line) added_row = True else: out_fd.write(line) finally: out_fd.close() finally: fd.close() # Remove old file and rename temp file os.unlink(change_file_path) os.rename(temp_file, change_file_path) IN_BODY = 0 IN_PREAMBLE = 1 IN_COMMENT = 2 IN_TAG_NAME = 3 IN_TAG_BODY = 4 IN_END_TAG_NAME = 5 IN_BODY_SEEN_LEFTANGLE = 6 IN_BODY_SEEN_LEFTANGLE_BANG = 7 IN_BODY_SEEN_LEFTANGLE_BANG_DASH = 9 IN_COMMENT_SEEN_DASH = 11 IN_COMMENT_SEEN_DASH_DASH = 12 IN_PREAMBLE_SEEN_QUESTION = 13 IN_TAG_BODY_SEEN_SLASH = 14 IN_TAG_SEEN_SLASH = 15 class TagScanner: def __init__( self, action_object ): self.state = IN_BODY self.tag_name = None self.action_object = action_object self.body_accumulator = u'' def end( self ): if self.state != IN_BODY: raise Exception("At end, in unknown state %d" % self.state) self.action_object.write(self.action_object.map_body(self.body_accumulator)) def accept_character( self, char ): uchar = unicode(char) if self.state == IN_BODY: if uchar == u'<': self.state = IN_BODY_SEEN_LEFTANGLE else: self.body_accumulator += uchar elif self.state == IN_PREAMBLE: if uchar == u'?': self.state = IN_PREAMBLE_SEEN_QUESTION self.action_object.write(uchar) else: self.action_object.write(uchar) elif self.state == IN_COMMENT: if uchar == u'-': self.state = IN_COMMENT_SEEN_DASH self.action_object.write(uchar) else: self.action_object.write(uchar) elif self.state == IN_TAG_NAME: if uchar <= u' ': self.action_object.found_tag(self.tag_name) self.action_object.write(uchar) self.state = IN_TAG_BODY elif uchar == u'>': self.action_object.found_tag(self.tag_name) self.action_object.write(uchar) self.state = IN_BODY elif uchar == u'/': self.action_object.found_tag(self.tag_name) self.action_object.write(uchar) self.state = IN_TAG_SEEN_SLASH else: self.tag_name += uchar self.action_object.write(uchar) elif self.state == IN_TAG_SEEN_SLASH: if uchar == u'>': self.action_object.found_end_tag(self.tag_name) self.action_object.write(uchar) self.state = IN_BODY self.tag_name = None elif uchar == u'/': self.action_object.write(uchar) else: self.action_object.write(uchar) self.state = IN_TAG_BODY elif self.state == IN_TAG_BODY: if uchar == u'/': self.state = IN_TAG_BODY_SEEN_SLASH self.action_object.write(uchar) elif uchar == u'>': self.state = IN_BODY self.action_object.write(uchar) else: self.action_object.write(uchar) elif self.state == IN_TAG_BODY_SEEN_SLASH: if uchar == u'>': self.action_object.found_end_tag(self.tag_name) self.action_object.write(uchar) self.state = IN_BODY self.tag_name = None elif uchar == u'/': self.action_object.write(uchar) else: self.action_object.write(uchar) self.state = IN_TAG_BODY elif self.state == IN_END_TAG_NAME: if uchar == u'>': self.state = IN_BODY self.action_object.found_end_tag(self.tag_name) self.action_object.write(uchar) self.tag_name = None else: self.action_object.write(uchar) self.tag_name += uchar elif self.state == IN_BODY_SEEN_LEFTANGLE: if uchar == u'!': self.state = IN_BODY_SEEN_LEFTANGLE_BANG elif uchar == u'?': self.action_object.write(self.action_object.map_body(self.body_accumulator)) self.body_accumulator = u"" self.action_object.write(u'<') self.action_object.write(u'?') self.body_accumulator = u"" self.state = IN_PREAMBLE elif uchar == u'/': self.state = IN_END_TAG_NAME self.action_object.write(self.action_object.map_body(self.body_accumulator)) self.body_accumulator = u"" self.action_object.write(u'<') self.action_object.write(u'/') self.tag_name = u"" elif uchar == u'<': self.body_accumulator += u'<' else: self.action_object.write(self.action_object.map_body(self.body_accumulator)) self.body_accumulator = u"" self.action_object.write(u'<') self.action_object.write(uchar) self.tag_name = u"" + uchar self.state = IN_TAG_NAME elif self.state == IN_BODY_SEEN_LEFTANGLE_BANG: if uchar == u'-': self.state = IN_BODY_SEEN_LEFTANGLE_BANG_DASH else: self.body_accumulator += u'<' self.body_accumulator += u'!' self.body_accumulator += uchar elif self.state == IN_BODY_SEEN_LEFTANGLE_BANG_DASH: if uchar == u'-': self.state = IN_COMMENT self.action_object.write(self.action_object.map_body(self.body_accumulator)) self.body_accumulator = u"" self.action_object.write(u"