annotate treepkg/subversion.py @ 224:6bac65dcf258

Handle the svn repositories and working copies in a more object oriented way
author Bernhard Herzog <bh@intevation.de>
date Thu, 08 Jan 2009 16:55:18 +0000
parents 1527c37bd7aa
children d2ddd037ddaf
rev   line source
224
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
1 # Copyright (C) 2007, 2008, 2009 by Intevation GmbH
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
2 # Authors:
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
3 # Bernhard Herzog <bh@intevation.de>
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
4 #
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
5 # This program is free software under the GPL (>=v2)
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
6 # Read the file COPYING coming with the software for details.
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
7
224
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
8 """Collection of subversion utility code"""
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
9
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
10 import os
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
11
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
12 import run
45
3e610233ccfe use cmdexpand when calling subprocesses
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
13 from cmdexpand import cmdexpand
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
14 from util import extract_value_for_key
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
15
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
16
208
1527c37bd7aa Allow checkouts of specific revision and to prohibit recursion during
Bernhard Herzog <bh@intevation.de>
parents: 79
diff changeset
17 def checkout(url, localdir, revision=None, recurse=True):
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
18 """Runs svn to checkout the repository at url into the localdir"""
208
1527c37bd7aa Allow checkouts of specific revision and to prohibit recursion during
Bernhard Herzog <bh@intevation.de>
parents: 79
diff changeset
19 args = []
1527c37bd7aa Allow checkouts of specific revision and to prohibit recursion during
Bernhard Herzog <bh@intevation.de>
parents: 79
diff changeset
20 if revision:
1527c37bd7aa Allow checkouts of specific revision and to prohibit recursion during
Bernhard Herzog <bh@intevation.de>
parents: 79
diff changeset
21 args.extend(["--revision", revision])
1527c37bd7aa Allow checkouts of specific revision and to prohibit recursion during
Bernhard Herzog <bh@intevation.de>
parents: 79
diff changeset
22 if not recurse:
1527c37bd7aa Allow checkouts of specific revision and to prohibit recursion during
Bernhard Herzog <bh@intevation.de>
parents: 79
diff changeset
23 args.append("-N")
1527c37bd7aa Allow checkouts of specific revision and to prohibit recursion during
Bernhard Herzog <bh@intevation.de>
parents: 79
diff changeset
24 run.call(cmdexpand("svn checkout -q @args $url $localdir", **locals()))
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
25
208
1527c37bd7aa Allow checkouts of specific revision and to prohibit recursion during
Bernhard Herzog <bh@intevation.de>
parents: 79
diff changeset
26 def update(localdir, revision=None, recurse=True):
79
570ac81865be Add revision parameter to update so that a checkout can be updated to a
Bernhard Herzog <bh@intevation.de>
parents: 45
diff changeset
27 """Runs svn update on the localdir.
570ac81865be Add revision parameter to update so that a checkout can be updated to a
Bernhard Herzog <bh@intevation.de>
parents: 45
diff changeset
28 The parameter revision, if given, is passed to svn as the value of
570ac81865be Add revision parameter to update so that a checkout can be updated to a
Bernhard Herzog <bh@intevation.de>
parents: 45
diff changeset
29 the --revision option.
570ac81865be Add revision parameter to update so that a checkout can be updated to a
Bernhard Herzog <bh@intevation.de>
parents: 45
diff changeset
30 """
208
1527c37bd7aa Allow checkouts of specific revision and to prohibit recursion during
Bernhard Herzog <bh@intevation.de>
parents: 79
diff changeset
31 args = []
79
570ac81865be Add revision parameter to update so that a checkout can be updated to a
Bernhard Herzog <bh@intevation.de>
parents: 45
diff changeset
32 if revision:
208
1527c37bd7aa Allow checkouts of specific revision and to prohibit recursion during
Bernhard Herzog <bh@intevation.de>
parents: 79
diff changeset
33 args.extend(["--revision", revision])
1527c37bd7aa Allow checkouts of specific revision and to prohibit recursion during
Bernhard Herzog <bh@intevation.de>
parents: 79
diff changeset
34 if not recurse:
1527c37bd7aa Allow checkouts of specific revision and to prohibit recursion during
Bernhard Herzog <bh@intevation.de>
parents: 79
diff changeset
35 args.append("-N")
1527c37bd7aa Allow checkouts of specific revision and to prohibit recursion during
Bernhard Herzog <bh@intevation.de>
parents: 79
diff changeset
36 run.call(cmdexpand("svn update -q @args $localdir", **locals()))
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
37
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
38 def export(src, dest):
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
39 """Runs svn export src dest"""
45
3e610233ccfe use cmdexpand when calling subprocesses
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
40 run.call(cmdexpand("svn export -q $src $dest", **locals()))
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
41
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
42 def last_changed_revision(svn_working_copy):
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
43 """return the last changed revision of an SVN working copy as an int"""
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
44 # Make sure we run svn under the C locale to avoid localized
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
45 # messages
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
46 env = os.environ.copy()
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
47 env["LANG"] = "C"
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
48
45
3e610233ccfe use cmdexpand when calling subprocesses
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
49 output = run.capture_output(cmdexpand("svn info $svn_working_copy",
3e610233ccfe use cmdexpand when calling subprocesses
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
50 **locals()),
3e610233ccfe use cmdexpand when calling subprocesses
Bernhard Herzog <bh@intevation.de>
parents: 0
diff changeset
51 env=env)
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
52 return int(extract_value_for_key(output.splitlines(),
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
53 "Last Changed Rev:"))
224
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
54
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
55
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
56 class SvnRepository(object):
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
57
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
58 """Describes a subversion repository"""
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
59
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
60 def __init__(self, url, external_subdirs=()):
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
61 """Initialize the subversion repository description
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
62 Parameters:
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
63 url -- The url of the repository
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
64 external_subdirs -- A list of subdirectories which are managed
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
65 by svn externals definitions
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
66 """
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
67 self.url = url
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
68 self.external_subdirs = external_subdirs
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
69
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
70 def checkout(self, localdir, revision=None):
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
71 """Checks out the repository into localdir. The revision
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
72 parameter should be an and indicates the revision to check out.
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
73 """
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
74 checkout(self.url, localdir, revision=revision)
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
75
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
76 def export(self, localdir, destdir):
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
77 """Exports the working copy in localdir to destdir"""
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
78 export(localdir, destdir)
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
79 for subdir in self.external_subdirs:
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
80 absdir = os.path.join(destdir, subdir)
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
81 if not os.path.isdir(absdir):
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
82 subversion.export(os.path.join(localdir, subdir), absdir)
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
83
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
84 def last_changed_revision(self, localdir):
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
85 """Returns the last changed revision of the working copy in localdir"""
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
86 return max([last_changed_revision(os.path.join(localdir, d))
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
87 for d in [localdir] + list(self.external_subdirs)])
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
88
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
89
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
90 class SvnWorkingCopy(object):
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
91
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
92 """Represents a checkout of a subversion repository"""
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
93
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
94 def __init__(self, repository, localdir, logger=None):
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
95 """
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
96 Initialize the working copy.
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
97 Parameters:
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
98 repository -- The SvnRepository instance describing the
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
99 repository
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
100 localdir -- The directory for the working copy
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
101 logger -- logging object to use for some info/debug messages
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
102 """
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
103 self.repository = repository
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
104 self.localdir = localdir
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
105 self.logger = logger
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
106
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
107 def log_info(self, *args):
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
108 if self.logger is not None:
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
109 self.logger.info(*args)
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
110
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
111 def update_or_checkout(self, revision=None):
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
112 """Updates the working copy or creates by checking out the repository"""
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
113 if os.path.exists(self.localdir):
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
114 self.log_info("Updating the working copy in %r", self.localdir)
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
115 update(self.localdir, revision=revision)
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
116 else:
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
117 self.log_info("The working copy in %r doesn't exist yet."
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
118 " Checking out from %r",
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
119 self.localdir, self.repository.url)
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
120 self.repository.checkout(self.localdir, revision=revision)
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
121
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
122 def export(self, destdir):
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
123 """Exports the working copy to destdir"""
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
124 self.repository.export(self.localdir, destdir)
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
125
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
126 def last_changed_revision(self):
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
127 """Returns the last changed rev of the working copy"""
6bac65dcf258 Handle the svn repositories and working copies in a more object oriented way
Bernhard Herzog <bh@intevation.de>
parents: 208
diff changeset
128 return self.repository.last_changed_revision(self.localdir)
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)