Skip to content

Commit

Permalink
Merge faa02a6 into 9be803c
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jan 9, 2019
2 parents 9be803c + faa02a6 commit f9150ab
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
18 changes: 18 additions & 0 deletions fs/examples/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
This directory contains a number of example command line apps.
They are intentionally very simple and not exactly a finished product, but
all these examples are completely functional.
You typically run then from the command line with the following:
python -m fs.examples.SCRIPT
See the docstrings for details.
Note that if the command requires a path, a FS URL will also work. So the
script will work with archives / servers etc.
"""
22 changes: 22 additions & 0 deletions fs/examples/count_py.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Display how much storage is used in your Python files.
Usage:
python -m fs.examples.count_py <PATH or FS URL>
"""

import sys

from fs import open_fs
from fs.filesize import traditional


fs_url = sys.argv[1]
count = 0

with open_fs(fs_url) as fs:
for _path, info in fs.walk.info(filter=["*.py"], namespaces=["details"]):
count += info.size

print(f'There is {traditional(count)} of Python in "{fs_url}"')
40 changes: 40 additions & 0 deletions fs/examples/find_dups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Find an print paths to files with identical contents.
Usage:
python -m fs.examples.find_dups <PATH or FS URL>
"""

from collections import defaultdict
import hashlib
import sys

from fs import open_fs


def get_hash(bin_file):
"""Get the md5 hash of a file."""
file_hash = hashlib.md5()
while True:
chunk = bin_file.read(1024 * 1024)
if not chunk:
break
file_hash.update(chunk)
return file_hash.hexdigest()


hashes = defaultdict(list)
with open_fs(sys.argv[1]) as fs:
for path in fs.walk.files():
with fs.open(path, "rb") as bin_file:
file_hash = get_hash(bin_file)
hashes[file_hash].append(path)

for paths in hashes.values():
if len(paths) > 1:
for path in paths[1:]:
print(f" {path}")
print()

17 changes: 17 additions & 0 deletions fs/examples/rm_pyc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
Remove all pyc files in a directory.
Usage:
python -m fs.examples.rm_pyc <PATH or FS URL>
"""

import sys

from fs import open_fs


with open_fs(sys.argv[1]) as fs:
count = fs.glob("**/*.pyc").remove()
print(f"{count} .pyc files remove")
29 changes: 29 additions & 0 deletions fs/examples/upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
Upload a file to a server (or other filesystem)
Usage:
python -m fs.examples.upload FILENAME <FS URL>
example:
python -m fs.examples.upload foo.txt ftp://example.org/uploads/
"""

import os
import sys

from fs import open_fs

_, file_path, fs_url = sys.argv
filename = os.path.basename(file_path)

with open_fs(fs_url) as fs:
if fs.exists(filename):
print("destination exists! aborting.")
else:
with open(file_path, "rb") as bin_file:
fs.upload(filename, bin_file)
print("upload successful!")

0 comments on commit f9150ab

Please sign in to comment.