Exemplo n.º 1
0
    def setUp(self):
        self.es_url = 'http://localhost:9200'
        self.start = parser.parse('1900-01-01')
        self.end = parser.parse('2100-01-01')
        self.data_sources = ['git']

        self.report = Report(self.es_url,
                             self.start,
                             self.end,
                             data_sources=self.data_sources)
Exemplo n.º 2
0
    def execute(self):
        cfg = self.conf

        global_project = cfg['general']['short_name']
        elastic = cfg['es_enrichment']['url']
        data_dir = cfg['report']['data_dir']
        end_date = cfg['report']['end_date']
        start_date = cfg['report']['start_date']
        offset = cfg['report']['offset']
        config_file = cfg['report']['config_file']
        interval = cfg['report']['interval']
        filters = cfg['report']['filters']

        if not os.path.exists(data_dir):
            os.makedirs(data_dir)

        # All the dates must be UTC, including those from command line
        if end_date == 'now':
            end_date = parser.parse(
                date.today().strftime('%Y-%m-%d')).replace(tzinfo=timezone.utc)
        else:
            end_date = parser.parse(end_date).replace(tzinfo=timezone.utc)
        # The end date is not included, the report must finish the day before
        end_date += timedelta(microseconds=-1)
        start_date = parser.parse(start_date).replace(tzinfo=timezone.utc)

        offset = offset if offset else None

        report = Report(elastic,
                        start=start_date,
                        end=end_date,
                        data_dir=data_dir,
                        filters=Report.get_core_filters(filters),
                        interval=interval,
                        offset=offset,
                        config_file=config_file)
        report.create()

        # First copy the reports template to create a new report from it
        tmp_path = tempfile.mkdtemp(prefix='report_')
        copy_tree("reports/report_template", tmp_path)
        # Copy the data generated to be used in LaTeX template
        copy_tree("report_data", os.path.join(tmp_path, "data"))
        copy_tree("report_data", os.path.join(tmp_path, "figs"))
        # Change the project global name
        cmd = ['sed -i s/TemplateProject/' + global_project + '/g *.tex']
        subprocess.call(cmd, shell=True, cwd=tmp_path)
        # Fix LaTeX special chars
        cmd = [r'sed -i "s/\&/\\\&/g" data/git_top_organizations_*']
        subprocess.call(cmd, shell=True, cwd=tmp_path)
        # Build the report
        subprocess.call("make", shell=True, cwd=tmp_path)
        # Copy the report
        copy_tree(os.path.join(tmp_path, "pdf"), "report_data/pdf")
        shutil.rmtree(tmp_path)
Exemplo n.º 3
0
    def execute(self):
        cfg = self.conf

        global_project = cfg['general']['short_name']
        elastic = cfg['es_enrichment']['url']
        data_dir = cfg['report']['data_dir']
        end_date = cfg['report']['end_date']
        start_date = cfg['report']['start_date']
        offset = cfg['report']['offset']
        config_file = cfg['report']['config_file']
        interval = cfg['report']['interval']
        filters = cfg['report']['filters']

        if not os.path.exists(data_dir):
            os.makedirs(data_dir)

        # All the dates must be UTC, including those from command line
        if end_date == 'now':
            end_date = parser.parse(date.today().strftime('%Y-%m-%d')).replace(tzinfo=timezone.utc)
        else:
            end_date = parser.parse(end_date).replace(tzinfo=timezone.utc)
        # The end date is not included, the report must finish the day before
        end_date += timedelta(microseconds=-1)
        start_date = parser.parse(start_date).replace(tzinfo=timezone.utc)

        offset = offset if offset else None

        report = Report(elastic, start=start_date,
                        end=end_date, data_dir=data_dir,
                        filters=Report.get_core_filters(filters),
                        interval=interval,
                        offset=offset,
                        config_file=config_file)
        report.create()

        # First copy the reports template to create a new report from it
        tmp_path = tempfile.mkdtemp(prefix='report_')
        copy_tree("reports/report_template", tmp_path)
        # Copy the data generated to be used in LaTeX template
        copy_tree("report_data", os.path.join(tmp_path, "data"))
        copy_tree("report_data", os.path.join(tmp_path, "figs"))
        # Change the project global name
        cmd = ['sed -i s/TemplateProject/' + global_project + '/g *.tex']
        subprocess.call(cmd, shell=True, cwd=tmp_path)
        # Fix LaTeX special chars
        cmd = [r'sed -i "s/\&/\\\&/g" data/git_top_organizations_*']
        subprocess.call(cmd, shell=True, cwd=tmp_path)
        # Build the report
        subprocess.call("make", shell=True, cwd=tmp_path)
        # Copy the report
        copy_tree(os.path.join(tmp_path, "pdf"), "report_data/pdf")
        shutil.rmtree(tmp_path)
Exemplo n.º 4
0
    def test_initialization(self):
        """Test whether attributes are initializated"""

        es_url = None
        start = None
        end = None

        with self.assertRaises(TypeError):
            report = Report()

        with self.assertRaises(SystemExit):
            report = Report(es_url, start, end)

        es_url = 'http://localhost:9200'
        start = parser.parse('1900-01-01')
        end = parser.parse('2100-01-01')
        data_sources = ['git']

        report = Report(es_url, start, end, data_sources=data_sources)
Exemplo n.º 5
0
    def test_period_name(self):
        """
        Test whether the period name for a date is build correctly
        :return:
        """

        period_name = "18-Q1"

        # The data is the next period date
        period_date = parser.parse('2018-04-01')
        self.assertEqual(Report.build_period_name(period_date), period_name)

        # The data is the start of a period
        period_date = parser.parse('2018-01-01')
        self.assertEqual(
            Report.build_period_name(period_date, start_date=True),
            period_name)

        # The period is not a quarter
        with self.assertRaises(RuntimeError):
            Report.build_period_name(period_date, interval="day")
Exemplo n.º 6
0
class TestReport(unittest.TestCase):
    """Basic tests for the Report class """
    def setUp(self):
        self.es_url = 'http://localhost:9200'
        self.start = parser.parse('1900-01-01')
        self.end = parser.parse('2100-01-01')
        self.data_sources = ['git']

        self.report = Report(self.es_url,
                             self.start,
                             self.end,
                             data_sources=self.data_sources)

    def test_initialization(self):
        """Test whether attributes are initializated"""

        es_url = None
        start = None
        end = None

        with self.assertRaises(TypeError):
            report = Report()

        with self.assertRaises(SystemExit):
            report = Report(es_url, start, end)

        es_url = 'http://localhost:9200'
        start = parser.parse('1900-01-01')
        end = parser.parse('2100-01-01')
        data_sources = ['git']

        report = Report(es_url, start, end, data_sources=data_sources)

    def test_replace_text_dir(self):
        """Test whether we can replace one string with another
        string in all the files containing the first string"""

        # -----Setting up variables to test the function-----
        # create a temp directory
        temp_path = tempfile.mkdtemp(prefix='manuscripts_')

        current_dir = os.path.dirname(os.path.abspath(__file__))
        # path to where the original report.tar.gz file is stored
        report_path = os.path.join(current_dir, 'data/test_report.tar.gz')

        # extract the file into the temp directory
        subprocess.check_call(['tar', '-xzf', report_path, '-C', temp_path])
        temp_report_path = os.path.join(temp_path, 'test_report')

        report_file1 = os.path.join(temp_report_path, 'report_file1.tex')
        report_file2 = os.path.join(temp_report_path, 'report_file2.tex')
        report_file3 = os.path.join(temp_report_path, 'report_file3.tex')
        data_file1 = os.path.join(temp_report_path, 'data/data_file1.tex')
        data_file2 = os.path.join(temp_report_path, 'data/data_file2.tex')

        # files that will be changed when replace_text_dir function is called
        tex_files_to_change = [report_file1, report_file2, data_file1]

        # files that should not change when replace_text_dir function is called
        tex_files_not_to_change = [report_file3, data_file2]

        # -----Test the function-----

        self.report.replace_text_dir(temp_report_path,
                                     "Open Source is Awesome!",
                                     "REPLACED-TEXT")
        self.report.replace_text_dir(temp_report_path,
                                     "Open Source is Awesome!",
                                     "REPLACED-TEXT", "data/*.tex")

        for file in tex_files_to_change:
            with open(file, 'r') as f:
                self.assertIn("REPLACED-TEXT", f.read())

        for file in tex_files_not_to_change:
            with open(file, 'r') as f:
                self.assertNotIn("REPLACED-TEXT", f.read())

        # -----Cleaning up-----
        shutil.rmtree(temp_path)

    def test_period_name(self):
        """
        Test whether the period name for a date is build correctly
        :return:
        """

        period_name = "18-Q1"

        # The data is the next period date
        period_date = parser.parse('2018-04-01')
        self.assertEqual(Report.build_period_name(period_date), period_name)

        # The data is the start of a period
        period_date = parser.parse('2018-01-01')
        self.assertEqual(
            Report.build_period_name(period_date, start_date=True),
            period_name)

        # The period is not a quarter
        with self.assertRaises(RuntimeError):
            Report.build_period_name(period_date, interval="day")

    def tearDown(self):
        pass