Skip to content

Commit 45b8ed0

Browse files
committed
commit N
1 parent 2278abf commit 45b8ed0

24 files changed

Lines changed: 866 additions & 35 deletions

demo/git_demo/1.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
username: oxidized # 单台设备时以此为准
3+
password: S3cr3tx # 单台设备时以此为准
4+
model: junos # 单台设备时以此为准
5+
interval: 3600 # 配置备份的间隔时间
6+
log: ~/.config/oxidized/log # 日志文件
7+
debug: false
8+
threads: 30 # 线程
9+
timeout: 20 # 超时时间
10+
retries: 3 # 失败重试次数
11+
prompt: !ruby/regexp /^([\w.@-]+[#>]\s?)$/ # 登录设备后的提示符判断
12+
crash:
13+
directory: ~/.config/oxidized/crashes
14+
hostnames: false
15+
rest: 0.0.0.0:8888 # 前端界面
16+
pid: ~/.config/oxidized/oxidized.pid
17+
input: # 连接方式
18+
default: ssh, telnet
19+
debug: false
20+
ssh:
21+
secure: false
22+
output:
23+
default: git # 以git方式存储
24+
git:
25+
user: Oxidized
26+
email: oxidized@example.com
27+
repo: "~/.config/oxidized/oxidized.git"
28+
source:
29+
default: csv
30+
csv:
31+
file: ~/.config/oxidized/router.db # CSV 格式的主机数据库
32+
delimiter: !ruby/regexp /:/
33+
map:
34+
name: 0 # CSV 里面的值映射关系
35+
ip: 1
36+
model: 2
37+
username: 3
38+
password: 4
39+
group: 5
40+
vars_map:
41+
enable: 6
42+
groups: # 为每个组进行单独的配置
43+
mikrotik:
44+
username: admin
45+
password: blank
46+
ubiquiti:
47+
username: ubnt
48+
password: ubnt
49+
model_map: # 为每种设备类型进行单独的配置
50+
cisco: ios
51+
username: admin
52+
password: password
53+
juniper: junos
54+
ironware:
55+
username: admin
56+
password: password
57+
vars:
58+
enable: enablepassword

demo/git_demo/git_demo1.ipynb

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"[官网文档](https://gitpython.readthedocs.io/en/stable/tutorial.html)"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 30,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"import logging\n",
17+
"import git\n",
18+
"import os\n",
19+
"logging.basicConfig(level=logging.DEBUG)"
20+
]
21+
},
22+
{
23+
"cell_type": "markdown",
24+
"metadata": {},
25+
"source": [
26+
"创建一个仓库目录"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 19,
32+
"metadata": {},
33+
"outputs": [
34+
{
35+
"name": "stdout",
36+
"output_type": "stream",
37+
"text": [
38+
"/root/learn-python/demo/git_demo/my_repo\n"
39+
]
40+
}
41+
],
42+
"source": [
43+
"# root_dir = os.path.abspath(os.path.dirname(__file__))\n",
44+
"root_dir = os.getcwd()\n",
45+
"my_repo_dir = os.path.join(root_dir,\"my_repo\")\n",
46+
"print(my_repo_dir)"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 20,
52+
"metadata": {},
53+
"outputs": [
54+
{
55+
"name": "stderr",
56+
"output_type": "stream",
57+
"text": [
58+
"DEBUG:git.cmd:Popen(['git', 'init'], cwd=/root/learn-python/demo/git_demo/my_repo, universal_newlines=False, shell=None, istream=None)\n"
59+
]
60+
},
61+
{
62+
"name": "stdout",
63+
"output_type": "stream",
64+
"text": [
65+
"False\n"
66+
]
67+
}
68+
],
69+
"source": [
70+
"# 创建一个仓库,返回 Repo 对象\n",
71+
"my_repo = git.Repo.init(my_repo_dir)\n",
72+
"print(my_repo.bare)"
73+
]
74+
},
75+
{
76+
"cell_type": "markdown",
77+
"metadata": {},
78+
"source": [
79+
"[What is a bare git repository?](https://www.saintsjd.com/2011/01/what-is-a-bare-git-repository/)"
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"execution_count": 21,
85+
"metadata": {},
86+
"outputs": [
87+
{
88+
"name": "stderr",
89+
"output_type": "stream",
90+
"text": [
91+
"DEBUG:git.cmd:Popen(['git', 'init', '--bare'], cwd=/root/learn-python/demo/git_demo/my_bare_repo, universal_newlines=False, shell=None, istream=None)\n"
92+
]
93+
},
94+
{
95+
"name": "stdout",
96+
"output_type": "stream",
97+
"text": [
98+
"True\n"
99+
]
100+
}
101+
],
102+
"source": [
103+
"# 创建一个 bare 仓库,返回 Repo 对象\n",
104+
"bare_repo_dir = os.path.join(root_dir,\"my_bare_repo\")\n",
105+
"bare_repo = git.Repo.init(repo_dir, bare=True)\n",
106+
"print(bare_repo.bare)"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 39,
112+
"metadata": {},
113+
"outputs": [
114+
{
115+
"name": "stderr",
116+
"output_type": "stream",
117+
"text": [
118+
"DEBUG:git.cmd:Popen(['git', 'diff', '--cached', '--abbrev=40', '--full-index', '--raw'], cwd=/root/learn-python/demo/git_demo/my_repo, universal_newlines=False, shell=None, istream=None)\n"
119+
]
120+
},
121+
{
122+
"data": {
123+
"text/plain": [
124+
"True"
125+
]
126+
},
127+
"execution_count": 39,
128+
"metadata": {},
129+
"output_type": "execute_result"
130+
}
131+
],
132+
"source": [
133+
"# 检查仓库是否是 dirty,即存在没有提交的文件\n",
134+
"my_repo.is_dirty()\n"
135+
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": 40,
140+
"metadata": {},
141+
"outputs": [
142+
{
143+
"name": "stderr",
144+
"output_type": "stream",
145+
"text": [
146+
"DEBUG:git.cmd:Popen(['git', 'diff', '--cached', '--abbrev=40', '--full-index', '--raw'], cwd=/root/learn-python/demo/git_demo/my_repo, universal_newlines=False, shell=None, istream=None)\n"
147+
]
148+
},
149+
{
150+
"data": {
151+
"text/plain": [
152+
"True"
153+
]
154+
},
155+
"execution_count": 40,
156+
"metadata": {},
157+
"output_type": "execute_result"
158+
}
159+
],
160+
"source": [
161+
"# 新建一个文件,再次查看 is_dirty() 属性\n",
162+
"file_name = os.path.join(my_repo_dir,\"readme.md\")\n",
163+
"open(file_name,'wb').close()\n",
164+
"# my_repo.index.add([file_name])\n",
165+
"my_repo.is_dirty()"
166+
]
167+
},
168+
{
169+
"cell_type": "code",
170+
"execution_count": 41,
171+
"metadata": {},
172+
"outputs": [
173+
{
174+
"name": "stderr",
175+
"output_type": "stream",
176+
"text": [
177+
"DEBUG:git.cmd:Popen(['git', 'status', '--porcelain', '--untracked-files'], cwd=/root/learn-python/demo/git_demo/my_repo, universal_newlines=False, shell=None, istream=None)\n"
178+
]
179+
},
180+
{
181+
"data": {
182+
"text/plain": [
183+
"[]"
184+
]
185+
},
186+
"execution_count": 41,
187+
"metadata": {},
188+
"output_type": "execute_result"
189+
}
190+
],
191+
"source": [
192+
"# 查看未提交的文件\n",
193+
"my_repo.untracked_files"
194+
]
195+
}
196+
],
197+
"metadata": {
198+
"interpreter": {
199+
"hash": "b9ad67ebd6cda51a6170d8f3102c2ab5635d4edf4d086fb17b0c03dbb914c2a6"
200+
},
201+
"kernelspec": {
202+
"display_name": "Python 3.8.12 64-bit",
203+
"language": "python",
204+
"name": "python3"
205+
},
206+
"language_info": {
207+
"codemirror_mode": {
208+
"name": "ipython",
209+
"version": 3
210+
},
211+
"file_extension": ".py",
212+
"mimetype": "text/x-python",
213+
"name": "python",
214+
"nbconvert_exporter": "python",
215+
"pygments_lexer": "ipython3",
216+
"version": "3.8.12"
217+
},
218+
"orig_nbformat": 4
219+
},
220+
"nbformat": 4,
221+
"nbformat_minor": 2
222+
}

demo/git_demo/git_demo1.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import logging
2+
import git
3+
import os
4+
5+
6+
"""https://github.com/nautobot/nautobot/blob/develop/nautobot/utilities/git.py"""
7+
"""https://gitpython.readthedocs.io/en/stable/tutorial.html"""
8+
9+
logging.basicConfig(level=logging.DEBUG)
10+
11+
root_dir = os.path.abspath(os.path.dirname(__file__))
12+
repo_dir = os.path.join(root_dir,"my_repo")
13+
14+
def init_repo(repo, *args, **kwargs):
15+
r = git.Repo.init(repo,*args, **kwargs)
16+
logging.info("创建仓库 %s 成功"%(repo))
17+
return r
18+
19+
# bare = False:在仓库目录下新建 `.git` 目录
20+
# bare = True:本来应该在 `.git`` 目录下的文件创建在了仓库根目录下
21+
22+
r = init_repo(repo_dir,bare=True)
23+
print(r.bare)
24+
25+
26+
# file_name = os.path.join(repo_dir,"readme.md")
27+
# open(file_name,'wb').close()
28+
29+
# r.index.add([file_name])
30+
# r.index.commit("first commit")

demo/git_demo/my_bare_repo/HEAD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master

demo/git_demo/my_bare_repo/config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unnamed repository; edit this file 'description' to name the repository.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message taken by
4+
# applypatch from an e-mail message.
5+
#
6+
# The hook should exit with non-zero status after issuing an
7+
# appropriate message if it wants to stop the commit. The hook is
8+
# allowed to edit the commit message file.
9+
#
10+
# To enable this hook, rename this file to "applypatch-msg".
11+
12+
. git-sh-setup
13+
test -x "$GIT_DIR/hooks/commit-msg" &&
14+
exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"}
15+
:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message.
4+
# Called by "git commit" with one argument, the name of the file
5+
# that has the commit message. The hook should exit with non-zero
6+
# status after issuing an appropriate message if it wants to stop the
7+
# commit. The hook is allowed to edit the commit message file.
8+
#
9+
# To enable this hook, rename this file to "commit-msg".
10+
11+
# Uncomment the below to add a Signed-off-by line to the message.
12+
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
13+
# hook is more suited to it.
14+
#
15+
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
16+
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
17+
18+
# This example catches duplicate Signed-off-by lines.
19+
20+
test "" = "$(grep '^Signed-off-by: ' "$1" |
21+
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
22+
echo >&2 Duplicate Signed-off-by lines.
23+
exit 1
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to prepare a packed repository for use over
4+
# dumb transports.
5+
#
6+
# To enable this hook, rename this file to "post-update".
7+
8+
exec git update-server-info
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to verify what is about to be committed
4+
# by applypatch from an e-mail message.
5+
#
6+
# The hook should exit with non-zero status after issuing an
7+
# appropriate message if it wants to stop the commit.
8+
#
9+
# To enable this hook, rename this file to "pre-applypatch".
10+
11+
. git-sh-setup
12+
test -x "$GIT_DIR/hooks/pre-commit" &&
13+
exec "$GIT_DIR/hooks/pre-commit" ${1+"$@"}
14+
:

0 commit comments

Comments
 (0)