Exemplo n.º 1
0
    def handle(self, *args, **options):
        source = options['source']

        if not source or not os.path.exists(os.path.expanduser(source)):
            raise Exception('{0} is not a valid CSV path'.format(source))

        functions = set()
        files = set()
        with open(source) as file_:
            reader = csv.reader(file_)
            for row in reader:
                if 'Function' in row[0]:
                    name = row[1]
                    file = row[2]
                    sloc = int(row[3])

                    function = Function()

                    function.name = name
                    function.file = file
                    function.sloc = sloc

                    if function not in functions:
                        functions.add(function)
                    else:
                        functions = self._update(function, functions)
                elif 'File' in row[0]:
                    name = row[2]
                    sloc = int(row[3])

                    file_ = File()

                    file_.name = name
                    file_.sloc = sloc

                    if file_ not in files:
                        files.add(file_)
                    else:
                        files = self._update(file_, files)

        if len(functions) > 0:
            logger.debug('Adding {0} functions.'.format(len(functions)))
            Function.objects.bulk_create(functions)
            logger.info('Loaded {0} functions.'.format(len(functions)))

        if len(files) > 0:
            logger.debug('Adding {0} files.'.format(len(files)))
            File.objects.bulk_create(files)
            logger.info('Loaded {0} files.'.format(len(files)))