Ubuntu Pastebin

Paste from nacc at Wed, 19 Oct 2016 23:34:24 +0000

Download as text
  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
=== added file 'ubuntutools/update_vcs.py'
--- ubuntutools/update_vcs.py	1970-01-01 00:00:00 +0000
+++ ubuntutools/update_vcs.py	2016-10-19 23:30:18 +0000
@@ -0,0 +1,185 @@
+# update_vcs.py - updates the Vcs field(s) of an Ubuntu package
+#
+# Copyright (C) 2016, Nishanth Aravamudan <nish.aravamudan@canonical.com>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+from __future__ import print_function
+
+"""This module is for updating the Vcs field(s) of an Ubuntu package."""
+
+import os
+import re
+
+import debian.changelog
+from ubuntutools.logger import Logger
+
+
+class VcsUpdateException(Exception):
+    pass
+
+
+class Control(object):
+    """Represents a debian/control file"""
+
+    def __init__(self, filename):
+        assert os.path.isfile(filename), "%s does not exist." % (filename)
+        self._filename = filename
+        self._content = open(filename).read()
+
+    def get_vcs(self):
+        """Returns the value(s) of the Vcs-{Git,Bzr,Browser...} field(s)."""
+        return re.findall("^(Vcs-(?:Arch|Bzr|Cvs|Darcs|Git|Hg|Mtn|Svn|Browser)): ?(.*)$",
+                            self._content,
+                            re.MULTILINE)
+
+    def get_original_vcs(self):
+        """Returns the value(s) of the XS-Debian-Vcs-{Git,Bzr,Browser...} field(s)."""
+        orig_vcs = re.findall("^(?:[XS]*-)?Debian-(Vcs-(?:Arch|Bzr|Cvs|Darcs|Git|Hg|Mtn|Svn|Browser)): ?(.*)$",
+                                    self._content, re.MULTILINE)
+        return orig_vcs 
+
+    def save(self, filename=None):
+        """Saves the control file."""
+        if filename:
+            self._filename = filename
+        control_file = open(self._filename, "w")
+        control_file.write(self._content)
+        control_file.close()
+
+    def reset_vcs(self, vcs):
+        """Sets the value of the Vcs- field(s)."""
+        for v in vcs:
+            pattern = re.compile("^XS-Debian-%s: ?.*$" % v[0], re.MULTILINE)
+            self._content = pattern.sub("%s: %s" % (v[0], v[1]), self._content)
+
+    def set_original_vcs(self, original_vcs):
+        replace = False
+        if self.get_original_vcs():
+            replace = True
+        for vcs in original_vcs:
+            ovcs = "XS-Debian-" + vcs[0] + ": " + vcs[1]
+            if replace:
+                pattern = re.compile("^(?:[XS]*-)?Debian-" + vcs[0] + ":.*$",
+                                     re.MULTILINE)
+                self._content = pattern.sub(ovcs, self._content)
+            else:
+                pattern = re.compile("^(" + vcs[0] + ":.*)$",
+                                     re.MULTILINE)
+                self._content = pattern.sub(ovcs,
+                                            self._content)
+
+
+def _get_distribution(changelog_file):
+    """get distribution of latest changelog entry"""
+    changelog = debian.changelog.Changelog(open(changelog_file), strict=False,
+                                           max_blocks=1)
+    distribution = changelog.distributions.split()[0]
+    # Strip things like "-proposed-updates" or "-security" from distribution
+    return distribution.split("-", 1)[0]
+
+
+def _find_files(debian_directory, verbose):
+    """Find possible control files.
+    Returns (changelog, control files list)
+    Raises an exception if none can be found.
+    """
+    possible_contol_files = [os.path.join(debian_directory, f) for
+                             f in ["control.in", "control"]]
+
+    changelog_file = os.path.join(debian_directory, "changelog")
+    control_files = [f for f in possible_contol_files if os.path.isfile(f)]
+
+    # Make sure that a changelog and control file is available
+    if len(control_files) == 0:
+        raise VcsUpdateException(
+                "No control file found in %s." % debian_directory)
+    if not os.path.isfile(changelog_file):
+        raise VcsUpdateException(
+                "No changelog file found in %s." % debian_directory)
+
+    # If the rules file accounts for XSBC-Debian-Vcs-*, we should not
+    # touch it in this package (e.g. the python package).
+    rules_file = os.path.join(debian_directory, "rules")
+    if os.path.isfile(rules_file) and \
+       'XS-Debian-Vcs-' in open(rules_file).read():
+        if verbose:
+            print("XS-Debian-Vcs-* are managed by 'rules' file. Doing nothing.")
+        control_files = []
+
+    return (changelog_file, control_files)
+
+
+def update_vcs(debian_directory, verbose=False):
+    """updates the Vcs field(s) of an Ubuntu package
+
+    * No modifications are made if no Vcs-Git or Vcs-Browser fields
+      are found. Otherwise, they are replaced with Xs-Debian-Vcs-Git
+      and Xs-Debian-Vcs-Browser, respectively.
+
+    VCS discussion: https://lists.ubuntu.com/archives/ubuntu-devel/2007-March/023332.html
+    """
+    try:
+        changelog_file, control_files = _find_files(debian_directory, verbose)
+    except VcsUpdateException as e:
+        Logger.error(str(e))
+        raise
+
+    distribution = _get_distribution(changelog_file)
+    for control_file in control_files:
+        control = Control(control_file)
+
+        original_vcs = control.get_vcs()
+        if len(original_vcs) == 0:
+            Logger.error("No Vcs-* fields found in %s.", control_file)
+            raise VcsUpdateException("No Vcs-* fields found")
+         
+        if distribution in ("stable", "testing", "unstable", "experimental"):
+            if verbose:
+                print("The package targets Debian. Doing nothing.")
+            return
+
+        if len(control.get_original_vcs()) != 0:
+            Logger.warn("Overwriting original Vcs: %s",
+                        control.get_original_vcs())
+
+        if verbose:
+            print("The original Vcs values are:")
+            for vcs in original_vcs:
+                print("  %s: %s" % (vcs[0], vcs[1]))
+        control.set_original_vcs(original_vcs)
+        control.save()
+
+    return
+
+
+def restore_vcs(debian_directory, verbose=False):
+    """Restore the original VCS"""
+    try:
+        changelog_file, control_files = _find_files(debian_directory, verbose)
+    except VcsUpdateException as e:
+        Logger.error(str(e))
+        raise
+
+    for control_file in control_files:
+        control = Control(control_file)
+        orig_vcs = control.get_original_vcs()
+        if not orig_vcs:
+            continue
+
+        if verbose:
+            print("Restoring original Vcs values:")
+            for vcs in orig_vcs:
+                print("  %s: %s" % (vcs[0], vcs[1]))
+        control.reset_vcs(orig_vcs)
+        control.save()

=== modified file 'update-maintainer'
--- update-maintainer	2012-05-06 17:42:39 +0000
+++ update-maintainer	2016-10-19 23:22:04 +0000
@@ -21,6 +21,9 @@
 from ubuntutools.update_maintainer import (update_maintainer,
                                            restore_maintainer,
                                            MaintainerUpdateException)
+from ubuntutools.update_vcs import (update_vcs,
+                                    restore_vcs,
+                                    VcsUpdateException)
 
 
 def main():
@@ -36,6 +39,10 @@
                       action='store_true', default=False)
     parser.add_option("-q", "--quiet", help="print no informational messages",
                       dest="quiet", action="store_true", default=False)
+    parser.add_option("-v", "--vcs",
+                      help="Apply the same operation (change/restore) "
+                            "to VCS fields",
+                      action='store_true', default=False)
     (options, args) = parser.parse_args()
 
     if len(args) != 0:
@@ -45,12 +52,18 @@
 
     if not options.restore:
         operation = update_maintainer
+        vcs_operation = update_vcs
     else:
         operation = restore_maintainer
+        vcs_operation = restore_vcs 
 
     try:
-        operation(options.debian_directory, not options.quiet)
-    except MaintainerUpdateException:
+        operation(options.debian_directory,
+                  not options.quiet)
+        if options.vcs:
+            vcs_operation(options.debian_directory,
+                          not options.quiet)
+    except (MaintainerUpdateException, VcsUpdateException):
         sys.exit(1)
 
 if __name__ == "__main__":
Download as text