以简单的终端方式添加和删除更新频道

A.B*_*.B. 15 command-line apt scripts development

我知道 Ubuntu 中有一个 GUISoftware & Updates可以启用更新通道

  • 更新
  • 建议的
  • 向后移植
  • 安全

如此屏幕截图所示:

在此处输入图片说明

我正在寻找一种简单的方法来使用命令从终端内执行此操作,例如

sudo apt-add-update enable updates
sudo apt-add-update enable proposed
sudo apt-add-update enable backports
sudo apt-add-update enable security

sudo apt-add-update disable updates
sudo apt-add-update disable proposed
sudo apt-add-update disable backports
sudo apt-add-update disable security
Run Code Online (Sandbox Code Playgroud)

还有一件额外的事情

sudo apt-add-update enable default

sudo apt-add-update disable default
Run Code Online (Sandbox Code Playgroud)

一些示例以更好地理解

  1. 一个空的 sources.list

  2. 一个已启用的存储库 ( main)

  3. 一行或两行中的两个或多个启用的存储库

  4. 有deb-src条目

    • cat /etc/apt/sources.list

      deb http://archive.ubuntu.com/ubuntu wily main universe
      deb-src http://archive.ubuntu.com/ubuntu wily main universe
      
      Run Code Online (Sandbox Code Playgroud)
    • sudo apt-add-update enable security

      deb http://archive.ubuntu.com/ubuntu wily main universe
      deb-src http://archive.ubuntu.com/ubuntu wily main universe
      deb http://archive.ubuntu.com/ubuntu wily-security main universe
      deb-src http://archive.ubuntu.com/ubuntu wily-security main universe
      
      Run Code Online (Sandbox Code Playgroud)
  5. 有非活动deb-src条目

    • cat /etc/apt/sources.list

      deb http://archive.ubuntu.com/ubuntu wily main universe
      # deb-src http://archive.ubuntu.com/ubuntu wily main universe
      
      Run Code Online (Sandbox Code Playgroud)
    • sudo apt-add-update enable security

      deb http://archive.ubuntu.com/ubuntu wily main universe
      # deb-src http://archive.ubuntu.com/ubuntu wily main universe
      deb http://archive.ubuntu.com/ubuntu wily-security main universe
      
      Run Code Online (Sandbox Code Playgroud)
  6. 那default件事

  7. 只有一个条目和disable动作

  8. 不同或相同存储库的不同或相同服务器,尊重每个服务器

    • cat /etc/apt/sources.list

      deb http://archive.ubuntu.com/ubuntu wily universe
      deb http://us.archive.ubuntu.com/ubuntu wily main
      
      Run Code Online (Sandbox Code Playgroud)
    • sudo apt-add-update enable security

      deb http://archive.ubuntu.com/ubuntu wily universe
      deb http://us.archive.ubuntu.com/ubuntu wily main
      deb http://archive.ubuntu.com/ubuntu wily-security universe
      deb http://us.archive.ubuntu.com/ubuntu wily-security main
      
      Run Code Online (Sandbox Code Playgroud)
  9. 不同存储库的不同 Ubuntu 版本,尊重每个版本

    • cat /etc/apt/sources.list

      deb http://archive.ubuntu.com/ubuntu wily main universe
      deb http://archive.ubuntu.com/ubuntu trusty main
      
      Run Code Online (Sandbox Code Playgroud)
    • sudo apt-add-update enable security

      deb http://archive.ubuntu.com/ubuntu wily main universe
      deb http://archive.ubuntu.com/ubuntu trusty main
      deb http://archive.ubuntu.com/ubuntu wily-security main universe
      deb http://archive.ubuntu.com/ubuntu trusty-security main
      
      Run Code Online (Sandbox Code Playgroud)
  10. ? 中的 PPA 或其他包源(非规范)sources.list。

    忽略!

  11. 不要更改协议,例如https, http, tor, ...

Ser*_*nyy 12

2015 年 11 月 27 日更新:

脚本的第二个版本仍然让我对源代码的臃肿以及我不得不使用几种不同的工具这一事实感到不满。因此我试图仅在 AWK 中重写脚本。

新脚本使用纯 AWK,并且更好地满足要求。本质上,它/etc/apt/sources.list每次运行时都会重写。

运行脚本后需要更新,所以sudo apt-get update在脚本成功退出后运行。

该文件必须启用可执行权限chmod +x add-update.awk并保存在$PATH变量中包含的任何目录中。特别建议将脚本保存到$HOME/bin文件夹(也必须附加到$PATH变量)。

原始/etc/apt/sources.list文件的备份不是必需的,但强烈建议。备份文件做

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

用法:

sudo ./add-update.awk -v ACTION=[enable|disable|help] -v SOURCE=[updates|backports|security|proposed]
Run Code Online (Sandbox Code Playgroud)

github上的源码

演示:

sergrep:$ cat /etc/apt/sources.list                                                                              
deb http://it.archive.ubuntu.com/ubuntu wily main universe
deb http://us.archive.ubuntu.com/ubuntu wily-backports main

deb-src http://it.archive.ubuntu.com/ubuntu wily main universe
deb-src http://us.archive.ubuntu.com/ubuntu wily-backports main
# removed

sergrep:$ sudo ./add-update.awk -v ACTION="enable" -v SOURCE="security"   
[sudo] password for xieerqi: 
/etc/apt/sources.list
<<< Script finished processing

sergrep:$ cat /etc/apt/sources.list                                                                              
deb http://it.archive.ubuntu.com/ubuntu wily main universe
deb http://it.archive.ubuntu.com/ubuntu wily-security  main universe
deb http://us.archive.ubuntu.com/ubuntu wily-backports main
deb http://us.archive.ubuntu.com/ubuntu wily-security  main
deb-src http://it.archive.ubuntu.com/ubuntu wily main universe
deb-src http://it.archive.ubuntu.com/ubuntu wily-security  main universe
deb-src http://us.archive.ubuntu.com/ubuntu wily-backports main
deb-src http://us.archive.ubuntu.com/ubuntu wily-security  main
Run Code Online (Sandbox Code Playgroud)

源代码

#!/usr/bin/awk -f
#
###########################################################
# Author: Serg Kolo
# Date: Nov 27,2015
# Purpose: A script that enables/disables 4 ubuntu sources
# (namely updates, backports, proposed, and security )
# much in a way like software-properties-gtk does
# Written for:  http://paste.ubuntu.com/13434218/
###########################################################
#
# Permission to use, copy, modify, and distribute this software is hereby granted
# without fee, provided that  the copyright notice above and this permission statement
# appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

function printUsage() {
    print "Usage: sudo ./add-update.awk -v ACTION=[enable|disable|help] -v SOURCE=[updates|backports|security|proposed]";
    exit
}

function checkSourceEnabled()
{
    if ( $3 ~ SOURCE) {
        print SOURCE" is enabled; exiting"
        VAL = 1
    }
    else {
        VAL = 0
    }
    return VAL
}

function disableSource()
{
    if ( $0 ~ SOURCE ) $0="# removed";
    j++;
    newLines[j]=$0;
}

function listStuff () {
    for(k=4; k<=NF; k++) if ( $k~/#/  ) {break} else {
            COMPONENTS=COMPONENTS" "$k
        };
    gsub(/\-.*/,"",$3);
    STRING=$1" "$2" "$3APPEND" "COMPONENTS;
    COMPONENTS=""
               return STRING;
}

function replaceFile()
{
    command="mv  /tmp/sources.list "ARGV[1]
            system(command);
}

############
#  MAIN
#############
BEGIN {

# argument checking sequence
# must remain written in if-else
# structure rather than case,
# to consider users who may not be able
# to install gawk due to broken sources.list
# which is what this script should be aimed at

# actions checked first so that 
# help message can be printed 
 if ( ACTION ==  "enable" ||
      ACTION == "disable" ||
      ACTION == "default"  ) {

    print "<<< ACTION ARG OK" 
  }
  else if (ACTION == "help" ){
    printUsage()
    exit
  }


  if ( SOURCE == "update" || 
       SOURCE == "security" || 
       SOURCE == "backports" || 
       SOURCE == "proposed" ) { 
       print "<<< SOURCE ARG OK"

  }

   else if ( ACTION != "default"  || ACTION != "help" ) {  
        print "<<< E: SOURCE ARG INCORRECT"; 
        printUsage(); 
        exit 1   }

    # static filename to operate on
    ARGV[ARGC++]="/etc/apt/sources.list";

    if (ACTION == "enable" ) {
        APPEND="-"SOURCE;
    } else{
        APPEND="";
    }

} # END OF BEGIN

$0~/^deb*/ && $0!~/partner/ && $0!~/extra/ {

    if ( ACTION == "enable" ) {
        j++;
        ARRAY[j]=$0
        ENABLED=checkSourceEnabled();

        if ( ENABLED ) {
            exit 1
        }
        else {
            j++;
            ARRAY[j]=listStuff();
        }

    }
    else if ( ACTION == "disable" ){
        disableSource() ;
    }
    else if ( ACTION == "default" && SOURCE == "default" ) {
        j++;
        defaultsArray[j]=$0;
        j++;
        defaultsArray[j]=listStuff();
    }
}

END {
    print "<<< Script finished processing" ;
    if ( ACTION =="enable" && ENABLED == 0 ){
     for(i=1;i<=j;i++)
        print ARRAY[i] |  "sort -u > /tmp/sources.list ";
     replaceFile();
     }
     else if ( ACTION == "disable" ) {
       for ( i=1;i<=j;i++  ) print newLines[i] | "sort -u > /tmp/sources.list"
       replaceFile();
     }
     else if (ACTION == "default" ){
        for ( i=1;i<=j;i++  ) print defaultsArray[i] | "sort -i -u > /tmp/sources.list"
        replaceFile();
     }
}

# END OF MAIN
Run Code Online (Sandbox Code Playgroud)

——

之前的版本

github 上的版本 #1(带有 AWK 和 SED 的 bash 脚本)

github 上的版本 #2(也是 bash 脚本)


xia*_*jie 12

我分析的源Ubuntu软件中心是写在python。

之后,我写了以下代码运行良好。aptsources是 Ubuntu 软件中心用来管理/etc/apt/sources.list和/etc/apt/sources.list.d.

#!/usr/bin/python

import aptsources.sourceslist
from subprocess import Popen, PIPE
import errno

import os

def _lsb_release():
    """Call lsb_release --idrc and return a mapping."""
    result = {'Codename': 'sid', 'Distributor ID': 'Debian',
              'Description': 'Debian GNU/Linux unstable (sid)',
              'Release': 'unstable'}
    try:
        out = Popen(['lsb_release', '-idrc'], stdout=PIPE).communicate()[0]
        # Convert to unicode string, needed for Python 3.1
        out = out.decode("utf-8")
        result.update(l.split(":\t") for l in out.split("\n") if ':\t' in l)
    except OSError as exc:
        if exc.errno != errno.ENOENT:
            logging.warning('lsb_release failed, using defaults:' % exc)
    return result


if __name__ == "__main__":
    if len(os.sys.argv) != 3 or \
        os.sys.argv[1] not in ['enable', 'disable'] or \
        os.sys.argv[2] not in ['updates', 'security', 'proposed', 'backports', 'default']:
        print "Usage: apt-add-update <enable|disable> <default|updates|security|proposed|backports>"
        os.sys.exit(0)

    release = _lsb_release()
    codename = release['Codename']

    sourcelist = aptsources.sourceslist.SourcesList()

    template = None

    main_uri = ""
    main_comps = []
    main_types = []
    child_comps = []
    child_types = []

    for tmpl in sourcelist.matcher.templates:
        if tmpl.name == codename and tmpl.distribution == "Ubuntu":
            template = tmpl
            break

    updated = False

    for source in sourcelist.list:
        if source.invalid:
            continue
        for comp in source.comps:
            if comp not in child_comps:
                child_comps.append(comp)

        if source.template and source.template.official and source.dist == codename and source.template.name == codename:
            main_uri = source.uri

            for comp in source.comps:
                if comp not in main_comps:
                    main_comps.append(comp)

            main_types.append(source.type)
        else:
            child_types.append(source.type)

        if source.dist == codename + "-" + os.sys.argv[2]:
            if os.sys.argv[1] == 'enable' and source.type in main_types:
                source.set_enabled(True)
            if os.sys.argv[1] == 'disable':
                source.set_enabled(False)
            updated = True

        if source.dist == codename and os.sys.argv[2] == "default":
            if os.sys.argv[1] == 'enable' and source.type in child_types:
                source.set_enabled(True)
            if os.sys.argv[1] == 'disable':
                source.set_enabled(False)
            updated = True

    if not main_uri:
        main_uri = "http://archive.ubuntu.com/ubuntu"

    if os.sys.argv[2] == "default" and not main_comps:
        main_comps = child_comps

    if not main_comps:
        main_comps = ['main', 'restricted', 'universe', 'multiverse']

    if not updated and os.sys.argv[1] != 'disable':

        if os.sys.argv[2] == "default":
            uri = main_uri
            if tmpl.base_uri:
                uri = tmpl.base_uri

            tmpl = template
            line = tmpl.type + " " + uri + " " + tmpl.name + " " + " ".join(main_comps)

            sourceentry = aptsources.sourceslist.SourceEntry(line)
            sourcelist.list.append(sourceentry)


        for tmpl in template.children:
            if tmpl.name != codename + "-" + os.sys.argv[2] and \
                not (os.sys.argv[2] == "default" and tmpl.name == codename):
                continue

            if os.sys.argv[2] == "default" and tmpl.type not in child_types:
                continue

            if os.sys.argv[2] != "default" and tmpl.type not in main_types:
                continue

            uri = main_uri
            if tmpl.base_uri:
                uri = tmpl.base_uri
            if not uri:
                continue

            line = tmpl.type + " " + uri + " " + tmpl.name + " " + " ".join(main_comps)

            sourceentry = aptsources.sourceslist.SourceEntry(line)
            sourcelist.list.append(sourceentry)

    sourcelist.save()
Run Code Online (Sandbox Code Playgroud)

使用文件名保存此代码,/usr/local/bin/apt-add-update. 然后运行如下。

$ sudo apt-add-update <enable|disable> <default|updates|security|proposed|backports>
Run Code Online (Sandbox Code Playgroud)

我为多发行版支持更新了它,例如和。trustywily

$ sudo apt-add-update <enable|disable> <default|updates|security|proposed|backports>
Run Code Online (Sandbox Code Playgroud)

参考网址是https://github.com/dupingping/apt-add-update/tree/master


use*_*.dz 7

用法:

## testing

apt-add-update  enable|disable  default|security|updates|proposed|backports  [file]


## real

sudo  apt-add-update  enable|disable  default|security|updates|proposed|backports
Run Code Online (Sandbox Code Playgroud)

这里的要点:

我解析了该文件,因为我自己正在寻找其他一些选项。

  • 检查是否有镜像,包括官方镜像(旧版本、端口)或社区提供的其他镜像,从python-apt包加载(它包含一个包含所有镜像的文件)。

    /usr/share/python-apt/templates/Ubuntu.mirrors
    
    Run Code Online (Sandbox Code Playgroud)
  • 始终在文件中保留官方 URI。因此,即使全部禁用,它也会为持久性设置保留一条注释行。它使用字母排序类型、url、dist、comp 将行解析为最小值。

  • 如果将输入文件作为第三个参数或没有写入权限,则打印到标准输出 /etc/apt/sources.list

  • 不支持[ options ]喜欢[ arch=amd64 ]。
  • 删除disable default了我强制用户指定要禁用的内容。
  • 我不喜欢最后一个选项 (9),因为我是故意混合 repo 的。因此,它们应该被视为第 3 方存储库。

下载:带有完整的测试集。

https://github.com/sneetsher/au_700860_apt_channels

脚本:

## testing

apt-add-update  enable|disable  default|security|updates|proposed|backports  [file]


## real

sudo  apt-add-update  enable|disable  default|security|updates|proposed|backports
Run Code Online (Sandbox Code Playgroud)


Pil*_*ot6 6

您可以通过使用非常简单的方式来完成add-apt-repository。

例如,您可以proposed通过添加

sudo add-apt-repository "http://archive.ubuntu.com/ubuntu/ $(lsb_release -sc)-proposed restricted universe multiverse main"
Run Code Online (Sandbox Code Playgroud)

它将添加deb和deb-src行到/etc/apt/sources.list. 但源代码行将被注释。

如果你add-apt-repository用-s参数运行,它不会注释掉deb-src.

您可以security通过

sudo add-apt-repository "http://security.ubuntu.com/ubuntu/ $(lsb_release -sc)-security restricted universe multiverse main"
Run Code Online (Sandbox Code Playgroud)

  • @AB不清楚你想要什么。您需要更简单的命令还是什么?此命令与您在 GUI 中所做的完全相同。 (2认同)

kos*_*kos 5

这正是所要求的;

如有必要,我会不时更新;

可以在此GitHub 存储库中找到最新版本;

要从 GitHub 存储库安装:

  • 安装git:sudo apt-get install git
  • 克隆存储库: git clone https://github.com/kos0/addRemoveDistribution.git

概要:

enableDisableDistribution.pl <enable|disable> <default|security|updates|proposed|backports>
Run Code Online (Sandbox Code Playgroud)

s1

s2

s3

enableDisableDistribution.pl <enable|disable> <default|security|updates|proposed|backports>
Run Code Online (Sandbox Code Playgroud)