1 #! /usr/bin/python 2 # -*- coding: utf-8 -*- 3 4 import getopt, sys 5 6 from sys import path 7 8 def usage(): 9 print 'Usage : {} [-lhw] extension motif'.format('xSearch') 10 exit(2) 11 12 def main(): 13 14 # pas d'arg du tout : on arrête 15 if len(sys.argv) == 1: 16 usage() 17 18 # on découpe entre options et "vrais" arguments 19 20 try: 21 opts, args = getopt.getopt(sys.argv[1:], "hlwx") 22 except: 23 usage() 24 exit('option inconnue') 25 26 27 print "{}() : opts = '{}'".format('main', opts) 28 29 if len(args) != 2: 30 usage() 31 32 mot = False 33 long = False 34 exe = True # par défaut lundi 2 avril 2018, 08:38:51 (UTC+0200) 35 36 for option, valopt in opts: 37 assert not valopt # aucune option n'a de valeur 38 if option == "-l": 39 long = True 40 if option == "-w": 41 mot = True 42 if option == "-x": 43 exe = False 44 elif option == '-h': 45 usage() 46 # else: 47 # assert False, "unhandled option" 48 49 extension = args[0] 50 motif = args[1] 51 # échange des deux args si nécessaire 52 if motif in ['py', 'sh', 'mm', 't2t', 'txt', 'wiki', 'dia']: 53 extension = args[1] 54 motif = args[0] 55 56 57 58 import os 59 60 options_affichage = '' 61 options_recherche = '' 62 63 if mot: 64 options_recherche += 'w' 65 66 if long: 67 options_affichage += '-l' 68 69 # prévoir une option '-i' ? 70 71 k = 'locate .{e}|grep "yves/2011.*\.{e}$" | xargs 2>/dev/null grep -li{options_recherche} {p} | xargs ls {options_affichage} -rtd'.format(e=extension, p=motif, options_affichage=options_affichage, options_recherche=options_recherche) 72 print "{}".format(k) # tout est là 73 74 if exe: 75 import os 76 os.system(k) 77 78 if __name__ == "__main__": 79 main() 80 # penser à catcher getopt.GetoptError: