calcyfy 3.09 KB
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Script to generate a README from a set of metadata spreadsheets in nested
directories and (TODO) to generate said README files

TODO:
- Keep track of checksums, deleted files
- File previews?
-   Choose to have a README per directory (Currently only does the top level
    with all the data in it)


LICENSE:
    This file is part of Calcyte (c) The University of Technology, Sydney

    Calcyte is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""

import argparse
import configparser
import os.path
import calcyte
from calcyte import metadata
from sys import stderr

#######################################
# Get and parse the program's arguments.
#######################################

parser = argparse.ArgumentParser()
parser.add_argument("dir", default=".", help="Directory to search for metadata.")
parser.add_argument("--recurse", "-r", action="store_true", help="Recurse into directories.")
parser.add_argument("--bagit", "-b", default=None, help="Path to a directory in which to create a bagit bag of the data for distribution.")
parser.add_argument("-force", "-f", action="store_true", help="Force deletion/replacement of output bag directory.")

args = parser.parse_args()

# Check for required argument.
if not os.path.isdir(args.dir):
    print(args.dir, "is not a directory", file=stderr)
    exit()

################################################
# Get any items in a calcyte configuration file.
################################################

# Look for these in the current directory or for the 'catalog_stem' directory.
config = configparser.ConfigParser()
curdir = os.getcwd()

if os.path.isfile(os.path.join(args.dir, 'calcify.conf')):
    # Use the config file found from the args.dir directory.
    config.read(os.path.join(args.dir, 'calcify.conf'))
elif os.path.isfile(os.path.join(curdir, 'calcify.conf')):
    # Use the config file found from the current directory.
    config.read(os.path.join(curdir, 'calcify.conf'))
else:
    # No config file was found. Use defaults as specified below.
    pass


#################
# Do all the work
#################

# Create an object containing all the files, directories and metadata.
record = calcyte.metadata.MetadataRecord(directory=args.dir, 
                                         recurse=args.recurse, 
                                         bagging=args.bagit)
if args.bagit:
    catalog_dir = record.bagit(args.bagit, args.force)
else:
    catalog_dir = args.dir
    
record.to_html(filename =os.path.join(catalog_dir, "CATALOG.html"))