-
-
Notifications
You must be signed in to change notification settings - Fork 92
Glasgow | 26- SDC-Mar | Taras Mykytiuk | Sprint 4 | Implement shell tools python #512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TarasMykytiuk
wants to merge
3
commits into
CodeYourFuture:main
Choose a base branch
from
TarasMykytiuk:implement_shell_tools_python
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import sys | ||
| import os | ||
|
|
||
|
|
||
| def main(): | ||
| args = sys.argv[1:] | ||
| flags = [] | ||
| addresses = [] | ||
| for item in args: | ||
| if item[0] == "-": | ||
| flags.append(item) | ||
| else: | ||
| addresses.append(item) | ||
|
|
||
| outputLines = [] | ||
| for fileAddress in addresses: | ||
| try: | ||
| lines = readFileByLines(fileAddress) | ||
| for line in lines: | ||
| outputLines.append(line) | ||
| except Exception as e: | ||
| print(e) | ||
| printLines(outputLines, flags) | ||
|
|
||
|
|
||
| def readFileByLines(fileAddress): | ||
| file = open(fileAddress, "r", encoding="utf-8") | ||
| content = file.read() | ||
| lines = content.split("\n") | ||
| if len(lines) != 0 and lines[-1] == "": | ||
| lines.pop() | ||
| return lines | ||
|
|
||
|
|
||
| def printLines(outputLines, flags): | ||
| lineNumber = 1 | ||
| for line in outputLines: | ||
| if ("-n" in flags and "-b" not in flags) or ("-b" in flags and line != ""): | ||
| output = " " + str(lineNumber) + " " + line | ||
| print(output) | ||
| lineNumber += 1 | ||
| else: | ||
| print(line) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import sys | ||
| import os | ||
| import pwd | ||
| import grp | ||
| import stat | ||
| from datetime import datetime | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def main(): | ||
| args = sys.argv[1:] | ||
| flags = [] | ||
| parentDir = ".." | ||
| currentDir = "." | ||
| path = currentDir | ||
| for item in args: | ||
| if item[0] == "-": | ||
| flags.append(item) | ||
| else: | ||
| path = item | ||
| scanResult = os.scandir(path) | ||
| isHiddenToShow = "-a" in flags | ||
| isStatsToList = "-l" in flags | ||
| content = sortDirContent(scanResult, isHiddenToShow) | ||
|
|
||
| if not isHiddenToShow and not isStatsToList: | ||
| output = "" | ||
| for item in content: | ||
| output += ( | ||
| addColorToStr("\033[34m", item.name) + " " | ||
| if Path(item).is_dir() | ||
| else item.name + " " | ||
| ) | ||
| print(output) | ||
| elif isHiddenToShow and not isStatsToList: | ||
| output = ( | ||
| addColorToStr("\033[34m", currentDir) | ||
| + " " | ||
| + addColorToStr("\033[34m", parentDir) | ||
| + " " | ||
| ) | ||
| for item in content: | ||
| output += ( | ||
| addColorToStr("\033[34m", item.name) + " " | ||
| if Path(item).is_dir() | ||
| else item.name + " " | ||
| ) | ||
| print(output) | ||
| elif not isHiddenToShow and isStatsToList: | ||
| print("total " + str(calcTotalSpace(content))) | ||
| printContentStats(content) | ||
| elif isHiddenToShow and isStatsToList: | ||
| print("total " + str(calcTotalSpace(content))) | ||
| currentDirObj = Path(currentDir) | ||
| parentDirObj = Path(parentDir) | ||
| printContentStats([currentDirObj, parentDirObj]) | ||
| printContentStats(content) | ||
|
|
||
|
|
||
| def calcTotalSpace(content): | ||
| sum = 0 | ||
| for item in content: | ||
| sum += item.stat().st_blocks | ||
| return sum // 2 | ||
|
|
||
|
|
||
| def addColorToStr(color, str): | ||
| reset = "\033[0m" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice idea to experiment with this |
||
| return color + str + reset | ||
|
|
||
|
|
||
| def sortDirContent(scanResult, isHiddenToShow): | ||
| files = [] | ||
| directories = [] | ||
| hidden = [] | ||
| for item in scanResult: | ||
| if item.name[0] == ".": | ||
| hidden.append(item) | ||
| elif os.path.isfile(item): | ||
| files.append(item) | ||
| elif os.path.isdir(item): | ||
| directories.append(item) | ||
| content = files + directories + hidden if isHiddenToShow else files + directories | ||
| return content | ||
|
|
||
|
|
||
| def readFilePermissions(path): | ||
| permissions = os.stat(path) | ||
| perm_str = stat.filemode(permissions.st_mode) | ||
| return perm_str | ||
|
|
||
|
|
||
| def printContentStats(content): | ||
| for item in content: | ||
| perm = stat.filemode(item.stat().st_mode) | ||
| links = str(item.stat().st_nlink) | ||
| owner = pwd.getpwuid(item.stat().st_uid).pw_name | ||
| group = grp.getgrgid(item.stat().st_gid).gr_name | ||
| size = str(item.stat().st_size).rjust(4) | ||
| lastModTime = formatTime(item.stat().st_mtime) | ||
| itemName = str(item) if str(item) == "." else item.name | ||
| if item.is_dir(): | ||
| itemName = addColorToStr("\033[34m", itemName) | ||
| print( | ||
| perm | ||
| + " " | ||
| + links | ||
| + " " | ||
| + owner | ||
| + " " | ||
| + group | ||
| + " " | ||
| + size | ||
| + " " | ||
| + lastModTime | ||
| + " " | ||
| + itemName | ||
| ) | ||
|
|
||
|
|
||
| def formatTime(timestamp): | ||
| time = datetime.fromtimestamp(timestamp) | ||
| month = time.strftime("%b") # str(time.month) | ||
| day = str(time.day) | ||
| hour = str(time.hour) | ||
| minute = str(time.minute) | ||
| return month + " " + day + " " + hour.rjust(2, "0") + ":" + minute.rjust(2, "0") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import sys | ||
| import os | ||
|
|
||
|
|
||
| def main(): | ||
| args = sys.argv[1:] | ||
| flags = [] | ||
| addresses = [] | ||
| for item in args: | ||
| if item[0] == "-": | ||
| flags.append(item) | ||
| else: | ||
| addresses.append(item) | ||
| isLinesNumDisplay = "-l" in flags | ||
| isWordsNumDisplay = "-w" in flags | ||
| isBytesNumDisplay = "-c" in flags | ||
| isNoFlags = len(flags) == 0 | ||
| totalLines = 0 | ||
| totalWords = 0 | ||
| totalBytes = 0 | ||
| for address in addresses: | ||
| file = open(address) | ||
| fileContent = file.read() | ||
| linesNum = countFileLines(fileContent) | ||
| wordsNum = countFileWords(fileContent) | ||
| bytes = os.path.getsize(address) | ||
| totalLines += linesNum | ||
| totalWords += wordsNum | ||
| totalBytes += bytes | ||
| output = " " | ||
| if isLinesNumDisplay or isNoFlags: | ||
| output += str(linesNum) + " " | ||
| if isWordsNumDisplay or isNoFlags: | ||
| output += str(wordsNum) + " " | ||
| if isBytesNumDisplay or isNoFlags: | ||
| output += str(bytes) + " " | ||
| print(output + address) | ||
| totalOutput = " " | ||
| if isLinesNumDisplay or isNoFlags: | ||
| totalOutput += str(totalLines) + " " | ||
| if isWordsNumDisplay or isNoFlags: | ||
| totalOutput += str(totalWords) + " " | ||
| if isBytesNumDisplay or isNoFlags: | ||
| totalOutput += str(totalBytes) + " " | ||
| print(totalOutput + "total") | ||
|
|
||
|
|
||
| def countFileLines(fileContent): | ||
| lines = fileContent.split("\n") | ||
| linesNum = len(lines) | ||
| if lines[linesNum - 1] == "": | ||
| linesNum -= 1 | ||
| return linesNum | ||
|
|
||
|
|
||
| def countFileWords(fileContent): | ||
| fileContent = fileContent.replace("\n", " ") | ||
| words = fileContent.split(" ") | ||
| words = list(filter(lambda word: word != "", words)) | ||
| return len(words) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you re-read the readme file? Are you sure it is asking for the
-largument?