1
|
|
|
import os |
2
|
|
|
import subprocess |
3
|
|
|
import tempfile |
4
|
|
|
|
5
|
|
|
def exec(x): |
6
|
|
|
subprocess.check_output(x,shell=True) |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
def create_package(): |
10
|
|
|
file_path=os.path.abspath(input("Give python file location where all functions and import statements are written please: ").strip()) |
11
|
|
|
if(input("Will you like to add more python files which are imported by the main file? Yes/No:").lower().strip()=="yes"): |
12
|
|
|
extra_file_paths=[i for i in input("Give the location of extra_files seperated by space").split() if(i!="")] |
13
|
|
|
else: |
14
|
|
|
extra_file_paths=[] |
15
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname: |
16
|
|
|
os.chdir(tmpdirname) |
17
|
|
|
exec("git clone https://github.com/Souvic/package_creator.git") |
18
|
|
|
exec("rm -rf ./package_creator/.git") |
19
|
|
|
|
20
|
|
|
package_name=input("Choose name of this package please: ") |
21
|
|
|
author_name=input("Author's name please: ") |
22
|
|
|
author_email=input("Author's EmailId please: ") |
23
|
|
|
|
24
|
|
|
python_requires=input("Required minimum Python version to run this package please(e.g. 3.6): ") |
25
|
|
|
|
26
|
|
|
rep_url=input("Go to github.com, create an empty repository with project name, copy-paste the link here please: ") |
27
|
|
|
if(not rep_url.lower().endswith(".git")): |
28
|
|
|
rep_url=rep_url+".git" |
29
|
|
|
with open("./package_creator/src/package_name/__init__.py","w") as f: |
30
|
|
|
f.write(open(file_path,"r").read()) |
31
|
|
|
exec("pipreqs ./package_creator/") |
32
|
|
|
print("install_requires in setup.cfg is filled up with minimum requirements from pipreqs package") |
33
|
|
|
print("Feel free to change setup.cfg after this in your github repo directly in case of different versions to be used") |
34
|
|
|
with open("./package_creator/requirements.txt") as f: |
35
|
|
|
install_requires="".join(["\n\t"+i for i in f.read().replace("==",">=").split("\n")]) |
36
|
|
|
|
37
|
|
|
with open("./package_creator/setup.cfg","r") as f: |
38
|
|
|
su=f.read() |
39
|
|
|
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) |
40
|
|
|
with open("./package_creator/setup.cfg","w") as f: |
41
|
|
|
f.write(su) |
42
|
|
|
for ii in extra_file_paths: |
43
|
|
|
exec(f"cp {ii} ./package_creator/src/package_name/") |
44
|
|
|
os.rename("./package_creator/src/package_name","./package_creator/src/"+package_name) |
45
|
|
|
|
46
|
|
|
#exec("python3 -m twine upload --repository testpypi") |
47
|
|
|
os.chdir("./package_creator/") |
48
|
|
|
exec("python3 -m build") |
49
|
|
|
exec("git init") |
50
|
|
|
exec("git add -A") |
51
|
|
|
#exec("git add .") |
52
|
|
|
exec("git commit -m 'First commit in the new package'") |
53
|
|
|
print("Uploading to github") |
54
|
|
|
print("Get ready with your github username and passtoken") |
55
|
|
|
exec("git branch -M main") |
56
|
|
|
exec(f"git remote add origin {rep_url}") |
57
|
|
|
exec("git push -u origin main") |
58
|
|
|
print("Done!!") |
59
|
|
|
print("All set up!!") |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
def uploadpackage(): |
63
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname: |
64
|
|
|
os.chdir(tmpdirname) |
65
|
|
|
rep_url=input("Copy-paste the link of the github repo of the project please: ") |
66
|
|
|
if(not rep_url.lower().endswith(".git")): |
67
|
|
|
rep_url=rep_url+".git" |
68
|
|
|
exec(f"git clone {rep_url}") |
69
|
|
|
dir_ = [i for i in os.listdir() if os.path.isdir(i)][0] |
70
|
|
|
os.chdir(dir_) |
71
|
|
|
exec(f"rm -rf ./dist") |
72
|
|
|
nft_=input("Are you uploading this package to PyPi for the first time? Yes/No:").lower().strip()=="no" |
73
|
|
|
if(nft_): |
74
|
|
|
with open("./setup.cfg","r") as f: |
75
|
|
|
zz=f.read().split("\n") |
76
|
|
|
if(zz[2].startswith("version")): |
77
|
|
|
kindex=2 |
78
|
|
|
else: |
79
|
|
|
for jkl in range(len(zz)): |
80
|
|
|
if(zz[jkl].startswith("version")): |
81
|
|
|
kindex=jkl |
82
|
|
|
break; |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
print(f"The old {zz[kindex]}") |
86
|
|
|
new_version_number=input("Choose a new version number: ") |
87
|
|
|
zz[kindex]="version = "+new_version_number |
88
|
|
|
with open("./setup.cfg","w") as f: |
89
|
|
|
f.write("\n".join(zz)) |
90
|
|
|
exec("python3 -m build") |
91
|
|
|
exec("git add -A") |
92
|
|
|
exec("git commit -m 'First commit in the new package'") |
93
|
|
|
if(nft_): |
94
|
|
|
exec(f"git tag v{new_version_number}") |
95
|
|
|
exec(f"git push origin v{new_version_number}") |
96
|
|
|
else: |
97
|
|
|
exec("git tag v0.0.1") |
98
|
|
|
exec("git push origin v0.0.1") |
99
|
|
|
exec("git push -u origin main") |
100
|
|
|
print("Updated github repo!") |
101
|
|
|
exec("twine upload dist/*") |
102
|
|
|
print("Updated to PyPi!") |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
def main(): |
106
|
|
|
if(input("Do you already have a github repo for the project? Yes/No:").lower().strip()=="yes"): |
107
|
|
|
uploadpackage() |
108
|
|
|
else: |
109
|
|
|
create_package() |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
|
113
|
|
|
|