artefactual / automation-tools

Tools to aid automation of Archivematica and AtoM.
GNU Affero General Public License v3.0
46 stars 33 forks source link

transfers: add config file for log/db/pid file paths #18

Closed hakamine closed 8 years ago

hakamine commented 8 years ago

Avoid creating log/db/pid files in the code path:

Hwesta commented 8 years ago

A bit of minor feedback, otherwise :+1:

hakamine commented 8 years ago

I think it's ready now. I had to move things around a little (such as putting the code of models.py inside a function, and taking ArgumentParser related code out of main in transfers.py) in order to allow the scripts to get config parameters from a config file specified in the command line. I did a few tests, seems to be working fine so far. The README has also been updated, added new information on installation, and how to set up multiple instances.

Hwesta commented 8 years ago

It's promising, but I have a few Python best practices/style suggestions. The docs work looks great!

A few general Python tips:

So, some suggestions for fixing this:

Some suggestions for what this might look like

def get_setting(setting, default=None):
    config = configparser.SafeConfigParser()
    config.read(CONFIG_FILE)
    config.get(setting, default)

# Example:
pid_file = get_setting('pidfile')
# At the top of the file
CONFIG_FILE = None

# Later...
def setup(config_file):
    global CONFIG_FILE
    CONFIG_FILE = config_file
    init(get_setting('database'))  # This is models.init

    # Configure logging
    default_logfile = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'automate-transfer.log')
    CONFIG = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'default': {
                'format': '%(levelname)-8s  %(asctime)s  %(filename)s:%(lineno)-4s %(message)s',
                'datefmt': '%Y-%m-%d %H:%M:%S',
            },
        },
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
                'formatter': 'default',
            },
            'file': {
                'class': 'logging.handlers.RotatingFileHandler',
                'formatter': 'default',
                'filename': get_setting('logfile', default_logfile),
                'backupCount': 2,
                'maxBytes': 10 * 1024,
            },
        },
        'loggers': {
            'transfer': {
                'level': 'INFO',  # One of INFO, DEBUG, WARNING, ERROR, CRITICAL
                'handlers': ['console', 'file'],
            },
        },
    }
    logging.config.dictConfig(CONFIG)
    # Other parsing code here...
    parser.add_argument('-c', '--config-file', help='Configuration file (log/db/PID files)', default='/etc/archivematica/automation-tools/transfers.conf')
    args = parser.parse_args()

    setup(args.config_file)
    # Rest of function...

Let me know if you need any clarifications about what I meant or what would be better.

hakamine commented 8 years ago

Thank you for all the advice and suggestions. Made changes following those, code looks more readable now (and is hopefully ready for merging)

Hwesta commented 8 years ago

Looks much better now, thanks! A couple of minor comments again, and you can probably squash the refactor commit into the initial one if you want.

hakamine commented 8 years ago

As mentioned in http://stackoverflow.com/questions/11536764/attempted-relative-import-in-non-package-even-with-init-py there is an issue invoking python scripts when relative imports are used (from . import models). Using the -m flag in python is a workaround that seems to fix this problem. README has been updated with this information (so hopefully it's ready to merge this time)

Hwesta commented 8 years ago

Looks great! Good find with the -m. :+1: