|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
3 | 3 | ''' |
4 | | -This script is used to create a local repository linked with a remote repository. |
| 4 | +This script is used to create a local repository linked with a remote repository. |
5 | 5 | ''' |
6 | 6 |
|
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 |
| 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 | 13 |
|
14 | 14 |
|
15 | 15 | # Checks if the environment variables are set or not. If not, raises an error as ValueError. # noqa: E501 |
|
18 | 18 | if not GITHUB_API_TOKEN: |
19 | 19 | raise ValueError("Please set the environment variable GITHUB_API_TOKEN in the github_secrets.py file") # noqa: E501 |
20 | 20 |
|
21 | | -# Base URL for the GitHub API which |
| 21 | +# Base URL for the GitHub API which is used to create a new repository # noqa: E501 |
22 | 22 | URL = "https://api.github.com/user/repos" |
23 | 23 |
|
24 | 24 | # Creates a parser object of the argparse class |
|
27 | 27 | parser = argparse.ArgumentParser(description='creates a local repository linked with a remote repository') # noqa: E501 |
28 | 28 |
|
29 | 29 | # Adds the arguments to the parser object |
30 | | -parser.add_argument('path', # This argument can be accessed using the `path` variable # noqa: E501 |
| 30 | +parser.add_argument('path', # This argument can be accessed using the `path` variable # noqa: E501 |
31 | 31 | metavar='PATH', |
32 | 32 | type=str, |
33 | 33 | help='Enter the path for the new repository') |
34 | | -parser.add_argument('name', # This argument can be accessed using the `name` variable # noqa: E501 |
35 | | - metavar='NAME', |
| 34 | +parser.add_argument('name', # This argument can be accessed using the `name` variable # noqa: E501 |
| 35 | + metavar='NAME', |
36 | 36 | type=str, |
37 | 37 | help='Enter a name for the new repository') |
38 | | -args = parser.parse_args() # parses the arguments passed to the script. The arguments are stored in the `args` variable # noqa: E501 |
| 38 | +args = parser.parse_args() # parses the arguments passed to the script. The arguments are stored in the `args` variable # noqa: E501 |
39 | 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 |
| 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 |
42 | 42 |
|
43 | 43 |
|
44 | 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 |
| 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 |
51 | 51 |
|
52 | 52 |
|
53 | 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 |
| 54 | +conn = http.client.HTTPSConnection("api.github.com") # creates a connection object |
55 | 55 | # The payload is the data that is sent |
56 | 56 | payload = json.dumps({ |
57 | 57 | "name": name, |
|
71 | 71 | # The response is read and decoded using utf-8 encoding and stored in the `data` variable |
72 | 72 | # The `data` variable is parsed using the json module and stored in the `response` variable # noqa: E501 |
73 | 73 | # The `response` variable contains the response received from the GitHub API, which is a JSON object # noqa: E501 |
74 | | -conn.request("POST", "/user/repos", payload, headers) |
| 74 | +conn.request("POST", "/user/repos", payload, headers) |
75 | 75 | res = conn.getresponse() |
76 | 76 | data = res.read().decode("utf-8") |
77 | 77 | response = json.loads(data) |
78 | 78 |
|
79 | 79 | print(response) |
80 | | -remote_url = response['svn_url'] # stores the remoteurl using the key `svn_url` in the `response` variable # noqa: E501 |
| 80 | +remote_url = response['svn_url'] # stores the remoteurl using the key `svn_url` in the `response` variable # noqa: E501 |
81 | 81 |
|
82 | 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 |
| 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