Exemple #1
0
def test_HTMLparser(config, upload_map):
    with open('tests/data/Test/index.html', 'r') as file:
        contents = file.read()

    parsed = HTMLparser(config, 'Test/index.html', contents, upload_map)

    assert md5hash(parsed) == '3efd614407d7bfac36b2832ea47d2512'
Exemple #2
0
def test_HTMLparser_srcset3(config, upload_map):
    with open('tests/data/Test/html/srcset3.html', 'r') as file:
        contents = file.read()
    parsed = HTMLparser(config, 'Test/html/srcset3.html', contents, upload_map)
    print(parsed)
    assert md5hash(parsed) == '644159856c926d23d6f137ee92a694a3'
Exemple #3
0
def test_HTMLparser_srcset2(config, upload_map):
    with open('tests/data/Test/html/srcset2.html', 'r') as file:
        contents = file.read()
    parsed = HTMLparser(config, 'Test/html/srcset2.html', contents, upload_map)
    print(parsed)
    assert md5hash(parsed) == '767a59b62c5b661d3d0c997b314b83de'
Exemple #4
0
def test_HTMLparser_srcset1(config, upload_map):
    with open('tests/data/Test/html/srcset1.html', 'r') as file:
        contents = file.read()
    parsed = HTMLparser(config, 'Test/html/srcset1.html', contents, upload_map)
    print(parsed)
    assert md5hash(parsed) == '44dc59824574b54e8f5894f2978fab22'
Exemple #5
0
def test_HTMLparser_img(config, upload_map):
    with open('tests/data/Test/html/img.html', 'r') as file:
        contents = file.read()
    parsed = HTMLparser(config, 'Test/html/img.html', contents, upload_map)
    assert md5hash(parsed) == 'ba2d3f459a58bb24113e481a169016b4'
Exemple #6
0
def test_HTMLparser_internal_styles(config, upload_map):
    with open('tests/data/Test/html/internal-styles.html', 'r') as file:
        contents = file.read()
    parsed = HTMLparser(config, 'Test/html/internal-styles.html', contents,
                        upload_map)
    assert md5hash(parsed) == 'f1bf98174c3a74ceb670af98514f069f'
Exemple #7
0
def test_HTMLparser_script(config, upload_map):
    with open('tests/data/Test/html/script.html', 'r') as file:
        contents = file.read()
    parsed = HTMLparser(config, 'index.html', contents, upload_map)
    assert md5hash(parsed) == '727d10aa1f48361ff0fb5236becc3f63'
Exemple #8
0
def test_HTMLparser_link(config, upload_map):
    with open('tests/data/Test/html/link.html', 'r') as file:
        contents = file.read()
    parsed = HTMLparser(config, 'index.html', contents, upload_map)
    assert md5hash(parsed) == '25ec984a0e9889dab0a0986b1eacee09'
def build_and_upload(files, browser, config, upload_map):
    """
    Replaces URLs in files and uploads changed files.

    Arguments:
        files: Custom file cache
        browser: mechanicalsoup.StatefulBrowser instance
        config: Configuration for this run
        upload_map: custom upload map

    Returns:
        Dictionary with no. of 'html', 'css' and 'js' files uploaded
    """

    counter = {
        'html': 0,
        'css': 0,
        'js': 0,
    }

    for file_dictionary in [files['html'], files['css'], files['js']]:
        for path in file_dictionary.keys():
            file_object = file_dictionary[path]
            path_str = str(file_object.path)
            ext = file_object.extension

            # open file
            try:
                with open(file_object.src_path, 'r') as file:
                    contents = file.read()
            except Exception:
                message = f'Could not open/read {file_object.path}. Skipping.'
                logger.error(message)
                continue  # FIXME Can this be improved?

            processed = None  # just so the linter doesn't freak out
            # parse and modify contents
            if ext == 'html':
                processed = HTMLparser(config, file_object.path, contents,
                                       upload_map)
            elif ext == 'css':
                processed = CSSparser(config, file_object.path, contents,
                                      upload_map)
            elif ext == 'js':
                processed = JSparser(contents)

            # calculate and store md5 hash of the modified contents
            build_hash = md5(processed.encode('utf-8')).hexdigest()

            if upload_map[ext][path_str]['md5'] == build_hash:
                message = f'Contents of {file_object.path} have been uploaded previously. Skipping.'
                logger.info(message)
            else:
                upload_map[ext][path_str]['md5'] = build_hash
                build_path = file_object.build_path
                try:
                    # create directory if doesn't exist
                    if not os.path.isdir(build_path.parent):
                        os.makedirs(build_path.parent)
                    # and write the processed contents
                    with open(build_path, 'w') as file:
                        file.write(processed)
                except Exception:
                    message = f"Couldn not write {str(file_object.build_path)}. Skipping."
                    logger.error(message)
                    continue
                    # FIXME Can this be improved?

                # upload
                successful = iGEM_upload_page(browser, processed,
                                              file_object.upload_URL)
                if not successful:
                    message = f'Could not upload {str(file_object.path)}. Skipping.'
                    logger.error(message)
                    continue
                    # FIXME Can this be improved?
                else:
                    counter[ext] += 1

    return counter