GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — SOMS/file_bakup.py at master · swpu-computer/SOMS

Expand file tree

/

file_bakup.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
56 lines (52 loc) · 1.5 KB
/

file_bakup.py

Copy path

File metadata and controls

56 lines (52 loc) · 1.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author: qitan
# @Email: [email protected]
# @Site: https://imaojia.com
# @File: file_bakup.py
# @Time: 18-2-2 下午1:40
import os
import shutil
import hashlib
def md5sum(fname):
""" 计算文件的MD5值
"""
def read_chunks(fh):
fh.seek(0)
chunk = fh.read(8096)
while chunk:
yield chunk
chunk = fh.read(8096)
else: #最后要将游标放回文件开头
fh.seek(0)
m = hashlib.md5()
if isinstance(fname, basestring) \
and os.path.exists(fname):
with open(fname, "rb") as fh:
for chunk in read_chunks(fh):
m.update(chunk)
#上传的文件缓存 或 已打开的文件流
elif fname.__class__.__name__ in ["StringIO", "StringO"] \
or isinstance(fname, file):
for chunk in read_chunks(fname):
m.update(chunk)
else:
return ""
return m.hexdigest()
def Backup(dest, tag, md5):
bkpath = os.path.join('/srv/salt', '%s-%s'%(dest.lstrip('/'),tag))
if not os.path.isdir(os.path.dirname(bkpath)):
os.makedirs(os.path.dirname(bkpath))
try:
md5_new = md5sum(dest)
if md5_new == md5:
return 1
else:
shutil.copyfile(dest, bkpath)
return 0
except:
return None
def Rollback(dest, tag, md5):
bkpath = os.path.join('/srv/salt', '%s-%s'%(dest.lstrip('/'), tag))
shutil.copyfile(bkpath, dest)
return 'ok'
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.