Example #1
0
        def __sha_hash__(self):
            if self._sha_hash_cache is None:
                base_hash = sha_hash(alternative._name)
                for fieldname in sorted(self._fields):
                    val = self._fields[fieldname]
                    default = default_initialize(typedict[fieldname])

                    if val != default:
                        base_hash = base_hash + sha_hash(fieldname) + sha_hash(
                            val)

                self._sha_hash_cache = base_hash + sha_hash(self._which)
            return self._sha_hash_cache
Example #2
0
    def _new(self, cls, kwds):
        if not self._writeable:
            raise Exception("Views are static. Please open a transaction.")

        if "_identity" in kwds:
            identity = kwds["_identity"]
            kwds = dict(kwds)
            del kwds["_identity"]
        else:
            identity = sha_hash(str(uuid.uuid4())).hexdigest

        o = cls(identity)

        writes = {}

        kwds = dict(kwds)
        for tname, t in cls.__types__.iteritems():
            if tname not in kwds:
                kwds[tname] = algebraic.default_initialize(t)

                if kwds[tname] is None:
                    raise Exception(
                        "Can't default initialize %s.%s of type %s" %
                        (cls.__name__, tname, t))

        for kwd, val in kwds.iteritems():
            if kwd not in cls.__types__:
                raise TypeError("Unknown field %s on %s" % (kwd, cls))

            coerced_val = algebraic.coerce_instance(val, cls.__types__[kwd])
            if coerced_val is None:
                raise TypeError("Can't coerce %s to %s" %
                                (val, cls.__types__[kwd]))

            writes[data_key(cls.__name__, identity, kwd)] = coerced_val

        writes[data_key(cls.__name__, identity, ".exists")] = True

        self._writes.update(writes)

        if cls.__name__ in self._db._indices:
            for index_name, index_fun in self._db._indices[
                    cls.__name__].iteritems():
                val = index_fun(o)

                if val is not None:
                    ik = index_key(cls.__name__, index_name, val)

                    if ik in self._writes:
                        self._writes[ik] = self._writes[ik] + (identity, )
                    else:
                        existing = self._get_dbkey(ik)
                        if existing is None:
                            existing = ()
                        else:
                            existing = tuple(existing)

                        self._writes[ik] = existing + (identity, )

        return o
Example #3
0
def index_key(obj_typename, field_name, value):
    if isinstance(value, int):
        value_hash = "int_" + str(value)
    else:
        value_hash = sha_hash(value).hexdigest

    return obj_typename + "-ix:" + field_name + ":" + value_hash
Example #4
0
        def resolveTest(testName):
            if testName not in resolved_tests:
                try:
                    if testName not in tests:
                        raise TestResolutionException(
                            "Can't find build %s in\n%s" %
                            (testName, "\n".join(
                                ["\t" + x for x in sorted(tests)])))
                    testDef = tests[testName]

                    self.assertArtifactSetValid(testDef)

                    resolved_tests[testName] = testDef._withReplacement(
                        dependencies={
                            k: resolveTestDep(v)
                            for k, v in testDef.dependencies.iteritems()
                        },
                        stages=self.sortTestStages(testDef.stages))

                    resolved_tests[testName]._withReplacement(
                        hash=sha_hash(resolved_tests[testName]).hexdigest)
                except UserWarning as e:
                    raise UserWarning("While processing test %s:\n%s" %
                                      (testName, e))

            return resolved_tests[testName]
Example #5
0
    def test_stable_sha_hashing(self):
        #adding default values to a type shouldn't disrupt its hashes
        leaf = Algebraic.Alternative("Leaf")
        leaf.A = {'a': int}
        leaf.B = {'b': int}
        leaf.setCreateDefault(lambda: leaf.A(0))

        not_leaf = Algebraic.Alternative("NotLeaf")
        not_leaf.A = {'z': float, 'leaf': leaf}

        not_leaf2 = Algebraic.Alternative("NotLeaf")
        not_leaf2.A = {'z': float, 'leaf': leaf, 'int': int}

        a_simple_notleaf = not_leaf.A(z=10.0, _fill_in_missing=True)
        a_simple_notleaf2 = not_leaf2.A(z=10.0, _fill_in_missing=True)

        a_different_notleaf = not_leaf.A(z=10.0,
                                         leaf=leaf.B(b=10),
                                         _fill_in_missing=True)
        a_different_notleaf2 = not_leaf2.A(z=10.0,
                                           leaf=leaf.B(b=10),
                                           _fill_in_missing=True)
        a_final_different_notleaf = not_leaf2.A(z=10.0,
                                                leaf=leaf.B(b=10),
                                                int=123,
                                                _fill_in_missing=True)

        self.assertEqual(sha_hash(a_simple_notleaf),
                         sha_hash(a_simple_notleaf2))

        self.assertNotEqual(sha_hash(a_simple_notleaf),
                            sha_hash(a_different_notleaf))
        self.assertEqual(sha_hash(a_different_notleaf),
                         sha_hash(a_different_notleaf2))

        self.assertNotEqual(sha_hash(a_simple_notleaf),
                            sha_hash(a_final_different_notleaf))
        self.assertNotEqual(sha_hash(a_different_notleaf),
                            sha_hash(a_final_different_notleaf))
Example #6
0
 def __sha_hash__(self):
     return sha_hash(self.bits)
Example #7
0
    def bootAmiCreator(self, platform, instanceType, baseAmi, setupScript):
        assert platform == "windows"

        setupScriptHash = sha_hash(setupScript).hexdigest

        if (baseAmi, setupScriptHash) in self.imagesBeingProducedByWorkers():
            logging.warn(
                "We tried to boot an image creator for %s/%s, but one is already up",
                (baseAmi, setupScriptHash))
            return

        if (baseAmi,
                setupScriptHash) in self.listWindowsImages(availableOnly=True):
            logging.warn(
                "We tried to boot an image creator for %s/%s, but the image already exists!",
                (baseAmi, setupScriptHash))
            return

        logging.info("Booting an AMI creator for ami %s and hash %s", baseAmi,
                     setupScriptHash)

        to_json = algebraic_to_json.Encoder().to_json

        baseAmi = baseAmi or self.config.machine_management.windows_ami

        looper_server_and_port = ("%s:%s" %
                                  (self.config.server_ports.server_address,
                                   self.config.server_ports.server_https_port))

        bootstrap_path = self.bootstrap_key_root + baseAmi + "_" + sha_hash(
            setupScript).hexdigest

        boot_script = (
            windows_ami_creator.replace(
                "__windows_box_password__",
                self.config.machine_management.windows_password).replace(
                    "__test_key__",
                    open(self.config.machine_management.path_to_keys,
                         "r").read()).replace(
                             "__test_key_pub__",
                             open(
                                 self.config.machine_management.path_to_keys +
                                 ".pub", "r").read()).
            replace("__bootstrap_bucket__",
                    self.config.machine_management.bootstrap_bucket).replace(
                        "__installation_key__",
                        bootstrap_path + "_InstallScript.ps1").replace(
                            "__reboot_script_key__",
                            bootstrap_path + "_RebootScript.ps1").replace(
                                "__bootstrap_log_key__",
                                bootstrap_path + "_BootstrapLog").replace(
                                    "__testlooper_server_and_port__",
                                    looper_server_and_port).
            replace(
                "__hosts__", "\n\n".join(
                    'echo "%s %s" |  Out-File -Append c:/Windows/System32/Drivers/etc/hosts -Encoding ASCII'
                    % (ip, hostname) for hostname, ip in
                    self.config.machine_management.host_ips.iteritems())))

        bucket = self.s3.Bucket(
            self.config.machine_management.bootstrap_bucket)

        reboot_script = (windows_bootstrap_script.replace(
            "__test_config__",
            json.dumps(
                {
                    "server_ports": to_json(self.config.server_ports),
                    "source_control": to_json(self.config.source_control),
                    "artifacts": to_json(self.config.artifacts)
                },
                indent=4)).replace("__testlooper_server_and_port__",
                                   looper_server_and_port))

        bucket.put_object(Key=bootstrap_path + "_RebootScript.ps1",
                          Body=reboot_script)
        bucket.put_object(Key=bootstrap_path + "_InstallScript.ps1",
                          Body=setupScript)

        self.bootWorker(
            platform,
            instanceType,
            amiOverride=baseAmi,
            bootScriptOverride=boot_script,
            nameValueOverride=self.config.machine_management.worker_name +
            "_ami_creator",
            extraTags={
                "BaseAmi": baseAmi,
                "SetupScriptHash": setupScriptHash
            },
            wantsTerminateOnShutdown=False)
Example #8
0
 def __sha_hash__(self):
     return sha_hash(self._identity) + sha_hash(type(self).__name__)