2
# Copyright 2007 Canonical Ltd. All rights reserved.
3
# This script uses relative imports.
4
# pylint: disable-msg=W0403
6
"""Script to print disk locations of modified branches.
8
This script will be used by IS for the rsync backups.
10
Only branches that have been modified since the specified time will be
11
returned. It is possible that the branch will have been modified only in the
12
web UI and not actually recieved any more revisions, and will be a false
15
If the branch is REMOTE it is ignored.
16
If the branch is HOSTED, both the hosted and mirrored area are returned.
17
If the branch is an IMPORT or MIRROR branch, only the mirrored area is shown.
20
# This script does not use the standard Launchpad script framework as it is
21
# not intended to be run by itself.
25
from datetime import datetime, timedelta
28
from time import strptime
32
from zope.component import getUtility
34
from canonical.codehosting.vfs.branchfs import branch_id_to_path
35
from canonical.config import config
36
from canonical.launchpad.database.branch import Branch
37
from canonical.launchpad.interfaces.branch import BranchType
38
from canonical.launchpad.scripts.base import LaunchpadScript
39
from canonical.launchpad.webapp.interfaces import (
40
IStoreSelector, MAIN_STORE, DEFAULT_FLAVOR)
43
class ModifiedBranchesScript(LaunchpadScript):
44
"""List branches modified since the specified time."""
47
"List branch paths for branches modified since the specified time.")
49
def add_my_options(self):
50
self.parser.add_option(
51
"-s", "--since", metavar="DATE",
52
help="A date in the format YYYY-MM-DD. Branches that "
53
"have been modified since this date will be returned.")
54
self.parser.add_option(
55
"-l", "--last-hours", metavar="HOURS", type="int",
56
help="Return the branches that have been modified in "
57
"the last HOURS number of hours.")
60
if (self.options.last_hours is not None and
61
self.options.since is not None):
62
print "Only one of --since or --last-hours can be specified."
65
if self.options.last_hours is not None:
67
datetime.utcnow() - timedelta(hours=self.options.last_hours))
68
elif self.options.since is not None:
70
parsed_time = strptime(self.options.since, '%Y-%m-%d')
71
last_modified = datetime(*(parsed_time[:3]))
76
print "One of --since or --last-hours needs to be specified."
78
# Make the datetime timezone aware.
79
last_modified = last_modified.replace(tzinfo=UTC)
81
"Looking for branches modified since %s", last_modified)
82
store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
83
branches = store.find(
84
Branch, Branch.date_last_modified > last_modified)
85
for branch in branches:
86
self.logger.info(branch.unique_name)
87
branch_type = branch.branch_type
88
if branch_type == BranchType.REMOTE:
89
self.logger.info("remote branch, skipping")
91
path = branch_id_to_path(branch.id)
92
if branch_type == BranchType.HOSTED:
94
config.codehosting.hosted_branches_root, path)
96
config.codehosting.mirrored_branches_root, path)
98
self.logger.info("Done.")
101
if __name__ == '__main__':
102
script = ModifiedBranchesScript('modified-branches')