~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to scripts/process-upload.py

  • Committer: Barry Warsaw
  • Date: 2007-03-23 17:08:13 UTC
  • mfrom: (3995 launchpad)
  • mto: This revision was merged to the branch mainline in revision 3998.
  • Revision ID: barry@canonical.com-20070323170813-45d0qlcvbazx0p2x
rocketfuel merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
an upload to a distro/whatever within the launchpad.
6
6
"""
7
7
 
 
8
import os
8
9
import _pythonpath
9
10
 
10
 
import os
11
 
import sys
12
 
from optparse import OptionParser
13
 
 
14
 
from contrib.glock import GlobalLock, LockAlreadyAcquired
15
 
 
16
11
from canonical.archivepublisher.uploadpolicy import policy_options
17
12
from canonical.archivepublisher.uploadprocessor import UploadProcessor
18
13
from canonical.config import config
19
 
from canonical.launchpad.scripts import (
20
 
    execute_zcml_for_scripts, logger, logger_options)
21
 
from canonical.lp import initZopeless
22
 
 
23
 
 
24
 
_default_lockfile = '/var/lock/process-upload.lock'
25
 
 
26
 
 
27
 
def main():
28
 
    options = readOptions()
29
 
    log = logger(options, "process-upload")
30
 
 
31
 
    locker = GlobalLock(_default_lockfile, logger=log)
32
 
    try:
33
 
        locker.acquire()
34
 
    except LockAlreadyAcquired:
35
 
        log.error("Cannot acquire lock.")
36
 
        return 1
37
 
 
38
 
    log.debug("Initialising connection.")
39
 
    ztm = initZopeless(dbuser=config.uploader.dbuser)
40
 
    execute_zcml_for_scripts()
41
 
 
42
 
    try:
43
 
        UploadProcessor(options, ztm, log).processUploadQueue()
44
 
    finally:
45
 
        locker.release()
46
 
 
47
 
    return 0
48
 
 
49
 
 
50
 
def readOptions():
51
 
    """Read the command-line options and return an options object."""
52
 
    parser = OptionParser()
53
 
    logger_options(parser)
54
 
    policy_options(parser)
55
 
 
56
 
    parser.add_option("-N", "--dry-run", action="store_true",
57
 
                      dest="dryrun", metavar="DRY_RUN", default=False,
58
 
                      help=("Whether to treat this as a dry-run or not. "
59
 
                            "Implicitly set -KM."))
60
 
 
61
 
    parser.add_option("-K", "--keep", action="store_true",
62
 
                      dest="keep", metavar="KEEP", default=False,
63
 
                      help="Whether to keep or not the uploads directory.")
64
 
 
65
 
    parser.add_option("-M", "--no-mails", action="store_true",
66
 
                      dest="nomails", default=False,
67
 
                      help="Whether to suppress the sending of mails or not.")
68
 
 
69
 
    parser.add_option("-J", "--just-leaf", action="store", dest="leafname",
70
 
                      default=None, help="A specific leaf dir to limit to.",
71
 
                      metavar = "LEAF")
72
 
 
73
 
    (options, args) = parser.parse_args()
74
 
 
75
 
    if len(args) != 1:
76
 
        raise ValueError("Need to be given exactly one non-option "
77
 
                         "argument, namely the fsroot for the upload.")
78
 
    options.base_fsroot = os.path.abspath(args[0])
79
 
 
80
 
    if not os.path.isdir(options.base_fsroot):
81
 
        raise ValueError("%s is not a directory" % options.base_fsroot)
82
 
 
83
 
    return options
84
 
 
 
14
from canonical.launchpad.scripts.base import (
 
15
    LaunchpadScript, LaunchpadScriptFailure)
 
16
 
 
17
 
 
18
class ProcessUpload(LaunchpadScript):
 
19
 
 
20
    def add_my_options(self):
 
21
        self.parser.add_option(
 
22
            "-n", "--dry-run", action="store_true",
 
23
            dest="dryrun", metavar="DRY_RUN", default=False,
 
24
            help=("Whether to treat this as a dry-run or not."
 
25
                  "Also implies -KM."))
 
26
 
 
27
        self.parser.add_option(
 
28
            "-K", "--keep", action="store_true",
 
29
            dest="keep", metavar="KEEP", default=False,
 
30
            help="Whether to keep or not the uploads directory.")
 
31
 
 
32
        self.parser.add_option(
 
33
            "-M", "--no-mails", action="store_true",
 
34
            dest="nomails", default=False,
 
35
            help="Whether to suppress the sending of mails or not.")
 
36
 
 
37
        self.parser.add_option(
 
38
            "-J", "--just-leaf", action="store", dest="leafname",
 
39
            default=None, help="A specific leaf dir to limit to.",
 
40
            metavar = "LEAF")
 
41
        policy_options(self.parser)
 
42
 
 
43
    def main(self):
 
44
        if not self.args:
 
45
            raise LaunchpadScriptFailure(
 
46
                "Need to be given exactly one non-option "
 
47
                "argument, namely the fsroot for the upload.")
 
48
 
 
49
        self.options.base_fsroot = os.path.abspath(self.args[0])
 
50
 
 
51
        if not os.path.isdir(self.options.base_fsroot):
 
52
            raise LaunchpadScriptFailure(
 
53
                "%s is not a directory" % self.options.base_fsroot)
 
54
 
 
55
        self.logger.debug("Initialising connection.")
 
56
        UploadProcessor(
 
57
            self.options, self.txn, self.logger).processUploadQueue()
 
58
 
 
59
    @property
 
60
    def lockfilename(self):
 
61
        """Return specific lockfilename according the policy used.
 
62
 
 
63
        Each different p-u policy requires and uses a different lockfile.
 
64
        This is because they are run by different users and are independent
 
65
        of each other.
 
66
        """
 
67
        return "process-upload-%s.lock" % self.options.context
85
68
 
86
69
if __name__ == '__main__':
87
 
    sys.exit(main())
 
70
    script = ProcessUpload('process-upload', dbuser=config.uploader.dbuser)
 
71
    script.lock_and_run()
 
72