Skip to content

Instantly share code, notes, and snippets.

@ficapy
Created March 11, 2016 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ficapy/cd7a44f9d15fbe53284d to your computer and use it in GitHub Desktop.
Save ficapy/cd7a44f9d15fbe53284d to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Ficapy
import os
import requests
import base64
import hmac
import datetime
from os.path import join, exists, relpath
from mimetypes import MimeTypes
from hashlib import sha1, md5
from pickle import dump, loads
from cStringIO import StringIO, InputType, OutputType
from lxml import html
KEY = '---------'
SECRET = '---------'
BUCKET = '---------'
url = '---------'
ABSOLUTE_PATH = '/Users/Ficapy/CodeSpace/Blog/public'
s = requests.Session()
headers = {
'Date': datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT'),
'Content-Type': 'text/html',
'HOST': url,
}
def signature(file_path):
global headers
path = join(ABSOLUTE_PATH, file_path)
headers['Content-Type'] = MimeTypes().guess_type(path)[0] or 'text/html'
sign = base64.b64encode(hmac.new(SECRET,
'PUT' + "\n"
+ '' + "\n"
+ headers['Content-Type'] + "\n"
+ headers['Date'] + "\n"
+ '/' + BUCKET + '/' + file_path, sha1).digest()).strip()
headers.update({'Authorization': 'OSS ' + KEY + ':' + sign})
def read_in_chunks(file_object, blocksize=1024):
file_object = file_object if isinstance(file_object, (InputType, OutputType)) else open(
join(ABSOLUTE_PATH, file_object))
file_object.seek(0)
while 1:
data = file_object.read(blocksize)
if not data:
break
yield data
def upload(file_path, file_object):
signature(file_path)
s.put('http://' + url + '/' + file_path, headers=headers, data=read_in_chunks(file_object)).raise_for_status()
print('upload done: ' + file_path)
# 首先请求oss_fuck_md5文件,没有则全部上传 有则和生成的md5对比,本地文件不存在或变动则上传
# 所有文件上传成功后最后上传.oss_fuck_md5文件
def local_md5():
file_md5_mp = {}
for root, dirnames, files in os.walk(ABSOLUTE_PATH):
for file in files:
fullpath = join(root, file)
rel = relpath(fullpath, ABSOLUTE_PATH)
file_md5_mp[rel] = md5(open(fullpath).read()).hexdigest()
return file_md5_mp
def remote_md5():
a = s.get('http://' + url + '/oss_fuck_md5')
if a.status_code != 200:
return {}
else:
return loads(a.content)
def path_process(file_path):
file_path = join(ABSOLUTE_PATH, file_path)
tree = html.parse(file_path).getroot()
hrefs = tree.xpath('//*[@href]')
for i, href in enumerate(hrefs):
rewrite = join(href.attrib['href'].split()[0], 'index.html')
if exists(join(ABSOLUTE_PATH, rewrite[1:])):
hrefs[i].attrib['href'] = rewrite
f = StringIO()
f.write(html.tostring(tree))
return f
def main():
remote = remote_md5()
locals = local_md5()
_locals = locals.copy()
for local in _locals.keys():
if _locals[local] != remote.get(local, None):
continue
else:
locals.pop(local)
for path in locals.keys():
if path.endswith('html'):
upload(path, path_process(path))
else:
upload(path, path)
f = StringIO()
dump(_locals, f)
upload('oss_fuck_md5', f)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment