def _generate_data_many_refs(self, n, timestamp=1400000000): root_commit = """\ blob mark :1 data 0 reset refs/heads/master commit refs/heads/master mark :2 author Joe <*****@*****.**> %(timestamp)d +0000 committer Joe <*****@*****.**> %(timestamp)d +0000 data 12 root commit M 100644 :1 .gitignore """ ref_commit = """ commit refs/heads/ref-%(ref)08d mark :%(mark)d author Joe <*****@*****.**> %(timestamp)d +0000 committer Joe <*****@*****.**> %(timestamp)d +0000 data 13 ref-%(ref)08d from :2 """ data = io.BytesIO() data.write(to_utf8(root_commit % {'timestamp': timestamp})) for idx in xrange(n): data.write(to_utf8(ref_commit % {'timestamp': timestamp + idx, 'mark': idx + 3, 'ref': idx})) return data.getvalue()
def _tracadmin(self, *args): """Internal utility method for calling trac-admin""" proc = Popen([sys.executable, os.path.join(self.trac_src, 'trac', 'admin', 'console.py'), self.tracdir], stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=close_fds, cwd=self.command_cwd) if args: if any('\n' in arg for arg in args): raise Exception( "trac-admin in interactive mode doesn't support " "arguments with newline characters: %r" % (args,)) # Don't quote first token which is sub-command name input = ' '.join(('"%s"' % to_utf8(arg) if idx else arg) for idx, arg in enumerate(args)) else: input = None out = proc.communicate(input=input)[0] if proc.returncode: print(out) logfile.write(out) raise Exception("Failed while running trac-admin with arguments %r.\n" "Exitcode: %s \n%s" % (args, proc.returncode, out)) else: # trac-admin is started in interactive mode, so we strip away # everything up to the to the interactive prompt return re.split(r'\r?\nTrac \[[^]]+\]> ', out, 2)[1]
def get_node(path, rev): if 'missing' in path: raise NoSuchNode(path, rev) basename = posixpath.basename(path) if 'file' in basename: kind = Node.FILE entries = () else: kind = Node.DIRECTORY if 'dir' in basename: entries = ['file.txt'] else: entries = ['dir1', 'dir2'] entries = [posixpath.join(path, entry) for entry in entries] content = b'Contents for %s' % to_utf8(path) node = Mock(Node, repos, path, rev, kind, created_path=path, created_rev=rev, get_entries=lambda: iter( get_node(entry, rev) for entry in entries), get_properties=lambda: {}, get_content=lambda: io.BytesIO(content), get_content_length=lambda: len(content), get_content_type=lambda: 'application/octet-stream', get_last_modified=lambda: t) return node
def _tracadmin(self, *args): """Internal utility method for calling trac-admin""" proc = Popen([ sys.executable, os.path.join(self.trac_src, 'trac', 'admin', 'console.py'), self.tracdir ], stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=close_fds, cwd=self.command_cwd) if args: if any('\n' in arg for arg in args): raise Exception( "trac-admin in interactive mode doesn't support " "arguments with newline characters: %r" % (args, )) # Don't quote first token which is sub-command name input = ' '.join(('"%s"' % to_utf8(arg) if idx else arg) for idx, arg in enumerate(args)) else: input = None out = proc.communicate(input=input)[0] if proc.returncode: print(out) self.logfile.write(out) raise Exception( "Failed while running trac-admin with arguments %r.\n" "Exitcode: %s \n%s" % (args, proc.returncode, out)) else: # trac-admin is started in interactive mode, so we strip away # everything up to the to the interactive prompt return re.split(r'\r?\nTrac \[[^]]+\]> ', out, 2)[1]
def adduser(self, user): """Add a user to the environment. The password will be set to the same as username.""" user = to_utf8(user) if call([sys.executable, os.path.join(self.trac_src, 'contrib', 'htpasswd.py'), '-b', self.htpasswd, user, user], close_fds=close_fds, cwd=self.command_cwd): raise Exception('Unable to setup password for user "%s"' % user)
def deluser(self, user): """Delete a user from the environment.""" user = to_utf8(user) self._tracadmin('session', 'delete', user) if call([sys.executable, os.path.join(self.trac_src, 'contrib', 'htpasswd.py'), '-D', self.htpasswd, user], close_fds=close_fds, cwd=self.command_cwd): raise Exception('Unable to remove password for user "%s"' % user)
def _get_xsendfile_header(self, req): header = self.xsendfile_header.strip() if self._xsendfile_header_re.match(header): return to_utf8(header) else: if not self._warn_xsendfile_header: self._warn_xsendfile_header = True self.log.warn("[trac] xsendfile_header is invalid: '%s'", header) return None
def _xsendfile_header(self): header = self.xsendfile_header.strip() if Request.is_valid_header(header): return to_utf8(header) else: if not self._warn_xsendfile_header: self._warn_xsendfile_header = True self.log.warning("[trac] xsendfile_header is invalid: '%s'", header) return None
def login(self, username): """Login as the given user""" username = to_utf8(username) tc.add_auth("", self.url, username, username) self.go_to_front() tc.find("Login") tc.follow("Login") # We've provided authentication info earlier, so this should # redirect back to the base url. tc.find("logged in as %s" % username) tc.find("Logout") tc.url(self.url) tc.notfind(internal_error)
def login(self, username): """Login as the given user""" username = to_utf8(username) tc.add_auth("", self.url, username, username) self.go_to_front() tc.find("Login") tc.follow(r"\bLogin\b") # We've provided authentication info earlier, so this should # redirect back to the base url. tc.find("logged in as %s" % username) tc.find("Logout") tc.url(self.url) tc.notfind(internal_error)
def save(self): """Write the configuration options to the primary file.""" # Only save options that differ from the inherited values sections = [] for section in self.sections(): options = [] for option in self[section]: default = None for parent in self.parents: if parent.has_option(section, option, defaults=False): default = parent.get(section, option) break if self.parser.has_option(section, option): current = self.parser.get(section, option) if current != default: options.append((option, current)) if options: sections.append((section, sorted(options))) content = ['# -*- coding: utf-8 -*-\n\n'] for section, options in sections: content.append('[%s]\n' % to_utf8(section)) for key, val in options: key_str = to_utf8(key) val_str = to_utf8(val) val_str = val_str.replace(CRLF, '\n') \ .replace('\n', '\n ') content.append('%s = %s\n' % (key_str, val_str)) content.append('\n') try: self._write(content) self._old_sections = deepcopy(self.parser._sections) except Exception: # Revert all changes to avoid inconsistencies self.parser._sections = deepcopy(self._old_sections) raise
def _render_lines(self, iterable, req): result = [] for line in iterable: rdict = {} d = getattr(self._line_re.search(line), 'groupdict', dummy)() for mode in ('channel', 'action', 'server'): prefix = mode[0] text = d.get('%s_text' % prefix) if not text is None: nick = d['%s_nickname' % prefix] break else: continue rdict['date'] = d['date'] rdict['time'] = d['time'] rdict['mode'] = mode rdict['text'] = to_utf8(text) rdict['nickname'] = nick rdict['class'] = 'nick-%d' % (sum(ord(c) for c in nick) % 8) result.append(rdict) return result
def change_sid(self, new_sid): assert self.req.authname == 'anonymous', \ 'Cannot change ID of authenticated session' assert new_sid, 'Session ID cannot be empty' if new_sid == self.sid: return if not to_utf8(new_sid).isalnum(): raise TracError(_("Session ID must be alphanumeric."), _("Error renaming session")) with self.env.db_transaction as db: if db("SELECT sid FROM session WHERE sid=%s", (new_sid,)): raise TracError(_("Session '%(id)s' already exists. " "Please choose a different session ID.", id=new_sid), _("Error renaming session")) self.env.log.debug("Changing session ID %s to %s", self.sid, new_sid) db("UPDATE session SET sid=%s WHERE sid=%s AND authenticated=0", (new_sid, self.sid)) db("""UPDATE session_attribute SET sid=%s WHERE sid=%s and authenticated=0 """, (new_sid, self.sid)) self.sid = new_sid self.bake_cookie()
def set(self, section, option, value=None): section_str = to_utf8(section) option_str = to_utf8(option) value_str = to_utf8(value) if value is not None else '' ConfigParser.set(self, section_str, option_str, value_str)
def test_utf8(self): self.assertEqual('à', to_utf8(u'à')) self.assertEqual('ç', to_utf8(u'ç'))
def test_exception_with_unicode_message(self): self.assertEqual('thė mèssägē', to_utf8(Exception(u'thė mèssägē')))
def remove_option(self, section, option): section_str = to_utf8(section) option_str = to_utf8(option) ConfigParser.remove_option(self, section_str, option_str)
def has_option(self, section, option): section_str = to_utf8(section) option_str = to_utf8(option) return ConfigParser.has_option(self, section_str, option_str)
def get(self, section, option, raw=False, vars=None): section_str = to_utf8(section) option_str = to_utf8(option) return to_unicode(ConfigParser.get(self, section_str, option_str, raw, vars))
def has_section(self, section): section_str = to_utf8(section) return ConfigParser.has_section(self, section_str)
def add_section(self, section): section_str = to_utf8(section) ConfigParser.add_section(self, section_str)
def to_utf8(self, content, mimetype=None): """Convert an encoded `content` to utf-8. :deprecated: since 0.10, you should use `unicode` strings only. """ return to_utf8(content, self.get_charset(content, mimetype))
def options(self, section): section_str = to_utf8(section) return map(to_unicode, ConfigParser.options(self, section_str))
def md5crypt(password, salt, magic='$1$'): """Based on FreeBSD src/lib/libcrypt/crypt.c 1.2 :param password: the plain text password to crypt :param salt: the raw salt :param magic: our magic string """ magic_arg = magic salt_arg = salt password = to_utf8(password) magic = to_utf8(magic) salt = to_utf8(salt) # /* The password first, since that is what is most unknown */ # /* Then our magic string */ # /* Then the raw salt */ m = hashlib.md5(password + magic + salt) # /* Then just as many characters of the MD5(pw,salt,pw) */ mixin = hashlib.md5(password + salt + password).digest() m.update(bytes(mixin[i % 16] for i in range(len(password)))) # /* Then something really weird... */ # Also really broken, as far as I can tell. -m def iter_password_or_zero(): i = len(password) while i: if i & 1: yield 0 else: yield password[0] i >>= 1 m.update(bytes(iter_password_or_zero())) final = m.digest() # /* and now, just to make sure things don't run too fast */ for i in range(1000): m2 = hashlib.md5() if i & 1: m2.update(password) else: m2.update(final) if i % 3: m2.update(salt) if i % 7: m2.update(password) if i & 1: m2.update(final) else: m2.update(password) final = m2.digest() # This is the bit that uses to64() in the original code. def iter_ito64(value): itoa64 = b'./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' \ b'abcdefghijklmnopqrstuvwxyz' for a, b, c in ((0, 6, 12), (1, 7, 13), (2, 8, 14), (3, 9, 15), (4, 10, 5)): v = final[a] << 16 | final[b] << 8 | final[c] for i in range(4): yield itoa64[v & 0x3f] v >>= 6 v = final[11] for i in range(2): yield itoa64[v & 0x3f] v >>= 6 return magic_arg + salt_arg + '$' + str(bytes(iter_ito64(final)), 'ascii')
def test_int(self): self.assertEqual('-1', to_utf8(-1)) self.assertEqual('0', to_utf8(0)) self.assertEqual('1', to_utf8(1))
def remove_section(self, section): section_str = to_utf8(section) ConfigParser.remove_section(self, section_str)
def test_boolean(self): self.assertEqual('True', to_utf8(True)) self.assertEqual('False', to_utf8(False))
def __init__(self, section, name, choices, doc=''): Option.__init__(self, section, name, to_utf8(choices[0]), doc)
def items(self, section, raw=False, vars=None): section_str = to_utf8(section) return [(to_unicode(k), to_unicode(v)) for k, v in ConfigParser.items(self, section_str, raw, vars)]
def to_utf8(self, content, mimetype=None): """Convert an encoded `content` to utf-8. ''Deprecated in 0.10. You should use `unicode` strings only.'' """ return to_utf8(content, self.get_charset(content, mimetype))
def test_unicode(self): self.assertEqual('à', to_utf8('à')) self.assertEqual('ç', to_utf8('ç'))
def parse_pkginfo(dist, name): return email.message_from_string(to_utf8(dist.get_metadata(name)))