def copy_folder(src, dest):
    _, leaf_folder = os.path.split(src)
    shutil.copytree(src, os.path.join(dest, leaf_folder))
    if os.path.exists(dest):
        success("Folder \"%s\" copied to target directory: \"%s\"" %
                (src, dest))
    else:
        failure("Failed copying folder \"%s\" to \"%s\"" % (src, dest))
def generate_representation_mets_files(package_path, package_name):
    # generate mets for each representation in representations folder
    reps_path = os.path.join(package_path, 'representations')
    if not os.path.exists(reps_path):
        failure(
            "Representation directory missing: %s\nPlease add a folder 'representations' to the corresponding representation folder"
            % reps_path)
    for name in os.listdir(reps_path):
        rep_path = os.path.join(reps_path, name)
        if os.path.isdir(rep_path):
            print "Operation: generating representation METS file of representation: %s" % name
            mets_data = {
                'packageid': package_name,
                'type': 'SIP',
                'schemas': os.path.join(rep_path, 'schemas'),
                'parent': ''
            }
            metsgen = MetsGenerator(rep_path)
            metsgen.createMets(mets_data)
            verify_file_created("Representation METS",
                                os.path.join(rep_path, 'METS.xml'))
        else:
            failure("Not a representation directory: %s" % rep_path)
def verify_file_created(file_type, file_path):
    if os.path.exists(file_path):
        success("%s file created successfully: %s" % (file_type, file_path))
    else:
        failure("Error creating %s file: %s" % (file_type, file_path))
def create_folder(package_folder):
    os.makedirs(package_folder)
    if os.path.exists(package_folder):
        success("Folder created in target directory: %s" % package_folder)
    else:
        failure("Failed creating package folder: %s" % package_folder)
    # prepare test execution
    print_headline("Test setup")
    # unpackage test data if it does not exist in the working directory
    if not os.path.exists(RootPackageDirectory):
        unzip = Unzip()
        unzip.extract_with_report(test_package, working_dir)
        print "Test package %s extracted to working directory %s" % (
            test_package, working_dir)
    # delete result directory
    if os.path.exists(ResultDirectory):
        shutil.rmtree(ResultDirectory)
        print "Existing result directory deleted: %s" % ResultDirectory
    # result directory must not exist
    if os.path.exists(ResultDirectory) and len(
            os.listdir(ResultDirectory)) > 0:
        failure("Error: destination directory is not empty.")
    # create result directory if it does not exist
    if not os.path.exists(ResultDirectory):
        os.makedirs(ResultDirectory)
        if os.path.exists(ResultDirectory):
            success("Target directory created: %s" % ResultDirectory)

    # start process on root folder
    print_headline("SIP generation")
    root_parent_dir, root_package_name = os.path.split(RootPackageDirectory)
    print "Start processing package %s using root folder: %s" % (
        root_package_name, root_parent_dir)
    process_package_folder(root_parent_dir, root_package_name)
    print_headline("Packaging")
    package_sips(ResultDirectory)