2018-12-11 15:57:58 +08:00
|
|
|
''' mbinary
|
|
|
|
#########################################################################
|
|
|
|
# File : headInfo.py
|
|
|
|
# Author: mbinary
|
|
|
|
# Mail: zhuheqin1@gmail.com
|
2019-01-31 12:09:46 +08:00
|
|
|
# Blog: https://mbinary.xyz
|
2018-12-11 15:57:58 +08:00
|
|
|
# Github: https://github.com/mbinary
|
|
|
|
# Created Time: 2018-07-08 14:48
|
|
|
|
# Description:
|
|
|
|
#########################################################################
|
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
from config import HEAD
|
|
|
|
count = 0
|
2020-04-15 12:28:20 +08:00
|
|
|
|
|
|
|
|
2018-12-11 15:57:58 +08:00
|
|
|
def handleFile(path):
|
|
|
|
global count
|
|
|
|
head = getHead(path)
|
2020-04-15 12:28:20 +08:00
|
|
|
if head == '':
|
|
|
|
return
|
|
|
|
with open(path, 'r', encoding='utf8', errors='ignore') as f:
|
2018-12-11 15:57:58 +08:00
|
|
|
s = f.read()
|
|
|
|
if 'mbinary' in s:
|
|
|
|
return
|
2020-04-15 12:28:20 +08:00
|
|
|
count += 1
|
2018-12-11 15:57:58 +08:00
|
|
|
name = os.path.basename(path)
|
2020-04-15 12:28:20 +08:00
|
|
|
print('[{count}]: Adding head info to {name}'.format(count=count, name=name))
|
|
|
|
with open(path, 'w') as f:
|
2018-12-11 15:57:58 +08:00
|
|
|
f.write(head+s)
|
|
|
|
|
2020-04-15 12:28:20 +08:00
|
|
|
|
2018-12-11 15:57:58 +08:00
|
|
|
def getHead(path):
|
|
|
|
name = os.path.basename(path)
|
|
|
|
# skip self or hidden file
|
2020-04-15 12:28:20 +08:00
|
|
|
if name == os.path.basename(__file__) or name[0] == '.':
|
|
|
|
return ''
|
|
|
|
suf = name[name.rfind('.')+1:]
|
|
|
|
begin = end = ''
|
2018-12-11 15:57:58 +08:00
|
|
|
if suf == 'py':
|
|
|
|
begin = end = "'''"
|
2020-04-15 12:28:20 +08:00
|
|
|
elif suf in ['c', 'cc', 'cpp', 'java']:
|
|
|
|
begin, end = '/*', '*/'
|
2018-12-11 15:57:58 +08:00
|
|
|
elif suf == 'sh':
|
|
|
|
begin = end = '#'
|
2020-04-15 12:28:20 +08:00
|
|
|
else:
|
|
|
|
return ''
|
2018-12-11 15:57:58 +08:00
|
|
|
timeStamp = time.localtime(os.stat(path).st_ctime)
|
2020-04-15 12:28:20 +08:00
|
|
|
ctime = time.strftime('%Y-%m-%d %H:%M', timeStamp)
|
|
|
|
return HEAD.format(begin=begin, end=end, ctime=ctime, name=name)
|
|
|
|
|
2018-12-11 15:57:58 +08:00
|
|
|
|
|
|
|
def handleDir(dirPath):
|
|
|
|
gen = os.walk(dirPath)
|
2020-04-15 12:28:20 +08:00
|
|
|
for path, dirs, files in gen:
|
|
|
|
for f in files:
|
|
|
|
handleFile(os.path.join(path, f))
|
|
|
|
|
|
|
|
|
2018-12-11 15:57:58 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
works = sys.argv[1:]
|
2020-04-15 12:28:20 +08:00
|
|
|
if works == []:
|
|
|
|
works = ['.']
|
2018-12-11 15:57:58 +08:00
|
|
|
for one in works:
|
|
|
|
if not os.path.exists(one):
|
|
|
|
print('[PathError]: {one} not exists'.format(one=one))
|
|
|
|
continue
|
|
|
|
if os.path.isdir(one):
|
|
|
|
handleDir(one)
|
|
|
|
else:
|
|
|
|
handleFile(one)
|