/usr/lib/python3/dist-packages/debdrylib/debdry.py is in debdry 0.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #!/usr/bin/python3
# coding: utf8
#
# Copyright (C) 2014 Enrico Zini <enrico@enricozini.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from .auto import Auto
from .tree import DebianDirectory
import os
import shutil
class Debdry:
def __init__(self, srcdir, debug=False):
self.srcdir = srcdir
self.debug = debug
self.dir_backup = os.path.join(srcdir, "debian/debdry")
self.dir_auto = os.path.join(srcdir, "debian.auto")
self.dir_debian = os.path.join(srcdir, "debian")
def is_debdry(self):
"""
Check if the debian/ directory is in debdry state
"""
return not os.path.exists(self.dir_backup)
def is_debianised(self):
"""
Check if the debian/ directory is in fully debianised state
"""
return os.path.exists(self.dir_backup)
def restore(self):
"""
Remove all the contents of debian/ and replace them with the contents
of debian/.debdry
"""
if not self.is_debianised():
raise RuntimeError("restore_debdry called on a non-debianised source")
# Delete everything except .debdry
for fn in os.listdir(self.dir_debian):
if fn == "debdry": continue
pathname = os.path.join(self.dir_debian, fn)
if os.path.isdir(pathname):
shutil.rmtree(pathname)
else:
os.unlink(pathname)
# Move all in .debdry one dir up
for fn in os.listdir(self.dir_backup):
os.rename(os.path.join(self.dir_backup, fn), os.path.join(self.dir_debian, fn))
# Remove .debdry
os.rmdir(self.dir_backup)
def debianise(self):
"""
Debianise the package, backing up debdry originals in dir_backup
"""
tmpdir = os.path.join(self.srcdir, "debian.debdry")
if os.path.isdir(tmpdir): shutil.rmtree(tmpdir)
if not os.path.exists(self.dir_debian):
os.makedirs(self.dir_debian)
# Move the debdry sources to debian.debdry
if os.path.exists(self.dir_backup):
os.rename(self.dir_backup, tmpdir)
shutil.rmtree(self.dir_debian)
else:
os.rename(self.dir_debian, tmpdir)
try:
# Perform automatic debianisation
Auto.debianise(self.srcdir)
# Move debian/ to debian.auto/
os.rename(self.dir_debian, self.dir_auto)
auto = DebianDirectory.scan(self.srcdir, self.dir_auto)
manual = DebianDirectory.scan(self.srcdir, tmpdir)
merged = DebianDirectory.combine(self.srcdir, self.dir_debian, auto, manual)
except:
# Rollback: restore debian.debdry to debian
self.maybe_preserve_workdir(tmpdir)
if os.path.isdir(self.dir_debian): shutil.rmtree(self.dir_debian)
os.rename(tmpdir, self.dir_debian)
raise
else:
# Commit: move debian.debdry to debian/debdry
os.rename(tmpdir, self.dir_backup)
finally:
if os.path.isdir(self.dir_auto): shutil.rmtree(self.dir_auto)
def diff(self):
"""
Take an existing, manually maintained debian/ dir, compute what it adds
to an auto-generated package, and put the results in debian/debdry
"""
tmpdir = os.path.join(self.srcdir, "debian.debdry")
if os.path.isdir(tmpdir): shutil.rmtree(tmpdir)
# Move debian to debian.debdry
os.rename(self.dir_debian, tmpdir)
# Remove debian.debdry/debdry if it exists
debdrydir = os.path.join(tmpdir, "debdry")
if os.path.exists(debdrydir):
shutil.rmtree(debdrydir)
try:
# Perform automatic debianisation
Auto.debianise(self.srcdir)
# Move debian/ to debian.auto/
os.rename(self.dir_debian, self.dir_auto)
# Diff with debdry
auto = DebianDirectory.scan(self.srcdir, self.dir_auto)
manual = DebianDirectory.scan(self.srcdir, tmpdir)
diffed = DebianDirectory.diff(self.srcdir, debdrydir, auto, manual)
except:
# Rollback: remove debian.debdry/debdry
self.maybe_preserve_workdir(debdrydir)
if os.path.isdir(debdrydir): shutil.rmtree(debdrydir)
raise
finally:
# Restore debian.debdry back to debian
os.rename(tmpdir, self.dir_debian)
if os.path.isdir(self.dir_auto): shutil.rmtree(self.dir_auto)
def maybe_preserve_workdir(self, pathname):
"""
Offer to preserve a work directory during rollback, if self.debug is
set and we are connected to a tty.
"""
if not self.debug or not os.isatty(0) or not os.isatty(1): return
if not os.path.isdir(pathname): return
renamed_pathname = pathname + ".debug"
want_rename = input(
"An error occurred and --debug is used: preserve {} as {}? (y/N) ".format(
pathname, renamed_pathname))
if want_rename.lower() != "y": return
if os.path.isdir(renamed_pathname):
shutil.rmtree(renamed_pathname)
# Hardlink the backup directory contents to the original directory
shutil.copytree(pathname, renamed_pathname, copy_function=os.link)
|