/usr/lib/python3/dist-packages/germinate/seeds.py is in python3-germinate 2.19.
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 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 | # -*- coding: UTF-8 -*-
"""Fetch seeds from a URL collection or from bzr."""
# Copyright (c) 2004, 2005, 2006, 2008, 2009, 2011, 2012 Canonical Ltd.
#
# Germinate 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 2, or (at your option) any
# later version.
#
# Germinate 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 Germinate; see the file COPYING. If not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301, USA.
from __future__ import print_function
import sys
import os
import tempfile
import atexit
import logging
try:
from urllib.parse import urljoin, urlparse as _urlparse
from urllib.request import Request, URLError, urlopen
except ImportError:
from urlparse import urljoin, urlparse as _urlparse
from urllib2 import Request, URLError, urlopen
import shutil
import re
import subprocess
import codecs
import io
import collections
import germinate.defaults
from germinate.tsort import topo_sort
# pychecker gets confused by __next__ for Python 3 support.
__pychecker__ = 'no-special'
__all__ = [
'SeedError',
'Seed',
'SeedStructure',
]
_logger = logging.getLogger(__name__)
_bzr_cache_dir = None
if sys.version >= '3':
_string_types = str
_text_type = str
else:
_string_types = basestring
_text_type = unicode
class AtomicFile(object):
"""Facilitate atomic writing of files. Forces UTF-8 encoding."""
def __init__(self, filename):
self.filename = filename
if sys.version_info[0] < 3:
self.fd = codecs.open(
'%s.new' % self.filename, 'w', 'UTF-8', 'replace')
else:
# io.open is available from Python 2.6, but we only use it with
# Python 3 because it raises exceptions when passed bytes.
self.fd = io.open(
'%s.new' % self.filename, mode='w',
encoding='UTF-8', errors='replace')
def __enter__(self):
return self.fd
def __exit__(self, exc_type, unused_exc_value, unused_exc_tb):
self.fd.close()
if exc_type is None:
os.rename('%s.new' % self.filename, self.filename)
# Not really necessary, but reduces pychecker confusion.
def write(self, s):
self.fd.write(s)
class SeedError(RuntimeError):
"""An error opening or parsing a seed."""
pass
def _ensure_unicode(s):
if isinstance(s, _text_type):
return s
else:
return _text_type(s, "utf8", "replace")
class Seed(object):
"""A single seed from a collection."""
def _open_seed(self, base, branch, name, bzr=False):
path = os.path.join(base, branch)
if not path.endswith('/'):
path += '/'
if bzr:
global _bzr_cache_dir
if _bzr_cache_dir is None:
_bzr_cache_dir = tempfile.mkdtemp(prefix='germinate-')
atexit.register(
shutil.rmtree, _bzr_cache_dir, ignore_errors=True)
checkout = os.path.join(_bzr_cache_dir, branch)
if not os.path.isdir(checkout):
command = ['bzr']
# https://bugs.launchpad.net/bzr/+bug/39542
if path.startswith('http:'):
command.append('branch')
_logger.info("Fetching branch of %s", path)
else:
command.extend(['checkout', '--lightweight'])
_logger.info("Checking out %s", path)
command.extend([path, checkout])
status = subprocess.call(command)
if status != 0:
raise SeedError("Command failed with exit status %d:\n"
" '%s'" % (status, ' '.join(command)))
return open(os.path.join(checkout, name))
else:
url = urljoin(path, name)
if not _urlparse(url).scheme:
fullpath = os.path.join(path, name)
_logger.info("Using %s", fullpath)
return open(fullpath)
_logger.info("Downloading %s", url)
req = Request(url)
req.add_header('Cache-Control', 'no-cache')
req.add_header('Pragma', 'no-cache')
return urlopen(req)
def __init__(self, bases, branches, name, bzr=False):
"""Read a seed from a collection."""
if isinstance(branches, _string_types):
branches = [branches]
self._name = name
self._base = None
self._branch = None
self._file = None
fd = None
ssh_host = None
for base in bases:
for branch in branches:
try:
fd = self._open_seed(base, branch, name, bzr)
self._base = base
self._branch = branch
break
except SeedError:
ssh_match = re.match(
r'bzr\+ssh://(?:[^/]*?@)?(.*?)(?:/|$)', base)
if ssh_match:
ssh_host = ssh_match.group(1)
except (OSError, IOError, URLError):
pass
if fd is not None:
break
if fd is None:
if bzr:
_logger.warning("Could not open %s from checkout of (any of):",
name)
for base in bases:
for branch in branches:
_logger.warning(' %s' % os.path.join(base, branch))
if ssh_host is not None:
_logger.error("Do you need to set your user name on %s?",
ssh_host)
_logger.error("Try a section such as this in "
"~/.ssh/config:")
_logger.error("")
_logger.error("Host %s", ssh_host)
_logger.error(" User YOUR_USER_NAME")
else:
_logger.warning("Could not open (any of):")
for base in bases:
for branch in branches:
path = os.path.join(base, branch)
if not path.endswith('/'):
path += '/'
_logger.warning(' %s' % urljoin(path, name))
raise SeedError("Could not open %s" % name)
try:
self._text = fd.read()
# In Python 3, we need to decode seed text read from URLs.
if sys.version_info[0] >= 3 and isinstance(self._text, bytes):
self._text = self._text.decode(errors="replace")
finally:
fd.close()
def open(self):
"""Open a file object with the text of this seed."""
if sys.version_info[0] < 3:
self._file = io.BytesIO(self._text)
else:
self._file = io.StringIO(self._text)
return self._file
def read(self, *args, **kwargs):
"""Read text from this seed."""
return self._file.read(*args, **kwargs)
def readline(self, *args, **kwargs):
"""Read a line from this seed."""
return self._file.readline(*args, **kwargs)
def readlines(self, *args, **kwargs):
"""Read a list of lines from this seed."""
return self._file.readlines(*args, **kwargs)
def __next__(self):
"""Read the next line from this seed."""
return next(self._file)
if sys.version < '3':
next = __next__
def close(self):
"""Close the file object for this seed."""
self._file.close()
def __enter__(self):
"""Open a seed context, returning a file object."""
return self.open()
def __exit__(self, unused_exc_type, unused_exc_value, unused_exc_tb):
"""Close a seed context."""
self.close()
@property
def name(self):
"""The seed's name."""
return self._name
@property
def base(self):
"""The base URL where this seed was found."""
return self._base
@property
def branch(self):
"""The name of the branch containing this seed."""
return self._branch
@property
def text(self):
"""The text of this seed."""
return self._text
def __lt__(self, other):
if not isinstance(other, Seed):
return NotImplemented
return self.text < other.text
def __le__(self, other):
if not isinstance(other, Seed):
return NotImplemented
return self.text <= other.text
def __eq__(self, other):
if not isinstance(other, Seed):
return NotImplemented
return self.text == other.text
def __ne__(self, other):
if not isinstance(other, Seed):
return NotImplemented
return self.text != other.text
def __ge__(self, other):
if not isinstance(other, Seed):
return NotImplemented
return self.text >= other.text
def __gt__(self, other):
if not isinstance(other, Seed):
return NotImplemented
return self.text > other.text
__hash__ = None
class CustomSeed(Seed):
"""A seed created from custom input data."""
def __init__(self, name, entries):
self._name = name
self._base = None
self._branch = None
self._text = '\n'.join(entries) + '\n'
class SingleSeedStructure(object):
"""A single seed collection structure file.
The input data is an ordered sequence of lines as follows:
SEED:[ INHERITED]
INHERITED is a space-separated list of seeds from which SEED inherits.
For example, "ship: base desktop" indicates that packages in the "ship"
seed may depend on packages in the "base" or "desktop" seeds without
requiring those packages to appear in the "ship" output. INHERITED may
be empty.
The lines should be topologically sorted with respect to inheritance,
with inherited-from seeds at the start.
Any line as follows:
include BRANCH
causes another seed branch to be included. Seed names will be resolved
in included branches if they cannot be found in the current branch.
This is for internal use; applications should use the SeedStructure
class instead.
"""
def __init__(self, branch, f):
"""Parse a single seed structure file."""
self.seed_order = []
self.inherit = {}
self.branches = [branch]
self.lines = []
self.features = set()
for line in f:
line = line.strip()
if not line:
continue
if line.startswith('#'):
continue
words = line.split()
if words[0].endswith(':'):
seed = words[0][:-1]
if '/' in seed:
raise SeedError(
"seed name '%s' may not contain '/'" % seed)
self.seed_order.append(seed)
self.inherit[seed] = list(words[1:])
self.lines.append(line)
elif words[0] == 'include':
self.branches.extend(words[1:])
elif words[0] == 'feature':
self.features.update(words[1:])
else:
_logger.error("Unparseable seed structure entry: %s", line)
class SeedStructure(collections.Mapping, object):
"""The full structure of a seed collection.
This deals with acquiring the seed structure files and recursively
acquiring any seed structure files it includes.
"""
def __init__(self, branch, seed_bases=None, bzr=False):
"""Open a seed collection and read all the seeds it contains."""
if seed_bases is None:
if bzr:
seed_bases = germinate.defaults.seeds_bzr
else:
seed_bases = germinate.defaults.seeds
seed_bases = seed_bases.split(',')
self._seed_bases = seed_bases
self._branch = branch
self._bzr = bzr
self._features = set()
self._seed_order, self._inherit, branches, self._lines = \
self._parse(self._branch, set())
self._seeds = {}
for seed in self._seed_order:
self._seeds[seed] = self.make_seed(
seed_bases, branches, seed, bzr=bzr)
self._expand_inheritance()
def _parse(self, branch, got_branches):
all_seed_order = []
all_inherit = {}
all_branches = []
all_structure = []
# Fetch this one
with self.make_seed(
self._seed_bases, branch, "STRUCTURE", self._bzr) as seed:
structure = SingleSeedStructure(branch, seed)
got_branches.add(branch)
# Recursively expand included branches
for child_branch in structure.branches:
if child_branch in got_branches:
continue
(child_seed_order, child_inherit, child_branches,
child_structure) = self._parse(child_branch, got_branches)
all_seed_order.extend(child_seed_order)
all_inherit.update(child_inherit)
for grandchild_branch in child_branches:
if grandchild_branch not in all_branches:
all_branches.append(grandchild_branch)
for child_structure_line in child_structure:
child_structure_name = child_structure_line.split()[0][:-1]
for i in range(len(all_structure)):
if (all_structure[i].split()[0][:-1] ==
child_structure_name):
del all_structure[i]
break
all_structure.append(child_structure_line)
# Attach the main branch's data to the end
all_seed_order.extend(structure.seed_order)
all_inherit.update(structure.inherit)
for child_branch in structure.branches:
if child_branch not in all_branches:
all_branches.append(child_branch)
for structure_line in structure.lines:
structure_name = structure_line.split()[0][:-1]
for i in range(len(all_structure)):
if all_structure[i].split()[0][:-1] == structure_name:
del all_structure[i]
break
all_structure.append(structure_line)
self._features.update(structure.features)
# We generally want to process branches in reverse order, so that
# later branches can override seeds from earlier branches
all_branches.reverse()
return all_seed_order, all_inherit, all_branches, all_structure
def make_seed(self, bases, branches, name, bzr=False):
"""Read a seed from this collection.
This can be overridden by subclasses in order to read seeds in a
different way.
"""
return Seed(bases, branches, name, bzr=bzr)
def _expand_inheritance(self):
"""Expand out incomplete inheritance lists."""
self._original_inherit = dict(self._inherit)
self._names = topo_sort(self._inherit)
for name in self._names:
seen = set()
new_inherit = []
for inheritee in self._inherit[name]:
for expanded in self._inherit[inheritee]:
if expanded not in seen:
new_inherit.append(expanded)
seen.add(expanded)
if inheritee not in seen:
new_inherit.append(inheritee)
seen.add(inheritee)
self._inherit[name] = new_inherit
def limit(self, seeds):
"""Restrict the seeds we care about to this list."""
self._names = []
for name in seeds:
for inherit in self._inherit[name]:
if inherit not in self._names:
self._names.append(inherit)
if name not in self._names:
self._names.append(name)
def add(self, name, entries, parent=None):
"""Add a custom seed."""
self._names.append(name)
if parent is not None:
self._inherit[name] = self._inherit[parent] + [parent]
else:
self._inherit[name] = [parent]
self._seeds[name] = CustomSeed(name, entries)
def inner_seeds(self, seedname):
"""Return this seed and the seeds from which it inherits."""
innerseeds = list(self._inherit[seedname])
innerseeds.append(seedname)
return innerseeds
def strictly_outer_seeds(self, seedname):
"""Return the seeds that inherit from this seed."""
outerseeds = []
for seed in self._names:
if seedname in self._inherit[seed]:
outerseeds.append(seed)
return outerseeds
def outer_seeds(self, seedname):
"""Return this seed and the seeds that inherit from it."""
outerseeds = [seedname]
outerseeds.extend(self.strictly_outer_seeds(seedname))
return outerseeds
def __iter__(self):
"""Return an iterator over the seeds in this collection."""
return iter(self._seeds)
def __len__(self):
"""Return the number of seeds in this collection."""
return len(self._seeds)
def __getitem__(self, seedname):
"""Get a particular seed from this collection."""
return self._seeds[seedname]
@property
def branch(self):
"""The name of this seed collection branch."""
return self._branch
@property
def features(self):
"""The feature flags set for this seed collection."""
return set(self._features)
@property
def supported(self):
"""The name of the "supported" seed (the last one in the structure)."""
return self._seed_order[-1]
@property
def names(self):
"""All the seed names in this collection."""
return list(self._names)
def write(self, filename):
"""Write the text of the seed STRUCTURE file."""
with AtomicFile(filename) as f:
for line in self._lines:
print(_ensure_unicode(line), file=f)
def write_dot(self, filename):
"""Write a dot file representing this structure."""
with AtomicFile(filename) as dotfile:
print("digraph structure {", file=dotfile)
print(" node [color=lightblue2, style=filled];", file=dotfile)
for seed in self._seed_order:
if seed not in self._original_inherit:
continue
for inherit in self._original_inherit[seed]:
print(" \"%s\" -> \"%s\";" % (inherit, seed),
file=dotfile)
print("}", file=dotfile)
def write_seed_text(self, filename, seedname):
"""Write the text of a seed in this collection."""
with AtomicFile(filename) as f:
with self._seeds[seedname] as seed:
for line in seed:
print(_ensure_unicode(line.rstrip('\n')), file=f)
|