Skip to content

Commit de788ba

Browse files
Merge pull request sahil-sagwekar2652#96 from SwastikGorai/doc
Added Documentation for create_repo.py
2 parents eef8edd + 4188cd3 commit de788ba

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

scripts/create_repo.py

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,85 @@
11
#!/usr/bin/env python
22

3-
import argparse
4-
import os
5-
import http.client
6-
import json
7-
from github_secrets import GITHUB_API_TOKEN, USERNAME
3+
'''
4+
This script is used to create a local repository linked with a remote repository.
5+
'''
86

7+
# Imports the required modules
8+
import argparse # required for parsing the command line arguments passed to the script
9+
import os # required for creating the directory and changing the directory and running the git commands # noqa: E501
10+
import http.client # required for making the http request
11+
import json # required for parsing the response received from the http request
12+
from github_secrets import GITHUB_API_TOKEN, USERNAME # required for authenticating the http request and setting the user agent # noqa: E501
13+
14+
15+
# Checks if the environment variables are set or not. If not, raises an error as ValueError. # noqa: E501
16+
# Environment variables are set in the github_secrets.py file which contains the GITHUB_API_TOKEN and USERNAME # noqa: E501
17+
# The GITHUB_API_TOKEN is the personal access token generated from the GitHub account. # noqa: E501
918
if not GITHUB_API_TOKEN:
1019
raise ValueError("Please set the environment variable GITHUB_API_TOKEN in the github_secrets.py file") # noqa: E501
1120

21+
# Base URL for the GitHub API which is used to create a new repository # noqa: E501
1222
URL = "https://api.github.com/user/repos"
1323

24+
# Creates a parser object of the argparse class
25+
# The parser object is used to parse the command line arguments passed to the script # noqa: E501
26+
# The parser object is used to create the help text for the script # noqa: E501
1427
parser = argparse.ArgumentParser(description='creates a local repository linked with a remote repository') # noqa: E501
1528

16-
parser.add_argument('path',
29+
# Adds the arguments to the parser object
30+
parser.add_argument('path', # This argument can be accessed using the `path` variable # noqa: E501
1731
metavar='PATH',
1832
type=str,
1933
help='Enter the path for the new repository')
20-
parser.add_argument('name',
34+
parser.add_argument('name', # This argument can be accessed using the `name` variable # noqa: E501
2135
metavar='NAME',
2236
type=str,
2337
help='Enter a name for the new repository')
24-
args = parser.parse_args()
38+
args = parser.parse_args() # parses the arguments passed to the script. The arguments are stored in the `args` variable # noqa: E501
39+
40+
name = args.name # stores the name of the repository from `args` in the name variable
41+
path = args.path # stores the path of the repository from `args` in the path variable
2542

26-
name = args.name
27-
path = args.path
2843

29-
os.chdir(path)
30-
os.mkdir(name)
31-
os.chdir(name)
32-
os.system('git init -b main')
33-
os.system('touch README.md')
34-
os.system('git add . && git commit -m "initial commit"')
35-
# os.system('git status')
44+
# The following codes creates a new directory with the name of the repository and initializes it with git using the `os` module # noqa: E501
45+
os.chdir(path) # changes the directory to the path specified in the `path` variable # noqa: E501
46+
os.mkdir(name) # creates a new directory with the name specified in the `name` variable # noqa: E501
47+
os.chdir(name) # changes the directory to the newly created directory # noqa: E501
48+
os.system('git init -b main') # This executes 'git init -b main' as a system command as if it were written in git bash. It initializes the directory with git and sets the default branch to `main` # noqa: E501
49+
os.system('touch README.md') # creates a README.md file # noqa: E501
50+
os.system('git add . && git commit -m "initial commit"') # adds the newly created README.md file to the staging area and commits it with the message# noqa: E501
3651

37-
conn = http.client.HTTPSConnection("api.github.com")
52+
53+
# The following code makes a POST request to the GitHub API to create a new repository # noqa: E501
54+
conn = http.client.HTTPSConnection("api.github.com") # creates a connection object
55+
# The payload is the data that is sent
3856
payload = json.dumps({
3957
"name": name,
4058
"description": "made with the GitHub API"
4159
})
60+
61+
# Metadata that is sent along with the request
62+
# The metadata contains the authorization token, the content type and the user agent # noqa: E501
4263
headers = {
4364
'Authorization': f'Bearer {GITHUB_API_TOKEN}',
4465
'Content-Type': 'application/json',
4566
'User-Agent': f'{USERNAME}'
4667
}
4768

69+
# The request is made to the URL with the payload and the headers
70+
# The response received is stored in the `res` variable
71+
# The response is read and decoded using utf-8 encoding and stored in the `data` variable
72+
# The `data` variable is parsed using the json module and stored in the `response` variable # noqa: E501
73+
# The `response` variable contains the response received from the GitHub API, which is a JSON object # noqa: E501
4874
conn.request("POST", "/user/repos", payload, headers)
4975
res = conn.getresponse()
5076
data = res.read().decode("utf-8")
5177
response = json.loads(data)
5278

5379
print(response)
54-
remote_url = response['svn_url']
80+
remote_url = response['svn_url'] # stores the remoteurl using the key `svn_url` in the `response` variable # noqa: E501
5581

56-
os.system(f'git remote add origin {remote_url}')
57-
os.system('git push origin main')
58-
print(f"\nREMOTE URL FOR \"{name}\" is: {remote_url}")
82+
# Runs the git commands as system commands # noqa: E501
83+
os.system(f'git remote add origin {remote_url}') # adds the remote url to the local repository
84+
os.system('git push origin main') # Pushes the local repository to the remote repository
85+
print(f"\nREMOTE URL FOR \"{name}\" is: {remote_url}") # Prints the remote url

0 commit comments

Comments
 (0)