Files
@ 0ea99a6aab68
Branch filter:
Location: rattail-project/rattail/rattail/commands.py
0ea99a6aab68
28.7 KiB
text/x-python
Various changes to allow custom commands to sit in front of non-Rattail database.
In particular some of the Continuum versioning code required protection
from import failures, and various assumptions about the "default" database
session were refactored.
In particular some of the Continuum versioning code required protection
from import failures, and various assumptions about the "default" database
session were refactored.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 | # -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2015 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Rattail 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 Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Console Commands
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import sys
import platform
import argparse
import datetime
import socket
import logging
from getpass import getpass
import edbob
from ._version import __version__
from .util import load_entry_points
from .console import Progress
log = logging.getLogger(__name__)
class ArgumentParser(argparse.ArgumentParser):
"""
Custom argument parser.
This overrides some of the parsing logic which is specific to the primary
command object.
"""
def parse_args(self, args=None, namespace=None):
args, argv = self.parse_known_args(args, namespace)
args.argv = argv
return args
def date_argument(string):
"""
Validate and coerce a date argument.
This function is designed be used as the ``type`` parameter when calling
``ArgumentParser.add_argument()``, e.g.::
parser = ArgumentParser()
parser.add_argument('--date', type=date_argument)
"""
try:
date = datetime.datetime.strptime(string, '%Y-%m-%d').date()
except ValueError:
raise argparse.ArgumentTypeError("Date must be in YYYY-MM-DD format")
return date
class Command(object):
"""
The primary command for the application.
This effectively *is* the ``rattail`` console application. It mostly
provides the structure for subcommands, which really do all the work.
This command is designed to be subclassed, should your application need
similar functionality.
"""
name = 'rattail'
version = __version__
description = "Retail Software Framework"
long_description = """
Rattail is a retail software framework.
Copyright (c) 2010-2012 Lance Edgar <lance@edbob.org>
This program comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
See the file COPYING.txt for more information.
"""
stdout = sys.stdout
stderr = sys.stderr
def __init__(self):
self.subcommands = load_entry_points('{0}.commands'.format(self.name))
def __unicode__(self):
return unicode(self.name)
@property
def db_config_section(self):
"""
Name of section in config file which should have database connection
info. This defaults to ``'rattail.db'`` but may be overridden so the
command framework can sit in front of a non-Rattail database if needed.
This is used to auto-configure a "default" database engine for the app,
when any command is invoked.
"""
return 'rattail.db'
@property
def db_session_factory(self):
"""
Reference to the "primary" ``Session`` class, which will be configured
automatically during app startup. Defaults to :class:`rattail.db.Session`.
"""
from rattail.db import Session
return Session
def iter_subcommands(self):
"""
Iterate over the subcommands.
This is a generator which yields each associated :class:`Subcommand`
class sorted by :attr:`Subcommand.name`.
"""
for name in sorted(self.subcommands):
yield self.subcommands[name]
def print_help(self):
"""
Print help text for the primary command.
The output will include a list of available subcommands.
"""
self.stdout.write("""{0}
Usage: {1} [options] <command> [command-options]
Options:
-c PATH, --config=PATH
Config path (may be specified more than once)
-n, --no-init Don't load config before executing command
-d, --debug Increase logging level to DEBUG
-P, --progress Show progress indicators (where relevant)
-v, --verbose Increase logging level to INFO
-V, --version Display program version and exit
Commands:\n""".format(self.description, self.name))
for cmd in self.iter_subcommands():
self.stdout.write(" {0:<16s} {1}\n".format(cmd.name, cmd.description))
self.stdout.write("\nTry '{0} help <command>' for more help.\n".format(self.name))
def run(self, *args):
"""
Parse command line arguments and execute appropriate subcommand.
"""
parser = ArgumentParser(
prog=self.name,
description=self.description,
add_help=False,
)
parser.add_argument('-c', '--config', action='append', dest='config_paths',
metavar='PATH')
parser.add_argument('-d', '--debug', action='store_true', dest='debug')
parser.add_argument('-n', '--no-init', action='store_true', default=False)
parser.add_argument('-P', '--progress', action='store_true', default=False)
parser.add_argument('--stdout', metavar='PATH', type=argparse.FileType('w'),
help="Optional path to which STDOUT should be effectively redirected.")
parser.add_argument('--stderr', metavar='PATH', type=argparse.FileType('w'),
help="Optional path to which STDERR should be effectively redirected.")
parser.add_argument('-v', '--verbose', action='store_true', dest='verbose')
parser.add_argument('-V', '--version', action='version',
version="%(prog)s {0}".format(self.version))
parser.add_argument('command', nargs='*')
# Parse args and determine subcommand.
args = parser.parse_args(list(args))
if not args or not args.command:
self.print_help()
return
# Show (sub)command help if so instructed, or unknown subcommand.
cmd = args.command.pop(0)
if cmd == 'help':
if len(args.command) != 1:
self.print_help()
return
cmd = args.command[0]
if cmd not in self.subcommands:
self.print_help()
return
cmd = self.subcommands[cmd](parent=self)
cmd.parser.print_help()
return
elif cmd not in self.subcommands:
self.print_help()
return
# Okay, we should be done needing to print help messages. Now it's
# safe to redirect STDOUT/STDERR, if necessary.
if args.stdout:
self.stdout = args.stdout
if args.stderr:
self.stderr = args.stderr
# Basic logging should be established before init()ing.
# Use root logger if setting logging flags.
log = logging.getLogger()
# TODO: Figure out the basic logging pattern we're after here.
# edbob.basic_logging()
logging.basicConfig()
if args.verbose:
log.setLevel(logging.INFO)
if args.debug:
log.setLevel(logging.DEBUG)
# Initialize everything...
config = None
if not args.no_init:
edbob.init(self.name, *(args.config_paths or []))
config = edbob.config
# Command line logging flags should override config.
if args.verbose:
log.setLevel(logging.INFO)
if args.debug:
log.setLevel(logging.DEBUG)
# Configure the default database engine.
try:
from rattail.db.util import configure_session_factory
except ImportError:
pass # assume no sqlalchemy
else:
configure_session_factory(config, section=self.db_config_section,
session_factory=self.db_session_factory)
# Maybe configure Continuum versioning.
if (self.db_config_section == 'rattail.db'
and config.getboolean('rattail.db', 'versioning.enabled')):
from rattail.db.continuum import configure_versioning
configure_versioning(config)
# And finally, do something of real value...
cmd = self.subcommands[cmd](self)
cmd.config = config
cmd.show_progress = args.progress
cmd.progress = Progress if args.progress else None
cmd._run(*(args.command + args.argv))
class Subcommand(object):
"""
Base class for application subcommands.
"""
name = 'UNDEFINED'
description = 'UNDEFINED'
def __init__(self, parent=None, show_progress=None):
self.parent = parent
self.stdout = getattr(parent, 'stdout', sys.stdout)
self.stderr = getattr(parent, 'stderr', sys.stderr)
self.show_progress = show_progress
self.progress = Progress if show_progress else None
self.parser = argparse.ArgumentParser(
prog='{0} {1}'.format(getattr(self.parent, 'name', 'UNDEFINED'), self.name),
description=self.description)
self.add_parser_args(self.parser)
def __repr__(self):
return "Subcommand(name={0})".format(repr(self.name))
def add_parser_args(self, parser):
"""
Configure additional arguments for the subcommand argument parser.
"""
pass
def _run(self, *args):
args = self.parser.parse_args(list(args))
return self.run(args)
def run(self, args):
"""
Run the subcommand logic.
"""
raise NotImplementedError
class AddUser(Subcommand):
"""
Adds a user to the database.
"""
name = 'adduser'
description = "Add a user to the database."
def add_parser_args(self, parser):
parser.add_argument('username',
help="Username for the new account.")
parser.add_argument('-A', '--administrator',
action='store_true',
help="Add the new user to the Administrator role.")
def run(self, args):
from rattail.db import Session
from rattail.db import model
from rattail.db.auth import set_user_password, administrator_role
session = Session()
if session.query(model.User).filter_by(username=args.username).count():
session.close()
self.stderr.write("User '{0}' already exists.\n".format(args.username))
return
passwd = ''
while not passwd:
try:
passwd = getpass("Enter a password for user '{0}': ".format(args.username))
except KeyboardInterrupt:
self.stderr.write("\nOperation was canceled.\n")
return
user = model.User(username=args.username)
set_user_password(user, passwd)
if args.administrator:
user.roles.append(administrator_role(session))
session.add(user)
session.commit()
session.close()
self.stdout.write("Created user: {0}\n".format(args.username))
class DatabaseSyncCommand(Subcommand):
"""
Controls the database synchronization service.
"""
name = 'dbsync'
description = "Manage the database synchronization service"
def add_parser_args(self, parser):
subparsers = parser.add_subparsers(title='subcommands')
start = subparsers.add_parser('start', help="Start service")
start.set_defaults(subcommand='start')
stop = subparsers.add_parser('stop', help="Stop service")
stop.set_defaults(subcommand='stop')
if sys.platform == 'linux2':
parser.add_argument('-p', '--pidfile',
help="Path to PID file", metavar='PATH')
parser.add_argument('-D', '--do-not-daemonize',
action='store_false', dest='daemonize', default=True,
help="Do not daemonize when starting.")
def run(self, args):
from rattail.db.sync import linux as dbsync
if args.subcommand == 'start':
try:
dbsync.start_daemon(self.config, args.pidfile, args.daemonize)
except KeyboardInterrupt:
if not args.daemonize:
self.stderr.write("Interrupted.\n")
else:
raise
elif args.subcommand == 'stop':
dbsync.stop_daemon(self.config, args.pidfile)
class Dump(Subcommand):
"""
Do a simple data dump.
"""
name = 'dump'
description = "Dump data to file."
def add_parser_args(self, parser):
parser.add_argument(
'--output', '-o', metavar='FILE',
help="Optional path to output file. If none is specified, "
"data will be written to standard output.")
parser.add_argument(
'model', help="Model whose data will be dumped.")
def get_model(self):
"""
Returns the module which contains all relevant data models.
By default this returns ``rattail.db.model``, but this method may be
overridden in derived commands to add support for extra data models.
"""
from rattail.db import model
return model
def run(self, args):
from rattail.db import Session
from rattail.db.dump import dump_data
model = self.get_model()
if hasattr(model, args.model):
cls = getattr(model, args.model)
else:
self.stderr.write("Unknown model: {0}\n".format(args.model))
sys.exit(1)
progress = None
if self.show_progress: # pragma no cover
progress = Progress
if args.output:
output = open(args.output, 'wb')
else:
output = self.stdout
session = Session()
dump_data(session, cls, output, progress=progress)
session.close()
if output is not self.stdout:
output.close()
class FileMonitorCommand(Subcommand):
"""
Interacts with the file monitor service; called as ``rattail filemon``.
This command expects a subcommand; one of the following:
* ``rattail filemon start``
* ``rattail filemon stop``
On Windows platforms, the following additional subcommands are available:
* ``rattail filemon install``
* ``rattail filemon uninstall`` (or ``rattail filemon remove``)
.. note::
The Windows Vista family of operating systems requires you to launch
``cmd.exe`` as an Administrator in order to have sufficient rights to
run the above commands.
.. See :doc:`howto.use_filemon` for more information.
"""
name = 'filemon'
description = "Manage the file monitor service"
def add_parser_args(self, parser):
subparsers = parser.add_subparsers(title='subcommands')
start = subparsers.add_parser('start', help="Start service")
start.set_defaults(subcommand='start')
stop = subparsers.add_parser('stop', help="Stop service")
stop.set_defaults(subcommand='stop')
if sys.platform == 'linux2':
parser.add_argument('-p', '--pidfile',
help="Path to PID file.", metavar='PATH')
parser.add_argument('-D', '--do-not-daemonize',
action='store_false', dest='daemonize', default=True,
help="Do not daemonize when starting.")
elif sys.platform == 'win32': # pragma no cover
install = subparsers.add_parser('install', help="Install service")
install.set_defaults(subcommand='install')
install.add_argument('-a', '--auto-start', action='store_true',
help="Configure service to start automatically.")
install.add_argument('-U', '--username',
help="User account under which the service should run.")
remove = subparsers.add_parser('remove', help="Uninstall (remove) service")
remove.set_defaults(subcommand='remove')
uninstall = subparsers.add_parser('uninstall', help="Uninstall (remove) service")
uninstall.set_defaults(subcommand='remove')
def run(self, args):
if sys.platform == 'linux2':
from rattail.filemon import linux as filemon
if args.subcommand == 'start':
filemon.start_daemon(self.config, args.pidfile, args.daemonize)
elif args.subcommand == 'stop':
filemon.stop_daemon(self.config, args.pidfile)
elif sys.platform == 'win32': # pragma no cover
self.run_win32(args)
else:
self.stderr.write("File monitor is not supported on platform: {0}\n".format(sys.platform))
sys.exit(1)
def run_win32(self, args): # pragma no cover
from rattail.win32 import require_elevation
from rattail.win32 import service
from rattail.win32 import users
from rattail.filemon import win32 as filemon
require_elevation()
options = []
if args.subcommand == 'install':
username = args.username
if username:
if '\\' in username:
server, username = username.split('\\')
else:
server = socket.gethostname()
if not users.user_exists(username, server):
sys.stderr.write("User does not exist: {0}\\{1}\n".format(server, username))
sys.exit(1)
password = ''
while password == '':
password = getpass("Password for service user: ").strip()
options.extend(['--username', r'{0}\{1}'.format(server, username)])
options.extend(['--password', password])
if args.auto_start:
options.extend(['--startup', 'auto'])
service.execute_service_command(filemon, args.subcommand, *options)
# If installing with custom user, grant "logon as service" right.
if args.subcommand == 'install' and args.username:
users.allow_logon_as_service(username)
# TODO: Figure out if the following is even required, or if instead we
# should just be passing '--startup delayed' to begin with?
# If installing auto-start service on Windows 7, we should update
# its startup type to be "Automatic (Delayed Start)".
# TODO: Improve this check to include Vista?
if args.subcommand == 'install' and args.auto_start:
if platform.release() == '7':
name = filemon.RattailFileMonitor._svc_name_
service.delayed_auto_start_service(name)
class ImportSubcommand(Subcommand):
"""
Base class for subcommands which use the data importing system.
"""
supports_versioning = True
def add_parser_args(self, parser):
if self.supports_versioning:
parser.add_argument('--no-versioning', action='store_true',
help="Disables versioning during the import. This is "
"intended to be useful e.g. during initial import, where "
"the process can be quite slow even without the overhead "
"of versioning.")
parser.add_argument('--dry-run', action='store_true',
help="Go through the motions and allow logging to occur, "
"but do not actually commit the transaction at the end.")
parser.add_argument('models', nargs='*', metavar='MODEL',
help="One or more models to import. If not specified, "
"then all supported models will be imported.")
def run(self, args):
log.info("begin {0} for data model(s): {1}".format(
self.name, ', '.join(args.models or ["ALL"])))
Session = self.parent.db_session_factory
if self.supports_versioning:
if args.no_versioning:
from rattail.db.continuum import disable_versioning
disable_versioning()
session = Session(continuum_user=self.continuum_user)
else:
session = Session()
self.import_data(args, session)
if args.dry_run:
session.rollback()
log.info("dry run, so transaction was rolled back")
else:
session.commit()
log.info("transaction was committed")
session.close()
@property
def continuum_user(self):
"""
Info needed to assign the Continuum user for the database session.
"""
def import_data(self, args, session):
"""
Custom logic for importing data, to be implemented by subclass.
"""
class ImportCSV(ImportSubcommand):
"""
Import data from a CSV file.
"""
name = 'import-csv'
description = "Import data from a CSV file."
def add_parser_args(self, parser):
super(ImportCSV, self).add_parser_args(parser)
parser.add_argument('importer',
help="Spec string for importer class which should handle the import.")
parser.add_argument('csv_path',
help="Path to the data file which will be imported.")
def import_data(self, args, session):
from rattail.db.importing.providers.csv import make_provider
provider = make_provider(self.config, session, args.importer, data_path=args.csv_path)
data = provider.get_data(progress=self.progress)
affected = provider.importer.import_data(data, provider.supported_fields, 'uuid',
progress=self.progress)
log.info("added or updated {0} {1} records".format(affected, provider.model_name))
class InitializeDatabase(Subcommand):
"""
Creates the initial Rattail tables within a database.
"""
name = 'initdb'
description = "Create initial tables in a database."
def add_parser_args(self, parser):
parser.add_argument('url', metavar='URL',
help="Database engine URL")
def run(self, args):
from sqlalchemy import create_engine
from alembic.util import obfuscate_url_pw
from rattail.db import model
engine = create_engine(args.url)
model.Base.metadata.create_all(engine)
print("Created initial tables for database:")
print(" {0}".format(obfuscate_url_pw(engine.url)))
class LoadHostDataCommand(Subcommand):
"""
Loads data from the Rattail host database, if one is configured.
"""
name = 'load-host-data'
description = "Load data from host database"
def run(self, args):
from .db import get_engines
from .db import load
engines = get_engines(self.config)
if 'host' not in engines:
sys.stderr.write("Host engine URL not configured.\n")
sys.exit(1)
proc = load.LoadProcessor(self.config)
proc.load_all_data(engines['host'], Progress)
class MakeConfigCommand(Subcommand):
"""
Creates a sample configuration file.
"""
name = 'make-config'
description = "Create a configuration file"
def add_parser_args(self, parser):
parser.add_argument('path', default='rattail.conf', metavar='PATH',
help="Path to the new file")
parser.add_argument('-f', '--force', action='store_true',
help="Overwrite an existing file")
def run(self, args):
import os
import os.path
import shutil
from rattail.files import resource_path
dest = os.path.abspath(args.path)
if os.path.exists(dest):
if os.path.isdir(dest):
sys.stderr.write("Path must include the filename; "
"you gave a directory:\n {0}\n".format(dest))
sys.exit(1)
if not args.force:
sys.stderr.write("File already exists "
"(use --force to overwrite):\n "
"{0}\n".format(dest))
sys.exit(1)
os.remove(dest)
src = resource_path('rattail:data/rattail.conf.sample')
shutil.copyfile(src, dest)
class MakeUserCommand(Subcommand):
"""
Creates a system user for Rattail.
"""
name = 'make-user'
description = "Create a system user account for Rattail"
def add_parser_args(self, parser):
parser.add_argument('-U', '--username', metavar='USERNAME', default='rattail',
help="Username for the new user; defaults to 'rattail'.")
parser.add_argument('--full-name', metavar='FULL_NAME',
help="Full (display) name for the new user.")
parser.add_argument('--comment', metavar='COMMENT',
help="Comment string for the new user.")
def run(self, args):
if sys.platform != 'win32':
sys.stderr.write("Sorry, only win32 platform is supported.\n")
sys.exit(1)
from rattail.win32 import users
from rattail.win32 import require_elevation
require_elevation()
if users.user_exists(args.username):
sys.stderr.write("User already exists: {0}\n".format(args.username))
sys.exit(1)
try:
password = None
while not password:
password = getpass(b"Enter a password: ").strip()
except KeyboardInterrupt:
sys.stderr.write("Operation canceled by user.")
sys.exit(2)
users.create_user(args.username, password,
full_name=args.full_name, comment=args.comment)
sys.stdout.write("Created user: {0}\n".format(args.username))
class PalmCommand(Subcommand):
"""
Manages registration for the HotSync Manager conduit; called as::
rattail palm
"""
name = 'palm'
description = "Manage the HotSync Manager conduit registration"
def add_parser_args(self, parser):
subparsers = parser.add_subparsers(title='subcommands')
register = subparsers.add_parser('register', help="Register Rattail conduit")
register.set_defaults(subcommand='register')
unregister = subparsers.add_parser('unregister', help="Unregister Rattail conduit")
unregister.set_defaults(subcommand='unregister')
def run(self, args):
from rattail import palm
from rattail.win32 import require_elevation
from rattail.exceptions import PalmError
require_elevation()
if args.subcommand == 'register':
try:
palm.register_conduit()
except PalmError, error:
sys.stderr.write(str(error) + '\n')
elif args.subcommand == 'unregister':
try:
palm.unregister_conduit()
except PalmError, error:
sys.stderr.write(str(error) + '\n')
class PurgeBatchesCommand(Subcommand):
"""
Purges stale batches from the database; called as:
.. code-block:: sh
rattail purge-batches
"""
name = 'purge-batches'
description = "Purge stale batches from the database"
def add_parser_args(self, parser):
parser.add_argument('-A', '--all', action='store_true',
help="Purge ALL batches regardless of purge date")
parser.add_argument('--date', '-D', type=date_argument,
help="Optional effective date for the purge. If "
"none is specified, the current date is assumed.")
def run(self, args):
from rattail.db import Session
from rattail.db.util import obfuscate_url_pw
from rattail.db.batches import util
log.info("purging batches from database: {0}".format(obfuscate_url_pw(Session.kw['bind'].url)))
normal = util.purge_batches(effective_date=args.date, purge_everything=args.all)
orphaned = util.purge_orphaned_batches()
log.info("purged {0} normal and {1} orphaned batches".format(normal, orphaned))
def main(*args):
"""
The primary entry point for the Rattail command system.
"""
if args:
args = list(args)
else:
args = sys.argv[1:]
cmd = Command()
cmd.run(*args)
|