Exemple #1
0
    def test_rename_files(self):
        expected = [
            (('output/dir1/sub1/invalid*.txt',
              'output/dir1/sub1/invalid_star_.txt'), {}),
            (('output/dir1/sub2/Conflict.html',
              'output/dir1/sub2/Conflict.2.html'), {}),
            (('output/dir1/sub3/confl"ict".html',
              'output/dir1/sub3/confl_q_ict_q_.html'), {}),
            (('output/dir1/sub3/Confl"ict".html',
              'output/dir1/sub3/Confl_q_ict_q_.2.html'), {}),
            (('output/dir1/sub4/Conflict', 'output/dir1/sub4/Conflict.2'), {}),
            (('output/dir1/sub4/conFlict', 'output/dir1/sub4/conFlict.3'), {}),
            (('output/dir1/sub5/Conflict', 'output/dir1/sub5/Conflict.2'), {}),
            (('output/dir1/sub6/load.php?modules=site&only=scripts',
              'output/dir1/sub6/site_scripts.js'), {}),
            (('output/dir1/sub6/load.php?modules=someext&only=styles',
              'output/dir1/sub6/ext.css'), {})
        ]

        actual = []

        def record_call(*args, **kwargs):
            actual.append((args, kwargs))

        with unittest.mock.patch('os.walk') as walk, \
                unittest.mock.patch('shutil.move') as move:
            walk.return_value = self.make_walk_result('output')
            move.side_effect = record_call

            with nostdout():
                rename_files('output', self.make_rename_map('output'))

        self.assertEqual(expected,
                         actual,
                         msg="Unexpected sequence of calls to shutil.move")
Exemple #2
0
def main():
    parser = argparse.ArgumentParser(prog='preprocess.py')
    parser.add_argument('--src',
                        type=str,
                        help='Source directory where raw website copy resides')

    parser.add_argument(
        '--dst',
        type=str,
        help='Destination folder to put preprocessed archive to')
    args = parser.parse_args()

    root = args.dst
    src = args.src

    # copy the source tree
    preprocess.rmtree_if_exists(root)
    shutil.copytree(src, root)

    preprocess.rearrange_archive(root)

    rename_map = preprocess.build_rename_map(root)
    preprocess.rename_files(root, rename_map)

    # clean the html files
    file_list = preprocess.find_html_files(root)

    with concurrent.futures.ProcessPoolExecutor() as executor:
        futures = [
            executor.submit(preprocess.preprocess_html_file, root, fn,
                            rename_map) for fn in file_list
        ]

        for future in futures:
            output = future.result()
            if len(output) > 0:
                print(output)

    # append css modifications

    with open("preprocess-css.css", "r", encoding='utf-8') as pp, \
            open(os.path.join(root, 'common/site_modules.css'), "a",
                 encoding='utf-8') as out:
        out.writelines(pp)

    # clean the css files

    for fn in [
            os.path.join(root, 'common/site_modules.css'),
            os.path.join(root, 'common/ext.css')
    ]:
        preprocess.preprocess_css_file(fn)

    preprocess.preprocess_startup_script(
        os.path.join(root, 'common/startup_scripts.js'))
    def test_rename_files(self):
        expected = [
            (('output/dir1/sub1/invalid*.txt',
              'output/dir1/sub1/invalid_star_.txt'), {}),
            (('output/dir1/sub2/Conflict.html',
              'output/dir1/sub2/Conflict.2.html'), {}),
            (('output/dir1/sub3/confl"ict".html',
              'output/dir1/sub3/confl_q_ict_q_.html'), {}),
            (('output/dir1/sub3/Confl"ict".html',
              'output/dir1/sub3/Confl_q_ict_q_.2.html'), {}),
            (('output/dir1/sub4/Conflict',
              'output/dir1/sub4/Conflict.2'), {}),
            (('output/dir1/sub4/conFlict',
              'output/dir1/sub4/conFlict.3'), {}),
            (('output/dir1/sub5/Conflict',
              'output/dir1/sub5/Conflict.2'), {}),
            (('output/dir1/sub6/load.php?modules=site&only=scripts',
              'output/dir1/sub6/site_scripts.js'), {}),
            (('output/dir1/sub6/load.php?modules=someext&only=styles',
              'output/dir1/sub6/ext.css'), {})
        ]

        actual = []

        def record_call(*args, **kwargs):
            actual.append((args, kwargs))

        with unittest.mock.patch('os.walk') as walk, \
                unittest.mock.patch('shutil.move') as move:
            walk.return_value = self.make_walk_result('output')
            move.side_effect = record_call

            with nostdout():
                rename_files('output', self.make_rename_map('output'))

        self.assertEqual(expected, actual,
                         msg="Unexpected sequence of calls to shutil.move")