Пример #1
0
def start_coverage():
    global cov, packages, RES_DIRNAME, RES_FULLDIRNAME, COVERAGE_FNAME, BEST_DIRNAME, BEST_COVERAGE_FNAME, SUMMARY_FNAME, BEST_SUMMARY_FNAME, VERSION_STAMP_FNAME, BEST_VERSION_STAMP_FNAME
    
    packages =setuptools.find_packages('.')
    includes = [os.path.join(pkg.replace('.', os.sep), '*') for pkg in packages]
    cov = coverage.coverage(config_file='.coveragerc')
    cov.config.include = includes

    # We keep our notes about previous best code-coverage results in a
    # folder named ".coverage-results".
    RES_DIRNAME='.coverage-results'
    RES_FULLDIRNAME=os.path.realpath(os.path.abspath(os.path.expanduser(RES_DIRNAME)))
    fileutil.make_dirs(RES_FULLDIRNAME)
    COVERAGE_FNAME=os.path.join(os.path.abspath(os.getcwd()),
            cov.config.data_file)
    BEST_DIRNAME=os.path.join(RES_FULLDIRNAME, 'best')
    fileutil.make_dirs(BEST_DIRNAME)
    BEST_COVERAGE_FNAME=os.path.join(BEST_DIRNAME, cov.config.data_file)
    SUMMARY_FNAME=os.path.join(RES_FULLDIRNAME, 'summary.txt')
    BEST_SUMMARY_FNAME=os.path.join(BEST_DIRNAME, 'summary.txt')
    VERSION_STAMP_FNAME=os.path.join(RES_FULLDIRNAME, 'version-stamp.txt')
    BEST_VERSION_STAMP_FNAME=os.path.join(BEST_DIRNAME, 'version-stamp.txt')

    # poke the internals of coverage to work-around this issue:
    # http://bitbucket.org/ned/coveragepy/issue/71/atexit-handler-results-in-exceptions-from-half-torn-down
    cov.atexit_registered = True
    cov.start()
Пример #2
0
    def test_du(self):
        basedir = "util/FileUtil/test_du"
        fileutil.make_dirs(basedir)
        d = os.path.join(basedir, "space-consuming")
        self.mkdir(d, "a/b")
        self.touch(d, "a/b/1.txt", data="a"*10)
        self.touch(d, "a/b/2.txt", data="b"*11)
        self.mkdir(d, "a/c")
        self.touch(d, "a/c/1.txt", data="c"*12)
        self.touch(d, "a/c/2.txt", data="d"*13)

        used = fileutil.du(basedir)
        self.failUnlessEqual(10+11+12+13, used)
Пример #3
0
    def test_du(self):
        basedir = "util/FileUtil/test_du"
        fileutil.make_dirs(basedir)
        d = os.path.join(basedir, "space-consuming")
        self.mkdir(d, "a/b")
        self.touch(d, "a/b/1.txt", data="a" * 10)
        self.touch(d, "a/b/2.txt", data="b" * 11)
        self.mkdir(d, "a/c")
        self.touch(d, "a/c/1.txt", data="c" * 12)
        self.touch(d, "a/c/2.txt", data="d" * 13)

        used = fileutil.du(basedir)
        self.failUnlessEqual(10 + 11 + 12 + 13, used)
Пример #4
0
def init_paths():
    global RES_DIRNAME, RES_FULLDIRNAME, COVERAGE_FNAME, BEST_DIRNAME, BEST_COVERAGE_FNAME, SUMMARY_FNAME, BEST_SUMMARY_FNAME, VERSION_STAMP_FNAME, BEST_VERSION_STAMP_FNAME

    # We keep our notes about previous best code-coverage results in a
    # folder named ".coverage-results".
    RES_DIRNAME='.coverage-results'
    RES_FULLDIRNAME=os.path.realpath(os.path.abspath(os.path.expanduser(RES_DIRNAME)))
    fileutil.make_dirs(RES_FULLDIRNAME)
    COVERAGE_FNAME=os.path.join(os.path.abspath(os.getcwd()), '.coverage')
    BEST_DIRNAME=os.path.join(RES_FULLDIRNAME, 'best')
    fileutil.make_dirs(BEST_DIRNAME)
    BEST_COVERAGE_FNAME=os.path.join(BEST_DIRNAME, '.coverage')
    SUMMARY_FNAME=os.path.join(RES_FULLDIRNAME, 'summary.txt')
    BEST_SUMMARY_FNAME=os.path.join(BEST_DIRNAME, 'summary.txt')
    VERSION_STAMP_FNAME=os.path.join(RES_FULLDIRNAME, 'version-stamp.txt')
    BEST_VERSION_STAMP_FNAME=os.path.join(BEST_DIRNAME, 'version-stamp.txt')
Пример #5
0
    def test_basic_test(self):
        pkgname='fakepackage4'
        modname='fakemodule4'
        modcontents='\n\
def foofunc():\n\
    x=1\n\
    y=x\n\
'
        testcontents='\n\
from twisted.trial import unittest\n\
from %s import %s\n\
class T(unittest.TestCase):\n\
    def test_thing(self):\n\
        %s.foofunc()\n\
' % (pkgname, modname, modname)

        mockstdout = Mock()
        realstdout=sys.stdout
        sys.stdout = mockstdout
        mockstderr = Mock()
        realstderr=sys.stderr
        sys.stderr = mockstderr
        something = None
        try:
            fileutil.make_dirs(pkgname)
            fileutil.write_file(os.path.join(pkgname, '__init__.py'), '')
            fileutil.write_file(os.path.join(pkgname, modname+'.py'), modcontents)
            fileutil.make_dirs(os.path.join(pkgname, 'test'))
            fileutil.write_file(os.path.join(pkgname, 'test', '__init__.py'), '')
            fileutil.write_file(os.path.join(pkgname, 'test', 'test_'+modname+'.py'), testcontents)
            sys.path.append(os.getcwd())
            trialcoverage.init_paths()
            trialcoverage.start_coverage()

            config = trial.Options()
            config.parseOptions(['--reporter', 'bwverbose-coverage', '%s.test' % pkgname])
            trial._initialDebugSetup(config)
            trialRunner = trial._makeRunner(config)
            suite = trial._getSuite(config)
            something = trialRunner.run(suite)

        finally:
            sys.stdout = realstdout
            sys.stderr = realstderr
            if sys.modules.has_key(pkgname):
                del sys.modules[pkgname]
            fileutil.rm_dir(pkgname)
Пример #6
0
def measure(stride):
    fileutil.rm_dir("build")
    fileutil.rm_dir("instdir")
    fileutil.remove_if_possible(os.path.join("zfec", "_fec.so"))
    fileutil.make_dirs("instdir")
    fname = os.path.join("benchresults", "comp_0-stride_%d"%stride)
    os.system("PYTHONPATH=instdir ./setup.py develop --install-dir=instdir --stride=%d >/dev/null" % stride)
    os.system("PYTHONPATH=instdir python -OO ./bench/bench_zfec.py >> %s" % fname)
    inf = open(fname, "rU")
    for l in inf:
        m = R.search(l)
        if m:
            result = int(m.group(1))
            if results.has_key(stride):
                print "stride: %d, results: %d (dup %d)" % (stride, result, results[stride])
            else:
                print "stride: %d, results: %d" % (stride, result)
            results[stride] = result
            break
Пример #7
0
 def _help_test_ignore_error(self, pkgname, modname, isrealpackage, modcontents):
     """ I return the mockstderr object so you can check what the
     code under test said to sys.stderr. """
     realstderr=sys.stderr
     mockstderr = Mock()
     sys.stderr = mockstderr
     try:
         fileutil.make_dirs(pkgname)
         if isrealpackage:
             fileutil.write_file(os.path.join(pkgname, '__init__.py'), "")
         fileutil.write_file(os.path.join(pkgname, modname+'.py'), modcontents)
         sys.path.append(os.getcwd())
         trialcoverage.import_all_python_files([pkgname])
         return mockstderr
     finally:
         sys.stderr = realstderr
         if sys.modules.has_key(pkgname):
             del sys.modules[pkgname]
         fileutil.rm_dir(pkgname)
Пример #8
0
    def UNFINISHED_test_successive_different_code(self):
        pkgname='fakepackage4'
        modname='fakemodule4'
        modcontents=''

        realstderr=sys.stderr
        mockstderr = Mock()
        sys.stderr = mockstderr
        try:
            fileutil.make_dirs(pkgname)
            fileutil.write_file(os.path.join(pkgname, '__init__.py'), "")
            fileutil.write_file(os.path.join(pkgname, modname+'.py'), modcontents)
            sys.path.append(os.getcwd())
            trialcoverage.import_all_python_files([pkgname])
            return mockstderr
        finally:
            sys.stderr = realstderr
            if sys.modules.has_key(pkgname):
                del sys.modules[pkgname]
            fileutil.rm_dir(pkgname)
Пример #9
0
def measure(stride):
    fileutil.rm_dir("build")
    fileutil.rm_dir("instdir")
    fileutil.remove_if_possible(os.path.join("zfec", "_fec.so"))
    fileutil.make_dirs("instdir")
    fname = os.path.join("benchresults", "comp_0-stride_%d" % stride)
    os.system(
        "PYTHONPATH=instdir ./setup.py develop --install-dir=instdir --stride=%d >/dev/null"
        % stride)
    os.system("PYTHONPATH=instdir python -OO ./bench/bench_zfec.py >> %s" %
              fname)
    inf = open(fname, "rU")
    for l in inf:
        m = R.search(l)
        if m:
            result = int(m.group(1))
            if results.has_key(stride):
                print "stride: %d, results: %d (dup %d)" % (stride, result,
                                                            results[stride])
            else:
                print "stride: %d, results: %d" % (stride, result)
            results[stride] = result
            break
Пример #10
0
 def mkdir(self, basedir, path, mode=0777):
     fn = os.path.join(basedir, path)
     fileutil.make_dirs(fn, mode)
Пример #11
0
 def mkdir(self, basedir, path, mode=0o777):
     fn = os.path.join(basedir, path)
     fileutil.make_dirs(fn, mode)
Пример #12
0
    def __init__(self, dbparentdir=None, dir=None, serialized = None, maxitems = 1000, recoverdb=true):
        """
        You can pass either dir or dbparentdir, but not both.  You pass `dbparentdir' if you
        don't know the id of the key (either because the key is being created or because it is
        being de-serialized).  In that case, SessionKeeper creates a new sub-directory of
        dbparentdir which is named by the mojosixbit encoding of the id of the key.  For
        example, you pass dbparentdir == "...mtmdb/", and it creates
        "...mtmdb/ABCDEFGHIJKLMNOPQRSTUVWXYZA/" and puts the key in a subdirectory named
        "...mtmdb/ABCDEFGHIJKLMNOPQRSTUVWXYZA/mesgen/".

        You pass `dir' if you already know the directory that the key is stored in.  For
        example, you pass dir == "...mtmdb/ABCDEFGHIJKLMNOPQRSTUVWXYZA/" and it looks in
        "...mtmdb/ABCDEFGHIJKLMNOPQRSTUVWXYZA/mesgen/" and uses the key therein.

        @param dbparentdir: the directory for all keys;  Subdirectories will be located or
            created, named by the mojosixbit encoding of the hash of the key.
        @param dir: the directory for this particular key

        @precondition: Exactly one of (dbparentdir, dir) must be not None.: ((dbparentdir is not None) and (dir is None)) or ((dbparentdir is None) and (dir is not None)): "dbparentdir: %s, dir: %s" % (hr(dbparentdir), hr(dir))
        """
        assert ((dbparentdir is not None) and (dir is None)) or ((dbparentdir is None) and (dir is not None)), "precondition: Exactly one of (dbparentdir, dir) must be not None." + " -- " + "dbparentdir: %s, dir: %s" % (hr(dbparentdir), hr(dir))

        debugprint("SessionKeeper.__init__()\n", v=1)
        
        if serialized:
            debugprint("COMPLAINT: passing in serialized secret keys is the old-style, just-a-hack-for-debug way of doing things.  You really want to just give me the directory to start from and I'll get the secret key stored in there in a file.\n", v=3)

        if dir:
            # The dbdir is under dir and named "mesgen".
            self._dir = os.path.normpath(dir)
            self._dbdir = os.path.normpath(os.path.join(self._dir, "mesgen"))

            if not serialized:
                f = open(os.path.normpath(os.path.join(dir, "key")), "r")
                serialized = f.read()
                f.close()

            skdict = mencode.mdecode(serialized)
            keyMV = modval.new_serialized(mojosixbit.a2b(skdict['private key serialized']))
  
            self.__my_public_key = keyutil.makePublicRSAKeyForCommunicating(keyMV)
            self.__my_public_key_id = idlib.make_id(self.__my_public_key, '')
        else:
            # The dbdir is under dbparentdir, under my id, and is named "mesgen".

            if serialized:
                skdict = mencode.mdecode(serialized)
                keyMV = modval.new_serialized(mojosixbit.a2b(skdict['private key serialized']))
            else:
                keyMV = modval.new_random(SIZE_OF_PUBLIC_KEYS, HARDCODED_RSA_PUBLIC_EXPONENT)

            self.__my_public_key = keyutil.makePublicRSAKeyForCommunicating(keyMV)
            self.__my_public_key_id = idlib.make_id(self.__my_public_key, 'broker')

            myid_aa = idlib.to_mojosixbit(self.__my_public_key_id)

            self._dir = os.path.normpath(os.path.join(dbparentdir, myid_aa))
            self._dbdir = os.path.normpath(os.path.join(self._dir, "mesgen"))
            fileutil.make_dirs(self._dbdir)

        # this lock is used to force single threaded access to this object as we've been having occasional DB_LOCK_DEADLOCK
        # problems with the database, most likely due to TCPCommsHandler accessing it from the asyncore thread as well as
        # CryptoCommsHandler accessing it from the DoQ thread.
        self.lock = threading.Lock()

        db_env = CleanLogDbEnv()
        db_env.set_lk_detect(db.DB_LOCK_DEFAULT)
        if recoverdb:
            recoverflag = db.DB_RECOVER
        else:
            recoverflag = 0

        privateflag = db.DB_PRIVATE

        try:
            db_env.open(self._dbdir, db.DB_CREATE | db.DB_INIT_MPOOL | db.DB_INIT_LOCK | db.DB_INIT_LOG | db.DB_INIT_TXN | privateflag | recoverflag)
        except db.DBError, dbe:
            debugprint('Failed to open the database environment the first time, reason: %s\nTrying again...\n', args=(dbe,), vs='mesgen', v=2)
            try:
                db_env.open(self._dbdir, db.DB_CREATE | db.DB_INIT_MPOOL | db.DB_INIT_LOCK | db.DB_INIT_LOG | db.DB_INIT_TXN | privateflag | recoverflag | db.DB_RECOVER)
            except db.DBError, dbe:
                debugprint('Failed to open the database environment the second time, reason: %s\nTrying again...\n', args=(dbe,), vs='mesgen', v=2)
                # XXX DOUBLE CHOCOLATEY HACK sometimes trying *again* after one open *without* DB_RECOVER works.
                db_env.open(self._dbdir, db.DB_CREATE | db.DB_INIT_MPOOL | db.DB_INIT_LOCK | db.DB_INIT_LOG | db.DB_INIT_TXN | privateflag | recoverflag & (~db.DB_RECOVER))