pypacklib.exec()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
nop 1
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(f"Your input is neither {yes_} nor {no_}. TRY again carefully..")
19
        
20
def format_readme(rep_u):
21
    #https://github.com/{user_name_}/{repo_or_package_name_}.git
22
    user_name_=rep_u.split("/")[-2]
23
    repo_or_package_name_=rep_u.split("/")[-1][:-4]
24
    with open("./package_creator/README.md","r") as f:
25
        readme=f.read()
26
    readme=readme.format(user_name_=user_name_,repo_or_package_name_=repo_or_package_name_)
27
    with open("./package_creator/README.md","w") as f:
28
        f.write(readme)
29
    
30
def create_package():
31
    file_path=os.path.abspath(os.path.expanduser(input("Give python file location where all functions and import statements are written please: ").strip()))
32
    if(check_("Will you like to add more python files which are imported by the main file? yes/no: ")):
33
        extra_file_paths=set([os.path.abspath(os.path.expanduser(i)) for i in input("Give the location of extra_files seperated by space : ").split() if(i!="")])
34
    else:
35
        extra_file_paths=[]
36
    with tempfile.TemporaryDirectory() as tmpdirname:
37
        os.chdir(tmpdirname)
38
        exec("git clone -q https://github.com/Souvic/package_creator.git")
39
        exec("rm -rf ./package_creator/.git")
40
41
        package_name=input("Choose name of this package please: ")
42
        author_name=input("Author's name please: ")
43
        author_email=input("Author's EmailId please: ")
44
45
        python_requires=input("Required minimum Python version to run this package please(e.g. 3.6): ")
46
47
        rep_url=input("Go to github.com, create an empty repository(without any file) with project name, copy-paste the link here please: ")
48
        if(not rep_url.lower().endswith(".git")):
49
            rep_url=rep_url+".git"
50
        format_readme(rep_url)
51
        with open("./package_creator/src/package_name/__init__.py","w") as f:
52
            f.write(open(file_path,"r").read())
53
        exec("pipreqs ./package_creator/")
54
        print("install_requires in setup.cfg is filled up with minimum requirements from pipreqs package")
55
        print("Feel free to change setup.cfg after this in your github repo directly in case of different versions to be used")
56
        with open("./package_creator/requirements.txt") as f:
57
            install_requires="".join(["\n\t"+i for i in f.read().replace("==",">=").split("\n")])
58
    
59
        with open("./package_creator/setup.cfg","r") as f:
60
            su=f.read()
61
        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)
62
        with open("./package_creator/setup.cfg","w") as f:
63
            f.write(su)
64
        for ii in extra_file_paths:
65
            exec(f"cp {ii} ./package_creator/src/package_name/")
66
        os.rename("./package_creator/src/package_name","./package_creator/src/"+package_name)
67
        
68
        #exec("python3 -m twine upload --repository testpypi")
69
        os.chdir("./package_creator/")
70
        exec("python3 -m build")
71
        exec("git init")
72
        exec("git add -A")
73
        #exec("git add .")
74
        exec("git commit -m 'First commit in the new package'")
75
        print("Uploading to github")
76
        print("Get ready with your github username and passtoken")
77
        exec("git branch -M main")
78
        exec(f"git remote add origin {rep_url}")
79
        exec("git push -u origin main")
80
        print("Done!!")
81
        print("All set up!!")
82
        print(f"\n\n\n__________________________\nYou may now go to {rep_url[:-4]} and change atleast README.md and description in setup.cfg")
83
        print("You may require to also change requirements.txt and install_requires,python_requires on setup.cfg if you feel so")
84
        print("To upload to PyPI and reflect the new changes, run this command(pypack) once again! This time you already have a github repo.")
85
     
86
    
87
def uploadpackage():
88
    with tempfile.TemporaryDirectory() as tmpdirname:
89
        os.chdir(tmpdirname)
90
        rep_url=input("Copy-paste the link of the github repo of the project please: ")
91
        if(not rep_url.lower().endswith(".git")):
92
            rep_url=rep_url+".git"
93
        exec(f"git clone -q {rep_url}")
94
        dir_ = [i for i in os.listdir() if os.path.isdir(i)][0]
95
        os.chdir(dir_)
96
        exec(f"rm -rf ./dist")
97
        
98
        print("\n\n\n_____________________________IMPORTANT________________________________\n\nYour package requires atleast these packages listed in requirements.txt and install_requires part of setup.cfg file.")
99
        print("These are listed by pipreqs")
100
        exec("pipreqs --print ./")
101
        print("\n\nThese are the packages listed in requirements.txt : ")
102
        exec("cat requirements.txt")
103
        tempprint=""
104
        print("\n\nThese are the packages listed in install_requires part of setup.cfg file : ")
105
        with open("setup.cfg","r") as f:
106
            flag=False
107
            for j in f:
108
                if(not j[0].isspace()):
109
                    flag=False
110
                if(flag):
111
                    tempprint+=j.strip()+"\n"
112
                if(j.startswith("install_requires")):
113
                   flag=True
114
        
115
        
116
        print(tempprint)
117
        print("If you notice any discrepency, abort now and update requirements.txt setup.cfg file(install_requires and python_requires).\n")
118
        if(check_("Abort/continue? (see above why..) Write 'abort' to stop exeution or 'continue' to go ahead uploading with current settings : ","abort","continue")):
119
            raise NameError('Aborted as you wished!! \nMake necessary changes on the repo now.')
120
        nft_=check_("Are you uploading this package to PyPi for the first time? yes/no: ","no","yes")
121
        if(nft_):
122
            with open("./setup.cfg","r") as f:
123
                zz=f.read().split("\n")
124
            if(zz[2].startswith("version")):
125
                kindex=2
126
            else:
127
                for jkl in range(len(zz)):
128
                    if(zz[jkl].startswith("version")):
129
                        kindex=jkl
130
                        break;
131
                    
132
            
133
            print(f"The old {zz[kindex]}")
134
            new_version_number=input("Choose a new version number: ")
135
            zz[kindex]="version = "+new_version_number
136
            with open("./setup.cfg","w") as f:
137
                f.write("\n".join(zz))
138
        exec("python3 -m build")
139
        exec("git add -A")
140
        exec("git commit -m 'New version is released now'")
141
        if(nft_):
142
            exec(f"git tag v{new_version_number}")
143
            exec(f"git push origin  v{new_version_number}")
144
        else:
145
            exec("git tag v0.0.1")
146
            exec("git push origin  v0.0.1")
147
        exec("git push -u origin main")
148
        print("Updated github repo!")
149
        exec("twine upload dist/*")
150
        print("Updated to PyPi! New version has been released!")
151
        
152
        
153
def main():
154
    if(check_("Have you already uploaded the project once to github repo ? yes/no: ")):
155
        uploadpackage()
156
    else:
157
        create_package()
158
        
159
    
160
       
161