-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.py
More file actions
106 lines (76 loc) · 2.88 KB
/
init.py
File metadata and controls
106 lines (76 loc) · 2.88 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
"""This script will setup"""
import argparse
import os
import shutil
import sys
BASE_DIR = os.getcwd()
NAME = 'github-poetry-starter'
SNAME = 'github_poetry_starter'
VERSION = "0.4.0"
DESCRIPTION = "GitHub Actions starter for python with python-poetry"
AUTHOR = 'hexatester'
EMAIL = 'hexatester@protonmail.com'
PDIR = os.path.join(BASE_DIR, 'github_poetry_starter')
TFILE = os.path.join(BASE_DIR, 'tests', 'test_github_poetry_starter.py')
def snake_case(text):
return text.lower().replace(' ', '_')
def kebab_case(text):
return text.lower().replace(' ', '-')
def remove(path):
"""param <path> could either be relative or absolute."""
# thanks https://stackoverflow.com/a/41789397
if os.path.isfile(path) or os.path.islink(path):
os.remove(path) # remove the file
elif os.path.isdir(path):
shutil.rmtree(path) # remove dir and all contains
else:
raise ValueError("file {} is not a file or dir.".format(path))
def rewrite(filepath, changes):
text = ''
with open(filepath) as f:
text = f.read()
for change in changes:
text = text.replace(*change)
with open(filepath, "w") as f:
f.write(text)
def main():
parser = argparse.ArgumentParser(description='Setup Poetry Starter.')
parser.add_argument('--name', dest='name', help='Project name', required=True)
parser.add_argument('--version', dest='version', help='Project version', default="0.1.0")
parser.add_argument('--description', dest='description', help='Project description', required=True)
parser.add_argument('--author', dest='author', help='Author name / username', required=True)
parser.add_argument('--author-email', dest='email', help='Author-email', required=True)
parser.add_argument('--module', dest='module', action='store_true')
parser.add_argument('--no-module', dest='module', action='store_false')
parser.set_defaults(module=True)
args = parser.parse_args()
if args.module:
NS_NAME = snake_case(args.name)
NDIR = os.path.join(BASE_DIR, NS_NAME)
TNFILE = os.path.join(BASE_DIR, 'tests', f'test_{NS_NAME}.py')
if os.path.isdir(PDIR):
os.rename(PDIR, NDIR)
if os.path.isfile(TFILE):
os.rename(TFILE, TNFILE)
rewrite(TNFILE, [(SNAME, NS_NAME)])
else:
remove(PDIR)
remove(TFILE)
with open('setup.py', 'w+') as f:
f.write("#!/usr/bin/env python\n")
REPLACE_PYPROJECT = [
(NAME, kebab_case(args.name)),
(VERSION, args.version),
(DESCRIPTION, args.description),
(AUTHOR, args.author),
(EMAIL, args.email),
]
rewrite('pyproject.toml', REPLACE_PYPROJECT)
REPLACE_SETUP = [
(", 'init.py'", '')
]
rewrite('setup.py', REPLACE_SETUP)
print("Please delete init.py file")
if __name__ == "__main__":
sys.exit(main())