2014年10月18日星期六

创建git私有仓库

以ubuntu服务器为例,如果要创建小范围的私有git服务器,是非常简单的,只需要如下几个简单步骤:
Step 1: 安装git
直接通过sudo apt-get install git即可完成。
Step 2: 创建git用户
git用户用来通过SSH连接git服务,输入命令:
$ sudo adduser git
Step 3: 创建证书登录
首先收集所有需要登录的用户公钥,然后导入到/home/git/.ssh/authorized_keys文件即可。
Step 4: 初始化git仓库
假设仓库位于/srv/sample.git,在/srv目录下输入命令:
$ sudo git init --bare sample.git
这样就创建了一个裸仓库,裸仓库没有working dir,因为服务器上的git仓库纯粹是为了共享,仓库目录一般以.git结尾。然后把owner改为git:
$ sudo chown -R git:git sample.git
Step 5: 防止登录shell
出于安全考虑,git用户不应该登录shell,可以编辑/etc/passwd,找到类似一行:
git:x:1001:1001:,,,:/home/git:/bin/bash
改为:
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
这样,git用户可以正常通过ssh使用git,但无法登录shell。
Step 6: 克隆仓库
在客户端就可以通过ssh克隆仓库了:
$ git clone git@server:/srv/sample.git
Cloning into 'sample'...
warning: You appear to have cloned an empty repository.
然后,就可以正常推送了:
$ touch README
$ git add README
$ git commit -m "add readme"
$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 212 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@ubuntu:/srv/sample.git
 * [new branch]      master -> master

2014年10月16日星期四

Python: List Comprehensions

Python: List Comprehensions

Note: Lines beginning with ">>>" and "..." indicate input to Python (these are the default prompts of the interactive interpreter). Everything else is output from Python.
Python supports a concept called "list comprehensions". It can be used to construct lists in a very natural, easy way, like a mathematician is used to do.
The following are common ways to describe lists (or sets, or tuples, or vectors) in mathematics.
S = {x² : x in {0 ... 9}}
V = (1, 2, 4, 8, ..., 2¹²)
M = {x | x in S and x even}
You probably know things like the above from mathematics lessons at school. In Python, you can write these expression almost exactly like a mathematician would do, without having to remember any special cryptic syntax.
This is how you do the above in Python:
>>> S = [x**2 for x in range(10)]
>>> V = [2**i for i in range(13)]
>>> M = [x for x in S if x % 2 == 0]
>>> 
>>> print S; print V; print M
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]
[0, 4, 16, 36, 64]
I'm sure you want to see a more complicated example. :-) The following is yet another way to compute prime numbers. The interesting thing is that we first build a list of non-prime numbers, using a single list comprehension, then use another list comprehension to get the "inverse" of the list, which are prime numbers.
>>> noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]
>>> primes = [x for x in range(2, 50) if x not in noprimes]
>>> print primes
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
NB: You can nest list comprehensions inside of each other, so you could write the above example with a single statement (without the need for the temporary variable "noprimes"). However, such lines tend to get long and less readable, so this is not recommended.
Of course, list comprehensions don't only work for numbers. Lists can contain any type of elements, including strings, nested lists and functions. You can even mix different types within a list.
The following works on a list of strings and produces a list of lists. Each of the sublists contains two strings and an integer.
>>> words = 'The quick brown fox jumps over the lazy dog'.split()
>>> print words
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
>>> 
>>> stuff = [[w.upper(), w.lower(), len(w)] for w in words]
>>> for i in stuff:
...     print i
... 
['THE', 'the', 3]
['QUICK', 'quick', 5]
['BROWN', 'brown', 5]
['FOX', 'fox', 3]
['JUMPS', 'jumps', 5]
['OVER', 'over', 4]
['THE', 'the', 3]
['LAZY', 'lazy', 4]
['DOG', 'dog', 3]
>>> 
>>> stuff = map(lambda w: [w.upper(), w.lower(), len(w)], words)
>>> for i in stuff:
...     print i
... 
['THE', 'the', 3]
['QUICK', 'quick', 5]
['BROWN', 'brown', 5]
['FOX', 'fox', 3]
['JUMPS', 'jumps', 5]
['OVER', 'over', 4]
['THE', 'the', 3]
['LAZY', 'lazy', 4]
['DOG', 'dog', 3]
The above example also demonstrates that you can do exactly the same thing with map() and a lambda function. However, there are cases when you cannot use map() and have to use a list comprehension instead, or vice versa. When you can use both, then it is often preferable to use a list comprehension, because this is more efficient and easier to read, most of the time.
You cannot use list comprehensions when the construction rule is too complicated to be expressed with "for" and "if" statements, or if the construction rule can change dynamically at runtime. In this case, you better use map() and / or filter() with an appropriate function. Of course, you can combine that with list comprehensions.

装机的一次感想

昨天在我的笔记本上装了1个Linux系统。
我先装了1个windows xp系统,然后下载了debian的mini iso,可以通过网络进行安装。
在将mini iso解压安装setup.exe后,重启机器,进入到debian的安装界面,后面在硬盘分区的时候,将整个硬盘全部用为linux,安装完毕后,发现windowxp系统不存在了。

我将想到了一个成语:鸠占鹊巢。

在将debian的启动iso装到内存后,就可以重新格式化硬盘,并通过网络下载其它附加的库等内容。跟生物界有很多相似,也许就是模仿的生物界吧。

每日英语

Anyone who lives within their means suffers from a lack of imagination.
吃穿不愁,神思不游。

2014年10月15日星期三

创建插入mysql数据库的python脚本

创建插入mysql数据库的python脚本
#!/usr/bin/env python
#coding=utf-8

#导入相关模块
import MySQLdb
import datetime
starttime = datetime.datetime.now()
#建立和mysql数据库的连接
conn = MySQLdb.connect(host='localhost',user='root',passwd='eagle')
#获取游标
curs = conn.cursor()
#执行SQL,创建一个数据库
#curs.execute("create database pythondb")
#选择连接哪个数据库
conn.select_db('pythondb')
#执行SQL,创建一个表
#curs.execute("create table test(id int,message varchar(50))")
#插入一条记录
#value = [1,"eagle"]
#curs.execute("insert into test values(%s,%s)",value)
#插入多条记录
values = []
for i in range(20000):
    values.append((i,'hello mysqldb' + str(i)))
curs.executemany("insert into test values(%s,%s)",values)
#提交修改                              
conn.commit()
#关闭游标连接,释放资源
curs.close()
#关闭连接
conn.close()
endtime=datetime.datetime.now()
print (endtime-starttime).seconds

每日英语

Friend is who can give you strength at last.
朋友是在最后可以给你力量的人。

2014年10月14日星期二

在用python脚本开windows azure虚机报错:ssl.SSLError: [Errno 336265218] _ssl.c:355: error:140B0002:SSL routines:SSL_CTX_use_PrivateKey_file:system lib

代码:
#coding:utf-8
#!/usr/bin/python
#
from azure import *

from azure.servicemanagement import *

from azure.servicemanagement.servicemanagementservice import ServiceManagementService

subscription_id = '9ffdf972-9f2c-4d25-9e68-6e6eaffd2cd7'

certificate_path = '/home/eagle/azure_demo/azuredemo.pem'

sms = ServiceManagementService(subscription_id, certificate_path)

result = sms.list_locations()

for location in result:

    print(location.name)


在用python脚本开windows azure虚机报错:
ssl.SSLError: [Errno 336265218] _ssl.c:355: error:140B0002:SSL routines:SSL_CTX_use_PrivateKey_file:system lib


发生该问题的原因为:
我使用的linux系统,未创建订阅的管理证书,创建成功后,脚本不会再报错

Linux下制作windows azure订阅管理证书

要使用编程方式管理订阅,需要一个订阅ID,和一个管理证书。

下面是Linux的制作证书的方法。

建立一个管理证书

任何与 Windows Azure 的交互都需要两个东西:
  • 一个订阅ID,
  • 一个 X509v3 管理证书。
我们假设你使用 Linux 运行这个脚本, 如果没有安装OpenSSL,请从root提示使用如下命令:
yum install openssl
以下将创建一个 .pem 文件,之后可被翻译成一个 .cer 文件,并导出和上传到Windows Azure。
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout elasta.pem -out elasta.pem
用下面命令导出 .cer:
openssl x509 -inform pem -in elasta.pem -outform der -out elasta.cer
这样你就得到它了,一个可以上传到你的 Windows Azure 订阅的管理证书。当做完这个时,你应该已经能够以编程方式使用 Windows Azure 了。

每日英语

You only live once, but if you do it right, once is enough.​
年华没虚度,一生也足矣。