Skip to content

Commit

Permalink
Add more python commands
Browse files Browse the repository at this point in the history
Signed-off-by: lewisevans2007 <[email protected]>
  • Loading branch information
lewisevans2007 committed Aug 31, 2023
1 parent 8d98c2c commit 1c08c9d
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 7 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ Here is a list of the commands and their availability:
- `pwd` - Print name of current/working directory `C++` `C` `Ultra light` `Rust` `Python`
- `cat` - Concatenate files and print on the standard output`C++` `C` `Ultra light` `Rust` `Python`
- `echo` - Display a line of text `C++` `C` `Ultra light` `Rust` `Python`
- `mkdir` - Create a directory`C++` `C` `Ultra light` `Rust`
- `rmdir` - Remove a directory `C++` `C` `Ultra light` `Rust`
- `rm` - Remove files or directories`C++` `C` `Ultra light` `Rust`
- `cp` - Copy files and directories `C++` `Ultra light` `Rust`
- `mv` - Move (rename) files `C++` `C` `Ultra light` `Rust`
- `touch` - Change file timestamps `C++` `C` `Ultra light` `Rust`
- `date` - Display the current time and date `C++` `C` `Ultra light` `Rust`
- `mkdir` - Create a directory`C++` `C` `Ultra light` `Rust` `Python`
- `rmdir` - Remove a directory `C++` `C` `Ultra light` `Rust` `Python`
- `rm` - Remove files or directories`C++` `C` `Ultra light` `Rust` `Python`
- `cp` - Copy files and directories `C++` `Ultra light` `Rust` `Python`
- `mv` - Move (rename) files `C++` `C` `Ultra light` `Rust` `Python`
- `touch` - Change file timestamps `C++` `C` `Ultra light` `Rust` `Python`
- `date` - Display the current time and date `C++` `C` `Ultra light` `Rust` `Python`

For documentation on the commands, see the [doc/README.md](doc/README.md) folder.

Expand Down
42 changes: 42 additions & 0 deletions src/python/cp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python3
# Light builtins's (Python version)
# cp - copy files or directories
# A lightweight repository of useful shell commands
# GitHub: https://www.github.com/lewisevans2007/light_builtins
# Licence: GNU General Public License v3.0
# By: Lewis Evans

import os
import sys

def main(args):
if len(args) < 2:
print("cp: missing file operand")
return 1
if len(args) > 2:
print("cp: extra operand")
return 1
if not os.path.exists(args[0]):
print("cp: cannot stat '" + args[0] + "': No such file or directory")
return 1
if os.path.isdir(args[0]):
print("cp: omitting directory '" + args[0] + "'")
return 1

try:
with open(args[0], 'r') as f:
data = f.read()
except:
print("cp: cannot stat '" + args[0] + "': Permission denied")
return 1

try:
with open(args[1], 'w') as f:
f.write(data)
except:
print("cp: cannot stat '" + args[1] + "': Permission denied")
return 1
return 0

if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
18 changes: 18 additions & 0 deletions src/python/date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python3
# Light builtins's (Python version)
# date - show the date and time
# A lightweight repository of useful shell commands
# GitHub: https://www.github.com/lewisevans2007/light_builtins
# Licence: GNU General Public License v3.0
# By: Lewis Evans

import sys
import datetime

def main(args):
date_time = datetime.datetime.now()
print(date_time)
return 0

if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
18 changes: 18 additions & 0 deletions src/python/mkdir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python3
# Light builtins's (Python version)
# mkdir - make directories
# A lightweight repository of useful shell commands
# GitHub: https://www.github.com/lewisevans2007/light_builtins
# Licence: GNU General Public License v3.0
# By: Lewis Evans

import os
import sys

def main(args):
for arg in args:
os.mkdir(arg)
return 0

if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
34 changes: 34 additions & 0 deletions src/python/mv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3
# Light builtins's (Python version)
# mv - move files or directories
# A lightweight repository of useful shell commands
# GitHub: https://www.github.com/lewisevans2007/light_builtins
# Licence: GNU General Public License v3.0
# By: Lewis Evans

import os
import sys

def main(args):
if len(args) < 2:
print("mv: missing file operand")
return 1
if len(args) > 2:
print("mv: extra operand")
return 1
if not os.path.exists(args[0]):
print("mv: cannot stat '" + args[0] + "': No such file or directory")
return 1
if os.path.isdir(args[0]):
print("mv: omitting directory '" + args[0] + "'")
return 1

try:
os.rename(args[0], args[1])
except:
print("mv: cannot stat '" + args[1] + "': Permission denied")
return 1
return 0

if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
18 changes: 18 additions & 0 deletions src/python/rm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python3
# Light builtins's (Python version)
# rm - remove files or directories
# A lightweight repository of useful shell commands
# GitHub: https://www.github.com/lewisevans2007/light_builtins
# Licence: GNU General Public License v3.0
# By: Lewis Evans

import os
import sys

def main(args):
for arg in args:
os.remove(arg)
return 0

if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
18 changes: 18 additions & 0 deletions src/python/rmdir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python3
# Light builtins's (Python version)
# rmdir - remove empty directories
# A lightweight repository of useful shell commands
# GitHub: https://www.github.com/lewisevans2007/light_builtins
# Licence: GNU General Public License v3.0
# By: Lewis Evans

import os
import sys

def main(args):
for arg in args:
os.rmdir(arg)
return 0

if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
31 changes: 31 additions & 0 deletions src/python/touch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python3
# Light builtins's (Python version)
# touch - create a file with the current time
# A lightweight repository of useful shell commands
# GitHub: https://www.github.com/lewisevans2007/light_builtins
# Licence: GNU General Public License v3.0
# By: Lewis Evans

import os
import sys

def main(args):
if len(args) < 1:
print("touch: missing file operand")
return 1
if len(args) > 1:
print("touch: extra operand")
return 1
if os.path.exists(args[0]):
print("touch: cannot create file '" + args[0] + "': File exists")
return 1
try:
with open(args[0], 'w') as f:
f.write("")
except:
print("touch: cannot create file '" + args[0] + "': Permission denied")
return 1
return 0

if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))

0 comments on commit 1c08c9d

Please sign in to comment.