4
5
2011
6

添加软件仓库到系统的小程序:addrepo(Fedora-Only)

代码已经推送到我的github,这里不再更新了

https://github.com/ekd123/ekode/tree/master/RepoUtilities (还有delrepo)

看ubuntu的一句话添加仓库的命令很眼红,为什么fedora没有?

为了让仓库添加更方便,我花了一个小时写了一个100行的python程序(看来我python才入门,Orz,其实都用在查找怎么下载文件上了)。

刚开始准备用shell写,刚写10行就又用Python写了(想了一会,shell基本上无力高级情况)

(比较丰富的例子,入门python可以看看~)

(本来里面用到了try..catch..,用来处理网址无效的,但是他居然说has no,一怒之下删掉了)

#!/usr/bin/python

## addrepo (GNU LGPLv3+)
## Written by Mike Ma
##
## This file can help you add a repo
## to your system

import os
import getpass
import sys
import urllib

#def var
repo_dir="/etc/yum.repos.d"
type=""
info1=""
info2=""
sfp_header="http://repos.fedorapeople.org/repos"

#def func
def show_usage():
    print ("""Usage: addrepo TYPE INFO [INFO]
TYPE:
     www       download repo from Internet.
                   info1: URL.
     sfp       standard fedora people repo. need two information
                   info1: group name.
                   info2: software name.
     fp        fedora people repo. need two information
                   info1: The username in FedoraPeople.
                   info2: repo file name. (no .repo)""")

def download_by_url(url,filename):
    print ("downloading " + url + " to " + filename)
    data = urllib.urlopen(url).read()
    f = file(filename,"w")
    f.write(data)
    f.close()
    print (data)
    print ("Done. Please check the file (if it's wrong, please delete that file and try the correct URL again)")

def check_http_and_add(url):
    if url.find("http://") == -1:
        return "http://" + url
    return url

def get_filename_from_url(url):
    arr=url.split("/")
    return arr[len(arr)-1]

def check_string(sstr):
    if sstr==None: return False
    if sstr==""  : return False
    return True

def show_error_and_exit_info():
    print ("information is invalid!")
    exit(-1)

# is it fedora?
if os.path.exists("/etc/fedora-release") == False:
    print ("You must run addrepo in Fedora");
    exit(-1)

# arg...
if len(sys.argv) < 3:
    show_usage()
    exit(-1)
if len(sys.argv) > 4:
    print ("Too many arguments")
    show_usage()
    exit (-1)
# is it root?
if getpass.getuser() != "root":
    print ("You must run addrepo as root")
    exit(-1)
# check dir
if os.path.exists(repo_dir) == False:
    print ("Can't find "+repo_dir)
    exit(-1)

############################################
type = sys.argv[1]  # doesn't need to check
info1 = sys.argv[2]
if check_string(info1) == False: show_error_and_exit_info()
if len(sys.argv) == 4: info2 = sys.argv[3]
if check_string(info2) == False: show_error_and_exit_info()

if type == "www":
    print ("Type: Repo from Internet")
    info1 = check_http_and_add(info1)
    download_by_url(info1, repo_dir + "/" + get_filename_from_url(info1))
    exit (0)
elif type == "sfp":
    print ("Type: Standard Fedorapeople.org Repo")
    url = sfp_header+"/"+info1+"/" + info2 + "/fedora-"+info2+".repo"
    download_by_url(url, repo_dir + "/" + get_filename_from_url(url))
    exit (0)
elif type == "fp":
    print ("Type: Non-standard Fedorapeople.org Repo")
    url = "http://"+info1+".fedorapeople.org/"+info2+".repo"
    download_by_url(url, repo_dir + "/" + get_filename_from_url(url))
    exit (0)
else:
    print ("Invalid type")
    show_usage()
    exit (0)

把它丢在/usr/bin下面,命名为addrepo,然后chmod +x addrepo就能用了。

有了这个东东,你就可以这样添加iceplayer的RPM源:

addrepo sfp zhtx iceplayer

(已死,别用)

这样添加chromium源:

addrepo sfp spot chromium

这样安装163源:

addrepo www mirrors.163.com/.help/fedora-163.repo
addrepo www mirrors.163.com/.help/fedora-updates-163.repo

(这里有自动判断有无http://功能)

哇咔咔,很方便吧?

其实我最大的希望是让这个东东成为Fedora的预装组件

如果有py大牛,修改了一定要给我说说啊,我当然也想用更好的~

PS:如果你在非Fedora的Linux上使用,结果会是……嘿嘿

Category: 技术 | Tags: | Read Count: 3439
Blackrose 说:
Apr 05, 2011 05:38:11 PM

有意思,表示赞赏!

Avatar_small
Robin Lee 说:
Apr 05, 2011 06:04:35 PM

不如考虑改进成管理软件库,既可以增加,又可以删除

还有rpmfusion也该支持

Avatar_small
纵横天下 说:
Apr 05, 2011 09:16:33 PM

名字就叫addrepo,如果是"管理",就脱题了(表示这是被语文老师害的)。

rpmfusion就是两个rpm,yum就行了。

(就算是删除,也可以直接rm)

Avatar_small
纵横天下 说:
Apr 05, 2011 09:56:37 PM

好了,增加了一个delrepo命令,见
https://github.com/ekd123/ekode/blob/master/RepoUtilities/delrepo

Avatar_small
纵横天下 说:
Apr 05, 2011 10:08:38 PM

写的不怎么地,这个版本甚至有一点BUG,在Git版里面修复了

ray 说:
Aug 13, 2011 05:21:55 PM

代码没什么好说的,亮点是楼主估计就是高中生。fedora,python入门了。


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Powered by Chito | Hosted at is-Programmer | Theme: Aeros 2.0 by TheBuckmaker.com