#NEWLINE# def record( args , filename ) : #NEWLINE# #INDENT# 'Record a snapshot in a json file, as specified by arguments in args.\n\n    Return 0 on success, 1 on failure.' #NEWLINE# LOGGER . debug ( 'In subcommand record.' ) #NEWLINE# os . chdir ( args . project ) #NEWLINE# projectpath = os . getcwd ( ) #NEWLINE# addons_list = [ ] #NEWLINE# try : #NEWLINE# #INDENT# with open ( 'addons.make' , 'r' ) as addons_make : #NEWLINE# #INDENT# for line in addons_make . readlines ( ) : #NEWLINE# #INDENT# addons_list . append ( line . rstrip ( ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# except IOError as exc : #NEWLINE# #INDENT# if ( exc . errno == errno . ENOENT ) : #NEWLINE# #INDENT# LOGGER . debug ( 'No addons.make file found.' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# #UNINDENT# if ( len ( addons_list ) is 0 ) : #NEWLINE# #INDENT# LOGGER . info ( 'No addons found.' ) #NEWLINE# #UNINDENT# with open ( 'config.make' , 'r' ) as config_make : #NEWLINE# #INDENT# of_path = '' #NEWLINE# for line in config_make . readlines ( ) : #NEWLINE# #INDENT# if ( 'OF_ROOT =' in line ) : #NEWLINE# #INDENT# of_path = line . split ( '=' , 1 ) [ ( - 1 ) ] . strip ( ) #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# if ( len ( of_path ) == 0 ) : #NEWLINE# #INDENT# LOGGER . error ( ( 'Did not find OF location in config.make in ' + os . getcwd ( ) ) ) #NEWLINE# return 1 #NEWLINE# #UNINDENT# #UNINDENT# LOGGER . info ( ( 'Processing OF at ' + of_path ) ) #NEWLINE# os . chdir ( of_path ) #NEWLINE# core_dict = { #NEWLINE# #INDENT# 'path' : of_path , #NEWLINE# #UNINDENT# } #NEWLINE# if ( validate_git_repo ( ) != 0 ) : #NEWLINE# #INDENT# LOGGER . error ( 'OF git repo could not be validated successfully.' ) #NEWLINE# return 1 #NEWLINE# #UNINDENT# LOGGER . debug ( 'Recording commit SHA' ) #NEWLINE# out = subprocess . check_output ( [ 'git' , 'rev-parse' , 'HEAD' ] , universal_newlines = True ) #NEWLINE# core_dict [ 'sha' ] = out . strip ( ) #NEWLINE# LOGGER . debug ( ( 'OF commit SHA: ' + core_dict [ 'sha' ] ) ) #NEWLINE# LOGGER . info ( 'Processing addons' ) #NEWLINE# addons_path = os . path . join ( os . getcwd ( ) , 'addons' ) #NEWLINE# os . chdir ( addons_path ) #NEWLINE# official_addons = [ ] #NEWLINE# with open ( '.gitignore' , 'r' ) as gitignore_file : #NEWLINE# #INDENT# for line in projectpath : #NEWLINE# #INDENT# if line . startswith ( '!ofx' ) : #NEWLINE# #INDENT# official_addons . append ( line [ 1 : ] . strip ( ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# addons_list = [ { #NEWLINE# #INDENT# 'name' : x , #NEWLINE# #UNINDENT# } for x in addons_list if ( x not in official_addons ) ] #NEWLINE# for addon in addons_list : #NEWLINE# #INDENT# LOGGER . info ( ( 'Processing addon ' + addon [ 'name' ] ) ) #NEWLINE# try : #NEWLINE# #INDENT# os . chdir ( os . path . join ( addons_path , addon [ 'name' ] ) ) #NEWLINE# #UNINDENT# except Exception as exc : #NEWLINE# #INDENT# if ( exc . errno == errno . ENOENT ) : #NEWLINE# #INDENT# LOGGER . error ( ( ( ( addon [ 'name' ] + ' does not exist at ' ) + addons_path ) + '.' ) ) #NEWLINE# sys . exit ( 'Aborting' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# #UNINDENT# ret = validate_git_repo ( strict = False ) #NEWLINE# if ( ret == 0 ) : #NEWLINE# #INDENT# out_string = subprocess . check_output ( [ 'git' , 'rev-parse' , 'HEAD' ] , universal_newlines = True ) #NEWLINE# addon [ 'sha' ] = out_string . strip ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ret == 2 ) : #NEWLINE# #INDENT# addon [ 'sha' ] = 'non-git' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# LOGGER . error ( ( addon [ 'name' ] + ' git repo could not be validated successfully.' ) ) #NEWLINE# return 1 #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# LOGGER . info ( 'Storing metadata' ) #NEWLINE# os . chdir ( projectpath ) #NEWLINE# try : #NEWLINE# #INDENT# with open ( filename , 'r' ) as metafile : #NEWLINE# #INDENT# json_object = json . load ( metafile ) #NEWLINE# LOGGER . info ( ( 'loaded data from ' + filename ) ) #NEWLINE# LOGGER . debug ( json_object ) #NEWLINE# #UNINDENT# #UNINDENT# except IOError as exc : #NEWLINE# #INDENT# if ( exc . errno == errno . ENOENT ) : #NEWLINE# #INDENT# LOGGER . info ( ( filename + ' does not exist yet. Creating..' ) ) #NEWLINE# open ( filename , 'w' ) . close ( ) #NEWLINE# json_object = json . loads ( '{ "snapshots": [] }' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# #UNINDENT# for entry in json_object [ 'snapshots' ] : #NEWLINE# #INDENT# if ( entry [ 'name' ] == args . name ) : #NEWLINE# #INDENT# if ( ( args . update is False ) and ( args . name is not 'latest' ) ) : #NEWLINE# #INDENT# LOGGER . error ( ( ( args . name + ': entry with the same name already exists. ' ) + 'Use -u option to overwrite.' ) ) #NEWLINE# return 1 #NEWLINE# #UNINDENT# json_object [ 'snapshots' ] . remove ( entry ) #NEWLINE# #UNINDENT# #UNINDENT# temp = { #NEWLINE# #INDENT# 'name' : args . name , #NEWLINE# 'date' : datetime . now ( ) . isoformat ( ) , #NEWLINE# 'description' : args . description , #NEWLINE# 'core' : core_dict , #NEWLINE# 'addons' : addons_list , #NEWLINE# #UNINDENT# } #NEWLINE# json_object [ 'snapshots' ] . append ( temp ) #NEWLINE# LOGGER . info ( ( 'Writing updated data to ' + filename ) ) #NEWLINE# with open ( filename , 'w' ) as metafile : #NEWLINE# #INDENT# json . dump ( json_object , metafile , indent = 1 , sort_keys = True ) #NEWLINE# #UNINDENT# return 0