def handle(self, *args, **options):

        csv_url, = args

        override_election = None
        override_election_slug = options['election']
        if override_election_slug:
            try:
                override_election = Election.objects.get(
                    slug=override_election_slug)
            except Election.DoesNotExist:
                msg = 'No election with slug {0} found'
                raise CommandError(msg.format(override_election_slug))

        election_name_to_election = {}

        mime_type_magic = magic.Magic(mime=True)
        storage = FileSystemStorage()

        r = requests.get(csv_url)
        r.encoding = 'utf-8'
        reader = StreamDictReader(r.text)
        for row in reader:
            post_or_area_header = get_column_header(
                POST_OR_AREA_COLUMN_HEADERS_TO_TRY, row)

            name = row[post_or_area_header]
            if not name:
                continue
            name = name.strip()

            # If there was no election specified, try to find it from
            # the 'Election' column (which has the election name):
            if override_election_slug:
                election = override_election
            else:
                if 'Election' not in row:
                    raise CommandError(
                        "There is no election name in the 'Election' column, so you must supply an election slug with --election"
                    )
                election_name = row['Election']
                election = election_name_to_election.get(election_name)
                if election is None:
                    election = Election.objects.get(name=election_name)
                    election_name_to_election[election_name] = election

            try:
                post = Post.objects.get(
                    label=name,
                    extra__elections=election,
                )
            except Post.DoesNotExist:
                msg = "Failed to find the post {0}, guessing it might be the area name instead"
                print(msg.format(name))
                # If the post name isn't there, try getting it from
                # the area:
                try:
                    area = Area.objects.get(name=name)
                except Area.DoesNotExist:
                    print("Failed to find area for {0}".format(name))
                    continue

                try:
                    post = Post.objects.get(area=area)
                except Post.DoesNotExist:
                    print("Failed to find post with for {0}".format(name))
                    continue

            # Check that the post is actually valid for this election:
            if election not in post.extra.elections.all():
                msg = "The post {post} wasn't in the election {election}"
                raise CommandError(
                    msg.format(post=post.label, election=election.name))

            document_url_column = get_column_header(PDF_COLUMN_HEADERS_TO_TRY,
                                                    row)
            document_url = row[document_url_column]
            if not document_url:
                print("No URL for {0}".format(name))
                continue
            existing_documents = OfficialDocument.objects.filter(
                document_type=OfficialDocument.NOMINATION_PAPER,
                post_id=post,
            )
            if existing_documents.count() > 0:
                if options['delete_existing']:
                    print("Removing existing documents")
                    existing_documents.delete()
                else:
                    print("Skipping {0} since it already had documents".format(
                        name))
                    continue
            try:
                downloaded_filename = download_file_cached(document_url)
            except requests.exceptions.ConnectionError:
                print("Connection failed for {0}".format(name))
                print("The URL was:", document_url)
                continue
            except requests.exceptions.MissingSchema:
                # This is probably someone putting notes in the URL
                # column, so ignore:
                print("Probably not a document URL for {0}: {1}".format(
                    name, document_url))
                continue
            mime_type = mime_type_magic.from_file(downloaded_filename)
            extension = mimetypes.guess_extension(mime_type)
            if mime_type not in allowed_mime_types:
                print("Ignoring unknown MIME type {0} for {1}".format(
                    mime_type,
                    name,
                ))
                continue
            filename = "official_documents/{post_id}/statement-of-persons-nominated{extension}".format(
                post_id=post.extra.slug,
                extension=extension,
            )
            with open(downloaded_filename, 'rb') as f:
                storage_filename = storage.save(filename, f)

            OfficialDocument.objects.create(
                document_type=OfficialDocument.NOMINATION_PAPER,
                uploaded_file=storage_filename,
                election=election,
                post=post,
                source_url=document_url)
            message = "Successfully added the Statement of Persons Nominated for {0}"
            print(message.format(name))
예제 #2
0
파일: runtornado.py 프로젝트: yvanss/zulip
    def handle(self, addrport: str, **options: bool) -> None:
        interactive_debug_listen()

        import django
        from tornado import httpserver

        try:
            addr, port = addrport.split(':')
        except ValueError:
            addr, port = '', addrport

        if not addr:
            addr = '127.0.0.1'

        if not port.isdigit():
            raise CommandError("%r is not a valid port number." % (port,))

        xheaders = options.get('xheaders', True)
        no_keep_alive = options.get('no_keep_alive', False)
        quit_command = 'CTRL-C'

        if settings.DEBUG:
            logging.basicConfig(level=logging.INFO,
                                format='%(asctime)s %(levelname)-8s %(message)s')

        def inner_run() -> None:
            from django.conf import settings
            from django.utils import translation
            translation.activate(settings.LANGUAGE_CODE)

            print("Validating Django models.py...")
            self.check(display_num_errors=True)
            print("\nDjango version %s" % (django.get_version()))
            print("Tornado server is running at http://%s:%s/" % (addr, port))
            print("Quit the server with %s." % (quit_command,))

            if settings.USING_RABBITMQ:
                queue_client = get_queue_client()
                # Process notifications received via RabbitMQ
                queue_client.register_json_consumer(notify_tornado_queue_name(int(port)),
                                                    process_notification)
                queue_client.register_json_consumer(tornado_return_queue_name(int(port)),
                                                    respond_send_message)

            try:
                # Application is an instance of Django's standard wsgi handler.
                application = create_tornado_application(int(port))
                if settings.AUTORELOAD:
                    zulip_autoreload_start()

                # start tornado web server in single-threaded mode
                http_server = httpserver.HTTPServer(application,
                                                    xheaders=xheaders,
                                                    no_keep_alive=no_keep_alive)
                http_server.listen(int(port), address=addr)

                setup_event_queue(int(port))
                add_client_gc_hook(missedmessage_hook)
                setup_tornado_rabbitmq()

                instance = ioloop.IOLoop.instance()

                if django.conf.settings.DEBUG:
                    instance.set_blocking_log_threshold(5)
                    instance.handle_callback_exception = handle_callback_exception
                instance.start()
            except KeyboardInterrupt:
                sys.exit(0)

        inner_run()
    def handle(self, *args, **options):

        if not options['user_id']:
            raise CommandError("You must specify a user ID with --user")

        user = User.objects.get(pk=options['user_id'])

        projects = {
            'Default Project': {
                'stacks': []
            },
            'Evaluation data set': {
                'stacks': []
            },
            'Focussed Ion Beam (FIB)': {
                'stacks': []
            }
        }

        # Define the details of a stack for two of these projects:

        projects['Default Project']['stacks'].append({
            'title':
            'Original data.',
            'dimension':
            Integer3D(4096, 4096, 16),
            'resolution':
            Double3D(3.2614000000000001, 3.2614000000000001, 60),
            'comment':
            '''<p>&copy;2007 by Stephan Saalfeld.</p>
<p>Rendered with <a href="http://www.povray.org/">POV-Ray&nbsp;v3.6</a>
using this <a href="http://fly.mpi-cbg.de/~saalfeld/download/volume.tar.bz2">scene-file</a>.</p>''',
            'mirrors': [{
                'title':
                'Public copy',
                'image_base':
                'http://fly.mpi-cbg.de/map/evaluation/original/',
            }]
        })

        projects['Focussed Ion Beam (FIB)']['stacks'].append({
            'title':
            'Focussed Ion Beam (FIB) stack of Rat Striatum',
            'dimension':
            Integer3D(2048, 1536, 460),
            'resolution':
            Double3D(5, 5, 9),
            'comment':
            '''
<p>&copy;2009 <a href="http://people.epfl.ch/graham.knott">Graham Knott</a>.</p>
<p>Public INCF data set available at the
<a href="http://www.incf.org/about/nodes/switzerland/data">Swiss INCF Node</a>.</p>''',
            'mirrors': [{
                'title':
                'FIB Public copy',
                'image_base':
                'http://incf.ini.uzh.ch/image-stack-fib/',
            }]
        })

        # Make sure that each project and its stacks exist, and are
        # linked via ProjectStack:

        for project_title in projects:
            project_object, _ = Project.objects.get_or_create(
                title=project_title)
            for stack_dict in projects[project_title]['stacks']:
                stack, _ = Stack.objects.get_or_create(
                    title=stack_dict['title'],
                    defaults={
                        'dimension': stack_dict['dimension'],
                        'resolution': stack_dict['resolution'],
                    })
                mirrors = list(StackMirror.objects.filter(stack=stack))
                if not mirrors:
                    for m in stack_dict['mirrors']:
                        mirrors.append(
                            StackMirror.objects.create(
                                stack=stack,
                                title=m['title'],
                                image_base=m['image_base']))
                ProjectStack.objects.get_or_create(project=project_object,
                                                   stack=stack)
            projects[project_title]['project_object'] = project_object

        # Also set up the FIB project for tracing with treelines:

        tracing_project = projects['Focussed Ion Beam (FIB)']['project_object']

        call_command('catmaid_setup_tracing_for_project', '--project_id',
                     str(tracing_project.id), '--user', str(user.id))
예제 #4
0
    def handle(self, *args, **options):
        base_url = options["baseurl"] or absolutify("")
        if options["nocache"]:
            cache_control = "no-cache"
        else:
            cache_control = "max-age=0"
        force = options["force"]
        invalidate_cdn_cache = not options["skip_cdn_invalidation"]

        if options["all"]:
            # Query all documents, excluding those whose `last_rendered_at` is
            # within `min_render_age` or NULL.
            min_render_age = datetime.datetime.now() - datetime.timedelta(
                seconds=options["min_age"]
            )
            docs = Document.objects.filter(
                Q(last_rendered_at__isnull=True)
                | Q(last_rendered_at__lt=min_render_age)
            )
            if options["locale"]:
                docs = docs.filter(locale=options["locale"])
            if options["not_locale"]:
                docs = docs.exclude(locale=options["not_locale"])
            if options["slugsearch"]:
                if options["slugsearch"].endswith("*"):
                    docs = docs.filter(
                        slug__startswith=options["slugsearch"].rstrip("*")
                    )
                elif "*" in options["slugsearch"]:
                    raise NotImplementedError("* can only be on the end")
                else:
                    docs = docs.filter(slug__contains=options["slugsearch"])
            docs = docs.order_by("-modified")
            docs = docs.values_list("id", flat=True)

            self.chain_render_docs(
                docs,
                cache_control,
                base_url,
                force,
                invalidate_cdn_cache=invalidate_cdn_cache,
            )

        else:
            # Accept page paths from command line, but be liberal
            # in what we accept, eg: /en-US/docs/CSS (full path);
            # /en-US/CSS (no /docs); or even en-US/CSS (no leading slash)
            paths = options["paths"]
            if not paths:
                raise CommandError("Need at least one document path to render")

            for path in paths:
                if path.startswith("/"):
                    path = path[1:]
                locale, sep, slug = path.partition("/")
                head, sep, tail = slug.partition("/")
                if head == "docs":
                    slug = tail
                doc = Document.objects.get(locale=locale, slug=slug)
                log.info(f"Rendering {doc} ({doc.get_absolute_url()})")
                try:
                    render_document(
                        doc.pk,
                        cache_control,
                        base_url,
                        force,
                        invalidate_cdn_cache=invalidate_cdn_cache,
                    )
                    log.debug("DONE.")
                except DocumentRenderingInProgress:
                    log.error("Rendering is already in progress for this document.")
예제 #5
0
    def export(self, request):
        self.full_clean()
        format = 'yaml'
        selected = []
        include_images = False
        include_categories = False

        for name, value in self.cleaned_data.items():
            if name == 'format':
                format = value
                continue

            if name == 'include_images':
                include_images = value
                continue

            if name == 'include_categories':
                include_categories = value
                continue

            opt, key = name.split('__')

            if opt=='export':
                if value:
                    selected.append(key)

        try:
            serializers.get_serializer(format)
        except KeyError:
            raise CommandError("Unknown serialization format: %s" % format)

        objects = []
        images = []
        categories = {}
        for slug in selected:
            product = Product.objects.get(slug=slug)
            objects.append(product)
            for subtype in product.get_subtypes():
                objects.append(getattr(product,subtype.lower()))
            objects.extend(list(product.price_set.all()))
            objects.extend(list(product.productimage_set.all()))
            if include_images:
                for image in product.productimage_set.all():
                    images.append(image.picture)
            if include_categories:
                for category in product.category.all():
                    wcategory = category
                    while wcategory and wcategory not in categories:
                        categories[wcategory] = 1
                        wcategory = wcategory.parent

        # Export all categories, translations.  Export images,translations if
        # desired.
        if include_categories:
            for category in categories.keys():
                objects.append(category)
                objects.extend(list(category.translations.all()))
                if include_images:
                    for image in category.images.all():
                        objects.append(image)
                        objects.extend(list(image.translations.all()))

        try:
            raw = serializers.serialize(format, objects, indent=False)
        except Exception, e:
            raise CommandError("Unable to serialize database: %s" % e)
 def handle(self, *args, **options):
     if not settings.DEBUG:
         raise CommandError(
             'As a safety precaution this command only works if DEBUG=True.'
         )
     self.fetch_versions_data(**options)
예제 #7
0
    def handle(self, *args, **options):
        if "/" not in options["job"]:
            raise CommandError(
                'Job must be specified in the form "grouping_name/module_name/JobClassName"'
            )
        job_class = get_job(options["job"])
        if not job_class:
            raise CommandError('Job "%s" not found' % options["job"])

        user = None
        request = None
        if options["commit"] and not options["username"]:
            # Job execution with commit=True uses change_logging(), which requires a user as the author of any changes
            raise CommandError("--username is mandatory when --commit is used")

        if options["username"]:
            User = get_user_model()
            try:
                user = User.objects.get(username=options["username"])
            except User.DoesNotExist as exc:
                raise CommandError("No such user") from exc

            request = RequestFactory().request(
                SERVER_NAME="nautobot_server_runjob")
            request.id = uuid.uuid4()
            request.user = user

        job_content_type = ContentType.objects.get(app_label="extras",
                                                   model="job")

        # Run the job and create a new JobResult
        self.stdout.write("[{:%H:%M:%S}] Running {}...".format(
            timezone.now(), job_class.class_path))

        job_result = JobResult.enqueue_job(
            run_job,
            job_class.class_path,
            job_content_type,
            user,
            data=
            {},  # TODO: parsing CLI args into a data dictionary is not currently implemented
            request=copy_safe_request(request) if request else None,
            commit=options["commit"],
        )

        # Wait on the job to finish
        while job_result.status not in JobResultStatusChoices.TERMINAL_STATE_CHOICES:
            time.sleep(1)
            job_result = JobResult.objects.get(pk=job_result.pk)

        # Report on success/failure
        groups = set(
            JobLogEntry.objects.filter(job_result=job_result).values_list(
                "grouping", flat=True))
        for group in sorted(groups):
            logs = JobLogEntry.objects.filter(job_result__pk=job_result.pk,
                                              grouping=group)
            success_count = logs.filter(
                log_level=LogLevelChoices.LOG_SUCCESS).count()
            info_count = logs.filter(
                log_level=LogLevelChoices.LOG_INFO).count()
            warning_count = logs.filter(
                log_level=LogLevelChoices.LOG_WARNING).count()
            failure_count = logs.filter(
                log_level=LogLevelChoices.LOG_FAILURE).count()

            self.stdout.write(
                "\t{}: {} success, {} info, {} warning, {} failure".format(
                    group,
                    success_count,
                    info_count,
                    warning_count,
                    failure_count,
                ))

            for log_entry in logs:
                status = log_entry.log_level
                if status == "success":
                    status = self.style.SUCCESS(status)
                elif status == "info":
                    status = status
                elif status == "warning":
                    status = self.style.WARNING(status)
                elif status == "failure":
                    status = self.style.NOTICE(status)

                if log_entry.log_object:
                    self.stdout.write(
                        f"\t\t{status}: {log_entry.log_object}: {log_entry.message}"
                    )
                else:
                    self.stdout.write(f"\t\t{status}: {log_entry.message}")

        if job_result.data["output"]:
            self.stdout.write(job_result.data["output"])

        if job_result.status == JobResultStatusChoices.STATUS_FAILED:
            status = self.style.ERROR("FAILED")
        elif job_result.status == JobResultStatusChoices.STATUS_ERRORED:
            status = self.style.ERROR("ERRORED")
        else:
            status = self.style.SUCCESS("SUCCESS")
        self.stdout.write("[{:%H:%M:%S}] {}: {}".format(
            timezone.now(), job_class.class_path, status))

        # Wrap things up
        self.stdout.write("[{:%H:%M:%S}] {}: Duration {}".format(
            timezone.now(), job_class.class_path, job_result.duration))
        self.stdout.write("[{:%H:%M:%S}] Finished".format(timezone.now()))
예제 #8
0
    def handle(self, *args, **options):
        if os.path.exists(options['output']):
            raise CommandError("File {} already exists".format(
                options['output']))

        STATUS_INTERVAL = 100

        # parse out the course into a coursekey
        if options['course']:
            try:
                course_key = CourseKey.from_string(options['course'])
            # if it's not a new-style course key, parse it from an old-style
            # course key
            except InvalidKeyError:
                course_key = CourseLocator.from_string(options['course'])

        print("Fetching enrolled students for {}".format(course_key))
        enrolled_students = User.objects.filter(
            courseenrollment__course_id=course_key)
        factory = RequestMock()
        request = factory.get('/')

        total = enrolled_students.count()
        print("Total enrolled: {}".format(total))
        course = courses.get_course_by_id(course_key)
        total = enrolled_students.count()
        start = datetime.datetime.now()
        rows = []
        header = None
        print("Fetching certificate data")
        cert_grades = {
            cert.user.username: cert.grade
            for cert in list(
                GeneratedCertificate.objects.filter(
                    course_id=course_key).prefetch_related('user'))
        }
        print("Grading students")
        for count, student in enumerate(enrolled_students):
            count += 1
            if count % STATUS_INTERVAL == 0:
                # Print a status update with an approximation of
                # how much time is left based on how long the last
                # interval took
                diff = datetime.datetime.now() - start
                timeleft = diff * (total - count) / STATUS_INTERVAL
                hours, remainder = divmod(timeleft.seconds, 3600)
                minutes, seconds = divmod(remainder, 60)
                print("{}/{} completed ~{:02}:{:02}m remaining".format(
                    count, total, hours, minutes))
                start = datetime.datetime.now()
            request.user = student
            grade = grades.grade(student, request, course)
            if not header:
                header = [
                    section['label'] for section in grade['section_breakdown']
                ]
                rows.append(
                    ["email", "username", "certificate-grade", "grade"] +
                    header)
            percents = {
                section['label']: section['percent']
                for section in grade['section_breakdown']
            }
            row_percents = [percents[label] for label in header]
            if student.username in cert_grades:
                rows.append([
                    student.email, student.username, cert_grades[
                        student.username], grade['percent']
                ] + row_percents)
            else:
                rows.append(
                    [student.email, student.username, "N/A", grade['percent']
                     ] + row_percents)
        with open(options['output'], 'wb') as f:
            writer = csv.writer(f)
            writer.writerows(rows)
예제 #9
0
    def handle(self, *args, **options):
        if Page.objects.count() > 2:
            raise CommandError('There already exists content in the database.')

        # Delete the existing 'Welcome to Wagtail' page, create homepage
        Page.objects.get(id=2).delete()
        Site.objects.all().delete()

        root = Page.objects.get(id=1)
        homepage = HomePage(title='Eureka')
        root.add_child(instance=homepage)
        homepage.save_revision().publish()

        site = Site.objects.create(hostname='localhost',
                                   root_page_id=homepage.id,
                                   is_default_site=True)

        # Improv Type Index
        improv_type_index = ImprovisationTypeIndexPage(
            title='Improvisation Types', show_in_menus=True)
        homepage.add_child(instance=improv_type_index)
        improv_type_index.save_revision().publish()

        # Improv Types
        for improv_type in IMPROV_TYPE_LIST:
            improv_type_page = ImprovisationTypePage(
                title=improv_type,
                body=[('rich_text', RichText('<p>TBD</p>'))])
            improv_type_index.add_child(instance=improv_type_page)
            improv_type_page.save_revision().publish()

        # Ear training
        ear_training_index = EarTrainingIndexPage(title='Ear Training',
                                                  show_in_menus=True)
        homepage.add_child(instance=ear_training_index)
        ear_training_index.save_revision().publish()

        # ET 1
        et_one = EarTrainingLevelPage(title='Introduction to Ear Training One',
                                      tab_title='Ear Training One',
                                      show_in_menus=True,
                                      body=[('rich_text',
                                             RichText('<p>TBD</p>'))])
        ear_training_index.add_child(instance=et_one)
        et_one.save_revision().publish()

        et_one_elements = [
            'Tempo', 'Dynamics', 'Texture', 'Meter', 'Rhythm Grid', 'Scales',
            'Intervals', 'Melody', 'Triads', 'Harmonic Progression',
            'Seventh Chords'
        ]
        create_ear_training_elements(et_one, et_one_elements)

        # Add extra content for A11y testing
        et_page = EarTrainingElementPage.objects.first()
        et_page.body = [('topic', {
            'title':
            'Out of tempo',
            'musical_elements': [{
                'element_title':
                'Out of tempo with intervals',
                'content':
                StreamValue(stream_block=StreamBlock([
                    ('rich_text', RichTextBlock()),
                    ('video', VideoEmbedBlock()),
                ]),
                            stream_data=[
                                ('rich_text', RichText('<p>TBD</p>')),
                                ('video', {
                                    'url': YT_TEST_VIDEO,
                                    'description': 'A very fine video'
                                }),
                            ])
            }]
        })]
        et_page.save_revision().publish()

        # ET 2
        et_two = EarTrainingLevelPage(title='Introduction to Ear Training Two',
                                      tab_title='Ear Training Two',
                                      show_in_menus=True,
                                      body=[('rich_text',
                                             RichText('<p>TBD</p>'))])
        ear_training_index.add_child(instance=et_two)
        et_two.save_revision().publish()

        et_two_elements = [
            'Tempo', 'Dynamics', 'Meter', 'Rhythm', 'Scales', 'Intervals',
            'Melody', 'Triads', 'Harmonic Progression', 'Seventh Chords'
        ]
        create_ear_training_elements(et_two, et_two_elements)

        # ET 3
        et_three = EarTrainingLevelPage(
            title='Introduction to Ear Training Three',
            tab_title='Ear Training Three',
            show_in_menus=True,
            body=[('rich_text', RichText('<p>TBD</p>'))])
        ear_training_index.add_child(instance=et_three)
        et_three.save_revision().publish()

        et_three_elements = [
            'Accents', 'Ties', 'Syncopation', 'Scales', 'Intervals', 'Melody',
            'Triads', 'Harmonic Progression', 'Seventh Chords'
        ]
        create_ear_training_elements(et_three, et_three_elements)

        # ET 4
        et_four = EarTrainingLevelPage(
            title='Introduction to Ear Training Four',
            tab_title='Ear Training Four',
            show_in_menus=True,
            body=[('rich_text', RichText('<p>TBD</p>'))])
        ear_training_index.add_child(instance=et_four)
        et_four.save_revision().publish()

        et_four_elements = [
            'Asymmetric Meters', 'Borrowed Divisions', 'Chromatic Intervals',
            'Scales', 'Melody', 'Triads', 'Augmented Sixth Chords',
            'Harmonic Progression'
        ]
        create_ear_training_elements(et_four, et_four_elements)

        # Improv combinations
        improv_combo_index = ImprovisationCombinationIndexPage(
            title='Improvisation Combinations', show_in_menus=True)
        homepage.add_child(instance=improv_combo_index)
        improv_combo_index.save_revision().publish()

        improv_combos = [
            'Begining Layering', 'Intermediate Layering', 'Advanced Layering'
        ]
        for i in improv_combos:
            combo_page = ImprovisationCombinationPage(
                title=i,
                show_in_menus=True,
                body=[('rich_text', RichText('<p>TBD</p>'))])
            improv_combo_index.add_child(instance=combo_page)
            combo_page.save_revision().publish()

        # Setup Main Menu
        menu_model = settings.models.MAIN_MENU_MODEL
        menu = menu_model.get_for_site(site)
        menu.add_menu_items_for_pages(
            homepage.get_descendants().filter(depth=homepage.depth + 1))
예제 #10
0
    def handle(self, *args, **options):

        self.verbosity = int(options.get('verbosity'))
        self.interactive = options.get('interactive')
        self.show_traceback = options.get('traceback')
        self.load_initial_data = options.get('load_initial_data')
        self.test_database = options.get('test_database', False)

        # Import the 'management' module within each installed app, to register
        # dispatcher events.
        for app_name in settings.INSTALLED_APPS:
            if module_has_submodule(import_module(app_name), "management"):
                import_module('.management', app_name)

        # Get the database we're operating from
        db = options.get('database')
        connection = connections[db]

        # If they asked for a migration listing, quit main execution flow and show it
        if options.get("list", False):
            return self.show_migration_list(connection, args)

        # Work out which apps have migrations and which do not
        executor = MigrationExecutor(connection, self.migration_progress_callback)

        # If they supplied command line arguments, work out what they mean.
        run_syncdb = False
        target_app_labels_only = True
        if len(args) > 2:
            raise CommandError("Too many command-line arguments (expecting 'appname' or 'appname migrationname')")
        elif len(args) == 2:
            app_label, migration_name = args
            if app_label not in executor.loader.migrated_apps:
                raise CommandError("App '%s' does not have migrations (you cannot selectively sync unmigrated apps)" % app_label)
            if migration_name == "zero":
                targets = [(app_label, None)]
            else:
                try:
                    migration = executor.loader.get_migration_by_prefix(app_label, migration_name)
                except AmbiguityError:
                    raise CommandError("More than one migration matches '%s' in app '%s'. Please be more specific." % (app_label, migration_name))
                except KeyError:
                    raise CommandError("Cannot find a migration matching '%s' from app '%s'." % (app_label, migration_name))
                targets = [(app_label, migration.name)]
            target_app_labels_only = False
        elif len(args) == 1:
            app_label = args[0]
            if app_label not in executor.loader.migrated_apps:
                raise CommandError("App '%s' does not have migrations (you cannot selectively sync unmigrated apps)" % app_label)
            targets = [key for key in executor.loader.graph.leaf_nodes() if key[0] == app_label]
        else:
            targets = executor.loader.graph.leaf_nodes()
            run_syncdb = True

        plan = executor.migration_plan(targets)

        # Print some useful info
        if self.verbosity >= 1:
            self.stdout.write(self.style.MIGRATE_HEADING("Operations to perform:"))
            if run_syncdb:
                self.stdout.write(self.style.MIGRATE_LABEL("  Synchronize unmigrated apps: ") + (", ".join(executor.loader.unmigrated_apps) or "(none)"))
            if target_app_labels_only:
                self.stdout.write(self.style.MIGRATE_LABEL("  Apply all migrations: ") + (", ".join(set(a for a, n in targets)) or "(none)"))
            else:
                if targets[0][1] is None:
                    self.stdout.write(self.style.MIGRATE_LABEL("  Unapply all migrations: ") + "%s" % (targets[0][0], ))
                else:
                    self.stdout.write(self.style.MIGRATE_LABEL("  Target specific migration: ") + "%s, from %s" % (targets[0][1], targets[0][0]))

        # Run the syncdb phase.
        # If you ever manage to get rid of this, I owe you many, many drinks.
        # Note that pre_migrate is called from inside here, as it needs
        # the list of models about to be installed.
        if run_syncdb:
            if self.verbosity >= 1:
                self.stdout.write(self.style.MIGRATE_HEADING("Synchronizing apps without migrations:"))
            created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
        else:
            created_models = []

        # Migrate!
        if self.verbosity >= 1:
            self.stdout.write(self.style.MIGRATE_HEADING("Running migrations:"))
        if not plan:
            if self.verbosity >= 1:
                self.stdout.write("  No migrations needed.")
        else:
            executor.migrate(targets, plan, fake=options.get("fake", False))

        # Send the post_migrate signal, so individual apps can do whatever they need
        # to do at this point.
        emit_post_migrate_signal(created_models, self.verbosity, self.interactive, connection.alias)
예제 #11
0
    def handle(self, *args, **options):
        '''
        Automatic import of project.
        '''

        # Check params
        if len(args) != 4:
            raise CommandError('Invalid number of parameters!')

        if options['file_format'] not in FILE_FORMATS:
            raise CommandError(
                'Invalid file format: %s' % options['file_format']
            )

        # Read params
        repo = args[1]
        branch = args[2]
        self.filemask = args[3]
        self.file_format = options['file_format']
        self.name_template = options['name_template']
        self.base_file_template = options['base_file_template']

        # Try to get project
        try:
            project = Project.objects.get(slug=args[0])
        except Project.DoesNotExist:
            raise CommandError(
                'Project %s does not exist, you need to create it first!' %
                args[0]
            )

        # Do we have correct mask?
        if '**' not in self.filemask:
            raise CommandError(
                'You need to specify double wildcard '
                'for subproject part of the match!'
            )

        if is_repo_link(repo):
            sharedrepo = repo
            try:
                sub_project = SubProject.objects.get(
                    project=project,
                    slug=repo.rsplit('/', 1)[-1]
                )
            except SubProject.DoesNotExist:
                raise CommandError(
                    'SubProject %s does not exist, '
                    'you need to create it first!' % repo
                )
            matches = self.get_matching_subprojects(
                sub_project.get_path(),
            )
        else:
            matches, sharedrepo = self.import_initial(project, repo, branch)

        # Create remaining subprojects sharing git repository
        for match in matches:
            name = self.format_string(self.name_template, match)
            template = self.format_string(self.base_file_template, match)
            slug = slugify(name)
            subprojects = SubProject.objects.filter(
                Q(name=name) | Q(slug=slug),
                project=project
            )
            if subprojects.exists():
                self.logger.warn(
                    'Subproject %s already exists, skipping',
                    name
                )
                continue

            self.logger.info('Creating subproject %s', name)
            SubProject.objects.create(
                name=name,
                slug=slug,
                project=project,
                repo=sharedrepo,
                branch=branch,
                template=template,
                file_format=self.file_format,
                filemask=self.filemask.replace('**', match)
            )
예제 #12
0
    def handle(self, *args, **options):
        colorama_init()
        org = get_org(options["org"])
        scheme, path, *rest = URN.to_parts(options["urn"])

        db = settings.DATABASES["default"]
        db_url = f"postgres://{db['USER']}:{db['PASSWORD']}@{db['HOST']}:{db['PORT']}/{db['NAME']}?sslmode=disable"
        redis_url = settings.CACHES["default"]["LOCATION"]

        try:
            print(
                f"✅ Mailroom version {mailroom.get_client().version()} running at️ {Fore.CYAN}{settings.MAILROOM_URL}{Fore.RESET}"
            )
        except ConnectionError:
            launch = f'mailroom -db="{db_url}" -redis={redis_url}'
            raise CommandError(
                f"Unable to connect to mailroom. Please launch it with...\n\n{launch}"
            )

        try:
            requests.get(COURIER_URL)
            print(
                f"✅ Courier running at️ {Fore.CYAN}{COURIER_URL}{Fore.RESET}")
        except ConnectionError:
            launch = f'courier -db="{db_url}" -redis={redis_url} -spool-dir="."'
            raise CommandError(
                f"Unable to connect to courier. Please launch it with...\n\n{launch}"
            )

        try:
            channel = TestChannel.create(org,
                                         org.administrators.first(),
                                         COURIER_URL,
                                         callback=self.response_callback,
                                         scheme=scheme)
            print(
                f"✅ Testing channel started at️ {Fore.CYAN}{channel.server.base_url}{Fore.RESET}"
            )
        except Exception as e:
            raise CommandError(f"Unable to start test channel: {str(e)}")

        print(
            f"\nSending messages to {Fore.CYAN}{org.name}{Fore.RESET} as {Fore.CYAN}{scheme}:{path}{Fore.RESET}. Use Ctrl+C to quit."
        )

        self.responses_wait = None
        try:
            while True:
                line = input(f"📱 {Fore.CYAN}{path}{Fore.RESET}> ")
                if not line:
                    continue

                msg_in = channel.incoming(path, line)

                # we wait up to 2 seconds for a response from courier
                self.responses_wait = threading.Event()
                self.responses_wait.wait(timeout=2)

                for response in org.msgs.filter(
                        direction="O", id__gt=msg_in.id).order_by("id"):
                    print(
                        f"💬 {Fore.GREEN}{response.channel.address}{Fore.RESET}> {response.text}"
                    )

        except KeyboardInterrupt:
            pass
예제 #13
0
try:
    try:
        from pip._internal.network.session import PipSession
    except ImportError:
        from pip._internal.download import PipSession
    from pip._internal.req.req_file import parse_requirements
    from pip._internal.utils.misc import get_installed_distributions
except ImportError:
    # pip < 10
    try:
        from pip import get_installed_distributions
        from pip.download import PipSession
        from pip.req import parse_requirements
    except ImportError:
        raise CommandError("Pip version 6 or higher is required")

try:
    import requests
    HAS_REQUESTS = True
except ImportError:
    HAS_REQUESTS = False


class Command(BaseCommand):
    help = "Scan pip requirement files for out-of-date packages."

    def add_arguments(self, parser):
        super(Command, self).add_arguments(parser)
        parser.add_argument("-t",
                            "--github-api-token",