Last active
July 28, 2019 01:44
-
-
Save youknowcast/fb2c659cba86788843ba0d6d18bb0f2a to your computer and use it in GitHub Desktop.
challenge3-2 ls
This file contains 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
from argparse import ArgumentParser, FileType | |
import os | |
import grp | |
import pwd | |
import stat | |
import datetime | |
parser = ArgumentParser( | |
prog='ls.py', | |
usage='', | |
description='show specified directory/file info.', | |
epilog='end', | |
add_help=True) | |
parser.add_argument('-l', | |
action='store_true', | |
help='show file mode, link, owner, group, size, timestamp, name', | |
required=False) | |
parser.add_argument('-a', | |
action='store_true', | |
help='include . ..', | |
required=False) | |
parser.add_argument('-i', | |
action='store_true', | |
help='show i-node number', | |
required=False) | |
parser.add_argument('target', | |
action='store', | |
nargs='?', | |
help='path', | |
default='./', | |
const='./') | |
args = parser.parse_args() | |
org_path = args.target | |
if os.path.exists(org_path) is False: | |
print('not exit such file: {}'.format(org_path)) | |
exit(-1) | |
# get target base directory(base_path) and move 2 it. | |
path = os.path.abspath(org_path) | |
if os.path.isfile(org_path): | |
base_path = os.path.dirname(path) | |
files = [org_path] | |
else: | |
base_path = path | |
files = os.listdir(path) | |
if args.a: | |
files = files + ['.', '..'] | |
files.sort() | |
# out "total" blocks line if "l" option is specified.' | |
if args.l: | |
print('total {}'.format(os.stat(path).st_blocks)) | |
os.chdir(base_path) | |
for f in files: | |
st = os.lstat(f) | |
data = {'path': f, 'inode': '', 'mode': '', 'uid': '', | |
'gid': '', 'size': '', 'link': '', 'mtime': ''} | |
if args.i: | |
data['inode'] = st.st_ino | |
if args.l: | |
fm = stat.filemode(st.st_mode) | |
data['mode'] = fm | |
data['uid'] = pwd.getpwuid(st.st_uid).pw_name | |
data['gid'] = grp.getgrgid(st.st_gid).gr_name | |
data['size'] = st.st_size | |
data['link'] = st.st_nlink | |
tmp_date = datetime.datetime.fromtimestamp(st.st_mtime) | |
data['mtime'] = tmp_date.strftime('%m %d %H:%M') | |
if fm[0] == 'l': | |
data['path'] = '{} -> {}'.format(data['path'], os.readlink(f)) | |
ret = "{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}".format( | |
data['inode'], | |
data['mode'], | |
data['link'], | |
data['uid'], | |
data['gid'], | |
data['size'], | |
data['mtime'], | |
data['path']).replace(' ', '') | |
print(ret.lstrip().replace("\t\t", "")) |
This file contains 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
#!/bin/sh | |
PYTHON_CMD=/usr/local/bin/python3 | |
$PYTHON_CMD --version | |
echo "generate direstory and symlink" | |
mkdir test | |
mkdir test2 | |
touch test/hoge | |
touch test2/huga | |
touch test2/moge | |
ln -s test2/huga test/sym_test | |
echo "test file tree is above:" | |
find . | |
echo | |
echo "[ls] shows files in current directory " | |
$PYTHON_CMD ls.py | |
echo ---- | |
echo "[ls .] shows files in current directory " | |
$PYTHON_CMD ls.py . | |
echo ---- | |
echo "[ls ..] shows files in parent directory " | |
$PYTHON_CMD ls.py .. | |
echo ---- | |
echo "[ls ls.py] shows \"ls.py\" file info" | |
$PYTHON_CMD ls.py ls.py | |
echo ---- | |
echo "[ls ~/.vimrc] shows \".vimrc\" in user home directory file info" | |
$PYTHON_CMD ls.py ~/.vimrc | |
echo ---- | |
echo "[ls /path/to/ls.py] shows \".ls.py\" in current directory file info" | |
pwd_ls="$(pwd)/ls.py" | |
$PYTHON_CMD ls.py $pwd_ls | |
echo ---- | |
echo "[ls -l ls.py] shows \"ls.py\" file info(with l option)" | |
$PYTHON_CMD ls.py -l ls.py | |
echo ---- | |
echo "[ls ls.py -l] shows \"ls.py\" file info(with l option), also" | |
$PYTHON_CMD ls.py ls.py -l | |
echo ---- | |
echo "[ls -a ls.py] shows \"ls.py\" file info(with a option)" | |
$PYTHON_CMD ls.py -a ls.py | |
echo ---- | |
echo "[ls -i ls.py] shows \"ls.py\" file info(with i option)" | |
$PYTHON_CMD ls.py -i ls.py | |
echo ---- | |
echo "[ls -ail ls.py] shows \"ls.py\" file info(with a,i,l option)" | |
$PYTHON_CMD ls.py -ail ls.py | |
echo ---- | |
echo "[ls -ail] shows current files info(with a,i,l option)" | |
$PYTHON_CMD ls.py -ail | |
echo ---- | |
echo "[ls -ail ..] shows files info in parent directory(with a,i,l option)" | |
$PYTHON_CMD ls.py -ail | |
echo ---- | |
echo "[ls -ail test] shows \"test\" info in parent directory(with a,i,l option)" | |
$PYTHON_CMD ls.py -ail test | |
echo ---- | |
echo "[ls -ail ~/.vimrc] shows \".vimrc\" info in user home directory(with a,i,l option)" | |
$PYTHON_CMD ls.py -ail ~/.vimrc | |
echo ---- | |
echo "[ls -ail /path/to/ls.py] shows \"ls.py\" info with full path(with a,i,l option)" | |
$PYTHON_CMD ls.py -ail $pwd_ls | |
rm -rf test | |
rm -rf test2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment