为了保证团队成员提交的代码是符合规范的,可以使用pre-commit来做代码检查。

安装

pre-commit安装很方便,执行`pip install pre-commit’即可。

添加到git hooks

执行pre-commit install, 将pre-commit添加到git hooks中

配置

在项目根目录下,添加.pre-commit-config.yaml文件即可进行配置,如下就是一个配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
-   repo: https://github.com/pre-commit/pre-commit-hooks
sha: v0.7.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: autopep8-wrapper
- id: check-docstring-first
- id: check-json
- id: check-added-large-files
- id: check-yaml
- id: debug-statements

- id: requirements-txt-fixer
- repo: https://github.com/pre-commit/pre-commit
sha: v0.11.0
hooks:
- id: validate_config
- id: validate_manifest
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
files: \.py$
exclude: test_gevent.py
args: [--rcfile=.pylintrc, --load-plugins=pylint_django]

此后,每次提交代码时,都会进行代码规范检查。

联系作者

pylint用于Python代码规范检查。默认代码风格遵循PEP08

使用配置文件

配置文件可以通过如下命令生成

1
pylint --generate-rcfile > .pylintrc

执行pylint时,可以通过指定–rcfile参数来加载配置文件。而默认配置文件加载顺序可以参考命令行参数这节。

Django代码检查

对于Django, 有pylint-django这个pylint插件用来代码检查。pip install pylint-djangop安装后,添加–load-plugins参数即可启用,如pylint --load-plugins pylint_django

警告忽略

有时pylint的检查不满足需求,太繁琐,此时可以忽略它。如在for d in data:里,会报Invalid variable错误,即C0103, 此时加上# pylint: disable=C0103可以忽略这个警告。

联系作者

在编写测试时遇到表单上传文件的问题,问了同事后,给了stackoverflow上how to unit test file upload in django链接, 在django.test.Client.post里看到如下例子

1
2
3
>>> c = Client()
>>> with open('wishlist.doc') as fp:
... c.post('/customers/wishes/', {'name': 'fred', 'attachment': fp})

对于图片,需要加上rb模式,例子如下

1
2
3
>>> c = Client()
>>> with open('wishlist.png', 'rb') as fp:
... c.post('/customers/wishes/', {'name': 'fred', 'attachment': fp})

解决问题。

联系作者

CentOS上安装mysql, 使用yum安装很方便。

按照A Quick Guide to Using the MySQL Yum Repository里的步骤即可。

配置镜像仓库

Download MySQL Yum Repository下载仓库, 服务器是CentOS6, 于是下载mysql57-community-release-el6-9.noarch.rpm,之后执行rpm -i mysql57-community-release-el6-9.noarch.rpm

下载镜像

yum install mysql-community-server下载mysql

启动MySQL

service mysqld start

修改root用户密码

  • 使用grep ‘temporary password’ /var/log/mysqld.log找到root用户密码,
  • mysql -uroot -p登录,输入grep得到的密码
  • 执行ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';修改root用户密码

联系作者

git克隆仓库时,报如下错误

1
2
3
4
Initialized empty Git repository in 
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/maqmall/eyaos_signature.git/info/refs

fatal: HTTP request failed

参考HTTPS cloning errors,发现是git版本过低,用的CentOS6默认是1.7.1, 于是升级git版本。如何升级git, 可参考之前写的CentOS6.5升级git

联系作者