forked from heqin-zhu/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheadinfo.py
More file actions
63 lines (60 loc) · 1.82 KB
/
headinfo.py
File metadata and controls
63 lines (60 loc) · 1.82 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
''' mbinary
#########################################################################
# File : headInfo.py
# Author: mbinary
# Mail: [email protected]
# Blog: https://mbinary.xyz
# 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
def handleFile(path):
global count
head = getHead(path)
if head =='': return
with open(path,'r',encoding='utf8',errors='ignore') as f:
s = f.read()
if 'mbinary' in s:
return
count +=1
name = os.path.basename(path)
print('[{count}]: Adding head info to {name}'.format(count=count,name=name))
with open(path,'w') as f:
f.write(head+s)
def getHead(path):
name = os.path.basename(path)
# skip self or hidden file
if name == os.path.basename(__file__) or name[0]=='.': return ''
suf = name[name.rfind('.')+1 :]
begin = end =''
if suf == 'py':
begin = end = "'''"
elif suf in ['c','cc','cpp','java']:
begin,end = '/*','*/'
elif suf == 'sh':
begin = end = '#'
else:return ''
timeStamp = time.localtime(os.stat(path).st_ctime)
ctime = time.strftime('%Y-%m-%d %H:%M',timeStamp)
return HEAD.format(begin=begin,end=end,ctime=ctime,name = name)
def handleDir(dirPath):
gen = os.walk(dirPath)
for path,dirs,files in gen:
for f in files: handleFile(os.path.join(path,f))
if __name__ == '__main__':
works = sys.argv[1:]
if works==[] : works = ['.']
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)