2

I wrote a couple of scripts to help users pull their current kernel source package, apply some patches of mine, and build it as a .deb package with make-kpkg. One of the few steps that requires manual intervention is enabling the "Source" downloads via:

  • "Ubuntu Software Center"
    • Edit...
      • Software Sources...
        • "Source Code"

enter image description here

Is there a way to do this automatically via the command line? It's to my understanding I could just un-comment the first few "deb-src" lines in /etc/apt/sources.list, like so:

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://ca.archive.ubuntu.com/ubuntu/ trusty main restricted
deb-src http://ca.archive.ubuntu.com/ubuntu/ trusty main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://ca.archive.ubuntu.com/ubuntu/ trusty-updates main restricted
deb-src http://ca.archive.ubuntu.com/ubuntu/ trusty-updates main restricted

This is somewhat error-prone though, given the sed scripts I've written don't necessarily work if the sources are out of the default order, or various other reasons. More importantly, I'd like this to work on Ubuntu 14.04, Ubuntu 16.04, etc (ie: as generic as possible).

Are there any command-line apt-related tools to accomplish this via a shell script?

Thank you.

Cloud
  • 527

4 Answers4

4

In the end, I just enabled all the src repos.

sed -i '/^#\sdeb-src /s/^#//' "/etc/apt/sources.list"

This doesn't require upgrading the OS to an arbitrary version.

Cloud
  • 527
2

I had a similar problem as you had and finaly I started doing the following script, to write a temporary sources.list and later removing it again:

# making a copy of the users original sources.list file
cp -v /etc/apt/sources.list /etc/apt/sources.list.orig &&

# writing a custom sources.list file
cat > /etc/apt/sources.list << "EOF"
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted
deb http://archive.ubuntu.com/ubuntu/ xenial main restricted
deb-src http://archive.ubuntu.com/ubuntu/ xenial multiverse main universe restricted
deb http://archive.ubuntu.com/ubuntu/ xenial-updates main restricted
deb-src http://archive.ubuntu.com/ubuntu/ xenial-updates multiverse main universe restricted
deb http://archive.ubuntu.com/ubuntu/ xenial universe
deb http://archive.ubuntu.com/ubuntu/ xenial-updates universe
deb http://archive.ubuntu.com/ubuntu/ xenial multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-updates multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://dearchive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://security.ubuntu.com/ubuntu xenial-security main restricted
deb-src http://security.ubuntu.com/ubuntu xenial-security multiverse main universe restricted
deb http://security.ubuntu.com/ubuntu xenial-security universe
deb http://security.ubuntu.com/ubuntu xenial-security multiverse
EOF

# running an update
apt-get update &&
apt-get -y dist-upgrade &&

# DO YOUR STUFF

# putting back the original sources.list
mv -v /etc/apt/sources.list /etc/apt/sources.list.tmp &&
cp -v /etc/apt/sources.list.orig /etc/apt/sources.list &&

# removing backup files
rm -rfv /etc/apt/sources.list.tmp &&
rm -rfv /etc/apt/sources.list.orig
Videonauth
  • 33,815
0

I've put together a perl one-liner that will uncomment only the deb-src lines that are preceded by an uncommented deb line:

perl -pi.orig -0e 's/^(deb .*\n)# (deb-src)/$1$2/mg' /etc/apt/sources.list

It will also keep the original one as /etc/apt/sources.list.orig. (so you can restore it later if you want)

This should work across releases, assuming they continue their habit of putting commented-out deb-src lines right after their corresponding deb lines.

Tara
  • 423
0

Connecting various pieces together, I came up with an easily reversible solution for enabling and disabling source downloads.

Enable source

From cloud and Stéphane Gourichon's solution, with sed modified to ignore non-matching lines and the output redirected with sudo to a temporary file under sources.list.d/:

sed -e '/^#\sdeb-src /s/^# *//;t;d' "/etc/apt/sources.list" \
| sudo tee /etc/apt/sources.list.d/source-repos-tmp.list > /dev/null

Disable source

sudo rm /etc/apt/sources.list.d/source-repos-tmp.list
Ben Mares
  • 217