docs: def build_dsc(debinfo,
                dist_dir,
                repackaged_dirname,
                orig_sdist=None,
                patch_posix=0,
                remove_expanded_source_dir=0,
                debian_dir_only=False,
                sign_dsc=False,
                ):
      """make debian source package"""
      #    A. Find new dirname and delete any pre-existing contents
  
      # dist_dir is usually 'deb_dist'
  
      # the location of the copied original source package (it was
      # re-recreated in dist_dir)
      if debian_dir_only:
          fullpath_repackaged_dirname = os.path.abspath(os.curdir)
      else:
          fullpath_repackaged_dirname = os.path.join(dist_dir,repackaged_dirname)
  
      ###############################################
      # 1. make temporary original source tarball
  
      #    Note that, for the final tarball, best practices suggest
      #    using "dpkg-source -b".  See
      #    http://www.debian.org/doc/developers-reference/ch-best-pkging-practices.en.html
  
      # Create the name of the tarball that qualifies as the upstream
      # source. If the original was specified, we'll link to
      # it. Otherwise, we generate our own .tar.gz file from the output
      # of "python setup.py sdist" (done above) so that we avoid
      # packaging .svn directories, for example.
  
      if not debian_dir_only:
          repackaged_orig_tarball = ('%(source)s_%(upstream_version)s.orig.tar.gz'%
                                     debinfo.__dict__)
          repackaged_orig_tarball_path = os.path.join(dist_dir,
                                                      repackaged_orig_tarball)
          if orig_sdist is not None:
              if os.path.exists(repackaged_orig_tarball_path):
                  os.unlink(repackaged_orig_tarball_path)
              link_func(orig_sdist,repackaged_orig_tarball_path)
          else:
              make_tarball(repackaged_orig_tarball,
                           repackaged_dirname,
                           cwd=dist_dir)
  
          # apply patch
          if debinfo.patch_file != '':
              apply_patch(debinfo.patch_file,
                          posix=patch_posix,
                          level=debinfo.patch_level,
                          cwd=fullpath_repackaged_dirname)
  
      for fname in ['Makefile','makefile']:
          if os.path.exists(os.path.join(fullpath_repackaged_dirname,fname)):
              sys.stderr.write('*'*1000 + '\n')
              sys.stderr.write('WARNING: a Makefile exists in this package. '
                               'stdeb will tell debhelper 7 to use setup.py '
                               'to build and install the package, and the '
                               'Makefile will be ignored.\n')
              sys.stderr.write('*'*1000 + '\n')
  
  
      ###############################################
      # 2. create debian/ directory and contents
      debian_dir = os.path.join(fullpath_repackaged_dirname,'debian')
      if not os.path.exists(debian_dir):
          os.mkdir(debian_dir)
  
      #    A. debian/changelog
      changelog = CHANGELOG_FILE%debinfo.__dict__
      with codecs.open( os.path.join(debian_dir,'changelog'),
                   mode='w', encoding='utf-8') as fd:
          fd.write(changelog)
  
      #    B. debian/control
      if debinfo.uploaders:
          debinfo.uploaders = 'Uploaders: %s\n' % ', '.join(debinfo.uploaders)
      else:
          debinfo.uploaders = ''
      control = CONTROL_FILE%debinfo.__dict__
      with codecs.open( os.path.join(debian_dir,'control'),
                        mode='w', encoding='utf-8') as fd:
          fd.write(control)
  
      #    C. debian/rules
      debinfo.percent_symbol = '%'
      rules = RULES_MAIN%debinfo.__dict__
  
      rules = rules.replace('        ','\t')
      rules_fname = os.path.join(debian_dir,'rules')
      with codecs.open( rules_fname,
                        mode='w', encoding='utf-8') as fd:
          fd.write(rules)
      os.chmod(rules_fname,0o755)
  
      #    D. debian/compat
      fd = open( os.path.join(debian_dir,'compat'), mode='w')
      fd.write('7\n')
      fd.close()
  
      #    E. debian/package.mime
      if debinfo.mime_file != '':
          if not os.path.exists(debinfo.mime_file):
              raise ValueError(
                  'a MIME file was specified, but does not exist: %s'%(
                  debinfo.mime_file,))
          link_func( debinfo.mime_file,
                   os.path.join(debian_dir,debinfo.package+'.mime'))
      if debinfo.shared_mime_file != '':
          if not os.path.exists(debinfo.shared_mime_file):
              raise ValueError(
                  'a shared MIME file was specified, but does not exist: %s'%(
                  debinfo.shared_mime_file,))
          link_func( debinfo.shared_mime_file,
                   os.path.join(debian_dir,
                                debinfo.package+'.sharedmimeinfo'))
  
      #    F. debian/copyright
      if debinfo.copyright_file != '':
          link_func( debinfo.copyright_file,
                   os.path.join(debian_dir,'copyright'))
  
      #    H. debian/<package>.install
      if len(debinfo.install_file_lines):
          fd = open( os.path.join(debian_dir,'%s.install'%debinfo.package), mode='w')
          fd.write('\n'.join(debinfo.install_file_lines)+'\n')
          fd.close()
  
      #    I. debian/<package>.udev
      if debinfo.udev_rules != '':
          fname = debinfo.udev_rules
          if not os.path.exists(fname):
              raise ValueError('udev rules file specified, but does not exist')
          link_func(fname,
                    os.path.join(debian_dir,'%s.udev'%debinfo.package))
  
      #    J. debian/source/format
      os.mkdir(os.path.join(debian_dir,'source'))
      fd = open( os.path.join(debian_dir,'source','format'), mode='w')
      fd.write('3.0 (quilt)\n')
      fd.close()
  
      fd = open( os.path.join(debian_dir,'source','options'), mode='w')
      fd.write('extend-diff-ignore="\.egg-info$"')
      fd.close()
  
      if debian_dir_only:
          return
  
      ###############################################
      # 3. unpack original source tarball
  
      debianized_package_dirname = fullpath_repackaged_dirname+'.debianized'
      if os.path.exists(debianized_package_dirname):
          raise RuntimeError('debianized_package_dirname exists: %s' %
                             debianized_package_dirname)
      #    A. move debianized tree away
      os.rename(fullpath_repackaged_dirname, debianized_package_dirname )
      if orig_sdist is not None:
          #    B. expand repackaged original tarball
          tmp_dir = os.path.join(dist_dir,'tmp-expand')
          os.mkdir(tmp_dir)
          try:
              expand_tarball(orig_sdist,cwd=tmp_dir)
              orig_tarball_top_contents = os.listdir(tmp_dir)
  
              # make sure original tarball has exactly one directory
              assert len(orig_tarball_top_contents)==1
              orig_dirname = orig_tarball_top_contents[0]
              fullpath_orig_dirname = os.path.join(tmp_dir,orig_dirname)
  
              #    C. remove original repackaged tree
              shutil.rmtree(fullpath_orig_dirname)
  
          finally:
              shutil.rmtree(tmp_dir)
  
      if 1:
          # check versions of debhelper and python-all
          debhelper_version_str = get_version_str('debhelper')
          if len(debhelper_version_str)==0:
              log.warn('This version of stdeb requires debhelper >= %s, but you '
                       'do not have debhelper installed. '
                       'Could not check compatibility.'%DH_MIN_VERS)
          else:
              if not dpkg_compare_versions(
                  debhelper_version_str, 'ge', DH_MIN_VERS ):
                  log.warn('This version of stdeb requires debhelper >= %s. '
                           'Use stdeb 0.3.x to generate source packages '
                           'compatible with older versions of debhelper.'%(
                      DH_MIN_VERS,))
  
          python_defaults_version_str = get_version_str('python-all')
          if len(python_defaults_version_str)==0:
              log.warn('This version of stdeb requires python-all >= %s, '
                       'but you do not have this package installed. '
                       'Could not check compatibility.'%PYTHON_ALL_MIN_VERS)
          else:
              if not dpkg_compare_versions(
                  python_defaults_version_str, 'ge', PYTHON_ALL_MIN_VERS):
                  log.warn('This version of stdeb requires python-all >= %s. '
                           'Use stdeb 0.6.0 or older to generate source packages '
                           'that use python-support.'%(
                      PYTHON_ALL_MIN_VERS,))
  
      #    D. restore debianized tree
      os.rename(fullpath_repackaged_dirname+'.debianized',
                fullpath_repackaged_dirname)
  
      #    Re-generate tarball using best practices see
      #    http://www.debian.org/doc/developers-reference/ch-best-pkging-practices.en.html
  
      if sign_dsc:
          args = ()
      else:
          args = ('-uc','-us')
  
      dpkg_buildpackage('-S','-sa',*args,cwd=fullpath_repackaged_dirname)
  
      if 1:
          shutil.rmtree(fullpath_repackaged_dirname)
  
      if not remove_expanded_source_dir:
          # expand the debian source package
          dsc_name = debinfo.source + '_' + debinfo.dsc_version + '.dsc'
          dpkg_source('-x',dsc_name,
                      cwd=dist_dir)