-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlab_storage.py
More file actions
145 lines (114 loc) · 4 KB
/
gitlab_storage.py
File metadata and controls
145 lines (114 loc) · 4 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
# coding: utf8
"""
Gitlab storage for django file storage
create date: 2020/09/03
author: baikejian
How to use: https://blog.csdn.net/max229max/article/details/108669769
"""
import base64
import logging
import os
import posixpath
from datetime import datetime
from django.conf import settings
from django.core.files.base import ContentFile, File
from django.core.files.storage import Storage
from django.utils.deconstruct import deconstructible
from django.utils.six import BytesIO
from django.utils.six.moves.urllib import parse as urlparse
from gitlab import Gitlab
@deconstructible
class GitlabStorage(Storage):
def __init__(self, url, private_token, project_id, branch, api_version='3'):
'''
初始化gitlab实例
url: https://xxxxxxxxxxx.com
private_token:xsdfsdfsdfsdfsdfsdfsdf
project_id: 123
branch: master
'''
self.gitapi = Gitlab(url, private_token=private_token)
self.project = self.gitapi.projects.get(project_id)
self.branch = branch
self._base_url = self.project.attributes.get('web_url')
def _save(self, gitlab_path, content):
logging.debug("Start saving...")
logging.debug(type(content))
commit_data = self.__create_commit_file_data(content, gitlab_path, f'auto commit {datetime.now()}')
# commit file
self.project.commits.create(commit_data)
logging.debug("Save file success.")
return gitlab_path
def _open(self, git_path, mode='rb'):
"""
获取git文件
"""
logging.debug(f"Open file {git_path}.")
f_raw = self.read(git_path)
return ContentFile(f_raw)
def read(self, git_path):
try:
logging.debug("Read file ...")
gfile = self.project.files.get(file_path=git_path, ref=self.branch)
base64_str = gfile.decode().decode('utf-8')
raw_content = base64.b64decode(base64_str)
return raw_content
except Exception as e:
raise Exception(f'Git file path incorrect! {e}')
def delete(self, git_path):
'''
delete git file
'''
jMap = {}
jMap["branch"] = self.branch
jMap["commit_message"] = f'auto delete {datetime.now()}'
jsonList = []
aMap = {}
aMap["action"] = "delete"
aMap["file_path"] = gitlab_path
jsonList.append(aMap)
jMap["actions"] = jsonList
self.project.commits.create(jMap)
logging.debug("delete file success.")
def exists(self, git_path):
try:
self.project.files.get(file_path=git_path, ref=self.branch)
return True
except Exception as e:
return False
def __create_commit_file_data(self, content, gitlab_path, message):
"""
获取提交数据
"""
jsonList = []
jMap = {}
jMap["branch"] = self.branch
jMap["commit_message"] = message
content.open(mode='rb')
aMap = {}
# 存在就更新, 不存在就创建
if self.exists(gitlab_path):
aMap["action"] = "update"
else:
aMap["action"] = "create"
aMap["file_path"] = gitlab_path
aMap["content"] = base64.b64encode(content.file.read()).decode("utf-8")
jsonList.append(aMap)
jMap["actions"] = jsonList
return jMap
def url(self, name):
return urlparse.urljoin('/', name)
if __name__ == "__main__":
GIT_URL = "http://gitlab.xxx.com"
GIT_PRIVATE_TOKEN = "sdfsdfsdfsdfsdf"
GIT_PROJECT_ID = "3172"
GIT_DIR_PATH = "timelimit_attachment/"
GIT_BRANCH = "master"
gitapi = GitlabStorage(GIT_URL, GIT_PRIVATE_TOKEN, GIT_PROJECT_ID, GIT_BRANCH)
f = open('./ops_platform/__init__.pyc')
print(type(f))
myfile = File(f)
gitapi._save('timelimit_attachment/Dockerfile', myfile)
readme = gitapi._open("timelimit_attachment/Dockerfile")
print(readme.read())