1
|
|
|
import os |
2
|
|
|
import tempfile |
3
|
|
|
|
4
|
|
|
def exec(x): |
5
|
|
|
if os.system(x)!=0: |
6
|
|
|
raise NameError('Look at above errors to understand why the execution failed!!') |
7
|
|
|
|
8
|
|
|
def check_(prompt_,yes_="yes",no_="no"): |
9
|
|
|
yes1_=yes_.lower().strip() |
10
|
|
|
no1_=no_.lower().strip() |
11
|
|
|
while(True): |
12
|
|
|
i_=input(prompt_).lower().strip() |
13
|
|
|
if(i_==yes1_): |
14
|
|
|
return(True) |
15
|
|
|
elif(i_==no1_): |
16
|
|
|
return(False) |
17
|
|
|
else: |
18
|
|
|
print("Your input is neither {yes_} nor {no_}. TRY again carefully..") |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
def create_package(): |
22
|
|
|
file_path=os.path.abspath(input("Give python file location where all functions and import statements are written please: ").strip()) |
23
|
|
|
if(check_("Will you like to add more python files which are imported by the main file? Yes/No:")): |
24
|
|
|
extra_file_paths=[i for i in input("Give the location of extra_files seperated by space").split() if(i!="")] |
25
|
|
|
else: |
26
|
|
|
extra_file_paths=[] |
27
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname: |
28
|
|
|
os.chdir(tmpdirname) |
29
|
|
|
exec("git clone -q https://github.com/Souvic/package_creator.git") |
30
|
|
|
exec("rm -rf ./package_creator/.git") |
31
|
|
|
|
32
|
|
|
package_name=input("Choose name of this package please: ") |
33
|
|
|
author_name=input("Author's name please: ") |
34
|
|
|
author_email=input("Author's EmailId please: ") |
35
|
|
|
|
36
|
|
|
python_requires=input("Required minimum Python version to run this package please(e.g. 3.6): ") |
37
|
|
|
|
38
|
|
|
rep_url=input("Go to github.com, create an empty repository with project name, copy-paste the link here please: ") |
39
|
|
|
if(not rep_url.lower().endswith(".git")): |
40
|
|
|
rep_url=rep_url+".git" |
41
|
|
|
with open("./package_creator/src/package_name/__init__.py","w") as f: |
42
|
|
|
f.write(open(file_path,"r").read()) |
43
|
|
|
exec("pipreqs ./package_creator/") |
44
|
|
|
print("install_requires in setup.cfg is filled up with minimum requirements from pipreqs package") |
45
|
|
|
print("Feel free to change setup.cfg after this in your github repo directly in case of different versions to be used") |
46
|
|
|
with open("./package_creator/requirements.txt") as f: |
47
|
|
|
install_requires="".join(["\n\t"+i for i in f.read().replace("==",">=").split("\n")]) |
48
|
|
|
|
49
|
|
|
with open("./package_creator/setup.cfg","r") as f: |
50
|
|
|
su=f.read() |
51
|
|
|
su=su.format(package_name=package_name,author_name=author_name,author_email=author_email,python_requires=python_requires,install_requires=install_requires,rep_url=rep_url) |
52
|
|
|
with open("./package_creator/setup.cfg","w") as f: |
53
|
|
|
f.write(su) |
54
|
|
|
for ii in extra_file_paths: |
55
|
|
|
exec(f"cp {ii} ./package_creator/src/package_name/") |
56
|
|
|
os.rename("./package_creator/src/package_name","./package_creator/src/"+package_name) |
57
|
|
|
|
58
|
|
|
#exec("python3 -m twine upload --repository testpypi") |
59
|
|
|
os.chdir("./package_creator/") |
60
|
|
|
exec("python3 -m build") |
61
|
|
|
exec("git init") |
62
|
|
|
exec("git add -A") |
63
|
|
|
#exec("git add .") |
64
|
|
|
exec("git commit -m 'First commit in the new package'") |
65
|
|
|
print("Uploading to github") |
66
|
|
|
print("Get ready with your github username and passtoken") |
67
|
|
|
exec("git branch -M main") |
68
|
|
|
exec(f"git remote add origin {rep_url}") |
69
|
|
|
exec("git push -u origin main") |
70
|
|
|
print("Done!!") |
71
|
|
|
print("All set up!!") |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
def uploadpackage(): |
75
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname: |
76
|
|
|
os.chdir(tmpdirname) |
77
|
|
|
rep_url=input("Copy-paste the link of the github repo of the project please: ") |
78
|
|
|
if(not rep_url.lower().endswith(".git")): |
79
|
|
|
rep_url=rep_url+".git" |
80
|
|
|
exec(f"git clone -q {rep_url}") |
81
|
|
|
dir_ = [i for i in os.listdir() if os.path.isdir(i)][0] |
82
|
|
|
os.chdir(dir_) |
83
|
|
|
exec(f"rm -rf ./dist") |
84
|
|
|
|
85
|
|
|
print("Your package requires atleast these packages listed in requirements.txt and install_requires part of setup.cfg file.") |
86
|
|
|
print("These are listed by pipreqs") |
87
|
|
|
exec("pipreqs --print ./") |
88
|
|
|
print("\n\nThese are the packages listed in requirements.txt : ") |
89
|
|
|
exec("cat requirements.txt") |
90
|
|
|
|
91
|
|
|
print("\n\nThese are the packages listed in install_requires part of setup.cfg file : ") |
92
|
|
|
with open("setup.cfg","r") as f: |
93
|
|
|
flag=False |
94
|
|
|
for j in f: |
95
|
|
|
if(not j[0].isspace()): |
96
|
|
|
flag=False |
97
|
|
|
if(flag): |
98
|
|
|
print(j) |
99
|
|
|
if(j.startswith("install_requires")): |
100
|
|
|
flag=True |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
|
104
|
|
|
print("Abort now and update requirements.txt setup.cfg file(install_requires and python_requires) if you notice any discrepency") |
105
|
|
|
if(check_("","abort","continue")): |
106
|
|
|
raise NameError('Aborted as you wished!! \nMake necessary changes on the repo now.') |
107
|
|
|
nft_=check_("Are you uploading this package to PyPi for the first time? Yes/No:","no","yes") |
108
|
|
|
if(nft_): |
109
|
|
|
with open("./setup.cfg","r") as f: |
110
|
|
|
zz=f.read().split("\n") |
111
|
|
|
if(zz[2].startswith("version")): |
112
|
|
|
kindex=2 |
113
|
|
|
else: |
114
|
|
|
for jkl in range(len(zz)): |
115
|
|
|
if(zz[jkl].startswith("version")): |
116
|
|
|
kindex=jkl |
117
|
|
|
break; |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
print(f"The old {zz[kindex]}") |
121
|
|
|
new_version_number=input("Choose a new version number: ") |
122
|
|
|
zz[kindex]="version = "+new_version_number |
123
|
|
|
with open("./setup.cfg","w") as f: |
124
|
|
|
f.write("\n".join(zz)) |
125
|
|
|
exec("python3 -m build") |
126
|
|
|
exec("git add -A") |
127
|
|
|
exec("git commit -m 'First commit in the new package'") |
128
|
|
|
if(nft_): |
129
|
|
|
exec(f"git tag v{new_version_number}") |
130
|
|
|
exec(f"git push origin v{new_version_number}") |
131
|
|
|
else: |
132
|
|
|
exec("git tag v0.0.1") |
133
|
|
|
exec("git push origin v0.0.1") |
134
|
|
|
exec("git push -u origin main") |
135
|
|
|
print("Updated github repo!") |
136
|
|
|
exec("twine upload dist/*") |
137
|
|
|
print("Updated to PyPi!") |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
def main(): |
141
|
|
|
if(check_("Do you already have a github repo for the project? Yes/No:")): |
142
|
|
|
uploadpackage() |
143
|
|
|
else: |
144
|
|
|
create_package() |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
|
148
|
|
|
|