[Pkg-bazaar-commits] ./bzr-builddeb/trunk.old r255: Port import-dsc to DistributionBranch.

James Westby jw+debian at jameswestby.net
Wed Dec 10 08:32:59 UTC 2008


------------------------------------------------------------
revno: 255
committer: James Westby <jw+debian at jameswestby.net>
branch nick: 2.0
timestamp: Wed 2008-08-27 17:59:55 +0100
message:
  Port import-dsc to DistributionBranch.
  
  The internal tests still need porting, but there is a lot
  duplicated with the DistributionBranch tests.
modified:
  __init__.py
  import_dsc.py
  tests/blackbox/test_do.py
  tests/blackbox/test_import_dsc.py
-------------- next part --------------
=== modified file '__init__.py'
--- a/__init__.py	2008-08-27 15:15:19 +0000
+++ b/__init__.py	2008-08-27 16:59:55 +0000
@@ -23,11 +23,14 @@
 """bzr-builddeb - manage packages in a Bazaar branch."""
 
 import os
+import shutil
 import subprocess
+import tempfile
 import urlparse
 
 from debian_bundle.changelog import Version
 
+from bzrlib import bzrdir
 from bzrlib.branch import Branch
 from bzrlib.commands import Command, register_command
 from bzrlib.config import ConfigObj
@@ -35,6 +38,7 @@
                            NoWorkingTree,
                            NotBranchError,
                            FileExists,
+                           AlreadyBranchError,
                            )
 from bzrlib.option import Option
 from bzrlib.trace import info, warning
@@ -56,6 +60,8 @@
 from bzrlib.plugins.builddeb.import_dsc import (
         DistributionBranch,
         DistributionBranchSet,
+        DscCache,
+        DscComp,
         )
 from bzrlib.plugins.builddeb.properties import BuildProperties
 from bzrlib.plugins.builddeb.util import (find_changelog,
@@ -408,11 +414,7 @@
                 raise BzrCommandError("There are uncommitted changes in the "
                         "working tree. You must commit before using this "
                         "command")
-            if no_user_config:
-                config_files = [(local_conf, True), (default_conf, False)]
-            else:
-                config_files = [(local_conf, True), (global_conf, True),
-                                     (default_conf, False)]
+            config = debuild_config(tree, tree, no_user_config)
             if config.merge:
                 raise BzrCommandError("Merge upstream in merge mode is not yet "
                         "supported")
@@ -466,104 +468,140 @@
 
 
 class cmd_import_dsc(Command):
-  """Import a series of source packages.
-
-  Provide a number of source packages (.dsc files), and they will
-  be imported to create a branch with history that reflects those
-  packages. You must provide the --to option with the name of the
-  branch that will be created, and the --initial option to indicate
-  this is an initial import.
-
-  If there are packages that are available on snapshot.debian.net
-  then you can use the --snapshot option to supplement the packages
-  you provide with those available on that service. Pass the name
-  of the source package as on snapshot.debian.net as this option,
-  i.e. to import all versions of apt
-
-    import-dsc --initial --snapshot apt
-
-  If you use the --snapshot option then you don't have to provide
-  any source packages on the command line, and if you omit the
-  --to option then the name of the package as passed to --snapshot
-  will be used as the branch name.
-
-  In addition to the above choices you can specify a file
-  (possibly remote) that contains a list of source packages (.dsc
-  files) to import. Each line is taken to be a URI or path to
-  import. The sources specified in the file are used in addition
-  to those specified by other methods.
-
-  If you have an existing branch containing packaging and you want to
-  import a .dsc from an upload done from outside the version control
-  system you can use this command. In this case you can only specify
-  one file on the command line, or use a file containing only a single
-  filename, and do not use the --initial option.
-  """
-
-  takes_args = ['files*']
-  
-  to_opt = Option('to', help="The branch to import to.", type=str)
-  snapshot_opt = Option('snapshot', help="Retrieve source packages from "
-                        "snapshot.debian.net.", type=str)
-  filename_opt = Option('file', help="File containing URIs of source "
-                        "packages to import.", type=str, argname="filename",
-                        short_name='F')
-  initial_opt = Option('initial',
-        help="Perform an initial import to create a new branch.")
-
-  takes_options = [to_opt, snapshot_opt, filename_opt, initial_opt]
-
-  def run(self, files_list, to=None, snapshot=None, filename=None,
-          initial=False):
-    from bzrlib.plugins.builddeb.import_dsc import DscImporter, SnapshotImporter
-    if files_list is None:
-      files_list = []
-    if filename is not None:
-      if isinstance(filename, unicode):
-        filename = filename.encode('utf-8')
-      base_dir, path = urlutils.split(filename)
-      sources_file = get_transport(base_dir).get(path)
-      for line in sources_file:
-        line.strip()
-        files_list.append(line)
-    if snapshot is None:
-      if not initial:
-        if len(files_list) != 1:
-          raise BzrCommandError("You must give the location of exactly one "
-                                "source package.")
-      else:
-        if len(files_list) < 1:
-          raise BzrCommandError("You must give the location of at least one "
-                                "source package to install, or use the "
-                                "--file or --snapshot options.")
-        if to is None:
-          raise BzrCommandError("You must specify the name of the "
-                                "destination branch using the --to option.")
-      importer = DscImporter(files_list)
-    else:
-      if not initial:
-        raise BzrCommandError("You cannot use the --snapshot option without "
-            "the --initial option.")
-      if to is None:
-        to = snapshot
-      importer = SnapshotImporter(snapshot, other_sources=files_list)
-    if initial:
-      orig_target = os.path.join(to, default_orig_dir)
-      importer.import_dsc(to, orig_target=orig_target)
-    else :
-      inc_to = '.'
-      if to is not None:
-        inc_to = to
-      _local_conf = os.path.join(inc_to, local_conf)
-      _global_conf = os.path.join(inc_to, global_conf)
-      _default_conf = os.path.join(inc_to, default_conf)
-      config = DebBuildConfig([(_local_conf, True), (_global_conf, True),
-                             (_default_conf, False)])
-      orig_target = config.orig_dir
-      if orig_target is None:
-        orig_target = os.path.join(inc_to, default_orig_dir)
-      importer.incremental_import_dsc(inc_to, orig_target=orig_target)
-
+    """Import a series of source packages.
+
+    Provide a number of source packages (.dsc files), and they will
+    be imported to create a branch with history that reflects those
+    packages.
+
+    The first argument is the distribution that these source packages
+    were uploaded to, one of "debian" or "ubuntu". It can also
+    be the target distribution from the changelog, e.g. "unstable",
+    which will be resolved to the correct distribution.
+
+    You can also specify a file (possibly remote) that contains a
+    list of source packages (.dsc files) to import using the --file
+    option. Each line is taken to be a URI or path to import. The
+    sources specified in the file are used in addition to those
+    specified on the command line.
+
+    If you have an existing branch containing packaging and you want to
+    import a .dsc from an upload done from outside the version control
+    system you can use this command.
+    """
+
+    takes_args = ['target_distribution', 'files*']
+    
+    filename_opt = Option('file', help="File containing URIs of source "
+                          "packages to import.", type=str, argname="filename",
+                          short_name='F')
+
+    takes_options = [filename_opt]
+
+    def import_many(self, db, files_list, orig_target):
+        cache = DscCache()
+        files_list.sort(cmp=DscComp(cache).cmp)
+        if not os.path.exists(orig_target):
+            os.makedirs(orig_target)
+        for dscname in files_list:
+            dsc = cache.get_dsc(dscname)
+            def get_dsc_part(from_transport, filename):
+                from_f = from_transport.get(filename)
+                contents = from_f.read()
+                to_f = open(os.path.join(orig_target, filename), 'wb')
+                try:
+                    to_f.write(contents)
+                finally:
+                    to_f.close()
+            base, filename = urlutils.split(dscname)
+            from_transport = cache.get_transport(dscname)
+            get_dsc_part(from_transport, filename)
+            for file_details in dsc['files']:
+                name = file_details['name']
+                get_dsc_part(from_transport, name)
+            db.import_package(os.path.join(orig_target, filename))
+
+    def run(self, target_distribution, files_list, filename=None):
+        from bzrlib.plugins.builddeb.errors import MissingChangelogError
+        target_distribution = target_distribution.lower()
+        distribution_name = lookup_distribution(target_distribution)
+        if distribution_name is None:
+            if target_distribution not in ("debian", "ubuntu"):
+                raise BzrCommandError("Unknown target distribution: %s" \
+                        % target_dist)
+            else:
+                distribution_name = target_distribution
+        try:
+            tree = WorkingTree.open_containing('.')[0]
+        except NotBranchError:
+            raise BzrCommandError("There is no tree to import the packages in to")
+        tree.lock_write()
+        try:
+            if tree.changes_from(tree.basis_tree()).has_changed():
+                raise BzrCommandError("There are uncommitted changes in the "
+                        "working tree. You must commit before using this "
+                        "command")
+            if files_list is None:
+                files_list = []
+            if filename is not None:
+                if isinstance(filename, unicode):
+                    filename = filename.encode('utf-8')
+                base_dir, path = urlutils.split(filename)
+                sources_file = get_transport(base_dir).get(path)
+                for line in sources_file:
+                    line.strip()
+                    files_list.append(line)
+            if len(files_list) < 1:
+                raise BzrCommandError("You must give the location of at least one "
+                                      "source package to install, or use the "
+                                      "--file option.")
+            config = debuild_config(tree, tree, False)
+            orig_dir = config.orig_dir or default_orig_dir
+            orig_target = os.path.join(tree.basedir, default_orig_dir)
+            db = DistributionBranch(distribution_name, tree.branch,
+                    None, tree=tree)
+            dbs = DistributionBranchSet()
+            dbs.add_branch(db)
+            try:
+                (changelog, larstiq) = find_changelog(tree, False)
+                last_version = changelog.version
+            except MissingChangelogError:
+                last_version = None
+            tempdir = tempfile.mkdtemp(dir=os.path.join(tree.basedir,
+                        '..'))
+            try:
+                if last_version is not None:
+                    upstream_tip = db._revid_of_upstream_version_from_branch(
+                            last_version)
+                    db._extract_upstream_tree(upstream_tip, tempdir)
+                else:
+                    to_location = os.path.join(tempdir,
+                            distribution_name + "-upstream")
+                    to_transport = get_transport(to_location)
+                    to_transport.ensure_base()
+                    format = bzrdir.format_registry.make_bzrdir('default')
+                    try:
+                        existing_bzrdir = bzrdir.BzrDir.open_from_transport(
+                                to_transport)
+                    except NotBranchError:
+                        # really a NotBzrDir error...
+                        create_branch = bzrdir.BzrDir.create_branch_convenience
+                        branch = create_branch(to_transport.base,
+                                format=format,
+                                possible_transports=[to_transport])
+                    else:
+                        if existing_bzrdir.has_branch():
+                            raise AlreadyBranchError(location)
+                        else:
+                            branch = existing_bzrdir.create_branch()
+                            existing_bzrdir.create_workingtree()
+                    db.upstream_branch = branch
+                    db.upstream_tree = branch.bzrdir.open_workingtree()
+                self.import_many(db, files_list, orig_target)
+            finally:
+                shutil.rmtree(tempdir)
+        finally:
+            tree.unlock()
 
 register_command(cmd_import_dsc)
 
@@ -596,9 +634,8 @@
   takes_args = ['command?']
 
   def run(self, command=None):
-
-    config = DebBuildConfig([(local_conf, True), (global_conf, True),
-                             (default_conf, False)])
+    t = WorkingTree.open_containing('.')[0]
+    config = debuild_config(t, t, False)
 
     if not config.merge:
       raise BzrCommandError("This command only works for merge mode "
@@ -612,7 +649,6 @@
       except KeyError:
         command = "/bin/sh"
       give_instruction = True
-    t = WorkingTree.open_containing('.')[0]
     (changelog, larstiq) = find_changelog(t, True)
     build_dir = config.build_dir
     if build_dir is None:
@@ -690,13 +726,9 @@
         try:
             if t.changes_from(t.basis_tree()).has_changed():
               raise BzrCommandError("There are uncommitted changes in the "
-                      "working tree. You must commit before using this command")
-            if no_user_config:
-                config_files = [(local_conf, True), (default_conf, False)]
-            else:
-                config_files = [(local_conf, True), (global_conf, True),
-                                     (default_conf, False)]
-            config = DebBuildConfig(config_files)
+                      "working tree. You must commit before using this "
+                      "command")
+            config = debuild_config(t, t, no_user_config)
             if not merge:
                 merge = config.merge
             (changelog, larstiq) = find_changelog(t, False)

=== modified file 'import_dsc.py'
--- a/import_dsc.py	2008-08-27 15:15:19 +0000
+++ b/import_dsc.py	2008-08-27 16:59:55 +0000
@@ -172,14 +172,14 @@
 
 
 def should_ignore(relative_path):
-  parts = splitpath(relative_path)
-  if not parts:
-    return False
-  for part in parts:
-    if part in files_to_ignore:
-      return True
-    if part.endswith(',v'):
-      return True
+    parts = splitpath(relative_path)
+    if not parts:
+        return False
+    for part in parts:
+        if part in files_to_ignore:
+            return True
+        if part.endswith(',v'):
+            return True
 
 
 def top_directory(path):

=== modified file 'tests/blackbox/test_do.py'
--- a/tests/blackbox/test_do.py	2008-08-26 15:41:59 +0000
+++ b/tests/blackbox/test_do.py	2008-08-27 16:59:55 +0000
@@ -68,13 +68,14 @@
     tree.add(source_files)
     return tree
 
-  def make_merge_mode_config(self):
+  def make_merge_mode_config(self, tree):
     os.mkdir('.bzr-builddeb/')
     f = open('.bzr-builddeb/default.conf', 'wb')
     try:
       f.write('[BUILDDEB]\nmerge = True\n')
     finally:
       f.close()
+    tree.add(['.bzr-builddeb/', '.bzr-builddeb/default.conf'])
 
   def make_upstream_tarball(self):
     self.build_tree(['test-0.1/', 'test-0.1/a'])
@@ -109,27 +110,28 @@
                         '/merge.html for more information.'], 'bd-do true')
 
   def test_fails_no_changelog(self):
-    self.make_merge_mode_config()
+    tree = self.make_branch_and_tree('.')
+    self.make_merge_mode_config(tree)
     self.run_bzr_error(['Could not find changelog'], 'bd-do true')
 
   def test_no_copy_on_fail(self):
-    self.make_merge_mode_config()
-    self.make_unpacked_source()
+    tree = self.make_unpacked_source()
+    self.make_merge_mode_config(tree)
     self.make_upstream_tarball()
     self.run_bzr_error(['Not updating the working tree as the command '
                         'failed.'], ['bd-do', 'touch debian/do && false'])
     self.failIfExists('debian/do')
 
   def test_copy_on_success(self):
-    self.make_merge_mode_config()
-    self.make_unpacked_source()
+    tree = self.make_unpacked_source()
+    self.make_merge_mode_config(tree)
     self.make_upstream_tarball()
     self.run_bzr(['bd-do', 'touch debian/do'])
     self.failUnlessExists('debian/do')
 
   def test_removed_files_are_removed_in_branch(self):
-    self.make_merge_mode_config()
-    self.make_unpacked_source()
+    tree = self.make_unpacked_source()
+    self.make_merge_mode_config(tree)
     self.make_upstream_tarball()
     self.run_bzr(['bd-do', 'rm debian/changelog'])
     # It might be nice if this was actually gone, but that would involve
@@ -138,29 +140,29 @@
     self.failUnlessExists('debian/changelog')
 
   def test_new_directories_created(self):
-    self.make_merge_mode_config()
-    self.make_unpacked_source()
+    tree = self.make_unpacked_source()
+    self.make_merge_mode_config(tree)
     self.make_upstream_tarball()
     self.run_bzr(['bd-do', 'mkdir debian/dir'])
     self.failUnlessExists('debian/dir')
 
   def test_contents_taken_from_export(self):
-    self.make_merge_mode_config()
-    self.make_unpacked_source()
+    tree = self.make_unpacked_source()
+    self.make_merge_mode_config(tree)
     self.make_upstream_tarball()
     self.run_bzr(['bd-do', 'echo a > debian/changelog'])
     self.assertFileEqual('a\n', 'debian/changelog')
 
   def test_export_purged(self):
-    self.make_merge_mode_config()
-    self.make_unpacked_source()
+    tree = self.make_unpacked_source()
+    self.make_merge_mode_config(tree)
     self.make_upstream_tarball()
     self.run_bzr(['bd-do', 'echo a > debian/changelog'])
     self.failIfExists(self.build_dir())
 
   def test_uses_shell(self):
-    self.make_merge_mode_config()
-    self.make_unpacked_source()
+    tree = self.make_unpacked_source()
+    self.make_merge_mode_config(tree)
     self.make_upstream_tarball()
     old_shell = os.environ['SHELL']
     os.environ['SHELL'] = "touch debian/shell"
@@ -171,8 +173,8 @@
     self.failUnlessExists('debian/shell')
 
   def test_export_upstream(self):
-    self.make_merge_mode_config()
-    self.make_unpacked_source()
+    tree = self.make_unpacked_source()
+    self.make_merge_mode_config(tree)
     f = open('.bzr-builddeb/default.conf', 'ab')
     try:
       f.write('export-upstream = upstream\n')

=== modified file 'tests/blackbox/test_import_dsc.py'
--- a/tests/blackbox/test_import_dsc.py	2008-03-05 17:00:51 +0000
+++ b/tests/blackbox/test_import_dsc.py	2008-08-27 16:59:55 +0000
@@ -76,9 +76,8 @@
 
   def test_import_dsc(self):
     self.make_real_source_package()
-    self.run_bzr('import-dsc --initial --to %s %s' % \
-        (self.package_name, self.dsc_name))
-    tree = WorkingTree.open(self.package_name)
+    tree = self.make_branch_and_tree('.')
+    self.run_bzr('import-dsc debian %s' % self.dsc_name)
     tree.lock_read()
     try:
       self.check_inventory_shape(tree.inventory,
@@ -87,37 +86,11 @@
       tree.unlock()
     self.assertEqual(len(tree.branch.revision_history()), 2)
 
-  def test_import_no_to(self):
-    self.make_real_source_package()
-    self.run_bzr_error(['You must specify the name of the destination branch '
-        'using the --to option.'], 'import-dsc --initial %s' % self.dsc_name)
-
-  def test_import_snapshot_incremental(self):
-    self.make_branch_and_tree('.')
-    self.make_real_source_package()
-    self.run_bzr_error(['You cannot use the --snapshot option without the '
-        '--initial option'],
-        'import-dsc --snapshot %s %s' % (self.package_name, self.dsc_name))
-
-  def test_import_snapshot_incremental_with_to(self):
-    self.make_branch_and_tree('target')
-    self.make_real_source_package()
-    self.run_bzr_error(['You cannot use the --snapshot option without the '
-        '--initial option'],
-        'import-dsc --snapshot %s --to target %s' % \
-        (self.package_name, self.dsc_name))
-
   def test_import_incremental_no_files(self):
     self.make_branch_and_tree('.')
     self.make_real_source_package()
-    self.run_bzr_error(['You must give the location of exactly one source '
-        'package.'], 'import-dsc')
-
-  def test_import_incremental_two_files(self):
-    self.make_branch_and_tree('.')
-    self.make_real_source_package()
-    self.run_bzr_error(['You must give the location of exactly one source '
-        'package.'], 'import-dsc %s %s' % (self.dsc_name, self.dsc_name))
+    self.run_bzr_error(['You must give the location of at least one source '
+        'package.'], 'import-dsc debian')
 
 # vim: ts=2 sts=2 sw=2
 



More information about the Pkg-bazaar-commits mailing list