shareit 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: shareit
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: A simple file sharing CLI tool.
5
5
  License: MIT
6
6
  Author: Fahad Ahammed
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "shareit"
3
- version = "v0.2.0"
3
+ version = "v0.3.0"
4
4
  description = "A simple file sharing CLI tool."
5
5
  authors = [{ name = "Fahad Ahammed", email = "iamfahadahmed@gmail.com" }]
6
6
  license = "MIT"
@@ -7,6 +7,9 @@ from rich.panel import Panel
7
7
  from rich.table import Table
8
8
  from rich.prompt import Prompt
9
9
 
10
+ import uuid
11
+ import shutil
12
+
10
13
  import http.server as http_server
11
14
  import socketserver
12
15
 
@@ -24,6 +27,13 @@ def read_pyproject_toml():
24
27
  return ""
25
28
 
26
29
 
30
+ def generate_temporary_dir(the_path):
31
+ """Generate a unique identifier for the file sharing session."""
32
+ dir_name = f".tmp_dir_{uuid.uuid4()}"
33
+ temp_dir = os.path.join(the_path, dir_name)
34
+ os.makedirs(temp_dir, exist_ok=True)
35
+ return dir_name
36
+
27
37
  def is_valid_ip(ip):
28
38
  try:
29
39
  ipaddress.ip_address(ip)
@@ -40,17 +50,33 @@ def get_all_ip_addresses():
40
50
  ip_dict.setdefault(iface, []).append(addr.address)
41
51
  return ip_dict
42
52
 
43
- def file_share(directory, host="0.0.0.0", port=18338):
53
+ def file_share(directory=None, file=None, host="0.0.0.0", port=18338):
44
54
  """Function to share files over the network."""
45
- # This function will implement the file sharing logic.
46
- # For now, it just prints the parameters.
55
+ if not directory and not file:
56
+ rprint("[bold red]Error:[/] You must specify either a directory or a file to share.")
57
+ exit(1)
58
+
47
59
  rprint(Panel.fit(f"[bold green]Sharing directory:[/] [yellow]{directory}[/] on [cyan]{host}:{port}[/]", title="[bold blue]shareit File Server"))
48
60
  if not os.path.isdir(directory):
49
61
  rprint(f"[bold red]Error:[/] The directory [yellow]{directory}[/] does not exist or is not a directory.")
50
62
  return
51
63
  rprint(f"[bold green]Directory [yellow]{directory}[/] is valid. Ready to share files.[/]")
52
64
 
65
+ # If a file is specified, copy it to the directory to share
66
+ if file:
67
+ if not os.path.isfile(file):
68
+ rprint(f"[bold red]Error:[/] The file [yellow]{file}[/] does not exist or is not a file.")
69
+ return
70
+ try:
71
+ # Copy the file to the directory to share
72
+ shutil.copy(file, directory)
73
+ rprint(f"[bold green]File [yellow]{file}[/] copied to [yellow]{directory}[/] for sharing.[/]")
74
+ except Exception as e:
75
+ rprint(f"[bold red]Error:[/] Failed to copy file: {e}")
76
+ return
77
+
53
78
  os.chdir(directory) # Change to the directory to share
79
+
54
80
  handler = http_server.SimpleHTTPRequestHandler
55
81
  with socketserver.TCPServer((host, port), handler) as httpd:
56
82
  rprint(Panel.fit("[bold green]File sharing service started successfully![/]", title="[bold blue]Server Status"))
@@ -64,6 +90,8 @@ def file_share(directory, host="0.0.0.0", port=18338):
64
90
  for ip in ips:
65
91
  if ip != "0.0.0.0":
66
92
  url = f"http://{ip}:{port}"
93
+ if file:
94
+ url += f"/{os.path.basename(file)}"
67
95
  table.add_row(iface, ip, url)
68
96
  rprint(table)
69
97
  else:
@@ -73,13 +101,25 @@ def file_share(directory, host="0.0.0.0", port=18338):
73
101
  try:
74
102
  httpd.serve_forever()
75
103
  except KeyboardInterrupt:
104
+ httpd.shutdown()
105
+
106
+ # Change the working directory to parent
107
+ parent_path = os.path.dirname(os.getcwd()) # Move to parent directory
108
+ os.chdir(parent_path)
109
+
110
+ # Remove the temporary directory if it was created and is inside the current working directory
111
+ if os.path.exists(directory) and os.path.basename(directory).startswith(".tmp_dir_"):
112
+ shutil.rmtree(directory, ignore_errors=True)
113
+
76
114
  rprint("\n[bold red]Server stopped by user.[/]")
77
115
 
78
116
 
79
117
  def main():
80
118
  parser = argparse.ArgumentParser(description="Share files over the network.")
81
- parser.add_argument('--version', action='version', version="shareit, " + "v" + read_pyproject_toml())
82
- parser.add_argument('--dir', type=str, default='.', help='Directory to share (default: current directory)')
119
+ parser.add_argument('--version', action='version', version="shareit, " + read_pyproject_toml())
120
+ group = parser.add_mutually_exclusive_group(required=True)
121
+ group.add_argument('--dir', type=str, help='Directory to share')
122
+ group.add_argument('--file', type=str, help='File to share')
83
123
  parser.add_argument('--host', type=str, default='0.0.0.0', help='Host to bind (default: 0.0.0.0)')
84
124
  parser.add_argument('--port', type=int, default=18338, help='Port to bind (default: 18338)')
85
125
  args = parser.parse_args()
@@ -119,7 +159,16 @@ def main():
119
159
  rprint(f"Using host: {the_host}")
120
160
  rprint(f"Using host: {the_host}")
121
161
  try:
122
- file_share(args.dir, the_host, args.port)
162
+ if args.dir:
163
+ rprint(f"[bold white]Sharing directory: {args.dir}[/]")
164
+ file_share(directory=args.dir, host=the_host, port=args.port)
165
+ else:
166
+ rprint(f"[bold white]Sharing file: {args.file}[/]")
167
+ if not os.path.isfile(args.file):
168
+ rprint(f"[bold red]Error:[/] The file [yellow]{args.file}[/] does not exist or is not a file.")
169
+ exit(1)
170
+ dir_path = generate_temporary_dir(the_path=".")
171
+ file_share(directory=dir_path,file=args.file, host=the_host, port=args.port)
123
172
  except Exception as e:
124
173
  rprint(f"[bold red]Error:[/] {e}")
125
174
  rprint("[bold red]Failed to start file sharing service.[/]")
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "shareit"
3
- version = "v0.2.0"
3
+ version = "v0.3.0"
4
4
  description = "A simple file sharing CLI tool."
5
5
  authors = [{ name = "Fahad Ahammed", email = "iamfahadahmed@gmail.com" }]
6
6
  license = "MIT"
File without changes
File without changes