Mercurial > hrm
changeset 0:d4b4b4445bae
Origination.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Mon, 08 Dec 2008 13:25:11 -0800 |
parents | |
children | cb08fb8f0e87 |
files | kharon.py |
diffstat | 1 files changed, 67 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/kharon.py Mon Dec 08 13:25:11 2008 -0800 @@ -0,0 +1,67 @@ +#! /usr/bin/env python3.0 + +import os +import sys +import json +import subprocess +import distutils.dir_util +from optparse import OptionParser + +HADES_DIR = os.path.expanduser('~/.Hades') +STATE_FILENAME = os.path.join(HADES_DIR, 'state.json') + +class Config(object): + def __init__(self, filename): + self.__filename = filename + self.nextid = 0 + if os.path.exists(self.__filename): + self.__dict__.update(json.load(open(self.__filename, 'r'))) + else: + self.save() + + def save(self): + state = {} + keys = [key for key in self.__dict__ + if not key.startswith('__')] + for key in keys: + state[key] = self.__dict__[key] + json.dump(state, open(self.__filename, 'w')) + +def shell(*params): + popen = subprocess.Popen(params) + popen.wait() + if popen.returncode: + raise Exception('Process failed: %s' % repr(params)) + +if __name__ == '__main__': + if not (os.path.exists(HADES_DIR) and os.path.isdir(HADES_DIR)): + shell('mkdir', HADES_DIR) + + parser = OptionParser() + (options, args) = parser.parse_args() + + if not args: + parser.print_help() + sys.exit(-1) + + config = Config(STATE_FILENAME) + files = [] + for arg in args: + filename = os.path.abspath(os.path.expanduser(arg)) + if not os.path.exists(filename): + print('File does not exist: %s' % arg) + sys.exit(-1) + files.append(filename) + + thisid = config.nextid + basedir = os.path.join(HADES_DIR, '%.9d' % thisid) + shell('mkdir', basedir) + config.nextid = thisid + 1 + config.save() + + print("Creating transaction %d." % thisid) + + for source in files: + dest = os.path.join(basedir, filename[1:]) + distutils.dir_util.mkpath(os.path.dirname(dest)) + shell('mv', source, dest)