Skip to content

Commit

Permalink
Clean up pylint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
WorldMaker committed Sep 13, 2017
1 parent 80028ec commit 3731101
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 111 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ docs/_build/

# PyBuilder
target/
.vscode/.ropeproject/
9 changes: 9 additions & 0 deletions musdex/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
"""
musdex
------
musdex is a VCS-aware zip archive tool
Current documentation site: http://pythonhosted.org/musdex/
"""

if __name__ == '__main__':
import os
import sys
Expand Down
28 changes: 23 additions & 5 deletions musdex/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,22 @@
# Copyright 2010 Max Battcher. Some rights reserved.
# Licensed for use under the Ms-RL. See attached LICENSE file.

"""
musdex
------
musdex is a VCS-aware zip archive tool
Current documentation site: http://pythonhosted.org/musdex/
"""

def main(booznik=False):
"""
Parse command line arguments and pass to individual commands
Booznik is a "reversed spell" in Zork lore and used to refer to when main
has been called from the command line as "xedsum" rather than "musdex".
"""
try:
from .config import load_config
except ImportError:
Expand All @@ -27,19 +42,19 @@ def main(booznik=False):

parser_extract = subparsers.add_parser('extract')
parser_extract.add_argument('--force', '-f', action="store_true",
default=False)
default=False)
parser_extract.add_argument('archive', nargs='*')
parser_extract.set_defaults(func=commands.extract)

parser_combine = subparsers.add_parser('combine')
parser_combine.add_argument('--force', '-f', action="store_true",
default=False)
default=False)
parser_combine.add_argument('archive', nargs='*')
parser_combine.set_defaults(func=commands.combine)

parser_add = subparsers.add_parser('add')
parser_add.add_argument('--new', '-n', action="store_true",
default=False)
default=False)
parser_add.add_argument('--handler')
parser_add.add_argument('archive', nargs='+')
parser_add.set_defaults(func=commands.add)
Expand All @@ -49,7 +64,7 @@ def main(booznik=False):
parser_remove.set_defaults(func=commands.remove)

args = sys.argv[1:]
if len(args) == 0:
if not any(args):
args = ['extract'] if not booznik else ['combine']
args = parser.parse_args(args)
if args.verbose:
Expand All @@ -60,6 +75,9 @@ def main(booznik=False):
args.func(args, config)

def xedsum():
"""
Boozniked entry point to musdex
"""
main(True)

if __name__ == "__main__":
Expand Down
111 changes: 67 additions & 44 deletions musdex/commands.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# VCS routines for musdex
#
"""
musdex commands
"""
# Copyright 2010 Max Battcher. Some rights reserved.
# Licensed for use under the Ms-RL. See attached LICENSE file.
import datetime
Expand All @@ -15,17 +16,20 @@
from .handlers import get_handler
from . import vcs

def _mtime(F):
return datetime.datetime(*time.localtime(os.path.getmtime(F))[:6])
def _mtime(filename):
return datetime.datetime(*time.localtime(os.path.getmtime(filename))[:6])

def add(args, config):
"""
Add a file for tracking by musdex
"""
index = load_index(config)

for archive in args.archive:
archive = os.path.relpath(archive)
if 'archives' in config \
and any(arc['filename'] == archive for arc in config['archives']):
logging.warn("Archive already configured: %s" % archive)
logging.warning("Archive already configured: %s", archive)
continue

handler = get_handler(args.handler)
Expand All @@ -35,29 +39,35 @@ def add(args, config):
if not os.path.exists(arcloc):
os.makedirs(arcloc)
vcs.add_file(config, arcloc)
for f, t in arch.combine(force=True):
index[f] = t
for filename, timestamp in arch.combine(force=True):
index[filename] = timestamp
else:
if not arch.check():
logging.error("Archive not supported by given handler: %s: %s" % (
args.handler, archive))
logging.error("Archive not supported by given handler: %s: %s",
args.handler, archive)
continue

logging.info("Extracting archive for the first time: %s" % archive)
logging.info("Extracting archive for the first time: %s", archive)
files = arch.extract(force=True)
for f, t in files:
index[f] = t
if f != arcloc: vcs.add_file(config, f)
for filename, timestamp in files:
index[filename] = timestamp
if filename != arcloc:
vcs.add_file(config, filename)

entry = {'filename': archive}
if args.handler: entry['handler'] = args.handler
if 'archives' not in config: config['archives'] = []
if args.handler:
entry['handler'] = args.handler
if 'archives' not in config:
config['archives'] = []
config['archives'].append(entry)

save_config(args, config)
save_index(config, index)

def remove(args, config):
"""
Remove a file from consideration by musdex
"""
index = load_index(config)

if 'archives' not in config:
Expand All @@ -71,24 +81,27 @@ def remove(args, config):
arcloc = os.path.join(BASEDIR, archive)

if not any(arc['filename'] == archive for arc in config['archives']):
logging.warn("Archive not configured: %s" % archive)
logging.warning("Archive not configured: %s", archive)
continue

logging.info("Removing archive files from VCS.")
for f in manifest:
if f.startswith(arcloc):
vcs.remove_file(config, f)
for filename in manifest:
if filename.startswith(arcloc):
vcs.remove_file(config, filename)

if f in index:
del index[f]
if filename in index:
del index[filename]

config['archives'] = [arc for arc in config['archives'] \
if arc['filename'] != archive]

save_config(args, config)
save_index(config, index)

def extract(args, config):
"""
Extract musdex tracked archive files
"""
index = load_index(config)
index_updated = False

Expand All @@ -98,7 +111,8 @@ def extract(args, config):
fmts = [(re.compile(regex), get_formatter(fname)) \
for regex, fname in config['post_extract']]

if args.archive: args.archive = map(os.path.relpath, args.archive)
if args.archive:
args.archive = map(os.path.relpath, args.archive)

manifest = vcs.manifest(config)

Expand All @@ -120,27 +134,34 @@ def extract(args, config):
handler = get_handler(hname)
arch = handler(arcf, arcloc, manifest=arcman)
files = arch.extract(force=args.force or arcloc not in index)
if files: index_updated = True
if files:
index_updated = True

for f, t in files:
if t is None: # File was removed
del index[f]
vcs.remove_file(config, f)
for filename, timestamp in files:
if timestamp is None: # File was removed
del index[filename]
vcs.remove_file(config, filename)
continue
index[f] = t
index[filename] = timestamp
for regex, fmt in fmts: # post-extract formatters
if regex.match(f):
logging.debug("Post-extraction: %s(%s)" % (fmt, f))
fmt(f)
if f != arcloc and f not in arcman: vcs.add_file(config, f)

if index_updated: save_index(config, index)
if regex.match(filename):
logging.debug("Post-extraction: %s(%s)", fmt, filename)
fmt(filename)
if filename != arcloc and filename not in arcman:
vcs.add_file(config, filename)

if index_updated:
save_index(config, index)

def combine(args, config):
"""
Combine musdex tracked index files
"""
index = load_index(config)
index_updated = False

if args.archive: args.archive = map(os.path.relpath, args.archive)
if args.archive:
args.archive = map(os.path.relpath, args.archive)

manifest = vcs.manifest(config)

Expand All @@ -153,7 +174,7 @@ def combine(args, config):
arcman = dict((f, index[f] if f in index else None) \
for f in manifest if f.startswith(arcloc))

logging.debug("Checking modification times for %s" % arcf)
logging.debug("Checking modification times for %s", arcf)
# Unless forced or first-time combination, we do a quick sanity
# check to see if any of the archive's files have changed
if not args.force and arcloc in index and not \
Expand All @@ -164,24 +185,26 @@ def combine(args, config):
bakfilename = None
if ('backup' not in config or config['backup']) \
and os.path.exists(arcf):
logging.debug('Backing up %s' % arcf)
logging.debug('Backing up %s', arcf)
bakfilename = arcf + '.bak~'
shutil.copyfile(arcf, bakfilename)

hname = archive['handler'] if 'handler' in archive else None
handler = get_handler(hname)
arch = handler(arcf, arcloc, manifest=arcman)
files = arch.combine(force=args.force or arcloc not in index)
if files: index_updated = True
if files:
index_updated = True

for f, t in files:
index[f] = t
for filename, timestamp in files:
index[filename] = timestamp

if bakfilename is not None and ('leave_backups' not in config
or not config['leave_backups']):
logging.debug('Removing backup %s' % bakfilename)
or not config['leave_backups']):
logging.debug('Removing backup %s', bakfilename)
os.remove(bakfilename)

if index_updated: save_index(config, index)
if index_updated:
save_index(config, index)

# vim: ai et ts=4 sts=4 sw=4
57 changes: 35 additions & 22 deletions musdex/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Configuration system for musdex
#
"""
Configuration system for musdex
"""
# Copyright 2010 Max Battcher. Some rights reserved.
# Licensed for use under the Ms-RL. See attached LICENSE file.
import logging
Expand All @@ -14,55 +15,67 @@
DEFAULT_INDEX = os.path.join(BASEDIR, ".musdex.index.yaml")

def load_config(args):
"""
Load configuration file
"""
conf = DEFAULT_CONFIG
if args.config:
conf = args.config
logging.debug("Loading configuration from %s" % conf)
logging.debug("Loading configuration from %s", conf)
if not os.path.exists(conf):
logging.info("No configuration file found at %s" % conf)
logging.info("No configuration file found at %s", conf)
return {}
f = open(conf, 'r')
config = yaml.load(f)
f.close()
configfile = open(conf, 'r')
config = yaml.load(configfile)
configfile.close()
return config

def save_config(args, config):
"""
Save configuration file
"""
conf = DEFAULT_CONFIG
if args.config:
conf = args.config
logging.debug("Saving configuration to %s" % conf)
logging.debug("Saving configuration to %s", conf)
confdir = os.path.dirname(conf)
if confdir and not os.path.exists(confdir):
logging.info("Config directory does not exist: %s" % confdir)
logging.info("Config directory does not exist: %s", confdir)
os.makedirs(confdir)
new_conf = not os.path.exists(conf)
f = open(conf, 'w')
yaml.dump(config, f)
f.close()
configfile = open(conf, 'w')
yaml.dump(config, configfile)
configfile.close()
if new_conf:
logging.info("Adding new configuration file to vcs: %s" % conf)
logging.info("Adding new configuration file to vcs: %s", conf)
vcs.add_file(config, conf)

# TODO: Perhaps the index should instead be a less verbose format?
def load_index(config):
"""
Load the index information (timestamp cache manifest)
"""
index = config['index'] if 'index' in config else DEFAULT_INDEX
if os.path.exists(index):
logging.debug("Loading existing index: %s" % index)
f = open(index, 'r')
index = yaml.load(f)
f.close()
logging.debug("Loading existing index: %s", index)
indexfile = open(index, 'r')
index = yaml.load(indexfile)
indexfile.close()
return index
return {}

def save_index(config, index):
"""
Save the index information (timestamp cache manifest)
"""
idx = config['index'] if 'index' in config else DEFAULT_INDEX
logging.debug("Saving index: %s" % idx)
logging.debug("Saving index: %s", idx)
idxdir = os.path.dirname(idx)
if idxdir and not os.path.exists(idxdir):
logging.info("Index directory does not exist: %s" % idxdir)
logging.info("Index directory does not exist: %s", idxdir)
os.makedirs(idxdir)
f = open(idx, 'w')
yaml.dump(index, f)
f.close()
indexfile = open(idx, 'w')
yaml.dump(index, indexfile)
indexfile.close()

# vim: ai et ts=4 sts=4 sw=4
Loading

0 comments on commit 3731101

Please sign in to comment.