-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpp
executable file
·81 lines (67 loc) · 2.59 KB
/
pp
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
#!/usr/bin/env python3
import logging
import subprocess
import os
PROJECT_REPO = 'ssh://git@github.com/hpolloni/sam-python-api.git'
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
def sh(cmd):
stdout_bytes = subprocess.check_output(cmd, shell=True)
return stdout_bytes.decode('unicode_escape')
def author():
try:
return sh('git config --global user.name').strip()
except Exception as e:
import getpass
return getpass.getuser().strip()
def gitemail():
try:
return sh('git config --global user.email').strip()
except Exception as e:
log.exception(e)
return 'nobody@nowhere.com'
def projectdir(projectName):
return os.path.join(os.getcwd(), projectName)
def rm_fr(path):
import shutil
if os.path.isdir(path) and not os.path.islink(path):
shutil.rmtree(path)
elif os.path.exists(path):
os.remove(path)
def templatereplace(filename, templateargs):
with open(filename, 'r') as f:
content = f.read()
for key,value in templateargs.items():
content = content.replace('<<' + key + '>>', value)
with open(filename, 'w') as f:
f.write(content)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Creates a project package')
parser.add_argument('--author', default=author(), type=str, help='Specifies the author name')
parser.add_argument('--email', default=gitemail(), type=str, help='Specifies the author email')
parser.add_argument('projectName', help='Project name')
args = parser.parse_args()
log.info('Generating: %s' % args.projectName)
log.info('Email: %s' % args.email)
log.info('Author: %s' % args.author)
log.info('File: %s' % __file__)
if os.path.sep in args.projectName:
raise Exception('Project name cant contain path separator')
pdir = projectdir(args.projectName)
log.info('Checking out project template into %s' % pdir)
sh('git clone --depth=1 %s %s' % (PROJECT_REPO, pdir))
rm_fr(os.path.join(pdir, '.git'))
rm_fr(os.path.join(pdir, 'pp'))
os.rename(os.path.join(pdir, 'example'), os.path.join(pdir, args.projectName))
log.info('Replacing templates')
for dirpath, dirnames, files in os.walk(pdir):
for f in files:
templateargs = {
'ProjectName': args.projectName,
'Author': args.author,
'Email': args.email
}
filename = os.path.join(dirpath, f)
log.info('Processing: %s' % filename)
templatereplace(filename, templateargs)