<s> import hashlib <EOL> import datetime <EOL> import mock <EOL> import factory . alchemy <EOL> from Crypto . PublicKey import RSA <EOL> from kozmic . models import ( db , User , DeployKey , Project , Membership , Hook , <EOL> TrackedFile , HookCall , Build , Job , Organization ) <EOL> class Factory ( factory . alchemy . SQLAlchemyModelFactory ) : <EOL> FACTORY_SESSION = None <EOL> ABSTRACT_FACTORY = True <EOL> @ classmethod <EOL> def _create ( cls , target_class , * args , ** kwargs ) : <EOL> obj = super ( Factory , cls ) . _create ( target_class , * args , ** kwargs ) <EOL> cls . FACTORY_SESSION . commit ( ) <EOL> return obj <EOL> def setup ( session ) : <EOL> Factory . FACTORY_SESSION = session <EOL> def reset ( ) : <EOL> factories = ( <EOL> UserFactory , <EOL> DeployKeyFactory , <EOL> ProjectFactory , <EOL> MembershipFactory , <EOL> UserRepositoryFactory , <EOL> OrganizationFactory , <EOL> OrganizationRepositoryFactory , <EOL> BuildFactory , <EOL> JobFactory , <EOL> HookFactory , <EOL> TrackedFileFactory , <EOL> HookCallFactory , <EOL> ) <EOL> for factory in factories : <EOL> factory . reset_sequence ( ) <EOL> _identity = lambda n : n <EOL> class UserFactory ( Factory ) : <EOL> FACTORY_FOR = User <EOL> id = factory . Sequence ( _identity ) <EOL> gh_id = factory . Sequence ( _identity ) <EOL> gh_name = factory . Sequence ( u'<STR_LIT>' . format ) <EOL> gh_login = factory . Sequence ( u'<STR_LIT>' . format ) <EOL> gh_avatar_url = factory . Sequence ( u'<STR_LIT>' . format ) <EOL> gh_access_token = '<STR_LIT>' <EOL> class UserRepositoryFactory ( Factory ) : <EOL> FACTORY_FOR = User . Repository <EOL> id = factory . Sequence ( _identity ) <EOL> gh_id = factory . Sequence ( lambda n : <NUM_LIT:1000> + n ) <EOL> gh_name = '<STR_LIT>' <EOL> gh_full_name = '<STR_LIT>' <EOL> is_public = False <EOL> @ factory . lazy_attribute <EOL> def gh_ssh_clone_url ( self ) : <EOL> return '<STR_LIT>' . format ( self . gh_full_name ) <EOL> @ factory . lazy_attribute <EOL> def gh_https_clone_url ( self ) : <EOL> return '<STR_LIT>' . format ( self . gh_full_name ) <EOL> class OrganizationFactory ( Factory ) : <EOL> FACTORY_FOR = Organization <EOL> id = factory . Sequence ( _identity ) <EOL> gh_id = factory . Sequence ( _identity ) <EOL> gh_login = '<STR_LIT>' <EOL> gh_name = '<STR_LIT>' <EOL> class OrganizationRepositoryFactory ( Factory ) : <EOL> FACTORY_FOR = Organization . Repository <EOL> id = factory . Sequence ( lambda n : n ) <EOL> gh_id = factory . Sequence ( lambda n : <NUM_LIT> + n ) <EOL> gh_name = '<STR_LIT>' <EOL> gh_full_name = '<STR_LIT>' <EOL> is_public = False <EOL> @ factory . lazy_attribute <EOL> def gh_ssh_clone_url ( self ) : <EOL> return '<STR_LIT>' . format ( self . gh_full_name ) <EOL> @ factory . lazy_attribute <EOL> def gh_https_clone_url ( self ) : <EOL> return '<STR_LIT>' . format ( self . gh_full_name ) <EOL> class MembershipFactory ( Factory ) : <EOL> FACTORY_FOR = Membership <EOL> class DeployKeyFactory ( Factory ) : <EOL> FACTORY_FOR = DeployKey <EOL> CACHED_RSA_KEY = RSA . generate ( <NUM_LIT> ) <EOL> class ProjectFactory ( Factory ) : <EOL> FACTORY_FOR = Project <EOL> id = factory . Sequence ( _identity ) <EOL> gh_id = factory . Sequence ( _identity ) <EOL> gh_name = factory . Sequence ( u'<STR_LIT>' . format ) <EOL> gh_full_name = factory . Sequence ( u'<STR_LIT>' . format ) <EOL> gh_login = factory . Sequence ( u'<STR_LIT>' . format ) <EOL> gh_ssh_clone_url = factory . Sequence ( u'<STR_LIT>' . format ) <EOL> gh_https_clone_url = factory . Sequence ( u'<STR_LIT>' . format ) <EOL> is_public = False <EOL> @ factory . post_generation <EOL> def generate_deploy_key ( project , create , extracted , ** kwargs ) : <EOL> if not project . is_public : <EOL> with mock . patch ( '<STR_LIT>' , <EOL> return_value = CACHED_RSA_KEY ) : <EOL> project . deploy_key = DeployKey ( passphrase = project . passphrase ) <EOL> project . deploy_key . gh_id = <NUM_LIT> <EOL> db . session . commit ( ) <EOL> class BuildFactory ( Factory ) : <EOL> FACTORY_FOR = Build <EOL> id = factory . Sequence ( _identity ) <EOL> gh_commit_author = '<STR_LIT>' <EOL> gh_commit_message = '<STR_LIT>' <EOL> gh_commit_ref = '<STR_LIT>' <EOL> status = '<STR_LIT>' <EOL> @ factory . lazy_attribute <EOL> def number ( self ) : <EOL> return len ( self . project . builds . all ( ) ) + <NUM_LIT:1> <EOL> @ factory . lazy_attribute <EOL> def created_at ( self ) : <EOL> days = self . number <EOL> return ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT:8> , <NUM_LIT:20> , <NUM_LIT:10> , <NUM_LIT> ) + <EOL> datetime . timedelta ( days = days ) ) <EOL> @ factory . lazy_attribute <EOL> def gh_commit_sha ( self ) : <EOL> digest = hashlib . sha1 ( ) <EOL> digest . update ( str ( self . id ) ) <EOL> return digest . hexdigest ( ) <EOL> class JobFactory ( Factory ) : <EOL> FACTORY_FOR = Job <EOL> id = factory . Sequence ( _identity ) <EOL> class TrackedFileFactory ( Factory ) : <EOL> FACTORY_FOR = TrackedFile <EOL> path = factory . Sequence ( u'<STR_LIT>' . format ) <EOL> class HookFactory ( Factory ) : <EOL> FACTORY_FOR = Hook <EOL> id = factory . Sequence ( _identity ) <EOL> gh_id = factory . Sequence ( _identity ) <EOL> title = factory . Sequence ( u'<STR_LIT>' . format ) <EOL> build_script = '<STR_LIT>' <EOL> docker_image = '<STR_LIT>' <EOL> class HookCallFactory ( Factory ) : <EOL> FACTORY_FOR = HookCall <EOL> id = factory . Sequence ( _identity ) <EOL> @ factory . lazy_attribute <EOL> def gh_payload ( self ) : <EOL> return { } </s>
