Changeset 3529 for lliurex-detect


Ignore:
Timestamp:
Feb 6, 2017, 1:46:43 PM (3 years ago)
Author:
mabarracus
Message:

Full compatibility with lliurex15

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lliurex-detect/trunk/fuentes/lliurex-detect.install/usr/bin/lliurex-detect

    r3527 r3529  
    1212import pwd,grp
    1313import argparse
    14 import glob
     14# import glob  # Commented: python 3.5 is not available on lliurex 15
     15import fnmatch # allows search recursively without glob
     16
    1517from subprocess import check_output
    1618
     
    2830
    2931def detect_flavour():
    30         file_name= [ filename for filename in glob.iglob('/usr/share/lliurex-cdd/**/cddflavour') ]
    31         try:
    32             with open(file_name[0],'r') as fileccd:
    33                 cdd= [ line.strip() for line in fileccd ]
    34                 cdd.sort()
    35                 return cdd
    36         except Exception as e:
    37             raise Exception('Error getting cddflavour: '+str(e))
     32# Use with python > 3.5 with glob new features
     33#        cdd_content=[]
     34#        file_name= [ filename for filename in glob.iglob('/usr/share/lliurex-cdd/**/cddflavour', recursive=True) ]
     35#        if len(file_name) > 0:
     36#           for cddfile in file_name:
     37#               try:
     38#                    with open(cddfile,'r') as fileccd:
     39#                       cdd= [ line.strip() for line in fileccd ]
     40#                       cdd_content.extend(cdd)
     41#               except Exception as e:
     42#                   raise Exception('cddflavour error '+str(e))
     43#        else:
     44#            file_name= [ filename for filename in glob.iglob('/usr/share/lliurex-cdd/**/cddversion', recursive=True) ]
     45#            if len(file_name) > 0:
     46#                for cddfile in file_name:
     47#                    try:
     48#                       with open(cddfile,'r') as fileccd:
     49#                          cdd= [ line.strip() for line in fileccd ]
     50#                          cdd_content.extend(cdd)
     51#                    except Exception as e:
     52#                        raise Exception('cddversion file error '+str(e))
     53#            else:
     54#               raise Exception('cddflavour or cddversion file not found! ')
     55#       cdd_content=list(set(cdd_content))
     56#       cdd_content.sort()
     57#       return cdd_content
     58
     59# Code for python < 3.5 without glob
     60        file_name=[]
     61        cdd_content=[]
     62        for root,dirnames,filenames in os.walk('/usr/share/lliurex-cdd'):
     63            for filename in fnmatch.filter(filenames,'cddflavour'):
     64                file_name.append(os.path.join(root,filename))
     65        if len(file_name) > 0:
     66            for cddfile in file_name:
     67                try:
     68                    with open(cddfile,'r') as fileccd:
     69                        cdd= [ line.strip() for line in fileccd ]
     70                        cdd_content.extend(cdd)
     71                except Exception as e:
     72                    raise Exception('cddflavour file error '+str(e))
     73        else:
     74            for root,dirnames,filenames in os.walk('/usr/share/lliurex-cdd'):
     75                for filename in fnmatch.filter(filenames,'cddversion'):
     76                    file_name.append(os.path.join(root,filename))
     77            if len(file_name) > 0:
     78                for cddfile in file_name:
     79                    try:
     80                        with open(cddfile,'r') as fileccd:
     81                            cdd= [ line.strip() for line in fileccd ]
     82                            cdd_content.extend(cdd)
     83                    except Exception as e:
     84                        raise Exception('cddversion file error '+str(e))
     85            else:
     86                raise Exception('cddflavour or cddversion file not found! ')
     87        cdd_content=list(set(cdd_content))
     88        cdd_content.sort()
     89        return cdd_content
    3890#def detect_flavour
    3991
    4092def detect_num_cdd():
    4193    try:
    42         num_cdd=check_output(['dpkg-query','--showformat=\'${Version}\'','--show','lliurex-version-timestamp']).decode('utf-8')
     94        with open(os.devnull, 'w') as devnull:
     95            num_cdd=check_output(['dpkg-query','--showformat=\'${Version}\'','--show','lliurex-version-timestamp'],stderr=devnull).decode('utf-8')
    4396        return num_cdd.strip('\'')
    44     except Exception as e:
    45         raise Exception('Error '+e)
     97    except Exception as e1:
     98        try:
     99            with open('/usr/share/lliurex-cdd/version','r') as cdd_num_file:
     100                return cdd_num_file.readline().strip()
     101        except Exception as e2:
     102            raise Exception('Error1: '+e1+' and Error2: '+e2)
    46103#def detect_num_cdd():
    47104
     
    184241        if exit_return_code_mode != False:
    185242            if result[exit_return_code_mode.upper()]=='yes':
    186                 exit(0)
     243                sys.exit(0)
    187244            else:
    188                 exit(1)
     245                sys.exit(1)
    189246        if len(result) > 0:
    190247            if eval_mode:
     
    268325    if args.test != None:
    269326        try:
    270             detect_flavour()
    271             exit(0)
     327            ret=detect_flavour()
    272328        except:
    273             exit(1)
     329            sys.exit(1)
     330        if args.test in ret:
     331            sys.exit(0)
     332        else:
     333            sys.exit(1)
    274334    if args.history != None:
    275335        print(get_history_version().rstrip())
     
    279339except Exception as e:
    280340    print('Error '+str(e))
    281     exit(1)
    282 
    283 exit (0)
     341    sys.exit(1)
     342
     343sys.exit(0)
Note: See TracChangeset for help on using the changeset viewer.