<s> """<STR_LIT>""" <EOL> import os <EOL> import re <EOL> import subprocess <EOL> import socket <EOL> import smtplib <EOL> import tempfile <EOL> import datetime <EOL> from email . mime . text import MIMEText <EOL> import mistune <EOL> import git <EOL> from cmddocs . utils import * <EOL> from cmddocs . rendering import md_to_ascii <EOL> def list_articles ( dir , extension ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> DEVNULL = open ( os . devnull , '<STR_LIT:wb>' ) <EOL> listing = subprocess . check_output ( [ "<STR_LIT>" , dir ] , stderr = DEVNULL ) <EOL> listing = remove_fileextension ( listing , extension ) <EOL> print ( listing ) <EOL> except OSError : <EOL> print ( "<STR_LIT>" ) <EOL> return False <EOL> except subprocess . CalledProcessError : <EOL> print ( "<STR_LIT>" ) <EOL> def list_directories ( dir ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> DEVNULL = open ( os . devnull , '<STR_LIT:wb>' ) <EOL> listing = subprocess . check_output ( [ "<STR_LIT>" , "<STR_LIT>" , dir ] , stderr = DEVNULL ) <EOL> print ( listing ) <EOL> except OSError : <EOL> print ( "<STR_LIT>" ) <EOL> return False <EOL> except subprocess . CalledProcessError : <EOL> print ( "<STR_LIT>" ) <EOL> def change_directory ( dir , datadir ) : <EOL> "<STR_LIT>" <EOL> d = os . path . join ( os . getcwd ( ) , dir ) <EOL> if datadir not in d : <EOL> d = datadir <EOL> if not dir : <EOL> d = datadir <EOL> try : <EOL> os . chdir ( d ) <EOL> return d <EOL> except OSError : <EOL> print ( "<STR_LIT>" % dir ) <EOL> def edit_article ( article , directory , editor , repo , default_commit_msg , extension , test , editorflags ) : <EOL> """<STR_LIT>""" <EOL> a = add_fileextension ( article , extension ) <EOL> a = os . path . join ( directory , a ) <EOL> d = os . path . dirname ( a ) <EOL> if not os . path . isdir ( d ) : <EOL> try : <EOL> os . makedirs ( d ) <EOL> except OSError : <EOL> print ( "<STR_LIT>" % d ) <EOL> return False <EOL> if test is False : <EOL> try : <EOL> if editorflags is False : <EOL> subprocess . call ( [ editor , a ] ) <EOL> else : <EOL> subprocess . call ( [ editor , editorflags , a ] ) <EOL> except OSError : <EOL> print ( "<STR_LIT>" % editor ) <EOL> return False <EOL> else : <EOL> try : <EOL> fp = open ( a , "<STR_LIT>" ) <EOL> content = "<STR_LIT>" <EOL> fp . write ( content ) <EOL> fp . close ( ) <EOL> except OSError : <EOL> print ( "<STR_LIT>" % editor ) <EOL> try : <EOL> repo . git . add ( a ) <EOL> if repo . is_dirty ( ) : <EOL> if test is False : <EOL> try : <EOL> msg = raw_input ( "<STR_LIT>" ) <EOL> if not msg : <EOL> msg = default_commit_msg <EOL> except OSError : <EOL> print ( "<STR_LIT>" ) <EOL> else : <EOL> msg = default_commit_msg <EOL> print ( "<STR_LIT>" ) <EOL> try : <EOL> repo . git . commit ( m = msg ) <EOL> except OSError : <EOL> print ( "<STR_LIT>" ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> except ( OSError , git . exc . GitCommandError ) as e : <EOL> print ( "<STR_LIT>" ) <EOL> def info_article ( article , dir , repo , extension ) : <EOL> "<STR_LIT>" <EOL> a = add_fileextension ( article , extension ) <EOL> a = os . path . join ( dir , a ) <EOL> try : <EOL> commits = repo . git . log ( a , follow = True , format = "<STR_LIT>" ) <EOL> commits = commits . split ( ) <EOL> n = len ( commits ) <EOL> except git . exc . GitCommandError : <EOL> print ( "<STR_LIT>" ) <EOL> return False <EOL> created = repo . git . show ( commits [ n - <NUM_LIT:1> ] , quiet = True , pretty = "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" % created ) <EOL> updated = repo . git . show ( commits [ <NUM_LIT:0> ] , quiet = True , pretty = "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" % updated ) <EOL> print ( "<STR_LIT>" % n ) <EOL> num_lines = <NUM_LIT:0> <EOL> num_words = <NUM_LIT:0> <EOL> num_chars = <NUM_LIT:0> <EOL> with open ( a , '<STR_LIT:r>' ) as f : <EOL> for line in f : <EOL> words = line . split ( ) <EOL> num_lines += <NUM_LIT:1> <EOL> num_words += len ( words ) <EOL> num_chars += len ( line ) <EOL> print ( "<STR_LIT>" % num_lines ) <EOL> print ( "<STR_LIT>" % num_words ) <EOL> print ( "<STR_LIT>" % num_chars ) <EOL> def mail_article ( article , dir , mailfrom , extension ) : <EOL> "<STR_LIT>" <EOL> a = add_fileextension ( article , extension ) <EOL> a = os . path . join ( dir , a ) <EOL> try : <EOL> fp = open ( a , '<STR_LIT:r>' ) <EOL> msg = MIMEText ( fp . read ( ) ) <EOL> fp . close ( ) <EOL> except IOError : <EOL> print ( "<STR_LIT>" ) <EOL> return False <EOL> mailto = raw_input ( "<STR_LIT>" ) <EOL> msg [ '<STR_LIT>' ] = article <EOL> msg [ '<STR_LIT>' ] = mailfrom <EOL> msg [ '<STR_LIT>' ] = mailto <EOL> s = smtplib . SMTP ( ) <EOL> try : <EOL> s . connect ( ) <EOL> s . sendmail ( mailfrom , [ mailto ] , msg . as_string ( ) ) <EOL> s . quit ( ) <EOL> except socket . error : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> return False <EOL> except smtplib . SMTPRecipientsRefused : <EOL> print ( "<STR_LIT>" ) <EOL> return False <EOL> def view_article ( article , dir , pager , extension , pagerflags , colors ) : <EOL> "<STR_LIT>" <EOL> a = add_fileextension ( article , extension ) <EOL> a = os . path . join ( dir , a ) <EOL> try : <EOL> article = open ( a , "<STR_LIT:r>" ) <EOL> except IOError : <EOL> print ( "<STR_LIT>" % article ) <EOL> return False <EOL> content = article . read ( ) <EOL> article . close ( ) <EOL> with tempfile . NamedTemporaryFile ( delete = False ) as tmp : <EOL> md = mistune . Markdown ( renderer = md_to_ascii ( colors ) ) <EOL> tmp . write ( md . render ( content ) ) <EOL> try : <EOL> if pagerflags is False : <EOL> subprocess . call ( [ pager , tmp . name ] ) <EOL> else : <EOL> subprocess . call ( [ pager , pagerflags , tmp . name ] ) <EOL> except OSError : <EOL> print ( "<STR_LIT>" % pager ) <EOL> try : <EOL> os . remove ( tmp . name ) <EOL> except OSError : <EOL> print ( "<STR_LIT>" % tmp . name ) <EOL> def delete_article ( article , dir , repo , extension ) : <EOL> """<STR_LIT>""" <EOL> a = add_fileextension ( article , extension ) <EOL> a = os . path . join ( dir , a ) <EOL> try : <EOL> repo . git . rm ( a ) <EOL> repo . git . commit ( m = "<STR_LIT>" % article ) <EOL> print ( "<STR_LIT>" % article ) <EOL> except : <EOL> if os . path . isdir ( a ) : <EOL> try : <EOL> os . rmdir ( a ) <EOL> print ( "<STR_LIT>" % a ) <EOL> except OSError : <EOL> print ( "<STR_LIT>" % a ) <EOL> else : <EOL> try : <EOL> os . remove ( a ) <EOL> print ( "<STR_LIT>" % a ) <EOL> except OSError : <EOL> print ( "<STR_LIT>" % a ) <EOL> return <EOL> def move_article ( dir , args , repo , extension ) : <EOL> "<STR_LIT>" <EOL> args = args . split ( ) <EOL> if len ( args ) != <NUM_LIT:2> : <EOL> print ( "<STR_LIT>" ) <EOL> return False <EOL> a = os . path . join ( dir , args [ <NUM_LIT:0> ] ) <EOL> a = add_fileextension ( a , extension ) <EOL> e = os . path . join ( dir , args [ <NUM_LIT:1> ] ) <EOL> e = add_fileextension ( e , extension ) <EOL> d = os . path . dirname ( e ) <EOL> if not os . path . isdir ( d ) : <EOL> os . makedirs ( d ) <EOL> try : <EOL> repo . git . mv ( a , e ) <EOL> repo . git . commit ( m = "<STR_LIT>" % ( a , e ) ) <EOL> print ( "<STR_LIT>" % ( a , e ) ) <EOL> except git . exc . GitCommandError : <EOL> print ( "<STR_LIT>" ) <EOL> def search_article ( keyword , directory , datadir , exclude ) : <EOL> """<STR_LIT>""" <EOL> c = <NUM_LIT:0> <EOL> r = re . compile ( keyword ) <EOL> print ( "<STR_LIT>" ) <EOL> for dirpath , dirs , files in os . walk ( directory ) : <EOL> dirs [ : ] = [ d for d in dirs if d not in exclude ] <EOL> for fname in files : <EOL> path = os . path . join ( dirpath , fname ) <EOL> if r . search ( path ) is not None : <EOL> print ( "<STR_LIT>" % <EOL> os . path . relpath ( path , datadir ) ) <EOL> c = c + <NUM_LIT:1> <EOL> print ( "<STR_LIT>" ) <EOL> for dirpath , dirs , files in os . walk ( directory ) : <EOL> dirs [ : ] = [ d for d in dirs if d not in exclude ] <EOL> for fname in files : <EOL> path = os . path . join ( dirpath , fname ) <EOL> f = open ( path , "<STR_LIT>" ) <EOL> for i , line in enumerate ( f ) : <EOL> if r . search ( line ) : <EOL> c = c + <NUM_LIT:1> <EOL> print ( "<STR_LIT>" % ( os . path . relpath ( path , datadir ) , <EOL> line . rstrip ( '<STR_LIT:\n>' ) ) ) <EOL> return "<STR_LIT>" % c <EOL> def show_diff ( args , repo , extension ) : <EOL> """<STR_LIT>""" <EOL> colorization = "<STR_LIT>" <EOL> unifiedopt = "<STR_LIT:0>" <EOL> args = args . split ( ) <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> if os . path . isfile ( os . path . join ( os . getcwd ( ) , add_fileextension ( args [ <NUM_LIT:1> ] , extension ) ) ) : <EOL> try : <EOL> print ( repo . git . diff ( '<STR_LIT>' + args [ <NUM_LIT:0> ] , add_fileextension ( args [ <NUM_LIT:1> ] , extension ) , <EOL> unified = unifiedopt , color = colorization ) ) <EOL> except git . exc . GitCommandError : <EOL> print ( "<STR_LIT>" ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> elif len ( args ) == <NUM_LIT:1> : <EOL> try : <EOL> print ( repo . git . diff ( '<STR_LIT>' + args [ <NUM_LIT:0> ] , <EOL> unified = unifiedopt , color = colorization ) ) <EOL> except git . exc . GitCommandError : <EOL> print ( "<STR_LIT>" ) <EOL> else : <EOL> try : <EOL> print ( repo . git . diff ( '<STR_LIT>' , unified = "<STR_LIT:0>" , color = "<STR_LIT>" ) ) <EOL> except git . exc . GitCommandError : <EOL> print ( "<STR_LIT>" ) <EOL> def show_log ( args , repo , extension ) : <EOL> """<STR_LIT>""" <EOL> args = args . split ( ) <EOL> format = "<STR_LIT>" <EOL> dateformat = "<STR_LIT>" <EOL> if len ( args ) >= <NUM_LIT:1> : <EOL> if os . path . isfile ( os . path . join ( os . getcwd ( ) , add_fileextension ( args [ <NUM_LIT:0> ] , extension ) ) ) : <EOL> file = add_fileextension ( args [ <NUM_LIT:0> ] , extension ) <EOL> try : <EOL> count = args [ <NUM_LIT:1> ] <EOL> print ( "<STR_LIT>" % ( count , file ) ) <EOL> print ( repo . git . log ( file , pretty = format , n = count , <EOL> date = dateformat , follow = True ) ) <EOL> except IndexError : <EOL> count = <NUM_LIT:10> <EOL> print ( "<STR_LIT>" % ( count , file ) ) <EOL> print ( repo . git . log ( file , pretty = format , n = count , <EOL> date = dateformat , follow = True ) ) <EOL> except git . exc . GitCommandError : <EOL> print ( "<STR_LIT>" ) <EOL> else : <EOL> count = args [ <NUM_LIT:0> ] <EOL> try : <EOL> file = add_fileextension ( args [ <NUM_LIT:1> ] , extension ) <EOL> print ( "<STR_LIT>" % ( count , file ) ) <EOL> print ( repo . git . log ( file , pretty = format , n = count , <EOL> date = dateformat , follow = True ) ) <EOL> except IndexError : <EOL> print ( "<STR_LIT>" % count ) <EOL> print ( repo . git . log ( pretty = format , n = count , <EOL> date = dateformat ) ) <EOL> except git . exc . GitCommandError : <EOL> print ( "<STR_LIT>" ) <EOL> elif len ( args ) == <NUM_LIT:0> : <EOL> count = <NUM_LIT:10> <EOL> print ( "<STR_LIT>" % count ) <EOL> try : <EOL> print ( repo . git . log ( pretty = format , n = count , date = dateformat ) ) <EOL> except git . exc . GitCommandError : <EOL> print ( "<STR_LIT>" ) <EOL> def undo_change ( args , repo ) : <EOL> """<STR_LIT>""" <EOL> args = args . split ( ) <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> try : <EOL> print ( repo . git . show ( args [ <NUM_LIT:0> ] , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> msg = raw_input ( "<STR_LIT>" ) <EOL> if msg == "<STR_LIT:y>" : <EOL> repo . git . revert ( args [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> except git . exc . GitCommandError : <EOL> print ( "<STR_LIT>" ) <EOL> def show_stats ( args , repo , datadir ) : <EOL> """<STR_LIT>""" <EOL> commits = repo . git . log ( format = "<STR_LIT>" ) <EOL> commits = commits . split ( '<STR_LIT:\n>' ) <EOL> n = len ( commits ) <EOL> print ( "<STR_LIT>" % commits [ <NUM_LIT:0> ] ) <EOL> print ( "<STR_LIT>" % commits [ n - <NUM_LIT:1> ] ) <EOL> print ( "<STR_LIT>" % n ) <EOL> f = commits [ n - <NUM_LIT:1> ] . split ( ) <EOL> today = datetime . datetime . today ( ) <EOL> first = datetime . datetime . strptime ( f [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> days = today - first <EOL> print ( "<STR_LIT>" % days . days ) <EOL> cpd = float ( days . days ) / n <EOL> print ( "<STR_LIT>" % cpd ) <EOL> folder_size = <NUM_LIT:0> <EOL> num_lines = <NUM_LIT:0> <EOL> num_words = <NUM_LIT:0> <EOL> num_chars = <NUM_LIT:0> <EOL> num_files = <NUM_LIT:0> <EOL> for ( path , dirs , files ) in os . walk ( datadir ) : <EOL> for file in files : <EOL> filename = os . path . join ( path , file ) <EOL> if "<STR_LIT>" not in filename : <EOL> num_files += <NUM_LIT:1> <EOL> folder_size += os . path . getsize ( filename ) <EOL> with open ( filename , '<STR_LIT:r>' ) as f : <EOL> for line in f : <EOL> words = line . split ( ) <EOL> num_lines += <NUM_LIT:1> <EOL> num_words += len ( words ) <EOL> num_chars += len ( line ) <EOL> print ( "<STR_LIT>" % ( folder_size / ( <NUM_LIT> * <NUM_LIT> ) ) ) <EOL> print ( "<STR_LIT>" % num_files ) <EOL> print ( "<STR_LIT>" % num_lines ) <EOL> print ( "<STR_LIT>" % num_words ) <EOL> print ( "<STR_LIT>" % num_chars ) </s>
