Skip to content

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ywangd committed Feb 14, 2021
2 parents 86c1546 + bfd31b4 commit a19a580
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 13 deletions.
79 changes: 79 additions & 0 deletions bin/diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python

import sys, os, re, json, argparse, time, pytz
import console

from datetime import datetime, timedelta
from difflib import unified_diff, ndiff

#_____________________________________________________
def argue():
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('lhs')
parser.add_argument('rhs')
args = parser.parse_args()
if args.verbose:
json.dump(vars(args),sys.stderr,indent=4)
return args

#_____________________________________________________
def sn(x):
return '%s\n'%x

#_____________________________________________________
def modified(f):
lmt = os.path.getmtime(f)
est = pytz.timezone('Australia/Sydney')
gmt = pytz.timezone('GMT')
tzf = '%Y-%m-%d %H:%M:%S'
gdt = datetime.utcfromtimestamp(lmt)
gdt = gmt.localize(gdt)
adt = est.normalize(gdt.astimezone(est))
return adt.strftime(tzf)

#_____________________________________________________
def diff(lhs,rhs):
if not os.path.isfile(lhs):
sys.stderr.write('%s not a file\n'%lhs)
sys.exit(1)
if os.path.isdir(rhs):
rhs = '%s/%s'%(rhs,os.path.basename(lhs))
if not os.path.isfile(rhs):
sys.stderr.write('%s not a file\n'%rhs)
sys.exit(1)

flhs = open(lhs).readlines()
frhs = open(rhs).readlines()

diffs = unified_diff(
flhs,
frhs,
fromfile=lhs,
tofile=rhs,
fromfiledate=modified(lhs),
tofiledate=modified(rhs)
)
for line in diffs:
if line.startswith('+'):
console.set_color(0,1,0)
if line.startswith('-'):
console.set_color(0,0,1)
sys.stdout.write(line)
console.set_color(1,1,1)
return

#_____________________________________________________
def main():
console.clear()
args = argue()
diff(
args.lhs.rstrip('/'),
args.rhs.rstrip('/')
)
return

#_____________________________________________________
if __name__ == '__main__': main()


45 changes: 45 additions & 0 deletions bin/ln.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python

import sys, os, re, json, argparse, time, pytz
import console
from datetime import datetime, timedelta
from difflib import unified_diff, ndiff

def argue():
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('-s', '--symbolic', action='store_true')
parser.add_argument('-f', '--force', action='store_true')
parser.add_argument('lhs')
parser.add_argument('rhs')
args = parser.parse_args()
if args.verbose:
json.dump(vars(args),sys.stderr,indent=4)
return args

def ln(lhs,rhs,symbolic=False):
if not os.path.exists(lhs):
sys.stderr.write('%s not found\n'%lhs)
sys.exit(1)
if os.path.isdir(rhs):
rhs = '%s/%s'%(rhs,os.path.basename(lhs))
if os.path.isfile(rhs):
sys.stderr.write('%s already exists\n'%rhs)
sys.exit(1)
if os.path.islink(rhs):
sys.stderr.write('%s already linked\n'%rhs)
sys.exit(1)

if symbolic:
os.symlink(lhs,rhs)
else:
os.link(lhs,rhs)
return

def main():
console.clear()
args = argue()
ln(args.lhs,args.rhs,args.symbolic)
return

if __name__ == '__main__': main()
38 changes: 25 additions & 13 deletions bin/ls.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def is_image(path):
else:
return False


def main(args):

ap = ArgumentParser()
ap.add_argument('-1', '--one-line', action='store_true', help='List one file per line')
ap.add_argument('-a', '--all', action='store_true', help='do not ignore entries starting with .')
Expand All @@ -105,37 +105,49 @@ def _filter(filename):
if ns.long:

def _fmt(filename, dirname=''):
suffix = ''

_stat = os.stat(os.path.join(dirname, filename))


home = os.environ['HOME']
fullpath = os.path.join(dirname, filename)
if os.path.isdir(fullpath):
filename = '{:30}'.format(filename)

if os.path.islink(fullpath):
filename = _stash.text_color(filename, 'yellow')
suffix = ' -> %s'%os.path.realpath(fullpath).replace(home,'~')
elif os.path.isdir(fullpath):
filename = _stash.text_color(filename, 'blue')
elif filename.endswith('.py'):
filename = _stash.text_color(filename, 'green')
elif is_archive(fullpath):
elif tarfile.is_tarfile(fullpath) or zipfile.is_zipfile(fullpath):
filename = _stash.text_color(filename, 'red')
elif is_image(fullpath):
elif imghdr.what(fullpath) is not None:
filename = _stash.text_color(filename, 'brown')

ret = filename + _stash.text_color(
' (%s) %s' % (sizeof_fmt(_stat.st_size),
time.strftime("%Y-%m-%d %H:%M:%S",
time.localtime(_stat.st_mtime))),
'gray'
)
' ({:8}) {}'.format(sizeof_fmt(_stat.st_size),
time.strftime(
"%Y-%m-%d %H:%M:%S",
time.localtime(_stat.st_mtime)),
),
'gray',
) + suffix

return ret
else:

def _fmt(filename, dirname=''):
fullpath = os.path.join(dirname, filename)
if os.path.isdir(fullpath):
if os.path.islink(fullpath):
return _stash.text_color(filename, 'yellow')
elif os.path.isdir(fullpath):
return _stash.text_color(filename, 'blue')
elif filename.endswith('.py'):
return _stash.text_color(filename, 'green')
elif is_archive(fullpath):
elif tarfile.is_tarfile(fullpath) or zipfile.is_zipfile(fullpath):
return _stash.text_color(filename, 'red')
elif is_image(fullpath):
elif imghdr.what(fullpath) is not None:
return _stash.text_color(filename, 'brown')
else:
return filename
Expand Down

0 comments on commit a19a580

Please sign in to comment.